From 00bcf55c83ac956de41211a70913a0663eaca529 Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Fri, 19 Aug 2022 08:01:58 +0100 Subject: [PATCH] Add ESP-NOW control --- src/EspNowControl.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 4 ++++ 2 files changed, 45 insertions(+) create mode 100644 src/EspNowControl.cpp diff --git a/src/EspNowControl.cpp b/src/EspNowControl.cpp new file mode 100644 index 0000000..a4c11ae --- /dev/null +++ b/src/EspNowControl.cpp @@ -0,0 +1,41 @@ +#ifndef EspNowControl_cpp +#define EspNowControl_cpp + +#include +#include +#include "LedManager.cpp" + +class EspNowControl { + typedef struct payload { + int id; + bool on; + float brightness; + unsigned long timer; + } payload; + + static LedManager *_leds; + + static void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { + payload data; + memcpy(&data, incomingData, sizeof(data)); + _leds->setLedProperties(data.id, data.on, data.brightness, data.timer); + } + + EspNowControl(); + +public: + static bool init(LedManager *leds) { + _leds = leds; + + WiFi.mode(WIFI_STA); + + if (esp_now_init() != ESP_OK) + return false; + + esp_now_register_recv_cb(OnDataRecv); + + return true; + } +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 5731405..fbeb66e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,10 +2,13 @@ #include "BluetoothLEDCallback.cpp" #include "BluetoothLightControl.cpp" #include "BluetoothService.cpp" +#include "EspNowControl.cpp" #include "LedManager.cpp" #include "TimerManager.cpp" #include +LedManager *EspNowControl::_leds = 0; // WTF, C++? + #define FADE_IN_DURATION 500 #define FADE_OUT_DURATION 1000 @@ -52,6 +55,7 @@ void setup() { _btService.init(); _btService.start(); + EspNowControl::init(&_leds); } void loop() {