Skip to content

Commit 54d04ec

Browse files
committed
Merge branch 'preview_espnow_pr3237' into mdev
2 parents 7049064 + 331089a commit 54d04ec

File tree

11 files changed

+438
-141
lines changed

11 files changed

+438
-141
lines changed

wled00/cfg.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,13 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
483483
CJSON(retainMqttMsg, if_mqtt[F("rtn")]);
484484
#endif
485485

486+
#ifndef WLED_DISABLE_ESPNOW
487+
JsonObject remote = doc["remote"];
488+
CJSON(enable_espnow_remote, remote[F("remote_enabled")]);
489+
getStringFromJson(linked_remote, remote[F("linked_remote")], 13);
490+
#endif
491+
492+
486493
#ifndef WLED_DISABLE_HUESYNC
487494
JsonObject if_hue = interfaces["hue"];
488495
CJSON(huePollingEnabled, if_hue["en"]);
@@ -964,6 +971,13 @@ void serializeConfig() {
964971
if_mqtt_topics[F("group")] = mqttGroupTopic;
965972
#endif
966973

974+
#ifndef WLED_DISABLE_ESPNOW
975+
JsonObject remote = doc.createNestedObject(F("remote"));
976+
remote[F("remote_enabled")] = enable_espnow_remote;
977+
remote[F("linked_remote")] = linked_remote;
978+
#endif
979+
980+
967981
#ifndef WLED_DISABLE_HUESYNC
968982
JsonObject if_hue = interfaces.createNestedObject("hue");
969983
if_hue["en"] = huePollingEnabled;

wled00/data/settings_wifi.htm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ <h3>Experimental</h3>
182182
Disable WiFi sleep: <input type="checkbox" name="WS"><br>
183183
<i>Can help with connectivity issues.<br>
184184
Do not enable if WiFi is working correctly, increases power consumption.</i>
185+
186+
<div id="remd">
187+
<h3>Wireless Remote</h3>
188+
<i>Listen for events over ESP-NOW<br>
189+
Keep disabled if not using a remote, increases power consumption.<br></i>
190+
191+
Enable Remote: <input type="checkbox" name="RE"><br>
192+
Hardware MAC: <input type="text" name="RMAC"><br>
193+
Last Seen: <span class="rlid">None</span> <br>
194+
</div>
195+
185196
<div id="ethd">
186197
<h3>Ethernet Type</h3>
187198
<select name="ETH">

wled00/fcn_declare.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,16 @@ bool presetsActionPending(void); // WLEDMM true if presetToApply, presetToSave,
204204
void initPresetsFile();
205205
void handlePresets();
206206
bool applyPreset(byte index, byte callMode = CALL_MODE_DIRECT_CHANGE);
207+
void applyPresetWithFallback(uint8_t presetID, uint8_t callMode, uint8_t effectID = 0, uint8_t paletteID = 0);
207208
inline bool applyTemporaryPreset() {return applyPreset(255);};
208209
void savePreset(byte index, const char* pname = nullptr, JsonObject saveobj = JsonObject());
209210
inline void saveTemporaryPreset() {savePreset(255);};
210211
void deletePreset(byte index);
211212
bool getPresetName(byte index, String& name);
212213

214+
//remote.cpp
215+
void handleRemote();
216+
213217
//set.cpp
214218
bool isAsterisksOnly(const char* str, byte maxLen);
215219
void handleSettingsSet(AsyncWebServerRequest *request, byte subPage);

wled00/html_settings.h

Lines changed: 143 additions & 135 deletions
Large diffs are not rendered by default.

wled00/ir.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ void decBrightness()
7070
}
7171
}
7272

73-
// apply preset or fallback to a effect and palette if it doesn't exist
7473
void presetFallback(uint8_t presetID, uint8_t effectID, uint8_t paletteID)
7574
{
76-
USER_PRINTF("presetFallback1 %d %d %d\n", presetID, effectID, paletteID);
77-
applyPreset(presetID, CALL_MODE_BUTTON_PRESET);
75+
//USER_PRINTF("presetFallback1 %d %d %d\n", presetID, effectID, paletteID);
76+
//applyPreset(presetID, CALL_MODE_BUTTON_PRESET);
7877
//these two will be overwritten if preset exists in handlePresets()
79-
USER_PRINTF("presetFallback2 %d %d %d\n", presetID, effectID, paletteID);
80-
effectCurrent = effectID;
81-
effectPalette = paletteID;
78+
applyPresetWithFallback(presetID, CALL_MODE_BUTTON_PRESET, effectID, paletteID);
79+
USER_PRINTF("applyPresetWithFallback %d %d %d\n", presetID, effectID, paletteID);
80+
//effectCurrent = effectID;
81+
//effectPalette = paletteID;
8282
}
8383

8484
byte relativeChange(byte property, int8_t amount, byte lowerBoundary, byte higherBoundary)

wled00/presets.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ bool applyPreset(byte index, byte callMode)
134134
return true;
135135
}
136136

137+
// apply preset or fallback to a effect and palette if it doesn't exist
138+
void applyPresetWithFallback(uint8_t index, uint8_t callMode, uint8_t effectID, uint8_t paletteID)
139+
{
140+
applyPreset(index, callMode);
141+
//these two will be overwritten if preset exists in handlePresets()
142+
effectCurrent = effectID;
143+
effectPalette = paletteID;
144+
}
145+
137146
void handlePresets()
138147
{
139148
if (presetToSave) {

wled00/remote.cpp

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
#include "wled.h"
2+
3+
#define ESP_NOW_STATE_UNINIT 0
4+
#define ESP_NOW_STATE_ON 1
5+
#define ESP_NOW_STATE_ERROR 2
6+
7+
#define NIGHT_MODE_DEACTIVATED -1
8+
#define NIGHT_MODE_BRIGHTNESS 5
9+
10+
#define WIZMOTE_BUTTON_ON 1
11+
#define WIZMOTE_BUTTON_OFF 2
12+
#define WIZMOTE_BUTTON_NIGHT 3
13+
#define WIZMOTE_BUTTON_ONE 16
14+
#define WIZMOTE_BUTTON_TWO 17
15+
#define WIZMOTE_BUTTON_THREE 18
16+
#define WIZMOTE_BUTTON_FOUR 19
17+
#define WIZMOTE_BUTTON_BRIGHT_UP 9
18+
#define WIZMOTE_BUTTON_BRIGHT_DOWN 8
19+
20+
#ifdef WLED_DISABLE_ESPNOW
21+
void handleRemote(){}
22+
#else
23+
24+
#if !defined(ARDUINO_ARCH_ESP32) && !defined(ESP_OK)
25+
#define ESP_OK 0 // add missing constant for stupid esp8266
26+
#endif
27+
28+
// This is kind of an esoteric strucure because it's pulled from the "Wizmote"
29+
// product spec. That remote is used as the baseline for behavior and availability
30+
// since it's broadly commercially available and works out of the box as a drop-in
31+
typedef struct message_structure {
32+
uint8_t program; // 0x91 for ON button, 0x81 for all others
33+
uint8_t seq[4]; // Incremetal sequence number 32 bit unsigned integer LSB first
34+
uint8_t byte5 = 32; // Unknown
35+
uint8_t button; // Identifies which button is being pressed
36+
uint8_t byte8 = 1; // Unknown, but always 0x01
37+
uint8_t byte9 = 100; // Unnkown, but always 0x64
38+
39+
uint8_t byte10; // Unknown, maybe checksum
40+
uint8_t byte11; // Unknown, maybe checksum
41+
uint8_t byte12; // Unknown, maybe checksum
42+
uint8_t byte13; // Unknown, maybe checksum
43+
} message_structure;
44+
45+
static int esp_now_state = ESP_NOW_STATE_UNINIT;
46+
static uint32_t last_seq = -1;
47+
static int brightnessBeforeNightMode = NIGHT_MODE_DEACTIVATED;
48+
static message_structure incoming;
49+
50+
// Pulled from the IR Remote logic but reduced to 10 steps with a constant of 3
51+
static const byte brightnessSteps[] = {
52+
6, 9, 14, 22, 33, 50, 75, 113, 170, 255
53+
};
54+
static const size_t numBrightnessSteps = sizeof(brightnessSteps) / sizeof(uint8_t);
55+
56+
static bool nightModeActive() {
57+
return brightnessBeforeNightMode != NIGHT_MODE_DEACTIVATED;
58+
}
59+
60+
static void activateNightMode() {
61+
brightnessBeforeNightMode = bri;
62+
bri = NIGHT_MODE_BRIGHTNESS;
63+
}
64+
65+
static bool resetNightMode() {
66+
if (!nightModeActive()) {
67+
return false;
68+
}
69+
bri = brightnessBeforeNightMode;
70+
brightnessBeforeNightMode = NIGHT_MODE_DEACTIVATED;
71+
return true;
72+
}
73+
74+
// increment `bri` to the next `brightnessSteps` value
75+
static void brightnessUp() {
76+
if (nightModeActive()) { return; }
77+
// dumb incremental search is efficient enough for so few items
78+
for (uint8_t index = 0; index < numBrightnessSteps; ++index) {
79+
if (brightnessSteps[index] > bri) {
80+
bri = brightnessSteps[index];
81+
break;
82+
}
83+
}
84+
}
85+
86+
// decrement `bri` to the next `brightnessSteps` value
87+
static void brightnessDown() {
88+
if (nightModeActive()) { return; }
89+
// dumb incremental search is efficient enough for so few items
90+
for (int index = numBrightnessSteps - 1; index >= 0; --index) {
91+
if (brightnessSteps[index] < bri) {
92+
bri = brightnessSteps[index];
93+
break;
94+
}
95+
}
96+
}
97+
98+
static void setOn() {
99+
if (resetNightMode()) {
100+
stateUpdated(CALL_MODE_BUTTON);
101+
}
102+
if (!bri) {
103+
toggleOnOff();
104+
}
105+
}
106+
107+
static void setOff() {
108+
if (resetNightMode()) {
109+
stateUpdated(CALL_MODE_BUTTON);
110+
}
111+
if (bri) {
112+
toggleOnOff();
113+
}
114+
}
115+
116+
static void presetWithFallback(uint8_t presetID, uint8_t effectID, uint8_t paletteID) {
117+
applyPresetWithFallback(presetID, CALL_MODE_BUTTON_PRESET, effectID, paletteID);
118+
}
119+
120+
// Callback function that will be executed when data is received
121+
#ifdef ESP8266
122+
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
123+
#else
124+
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
125+
#endif
126+
127+
sprintf (last_signal_src, "%02x%02x%02x%02x%02x%02x",
128+
mac [0], mac [1], mac [2], mac [3], mac [4], mac [5]);
129+
130+
if (strcmp(last_signal_src, linked_remote) != 0) {
131+
USER_PRINT(F("ESP Now Message Received from Unlinked Sender: "));
132+
USER_PRINTLN(last_signal_src);
133+
return;
134+
}
135+
136+
if (len != sizeof(incoming)) {
137+
USER_PRINT(F("Unknown incoming ESP Now message received of length "));
138+
USER_PRINTLN(len);
139+
return;
140+
}
141+
142+
memcpy(&(incoming.program), incomingData, sizeof(incoming));
143+
uint32_t cur_seq = incoming.seq[0] | (incoming.seq[1] << 8) | (incoming.seq[2] << 16) | (incoming.seq[3] << 24);
144+
145+
if (cur_seq == last_seq) {
146+
return;
147+
}
148+
149+
150+
USER_PRINT(F("\nIncoming ESP Now Packet["));
151+
USER_PRINT(cur_seq);
152+
USER_PRINT(F("] from sender["));
153+
USER_PRINT(last_signal_src);
154+
USER_PRINT(F("] button: "));
155+
USER_PRINTLN(incoming.button);
156+
switch (incoming.button) {
157+
case WIZMOTE_BUTTON_ON : setOn(); stateUpdated(CALL_MODE_BUTTON); break;
158+
case WIZMOTE_BUTTON_OFF : setOff(); stateUpdated(CALL_MODE_BUTTON); break;
159+
case WIZMOTE_BUTTON_ONE : presetWithFallback(1, FX_MODE_STATIC, 0); resetNightMode(); break;
160+
case WIZMOTE_BUTTON_TWO : presetWithFallback(2, FX_MODE_BREATH, 0); resetNightMode(); break;
161+
case WIZMOTE_BUTTON_THREE : presetWithFallback(3, FX_MODE_FIRE_FLICKER, 0); resetNightMode(); break;
162+
case WIZMOTE_BUTTON_FOUR : presetWithFallback(4, FX_MODE_RAINBOW, 0); resetNightMode(); break;
163+
case WIZMOTE_BUTTON_NIGHT : activateNightMode(); stateUpdated(CALL_MODE_BUTTON); break;
164+
case WIZMOTE_BUTTON_BRIGHT_UP : brightnessUp(); stateUpdated(CALL_MODE_BUTTON); break;
165+
case WIZMOTE_BUTTON_BRIGHT_DOWN : brightnessDown(); stateUpdated(CALL_MODE_BUTTON); break;
166+
default: break;
167+
168+
}
169+
170+
last_seq = cur_seq;
171+
}
172+
173+
void handleRemote() {
174+
if (enable_espnow_remote) {
175+
if (esp_now_state == ESP_NOW_STATE_UNINIT) {
176+
USER_PRINTLN(F("\nInitializing ESP_NOW listener!\n"));
177+
// Init ESP-NOW
178+
if (esp_now_init() != ESP_OK) {
179+
USER_PRINTLN(F("Error initializing ESP-NOW"));
180+
esp_now_state = ESP_NOW_STATE_ERROR;
181+
}
182+
183+
#ifdef ESP8266
184+
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
185+
#endif
186+
187+
esp_now_register_recv_cb(OnDataRecv);
188+
esp_now_state = ESP_NOW_STATE_ON;
189+
}
190+
} else {
191+
if (esp_now_state == ESP_NOW_STATE_ON) {
192+
USER_PRINTLN(F("Disabling ESP-NOW Remote Listener"));
193+
if (esp_now_deinit() != ESP_OK) {
194+
USER_PRINTLN(F("Error de-initializing ESP-NOW"));
195+
}
196+
esp_now_state = ESP_NOW_STATE_UNINIT;
197+
} else if (esp_now_state == ESP_NOW_STATE_ERROR) {
198+
//Clear any error states (allows retrying by cycling)
199+
esp_now_state = ESP_NOW_STATE_UNINIT;
200+
}
201+
}
202+
}
203+
204+
#endif

wled00/set.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
4848

4949
noWifiSleep = request->hasArg(F("WS"));
5050

51+
#ifndef WLED_DISABLE_ESPNOW
52+
enable_espnow_remote = request->hasArg(F("RE"));
53+
strlcpy(linked_remote,request->arg(F("RMAC")).c_str(), 13);
54+
55+
//Normalize MAC format to lowercase
56+
strlcpy(linked_remote,strlwr(linked_remote), 13);
57+
#endif
58+
5159
#ifdef WLED_USE_ETHERNET
5260
ethernetType = request->arg(F("ETH")).toInt();
5361
WLED::instance().initEthernet();

wled00/wled.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ void WLED::loop()
105105
handleIR(); // 2nd call to function needed for ESP32 to return valid results -- should be good for ESP8266, too
106106
#endif
107107
handleConnection();
108+
#ifndef WLED_DISABLE_ESPNOW
109+
handleRemote();
110+
#endif
108111
handleSerial();
109112

110113
#if defined(ARDUINO_ARCH_ESP32) && defined(WLEDMM_PROTECT_SERVICE) // WLEDMM experimental: handleNotifications() calls strip.show(); handleTransitions modifies segments

wled00/wled.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#define WLED_ENABLE_WEBSOCKETS
4646
#endif
4747

48+
//#define WLED_DISABLE_ESPNOW // Removes dependence on esp now
49+
4850
#define WLED_ENABLE_FS_EDITOR // enable /edit page for editing FS content. Will also be disabled with OTA lock
4951

5052
// to toggle usb serial debug (un)comment the following line
@@ -77,6 +79,9 @@
7779
{
7880
#include <user_interface.h>
7981
}
82+
#ifndef WLED_DISABLE_ESPNOW
83+
#include <espnow.h>
84+
#endif
8085
#else // ESP32
8186
#include <HardwareSerial.h> // ensure we have the correct "Serial" on new MCUs (depends on ARDUINO_USB_MODE and ARDUINO_USB_CDC_ON_BOOT)
8287
#include <WiFi.h>
@@ -93,6 +98,10 @@
9398
#include <LittleFS.h>
9499
#endif
95100
#include "esp_task_wdt.h"
101+
102+
#ifndef WLED_DISABLE_ESPNOW
103+
#include <esp_now.h>
104+
#endif
96105
#endif
97106
#include <Wire.h>
98107
#include <SPI.h>
@@ -459,6 +468,12 @@ WLED_GLOBAL bool hueApplyColor _INIT(true);
459468

460469
WLED_GLOBAL uint16_t serialBaud _INIT(1152); // serial baud rate, multiply by 100
461470

471+
#ifndef WLED_DISABLE_ESPNOW
472+
WLED_GLOBAL bool enable_espnow_remote _INIT(false);
473+
WLED_GLOBAL char linked_remote[13] _INIT("");
474+
WLED_GLOBAL char last_signal_src[13] _INIT("");
475+
#endif
476+
462477
// Time CONFIG
463478
WLED_GLOBAL bool ntpEnabled _INIT(false); // get internet time. Only required if you use clock overlays or time-activated macros
464479
WLED_GLOBAL bool useAMPM _INIT(false); // 12h/24h clock format

0 commit comments

Comments
 (0)