SensESP 3.3.0
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
semaphore_value.h
Go to the documentation of this file.
1#ifndef SENSESP_SRC_SENSESP_SYSTEM_SEMAPHORE_VALUE_H_
2#define SENSESP_SRC_SENSESP_SYSTEM_SEMAPHORE_VALUE_H_
3
4#include "sensesp.h"
5
6#include <freertos/semphr.h>
7
8namespace sensesp {
9
23template <typename T>
24class SemaphoreValue : public ValueConsumer<T> {
25 public:
27 semaphore_ = xSemaphoreCreateBinaryStatic(&semaphore_buffer_);
28 mutex_ = xSemaphoreCreateMutexStatic(&mutex_buffer_);
29 }
30
37 bool wait(T& destination, unsigned int max_duration_ms) {
38 if (xSemaphoreTake(semaphore_, max_duration_ms / portTICK_PERIOD_MS) ==
39 pdTRUE) {
40 xSemaphoreTake(mutex_, portMAX_DELAY);
41 destination = value_;
42 xSemaphoreGive(mutex_);
43 return true;
44 }
45 return false;
46 }
47
52 bool take(unsigned int max_duration_ms) {
53 return xSemaphoreTake(semaphore_, max_duration_ms / portTICK_PERIOD_MS) ==
54 pdTRUE;
55 }
56
57 void set(const T& new_value) override {
58 xSemaphoreTake(mutex_, portMAX_DELAY);
59 value_ = new_value;
60 xSemaphoreGive(mutex_);
61 xSemaphoreGive(semaphore_);
62 }
63
64 void clear() { xSemaphoreTake(semaphore_, 0); }
65
66 protected:
68 StaticSemaphore_t semaphore_buffer_;
69 SemaphoreHandle_t semaphore_;
70 StaticSemaphore_t mutex_buffer_;
71 SemaphoreHandle_t mutex_;
72};
73
74} // namespace sensesp
75
76#endif // SENSESP_SRC_SENSESP_SYSTEM_SEMAPHORE_VALUE_H_
SemaphoreHandle_t semaphore_
bool take(unsigned int max_duration_ms)
Take the semaphore, ignoring the value.
StaticSemaphore_t semaphore_buffer_
bool wait(T &destination, unsigned int max_duration_ms)
Wait for the value to be updated.
StaticSemaphore_t mutex_buffer_
void set(const T &new_value) override
SemaphoreHandle_t mutex_
A base class for piece of code (like a transform) that accepts data for input. ValueConsumers can acc...