SensESP 3.0.1
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_ = xSemaphoreCreateBinary();
28 }
29
36 bool wait(T& destination, unsigned int max_duration_ms) {
37 if (xSemaphoreTake(semaphore_, max_duration_ms / portTICK_PERIOD_MS) ==
38 pdTRUE) {
39 destination = value_;
40 return true;
41 }
42 return false;
43 }
44
49 bool take(unsigned int max_duration_ms) {
50 return xSemaphoreTake(semaphore_, max_duration_ms / portTICK_PERIOD_MS) ==
51 pdTRUE;
52 }
53
54 void set(const T& new_value) override {
55 value_ = new_value;
56 xSemaphoreGive(semaphore_);
57 }
58
59 void clear() { xSemaphoreTake(semaphore_, 0); }
60
61 protected:
63 SemaphoreHandle_t semaphore_;
64};
65
66} // namespace sensesp
67
68#endif // SENSESP_SRC_SENSESP_SYSTEM_SEMAPHORE_VALUE_H_
A value container wrapped in a semaphore.
SemaphoreHandle_t semaphore_
bool take(unsigned int max_duration_ms)
Take the semaphore, ignoring the value.
bool wait(T &destination, unsigned int max_duration_ms)
Wait for the value to be updated.
void set(const T &new_value) override
A base class for piece of code (like a transform) that accepts data for input. ValueConsumers can acc...