Move ESP-NOW lighting control to lib
This commit is contained in:
parent
5b69f482c3
commit
76b8fed961
5 changed files with 33 additions and 34 deletions
|
@ -1,18 +1,12 @@
|
||||||
#ifndef EspNowControl_cpp
|
#ifndef EspNowLightControlClient_cpp
|
||||||
#define EspNowControl_cpp
|
#define EspNowLightControlClient_cpp
|
||||||
|
|
||||||
#include <esp_now.h>
|
#include <esp_now.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include "LightControlPayload.cpp"
|
||||||
|
|
||||||
class EspNowControl {
|
class EspNowLightControlClient {
|
||||||
typedef struct payload {
|
uint8_t broadcastAddress[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||||
int id;
|
|
||||||
bool on;
|
|
||||||
float brightness;
|
|
||||||
unsigned long timer;
|
|
||||||
} payload;
|
|
||||||
|
|
||||||
uint8_t broadcastAddress[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool init() {
|
bool init() {
|
||||||
|
@ -33,21 +27,21 @@ public:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void send(){
|
void send() {
|
||||||
for (int i=1; i<=5; i++){
|
for (int i = 1; i <= 5; i++) {
|
||||||
payload data = payload();
|
LightControlPayload data = LightControlPayload();
|
||||||
data.id = i;
|
data.id = i;
|
||||||
data.brightness = 1;
|
data.brightness = 1;
|
||||||
data.on = false;
|
data.on = false;
|
||||||
data.timer = 0;
|
data.timer = 0;
|
||||||
|
|
||||||
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &data, sizeof(data));
|
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t*)&data, sizeof(data));
|
||||||
if (result == ESP_OK)
|
if (result == ESP_OK)
|
||||||
Serial.println("Sent with success");
|
Serial.println("Sent with success");
|
||||||
else{
|
else {
|
||||||
Serial.println("Error sending the data");
|
Serial.println("Error sending the data");
|
||||||
Serial.println(esp_err_to_name(result));
|
Serial.println(esp_err_to_name(result));
|
||||||
}
|
}
|
|
@ -1,27 +1,21 @@
|
||||||
#ifndef EspNowControl_cpp
|
#ifndef EspNowLightControlServer_cpp
|
||||||
#define EspNowControl_cpp
|
#define EspNowLightControlServer_cpp
|
||||||
|
|
||||||
#include <esp_now.h>
|
#include <esp_now.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include "LedManager.cpp"
|
#include "LedManager.cpp"
|
||||||
|
#include "LightControlPayload.cpp"
|
||||||
|
|
||||||
class EspNowControl {
|
class EspNowLightControlServer {
|
||||||
typedef struct payload {
|
|
||||||
int id;
|
|
||||||
bool on;
|
|
||||||
float brightness;
|
|
||||||
unsigned long timer;
|
|
||||||
} payload;
|
|
||||||
|
|
||||||
static LedManager *_leds;
|
static LedManager *_leds;
|
||||||
|
|
||||||
static void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
static void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
||||||
payload data;
|
LightControlPayload data;
|
||||||
memcpy(&data, incomingData, sizeof(data));
|
memcpy(&data, incomingData, sizeof(data));
|
||||||
_leds->setLedProperties(data.id, data.on, data.brightness, data.timer);
|
_leds->setLedProperties(data.id, data.on, data.brightness, data.timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
EspNowControl();
|
EspNowLightControlServer();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static bool init(LedManager *leds) {
|
static bool init(LedManager *leds) {
|
11
lib/EspNowLightControl/LightControlPayload.cpp
Normal file
11
lib/EspNowLightControl/LightControlPayload.cpp
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef LightControlPayload_cpp
|
||||||
|
#define LightControlPayload_cpp
|
||||||
|
|
||||||
|
struct LightControlPayload {
|
||||||
|
int id;
|
||||||
|
bool on;
|
||||||
|
float brightness;
|
||||||
|
unsigned long timer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,7 +1,7 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "EspNowControl.cpp"
|
#include "EspNowLightControlClient.cpp"
|
||||||
|
|
||||||
EspNowControl _control;
|
EspNowLightControlClient _control;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
|
@ -3,13 +3,13 @@
|
||||||
#include "BluetoothLEDCallback.cpp"
|
#include "BluetoothLEDCallback.cpp"
|
||||||
#include "BluetoothLightControl.cpp"
|
#include "BluetoothLightControl.cpp"
|
||||||
#include "BluetoothService.cpp"
|
#include "BluetoothService.cpp"
|
||||||
#include "EspNowControl.cpp"
|
#include "EspNowLightControlServer.cpp"
|
||||||
#include "LedManager.cpp"
|
#include "LedManager.cpp"
|
||||||
#include "TimerManager.cpp"
|
#include "TimerManager.cpp"
|
||||||
#include "TouchInput.cpp"
|
#include "TouchInput.cpp"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
LedManager *EspNowControl::_leds = 0; // WTF, C++?
|
LedManager *EspNowLightControlServer::_leds = 0; // WTF, C++?
|
||||||
|
|
||||||
#define FADE_IN_DURATION 500
|
#define FADE_IN_DURATION 500
|
||||||
#define FADE_OUT_DURATION 1000
|
#define FADE_OUT_DURATION 1000
|
||||||
|
@ -65,7 +65,7 @@ void setup() {
|
||||||
|
|
||||||
_btService.init();
|
_btService.init();
|
||||||
_btService.start();
|
_btService.start();
|
||||||
EspNowControl::init(&_leds);
|
EspNowLightControlServer::init(&_leds);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue