|
@@ -0,0 +1,71 @@
|
|
|
+#include "kbf/rtos.h"
|
|
|
+
|
|
|
+#include <esp_log.h>
|
|
|
+
|
|
|
+using namespace kbf;
|
|
|
+using std::vector;
|
|
|
+
|
|
|
+rtos::EventGroup::EventGroup() {
|
|
|
+ handle = xEventGroupCreate();
|
|
|
+}
|
|
|
+
|
|
|
+void rtos::EventGroup::setBit(int bit) const {
|
|
|
+ ESP_LOGD(TAG, "setBit(%d)", bit);
|
|
|
+ xEventGroupSetBits(handle, 1 << bit);
|
|
|
+}
|
|
|
+
|
|
|
+void rtos::EventGroup::clearBit(int bit) const {
|
|
|
+ ESP_LOGD(TAG, "clearBit(%d)", bit);
|
|
|
+ xEventGroupClearBits(handle, 1 << bit);
|
|
|
+}
|
|
|
+
|
|
|
+void rtos::EventGroup::clear() const {
|
|
|
+ ESP_LOGD(TAG, "clear()");
|
|
|
+ xEventGroupClearBits(handle, 0b11111111);
|
|
|
+}
|
|
|
+
|
|
|
+bool rtos::EventGroup::getBit(int bit) const {
|
|
|
+ ESP_LOGD(TAG, "getBit(%d)", bit);
|
|
|
+ return xEventGroupGetBits(handle) & (1 << bit);
|
|
|
+}
|
|
|
+
|
|
|
+bool rtos::EventGroup::operator[](int bit) const {
|
|
|
+ return getBit(bit);
|
|
|
+}
|
|
|
+
|
|
|
+bool rtos::EventGroup::waitForBit(int bit, int timeoutMs) const {
|
|
|
+ auto ticksToWait = timeoutMs == -1 ? -1 : timeoutMs / portTICK_PERIOD_MS;
|
|
|
+ ESP_LOGD(TAG, "waitForBit(%d) start (timeout=%d, ticksToWait=%d)", bit, timeoutMs, ticksToWait);
|
|
|
+ xEventGroupWaitBits(handle, 1 << bit, pdFALSE, pdTRUE, ticksToWait);
|
|
|
+ ESP_LOGD(TAG, "waitForBit(%d) finish", bit);
|
|
|
+ return getBit(bit);
|
|
|
+}
|
|
|
+
|
|
|
+bool rtos::EventGroup::wait(const vector<int> &bits, int timeoutMs, bool all) const {
|
|
|
+ auto ticksToWait = timeoutMs == -1 ? -1 : timeoutMs / portTICK_PERIOD_MS;
|
|
|
+ ESP_LOGD(TAG, "wait(all=%s) start (timeout=%d, ticksToWait=%d)", all ? "true" : "false", timeoutMs, ticksToWait);
|
|
|
+ EventBits_t eventBits{};
|
|
|
+
|
|
|
+ for (int bit : bits) {
|
|
|
+ eventBits |= 1 << bit;
|
|
|
+ }
|
|
|
+ ESP_LOGD(TAG, "eventBits=%d", eventBits);
|
|
|
+ xEventGroupWaitBits(handle, eventBits, pdFALSE, all, ticksToWait);
|
|
|
+
|
|
|
+ bool result = true;
|
|
|
+ eventBits = xEventGroupGetBits(handle);
|
|
|
+ for (int bit : bits) {
|
|
|
+ if (!(eventBits & (1 << bit))) result = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ ESP_LOGD(TAG, "wait(all=%s) finish=%s; eventBits=%d", all ? "true" : "false", result ? "true" : "false", eventBits);
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+bool rtos::EventGroup::waitForAll(const vector<int> &bits, int timeoutMs) const {
|
|
|
+ return wait(bits, timeoutMs, true);
|
|
|
+}
|
|
|
+
|
|
|
+bool rtos::EventGroup::waitForAny(const vector<int> &bits, int timeoutMs) const {
|
|
|
+ return wait(bits, timeoutMs, false);
|
|
|
+}
|