-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathble_dimmer.ino
411 lines (353 loc) · 9.63 KB
/
ble_dimmer.ino
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Isaac K. Ko
* ----------------------------------------------------------------------------
*/
/* References
* http://playground.arduino.cc/Main/ACPhaseControl
* http://playground.arduino.cc/Code/ACPhaseControl
* http://alfadex.com/2014/02/dimming-230v-ac-with-arduino-2/
* http://arduinotehniq.blogspot.kr/2014/10/ac-light-dimmer-with-arduino.html
* https://arduinodiy.wordpress.com/2012/10/19/dimmer-arduino/
* http://www.instructables.com/id/Arduino-controlled-light-dimmer-The-circuit/
*/
#include <stdarg.h>
#include <TimerOne.h> // https://github.com/PaulStoffregen/TimerOne
#include <SoftwareSerial.h> // https://www.arduino.cc/en/Reference/SoftwareSerial
#include <PacketSerial.h> // https://github.com/bakercp/PacketSerial
/* device type */
#define SWITCH 0x01
#define DIMMER 0x02
const byte devType = DIMMER;
// undefine for release
//#define __DEBUG__
/* data type */
#define TYPE_RAW 0x01
#define TYPE_DIMMING1 0x02
#define TYPE_UNDEF6 0x04
#define TYPE_UNDEF5 0x08
#define TYPE_UNDEF4 0x10
#define TYPE_UNDEF3 0x20
#define TYPE_UNDEF2 0x40
#define TYPE_UNDEF1 0x80
/* for dimmer */
#define ZERO_CROSS_INT 0 // for zero crossing detect (int0 is pin 2)
#define HZ 60 // 60 in the most America, Korea, Taiwan, Phillippines
// 50 in China, Europe, Japan, and rest of.
/* for bluetooth */
#define BLE_RX 10 // serial pin: connect reverse order
#define BLE_TX 11
#define BLE_DATA_MAX 20 // payload size
/* outlet pin */
const byte outlet[] = {3, 5};
const byte nOutlet = sizeof(outlet) / sizeof(outlet[0]);
/* for dimmer */
/* NOTE: calcuation of dimPeriod
* sec to us, hertz to frequency, half cycle, range
* zero crossing period: 8,333us for 60Hz. 10,000us for 50Hz.
* dimPeriod when dimRange is 128: 65 for 60Hz, 78 for 50Hz.
* dimPeriod when dimRange is 258: 32 for 60Hz, 39 for 50Hz.
* NOTE: When dim range is 128,
dimming values below 10 and above 124 not works in experimental.
* So we use threshold low and high.
*/
const byte dimRange = 128; // greater than 128 may not work
const byte dimThresLo = dimRange >> 3; // approximately 10% of dimRange
const byte dimThresHi = dimRange - dimThresLo; // approximately 90% of dimRange
const byte dimPeriod = (byte)((1000.0 * 1000.0) / (HZ * 2 * dimRange));
volatile byte dimming[nOutlet] = {0}; // the time to idle low on the outlet[]
volatile byte crossing[nOutlet] = {0}; // zero-crossing event
volatile byte trigCnt = 0;
SoftwareSerial bleSerial(BLE_RX, BLE_TX);
PacketSerial pktSerial;
void setup()
{
Serial.begin(9600);
// wait for serial port to connect
// needed for native USB port only
while (!Serial) { ; }
for (int i=0; i<nOutlet; i++)
{
pinMode(outlet[i], OUTPUT);
}
bleSerial.begin(9600);
pktSerial.setPacketHandler(&bleParser);
pktSerial.begin(&bleSerial);
if (devType == DIMMER)
{
pinMode(ZERO_CROSS_INT, INPUT);
attachInterrupt(ZERO_CROSS_INT, zeroCrossInt, RISING);
Timer1.initialize();
Timer1.attachInterrupt(triggerTriac, dimPeriod);
}
}
void zeroCrossInt()
{
// set event flag
for (int i=0; i<nOutlet; i++)
{
// dimming[] indicate delay time. lower is on, higher is off
if (dimming[i] < dimThresLo)
{
digitalWrite(outlet[i], HIGH);
crossing[i] = 0;
}
else if (dimming[i] > dimThresHi)
{
digitalWrite(outlet[i], LOW);
crossing[i] = 0;
}
else
{
crossing[i] = 1;
}
}
// reset count
trigCnt = 0;
}
/* NOTE: Do not use delay() in interrupt. It depends on interrupt itself.
* Instead, use delayMicroseconds() that run busy loop(NOP).
*/
void triggerTriac()
{
for (int i=0; i<nOutlet; i++)
{
if (crossing[i] && (trigCnt == dimming[i]))
{
digitalWrite(outlet[i], HIGH);
delayMicroseconds(10); // propagation delay
digitalWrite(outlet[i], LOW);
crossing[i] = 0; // reset event flag
}
}
trigCnt++;
}
// event handler
typedef struct {
byte port;
byte value;
} rawValue;
/* Payload (data)
* .----------------------------------------.
* | rawValue1 | rawVale2 | ... | rawValueN |
* |-----------+----------+-----+-----------|
* | 2 | 2 | ... | 2 |
* '----------------------------------------'
* N is up to 9 ( (BLE_DATA_MAX-2) / sizeof(rawValue) )
*/
void bleRaw(byte *data, byte sz)
{
byte rvSize = sizeof(rawValue);
rawValue *rv = NULL;
// sanity check
if (sz < rvSize) { return; }
if ((sz % rvSize) != 0) { return; }
if ((sz / rvSize) > nOutlet) { return; }
dumpPkt(data, sz);
for (int i=0; i<sz; i+=rvSize)
{
rv = (rawValue *)(data + i);
if (rv->port == 0) { continue; }
rv->port--;
if (devType == SWITCH)
{
if (rv->value >= 0x80)
{
digitalWrite(outlet[rv->port], HIGH);
syslog("LED %d(%dpin) ON(0x%02X)",
rv->port, outlet[rv->port], rv->value);
}
else
{
digitalWrite(outlet[rv->port], LOW);
syslog("LED %d(%dpin) OFF(0x%02X)",
rv->port, outlet[rv->port], rv->value);
}
}
else
{
dimming[rv->port] = (dimRange - rv->value);
syslog("LED %d(%dpin) Value(0x%02X)",
rv->port, outlet[rv->port], rv->value);
}
}
}
// y = ax^2 + bx + c
inline byte bleDimmingLinear(byte x, int8_t a, byte da, int8_t b, byte db, int8_t c)
{
int y = 0;
// prevent divide by zero
if (da == 0) { da = 1; }
if (db == 0) { db = 1; }
y = a*x*x/da + b*x/db + c;
if (y > dimRange) { return dimRange; }
else if (y < 0) { return 0; }
return y;
}
/* NOTE: small divider could be OK.
you can use 4bit per divider and merge two byte divider into one byte.
then, we can control three outlet within 20 bytes
*/
typedef struct {
byte port;
int8_t a; // coefficient. signed byte
uint8_t da; // divider for coefficient
int8_t b;
uint8_t db;
int8_t c; // last constant doesn't need divider
} dimValue;
/* duration * 10 = 1 seconds
*
* Payload (data)
* .-----------------------------------------.
* | dimValue1 | dimValue2 | ... | dimValueN |
* |-----------+-----------+-----+-----------|
* | 6 | 6 | ... | 6 |
* '-----------------------------------------'
* N is up to 3 ( (BLE_DATA_MAX-2) / sizeof(dimValue) )
* duration is transition time
* if function is y = -1/128x^2 + 2x, you can set
* (ca, da) = (-1, 128)
* (cb, db) = ( 2, 1)
* (cc ) = ( 0 )
*
* NOTE: this funtion use busy loop
*/
void bleDimming1(const byte *data, const byte sz, const byte duration)
{
const byte dimSize = sizeof(dimValue);
const byte delta = duration*100 / dimRange;
dimValue *dv = NULL;
byte y = 0;
// sanity check
if (duration > dimRange) { return; }
if (sz < dimSize) { return; }
if ((sz % dimSize) != 0) { return; }
if ((sz / dimSize) > nOutlet) { return; }
if (devType != DIMMER) { return; }
dumpPkt(data, sz);
for (int i=0; i<dimRange; i++)
{
for (int j=0; j<sz; j+=dimSize)
{
dv = (dimValue *)(data + j);
if (dv->port == 0) { continue; }
if (i == 0)
{
syslog("LED %d(%dpin) for (%d/10)s [y=(%d/%d)x^2 + (%d/%d)x + (%d)]",
(dv->port-1), outlet[(dv->port-1)], duration,
dv->a, dv->da, dv->b, dv->db, dv->c);
}
y = bleDimmingLinear(i, dv->a, dv->da, dv->b, dv->db, dv->c);
dimming[(dv->port-1)] = dimRange - y;
}
delay(delta);
}
}
/* Datagram (buffer)
.----------------------------------.
| Type | Length | Payload |
|------+--------+------------------|
| 1 | 1 | BLE_DATA_MAX - 2 |
'----------------------------------'
*/
void bleParser(const byte* buffer, size_t size)
{
byte datagram[BLE_DATA_MAX] = {0};
byte type, sz;
char buf[128];
if (buffer == NULL)
return;
if (size > BLE_DATA_MAX)
return;
dumpPkt(buffer, size);
memcpy(datagram, buffer, size);
// parse header
type = datagram[0];
sz = datagram[1];
if (sz < 2 || sz > BLE_DATA_MAX)
return;
switch(type)
{
case TYPE_RAW:
bleRaw(datagram + 2, sz - 2);
break;
case TYPE_DIMMING1:
if (sz < 3) { break; }
bleDimming1(datagram + 3, sz - 3, *(datagram + 2));
break;
default:
break;
}
}
void debug_dimmer()
{
// fixed dimming
#if 0
// 0%
rawValue rv0 = {1, 0};
bleRaw((byte*)&rv0, sizeof(rawValue));
delay(3000);
#endif
#if 0
// 50%
rawValue rv50 = {1, 64};
bleRaw((byte*)&rv50, sizeof(rawValue));
delay(3000);
#endif
#if 0
// 100%
rawValue rv100 = {1, 128};
bleRaw((byte*)&rv100, sizeof(rawValue));
delay(3000);
#endif
// continuous dimming
#if 0
// y = x
dimValue dv1 = {1, 0, 1, 1, 1, 0};
bleDimming1((byte*)&dv1, sizeof(dimValue), 50);
#endif
#if 0
// y = 1/128x^2
dimValue dv2 = {1, 1, 128, 0, 1, 0};
bleDimming1((byte*)&dv2, sizeof(dimValue), 50);
#endif
#if 0
// y = -1/128x^2 + 2x
dimValue dv3 = {1, -1, 128, 2, 1, 0};
bleDimming1((byte*)&dv3, sizeof(dimValue), 50);
#endif
}
void loop()
{
pktSerial.update();
#if defined(__DEBUG__)
debug_dimmer();
#endif
}
void dumpPkt(const byte* packet, size_t size)
{
#if defined(__DEBUG__)
char buf[10] = "";
Serial.print("DUMP ");
for(int i=0; i<size; i++)
{
sprintf(buf, "[%02X] ", packet[i]);
Serial.print(buf);
}
Serial.println();
#endif
}
void syslog(char *fmt, ... )
{
#if defined(__DEBUG__)
char buf[128];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), (const char *)fmt, args);
va_end(args);
Serial.println(buf);
#endif
}