-
Notifications
You must be signed in to change notification settings - Fork 3
/
qswifidimmer.cpp
299 lines (268 loc) · 9.34 KB
/
qswifidimmer.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "espMQTT.h"
#include "qswifidimmer.h"
/*
MOESHOUSE QS WIFI DIMMER D01 (QS-WIFI-D01) tested ok
MOESHOUSE QS WIFI DIMMER D01 (QS-WIFI-D02) tested ok
GPIO13 = S1
GPIO5 = S2
*/
static uint8_t qswifidimmer_nrofchannels = 0;
static uint8_t qswifidimmer_dimvalue[2] = {100, 100};
static uint8_t qswifidimmer_dimoffset[2] = {20 , 20};
static bool qswifidimmer_dimstate[2] = {0, 0};
static bool qswifidimmer_dimenabled[2] = {1, 1};
void(*_qswifidimmer_callback)(uint8_t, uint8_t, bool);
void(*_qswifidimmer_switchcallback)(uint8_t, bool);
void qswifidimmer_init(const uint8_t nrofchannels, void(*callback)(uint8_t, uint8_t, bool))
{
_qswifidimmer_callback = callback;
Serial.begin(9600); //Init serial 9600 baud
Serial.setDebugOutput(false);
// Define all unused GPIO as output to prevent inteference and slow wifi
pinMode(0, OUTPUT);
digitalWrite(0, 0);
// GPIO 1 is TX
pinMode(2, OUTPUT);
digitalWrite(2, 0);
pinMode(3, FUNCTION_0); // Change RX pin to GPIO
pinMode(3, OUTPUT);
digitalWrite(3, 0);
pinMode(4, OUTPUT);
// Gpio 5 = Switch input 2
digitalWrite(4, 0);
// Pin 6 t/m 11 = flash
pinMode(12, OUTPUT);
digitalWrite(12, 0);
// Gpio 13 = Switch input 1
pinMode(14, OUTPUT);
digitalWrite(14, 0);
pinMode(15, OUTPUT);
digitalWrite(15, 0);
pinMode(16, OUTPUT);
digitalWrite(16, 0);
qswifidimmer_nrofchannels = nrofchannels;
qswifidimmer_setdimvalue(0, 0);
qswifidimmer_setdimstate(0, 0);
pinMode(13, INPUT_PULLUP);
if (qswifidimmer_nrofchannels == 2)
{
qswifidimmer_setdimvalue(0, 1);
qswifidimmer_setdimstate(0, 1);
pinMode(5, INPUT_PULLUP);
}
else
{
qswifidimmer_nrofchannels = 1;
pinMode(5, OUTPUT);
digitalWrite(5, 0);
}
}
void qswifidimmer_setswitchcallback(void(*callback)(uint8_t, bool))
{
_qswifidimmer_switchcallback = callback;
for (uint8_t i = 0; i < qswifidimmer_nrofchannels; i++) _qswifidimmer_switchcallback(i, false);
}
void qswifidimmer_send(uint8_t dimchannel)
{
uint16_t calculateddimvalue = (((qswifidimmer_dimvalue[0] * (255 - qswifidimmer_dimoffset[0]))) / 100) + qswifidimmer_dimoffset[0];
if (qswifidimmer_nrofchannels == 1)
{
// Code for QS-WIFI-D01
Serial.write(0xFF); //Header
Serial.write(0x55); //Header
Serial.write(qswifidimmer_dimstate[0] ? calculateddimvalue : 0); //Value CH1
Serial.write(0x05); //Footer
Serial.write(0xDC); //Footer
Serial.write(0x0A); //Footer
}
if (qswifidimmer_nrofchannels == 2)
{
// Code for QS-WIFI-D02
Serial.write(0xFF); //Header
Serial.write(0x55); //Header
Serial.write(dimchannel + 1); //Channel
Serial.write(qswifidimmer_dimstate[0] ? calculateddimvalue : 0); //Value CH1
calculateddimvalue = (((qswifidimmer_dimvalue[1] * (255 - qswifidimmer_dimoffset[1]))) / 100) + qswifidimmer_dimoffset[1];
Serial.write(qswifidimmer_dimstate[1] ? calculateddimvalue : 0); //Value CH2
Serial.write(0x05); //Footer
Serial.write(0xDC); //Footer
Serial.write(0x0A); //Footer
}
}
void qswifidimmer_handle()
{
static bool S[2] = {0, 0};
static bool Spulse[2] = {0, 0};
static uint16_t Scounter[2] = {0, 0};
static uint32_t Stime[2] = {0, 0};
static uint32_t Stimeout[2] = {0, 0};
static bool Sdimup[2] = {1, 1};
static bool fullbrightstart[2] = {0, 0};
static uint8_t dimchannel = 0;
uint32_t msec = millis(); // store millis in variable because otherwise wifi gets slow.
// Iterate trough dimchannels (D01 or D02 dimmer)
if (dimchannel >= qswifidimmer_nrofchannels) dimchannel = 0;
yield();
// Count pulses on input (50hz when button is pressed, but 2 pulses per full sine, so 10ms per pulse)
bool Stemp = (dimchannel == 0 ? digitalRead(13) : digitalRead(5));
Spulse[dimchannel] = 0;
if (Stemp != S[dimchannel])
{
S[dimchannel] = Stemp;
Scounter[dimchannel]++;
Stime[dimchannel] = msec;
Spulse[dimchannel] = 1;
}
yield();
// If 6 pulses are counted do a callback to inform switch is pressed
if (Scounter[dimchannel] == 6)
{
// Callback switch is pressed
_qswifidimmer_switchcallback(dimchannel, true);
}
yield();
// Filter ghost pulses
if (Stime[dimchannel] + 150 < msec) Scounter[dimchannel] = 0;
// When switch was pressed for more then 5 pulses and released for about 100ms switch from on to off or from off to on and also reset counters etc.
if ((Scounter[dimchannel] > 5) && (Stime[dimchannel] + 100 < msec))
{
if (Scounter[dimchannel] < 100)
{
// Toggle lamp on / off
qswifidimmer_dimstate[dimchannel] = qswifidimmer_dimstate[dimchannel] ? 0 : 1;
// If dimming is disabled set dimvalue to 100 when dimstate is 1 or 0 if dimstate is 0
if (!qswifidimmer_dimenabled[dimchannel])
{
qswifidimmer_dimvalue[dimchannel] = qswifidimmer_dimstate[dimchannel] ? 100 : 0;
}
// Send update to dimchannel
qswifidimmer_send(dimchannel);
// Callback to parent
_qswifidimmer_callback(dimchannel, qswifidimmer_dimstate[dimchannel] ? qswifidimmer_dimvalue[dimchannel] : 0, qswifidimmer_dimstate[dimchannel]);
}
// Callback switch is released
_qswifidimmer_switchcallback(dimchannel, false);
Scounter[dimchannel] = 0;
Stime[dimchannel] = 0;
fullbrightstart[dimchannel] = 0;
}
yield();
// If lamps are off and switch is pressed more than 100 pulses (10ms*100=2000ms), put lamps on with full brightness
if (!qswifidimmer_dimstate[dimchannel] && Scounter[dimchannel] > 100)
{
qswifidimmer_dimstate[dimchannel] = true;
qswifidimmer_dimvalue[dimchannel] = 100;
qswifidimmer_send(dimchannel);
_qswifidimmer_callback(dimchannel, qswifidimmer_dimstate[dimchannel] ? qswifidimmer_dimvalue[dimchannel] : 0, qswifidimmer_dimstate[dimchannel]);
fullbrightstart[dimchannel] = 1;
}
yield();
// If dimming for this channel is enabled and state is on and lamps are not just set on with fullbright start changing dimlevel
if ((qswifidimmer_dimenabled[dimchannel]) && (qswifidimmer_dimstate[dimchannel]) && !fullbrightstart[dimchannel])
{
// When switch is released reset pulse counter and timeout, change dim direction and send values to callback handler
if ((Stimeout[dimchannel] > 0) && (Stimeout[dimchannel] < msec))
{
Scounter[dimchannel] = 0;
Stimeout[dimchannel] = 0;
Sdimup[dimchannel] = Sdimup[dimchannel] ? 0 : 1;
_qswifidimmer_callback(dimchannel, qswifidimmer_dimstate[dimchannel] ? qswifidimmer_dimvalue[dimchannel] : 0, qswifidimmer_dimstate[dimchannel]);
}
if (Scounter[dimchannel] > 100)
{
Stimeout[dimchannel] = msec + 50;
if (Sdimup[dimchannel])
{
if (qswifidimmer_dimvalue[dimchannel] < 100)
{
if ((Scounter[dimchannel] % 3) == 0)
{
if (Spulse[dimchannel])
{
qswifidimmer_dimvalue[dimchannel] = qswifidimmer_dimvalue[dimchannel] + 1;
qswifidimmer_send(dimchannel);
}
}
}
else Sdimup[dimchannel] = 0;
}
else
{
if (qswifidimmer_dimvalue[dimchannel] > 1)
{
if ((Scounter[dimchannel] % 3) == 0)
{
if (Spulse[dimchannel])
{
qswifidimmer_dimvalue[dimchannel] = qswifidimmer_dimvalue[dimchannel] - 1;
qswifidimmer_send(dimchannel);
}
}
}
else Sdimup[dimchannel] = 1;
}
}
}
yield();
dimchannel++;
}
void qswifidimmer_setdimoffset(uint8_t value, uint8_t dimchannel)
{
if (dimchannel < qswifidimmer_nrofchannels)
{
qswifidimmer_dimoffset[dimchannel] = MIN(value, 100);
}
}
uint8_t qswifidimmer_getdimoffset(uint8_t dimchannel)
{
if (dimchannel < qswifidimmer_nrofchannels)
{
return qswifidimmer_dimoffset[dimchannel];
}
return 0;
}
void qswifidimmer_setdimvalue(uint8_t value, uint8_t dimchannel)
{
if (dimchannel < qswifidimmer_nrofchannels)
{
if ((qswifidimmer_dimvalue[dimchannel] != value) || !qswifidimmer_dimstate[dimchannel])
{
if (value != 0) qswifidimmer_dimvalue[dimchannel] = value;
qswifidimmer_dimstate[dimchannel] = value > 0 ? true : false;
qswifidimmer_send(dimchannel);
_qswifidimmer_callback(dimchannel, qswifidimmer_dimstate[dimchannel] ? qswifidimmer_dimvalue[dimchannel] : 0, qswifidimmer_dimstate[dimchannel]);
}
}
}
uint8_t qswifidimmer_getdimvalue(uint8_t dimchannel)
{
return dimchannel < qswifidimmer_nrofchannels ? qswifidimmer_dimvalue[dimchannel] : 0;
}
void qswifidimmer_setdimstate(bool dimstate, uint8_t dimchannel)
{
if (dimchannel < qswifidimmer_nrofchannels)
{
if (qswifidimmer_dimstate[dimchannel] != dimstate)
{
qswifidimmer_dimstate[dimchannel] = dimstate;
DEBUG_V("Setting dimstate Channel=%d, Value=%d...\n", dimchannel, dimstate);
qswifidimmer_send(dimchannel);
_qswifidimmer_callback(dimchannel, qswifidimmer_dimstate[dimchannel] ? qswifidimmer_dimvalue[dimchannel] : 0, qswifidimmer_dimstate[dimchannel]);
}
}
}
bool qswifidimmer_getdimstate(uint8_t dimchannel)
{
return dimchannel < qswifidimmer_nrofchannels ? qswifidimmer_dimstate[dimchannel] : 0;
}
void qswifidimmer_setdimenabled(bool dimenabled, uint8_t dimchannel)
{
if (dimchannel < qswifidimmer_nrofchannels)
{
qswifidimmer_dimenabled[dimchannel] = dimenabled;
}
}
bool qswifidimmer_getdimenabled(uint8_t dimchannel)
{
return dimchannel < qswifidimmer_nrofchannels ? qswifidimmer_dimenabled[dimchannel] : 0;
}