-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwled_handler.cpp
139 lines (118 loc) · 4.73 KB
/
wled_handler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "wled_handler.h"
#include <WiFi.h>
#include <esp_now.h>
extern uint32_t sequenceNumber;
extern const uint8_t broadcastAddress[];
extern void logEspNowResult(esp_err_t result, const String& command, int channel); // Declare the function
// WLED-specific message handler
void handleWizMessage(DynamicJsonDocument& doc) {
int buttonCode = doc["button"];
if (buttonCode == 0) {
Serial.println("Invalid button code");
return;
}
Serial.print("Button Code: ");
Serial.println(buttonCode);
remote_message_struct outgoingMessage;
outgoingMessage.program = 0x81;
switch (buttonCode) {
case 1:
outgoingMessage.program = 0x91; // Toggle on/off (including nightlight mode)
outgoingMessage.button = 1;
break;
case 2:
outgoingMessage.button = 2; // Brightness up
break;
case 3:
outgoingMessage.button = 16; // Mode up
break;
case 17:
outgoingMessage.button = 17; // Mode down
break;
case 18:
outgoingMessage.button = 18; // Speed up
break;
case 19:
outgoingMessage.button = 19; // Speed down
break;
case 9:
outgoingMessage.button = 9; // Color up
break;
case 8:
outgoingMessage.button = 8; // Color down
break;
default:
Serial.println("Unsupported button code for wizremote");
return;
}
sequenceNumber++;
outgoingMessage.seq[0] = sequenceNumber & 0xFF;
outgoingMessage.seq[1] = (sequenceNumber >> 8) & 0xFF;
outgoingMessage.seq[2] = (sequenceNumber >> 16) & 0xFF;
outgoingMessage.seq[3] = (sequenceNumber >> 24) & 0xFF;
outgoingMessage.byte5 = 0x20;
outgoingMessage.byte8 = 0x01;
outgoingMessage.byte9 = 0x64;
int channel = doc["channel"]; // Get the channel from the JSON payload
// Check if the channel is valid
if (channel >= 1 && channel <= 14) {
// Send on the specified channel
WiFi.setChannel(channel);
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t*)&outgoingMessage, sizeof(outgoingMessage));
logEspNowResult(result, String(buttonCode), channel);
} else {
// Broadcast on all channels if the channel is invalid (or 0)
esp_err_t result = ESP_OK; // Assume success initially
for (int i = 1; i <= 14; ++i) {
WiFi.setChannel(i);
delay(10);
result = esp_now_send(broadcastAddress, (uint8_t*)&outgoingMessage, sizeof(outgoingMessage));
if (result != ESP_OK) {
break; // Stop if an error occurs on any channel
}
}
// Log the result only once after the loop
logEspNowResult(result, String(buttonCode), 0); // 0 indicates broadcast
}
}
void handleJsonRemoteMessage(DynamicJsonDocument& doc) {
int buttonCode = doc["button"];
if (buttonCode < 1 || buttonCode > 255) {
Serial.println("Invalid button code for json_remote (must be between 1 and 255)");
return;
}
Serial.print("Button Code (json_remote): ");
Serial.println(buttonCode);
remote_message_struct outgoingMessage;
outgoingMessage.program = 0x81;
outgoingMessage.button = buttonCode;
sequenceNumber++;
outgoingMessage.seq[0] = sequenceNumber & 0xFF;
outgoingMessage.seq[1] = (sequenceNumber >> 8) & 0xFF;
outgoingMessage.seq[2] = (sequenceNumber >> 16) & 0xFF;
outgoingMessage.seq[3] = (sequenceNumber >> 24) & 0xFF;
outgoingMessage.byte5 = 0x20;
outgoingMessage.byte8 = 0x01;
outgoingMessage.byte9 = 0x64;
int channel = doc["channel"];
// Check if the channel is valid
if (channel >= 1 && channel <= 14) {
// Send on the specified channel
WiFi.setChannel(channel);
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t*)&outgoingMessage, sizeof(outgoingMessage));
logEspNowResult(result, String(buttonCode), channel);
} else {
// Broadcast on all channels if the channel is invalid (or 0)
esp_err_t result = ESP_OK; // Assume success initially
for (int i = 1; i <= 14; ++i) {
WiFi.setChannel(i);
delay(10);
result = esp_now_send(broadcastAddress, (uint8_t*)&outgoingMessage, sizeof(outgoingMessage));
if (result != ESP_OK) {
break; // Stop if an error occurs on any channel
}
}
// Log the result only once after the loop
logEspNowResult(result, String(buttonCode), 0); // 0 indicates broadcast
}
}