41 lines
725 B
C++
41 lines
725 B
C++
#ifndef EspNowControl_cpp
|
|
#define EspNowControl_cpp
|
|
|
|
#include <esp_now.h>
|
|
#include <WiFi.h>
|
|
#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
|