-
Notifications
You must be signed in to change notification settings - Fork 2
/
HB-RC-6-PBU-LED.ino
512 lines (454 loc) · 14.4 KB
/
HB-RC-6-PBU-LED.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2019-08-26 jp112sdl Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
// ci-test=yes board=644p aes=no
// define this to read the device id, serial and device type from bootloader section
// #define USE_OTA_BOOTLOADER
#define EI_NOTEXTERNAL
#include <EnableInterrupt.h>
#include <SPI.h>
#include <AskSinPP.h>
#include <LowPower.h>
#include <Register.h>
#include <MultiChannelDevice.h>
#include <FastLED.h>
#include <Switch.h>
#include <Remote.h>
#ifdef __AVR_ATmega328P__
#define CONFIG_BUTTON_PIN 8
#define WSLED_PIN 9
#define WSLED_ACTIVATE_PIN 6
#define ONBOARD_LED_PIN1 4
#define ONBOARD_LED_PIN2 5
#define CC1101_CS 10
#define CC1101_GDO0 2
#define BTN1_PIN 14
#define BTN2_PIN 15
#define BTN3_PIN 16
#define BTN4_PIN 17
#define BTN5_PIN 18
#define BTN6_PIN 19
#endif
#ifdef __AVR_ATmega644P__
//Pin Definitionen (when using 644P(A): use Standard Pinout)
#define CONFIG_BUTTON_PIN 1 //PB1
#define WSLED_PIN 13 //PD5
#define WSLED_ACTIVATE_PIN 20 //PC4
#define ONBOARD_LED_PIN1 18 //PC2
#define ONBOARD_LED_PIN2 19 //PC3
#define CC1101_CS 4 //PB4
#define CC1101_GDO0 10 //PD2
#define BTN1_PIN 26 //PA2
#define BTN2_PIN 25 //PA1
#define BTN3_PIN 24 //PA0
#define BTN4_PIN 27 //PA3
#define BTN5_PIN 28 //PA4
#define BTN6_PIN 29 //PA5
#endif
//Einstellungen für die RGB LEDs
#define WSNUM_LEDS 6
#define WSLED_TYPE SK6812 //LED Typ
//#define WSLED_TYPE WS2812 //LED Typ
#define WSCOLOR_ORDER GRB //Farbreihenfolge
#define PEERS_PER_LED_CHANNEL 4
#define PEERS_PER_RC_CHANNEL 4
using namespace as;
const struct DeviceInfo PROGMEM devinfo = {
{0xF3, 0x3E, 0x00}, // Device ID
"JPRC6LED01", // Device Serial
{0xF3, 0x3E}, // Device Model
0x10, // Firmware Version
as::DeviceType::Dimmer, // Device Type
{0x01, 0x01} // Info Bytes
};
typedef AskSin<DualStatusLed<ONBOARD_LED_PIN1, ONBOARD_LED_PIN2>, NoBattery, Radio<LibSPI<CC1101_CS>, CC1101_GDO0>> Hal;
Hal hal;
DEFREGISTER(OUReg0, MASTERID_REGS)
class OUList0 : public RegList0<OUReg0> {
public:
OUList0(uint16_t addr) : RegList0<OUReg0>(addr) {}
void defaults () {
clear();
}
};
DEFREGISTER(LEDReg1, CREG_AES_ACTIVE)
class LEDList1 : public RegList1<LEDReg1> {
public:
LEDList1(uint16_t addr) : RegList1<LEDReg1>(addr) {}
void defaults () {
clear();
}
};
DEFREGISTER(OUReg3, SWITCH_LIST3_STANDARD_REGISTER, PREG_ACTTYPE, PREG_ACTNUM, PREG_ACTINTENS);
typedef RegList3<OUReg3> SwPeerListEx;
class OUList3 : public SwitchList3Tmpl<SwPeerListEx> {
public:
OUList3(uint16_t addr) : SwitchList3Tmpl<SwPeerListEx>(addr) {}
void defaults() {
SwitchList3Tmpl<SwPeerListEx>::defaults();
}
void even () {
SwitchList3Tmpl<SwPeerListEx>::even();
}
void odd () {
SwitchList3Tmpl<SwPeerListEx>::odd();
}
void single () {
SwitchList3Tmpl<SwPeerListEx>::single();
}
};
class Pixels {
private:
uint8_t state;
bool pwr;
protected:
CRGB leds[WSNUM_LEDS];
private:
#ifdef WSLED_ACTIVATE_PIN
void powerUp() {
if (pwr == false) {
digitalWrite(WSLED_ACTIVATE_PIN, LOW);
_delay_ms(5);
pwr = true;
}
}
void powerDown() {
FastLED.show();
if (pwr == true) {
digitalWrite(WSLED_ACTIVATE_PIN, HIGH);
pwr = false;
}
}
#else
void powerUp() {}
void powerDown() { FastLED.show(); }
#endif
public:
Pixels () : state(0), pwr(false) {}
~Pixels () {}
void init() {
#ifdef WSLED_ACTIVATE_PIN
pinMode(WSLED_ACTIVATE_PIN, OUTPUT);
#endif
powerUp();
FastLED.addLeds<WSLED_TYPE, WSLED_PIN, WSCOLOR_ORDER>(leds, WSNUM_LEDS);
FastLED.setBrightness(255);
uint32_t bootColors[3] = {CRGB::Red, CRGB::Green, CRGB::Blue };
for (uint8_t i = 0; i < 3; i++) {
fill_solid(leds, WSNUM_LEDS, bootColors[i]);
FastLED.show();
_delay_ms(400);
}
fill_solid(leds, WSNUM_LEDS, CRGB::Black);
powerDown();
}
void setLed(uint8_t ledNum, uint8_t color, uint8_t brightness) {
if (brightness > 0) state |= 1UL << (ledNum); else state &= ~(1UL << (ledNum));
leds[ledNum] = CHSV(color, color < 200 ? 255 : 0, brightness);
if ( state > 0 ) powerUp();
FastLED.show();
if ( state == 0 ) powerDown();
}
} pixels;
class LEDChannel : public ActorChannel<Hal, LEDList1, OUList3, PEERS_PER_LED_CHANNEL, OUList0, SwitchStateMachine> {
public:
class LEDOffDelayAlarm : public Alarm {
LEDChannel& chan;
public:
LEDOffDelayAlarm (LEDChannel& c) : Alarm(0), chan(c) {}
virtual ~LEDOffDelayAlarm () {}
void trigger (__attribute__ ((unused)) AlarmClock& clock) {
chan.ledOff(true);
}
};
class LEDBlinkAlarm : public Alarm {
LEDChannel& chan;
private:
uint8_t decis;
bool even;
public:
LEDBlinkAlarm (LEDChannel& c) : Alarm(0), chan(c), decis(0), even(false) {}
virtual ~LEDBlinkAlarm () {}
void start(uint8_t d) {
decis = d;
even = false;
sysclock.cancel(*this);
this->set(decis2ticks(decis));
sysclock.add(*this);
}
void trigger (__attribute__ ((unused)) AlarmClock& clock) {
this->set(decis2ticks(decis));
pixels.setLed(chan.number() -7 , chan.Color(), even ? chan.Brightness() : 0);
even = !even;
clock.add(*this);
}
};
private:
bool first;
uint8_t color;
uint8_t brightness;
protected:
LEDOffDelayAlarm ledOffDelayAlarm;
LEDBlinkAlarm ledBlinkAlarm;
typedef ActorChannel<Hal, LEDList1, OUList3, PEERS_PER_LED_CHANNEL, OUList0, SwitchStateMachine> BaseChannel;
public:
LEDChannel () : BaseChannel(), first(true), color(0), brightness(0), ledOffDelayAlarm(*this), ledBlinkAlarm(*this) {}
virtual ~LEDChannel() {}
uint8_t Color() {
return color;
}
uint8_t Brightness() {
return brightness;
}
void updateLED(bool blackOut=false) {
pixels.setLed(number() -7 , color, blackOut ? 0: brightness);
}
void ledOff(bool setCh) {
sysclock.cancel(ledOffDelayAlarm);
sysclock.cancel(ledBlinkAlarm);
updateLED(true);
if (setCh) BaseChannel::set( 0x00, 0x00, 0xffff );
}
void setLedOffDelay(uint16_t dly) {
DPRINT("set off delay ");DDECLN(dly);
sysclock.cancel(ledOffDelayAlarm);
ledOffDelayAlarm.set(dly);
sysclock.add(ledOffDelayAlarm);
}
void ledOn(bool setCh) {
sysclock.cancel(ledBlinkAlarm);
updateLED();
if (setCh) BaseChannel::set( 0xc8, 0x00, 0xffff );
}
void setLedColor(uint8_t val) {
switch (val) {
case 11:
color = 0; // Red;
break;
case 21:
color = 96; // Green;
break;
case 31:
color = 64; // Yellow;
break;
case 41:
color = 171; // Blue;
break;
case 51:
color = 192; // Violet;
break;
case 61:
color = 132; // Turquoise;
break;
case 71:
color = 200; // White;
break;
case 81:
color = 32; // Orange;
break;
default:
color = 0; // Black;
brightness = 0;
break;
}
updateLED();
}
void setLedBrightness(uint8_t val) {
brightness = val;
updateLED();
}
void setLedBlink(uint8_t decis) {
if (decis > 0)
ledBlinkAlarm.start(decis);
}
bool process (const ActionSetMsg& msg) {
BaseChannel::set( msg.value(), msg.ramp(), msg.delay() );
return true;
}
bool process (const ActionCommandMsg& msg) {
static uint8_t lastmsgcnt = 0;
if (msg.count() != lastmsgcnt) {
lastmsgcnt = msg.count();
setLedBrightness(msg.value(1));
uint8_t color = msg.value(3);
if (color == 0) {
ledOff(true);
} else {
setLedColor(color);
uint16_t t = ((msg.value(msg.len() - 2)) << 8) + (msg.value(msg.len() - 1));
if (t > 0 && t != 0x83CA) {
setLedOffDelay(AskSinBase::intTimeCvt(t));
}
ledOn(true);
setLedBlink(msg.value(2));
}
}
return true;
}
uint8_t getConditionForStatePl(uint8_t stat,const SwPeerListEx& lst) const {
switch( stat ) {
case AS_CM_JT_ONDELAY: return lst.ctDlyOn();
case AS_CM_JT_ON: return lst.ctOn();
case AS_CM_JT_OFFDELAY: return lst.ctDlyOff();
case AS_CM_JT_OFF: return lst.ctOff();
}
return AS_CM_CT_X_GE_COND_VALUE_LO;
}
void runPl (const SwPeerListEx& pl,uint8_t cnt, uint8_t nextstate) {
if (cnt != lastmsgcnt) {
lastmsgcnt = cnt;
DPRINT("runPl ");DDECLN(number());
//DPRINT(F("ACT_TYPE ")); DDECLN(pl.actType()); // Farbe
//DPRINT(F("ACT_NUM ")); DDECLN(pl.actNum()); // BPM
//DPRINT(F("ACT_INTENS ")); DDECLN(pl.actIntens());// Helligkeit
//DPRINT(F("OFFDELAY ")); DDECLN(pl.offDly()); // Ausschaltverzögerung
if (pl.actType() == 0 || nextstate == AS_CM_JT_OFF || nextstate == AS_CM_JT_OFFDELAY) {
ledOff(true);
} else {
setLedColor(pl.actType());
setLedBrightness(pl.actIntens());
if (pl.offDly() > 0)
setLedOffDelay(AskSinBase::byteTimeCvt(pl.offDly()));
ledOn(true);
setLedBlink(pl.actNum());
}
}
}
void sensorPl (const SwPeerListEx& lst,uint8_t counter,uint8_t value) {
uint8_t cond = getConditionForStatePl(state,lst);
DPRINT("state = ");DDEC(state);DPRINT(", cond = ");DDEC(cond);DPRINT(", value = ");DDECLN(value);
bool doit = false;
switch( cond ) {
case AS_CM_CT_X_GE_COND_VALUE_LO:
doit = (value >= lst.ctValLo());
break;
case AS_CM_CT_X_GE_COND_VALUE_HI:
doit = (value >= lst.ctValHi());
break;
case AS_CM_CT_X_LT_COND_VALUE_LO:
doit = (value < lst.ctValLo());
break;
case AS_CM_CT_X_LT_COND_VALUE_HI:
doit = (value < lst.ctValHi());
break;
case AS_CM_CT_COND_VALUE_LO_LE_X_LT_COND_VALUE_HI:
doit = ((lst.ctValLo() <= value) && (value < lst.ctValHi()));
break;
case AS_CM_CT_X_LT_COND_VALUE_LO_OR_X_GE_COND_VALUE_HI:
doit =((value < lst.ctValLo()) || (value >= lst.ctValHi()));
break;
}
if( doit == true ) {
runPl(lst,counter, getNextState());
}
}
bool process (const SensorEventMsg& msg) {
bool lg = msg.isLong();
Peer p(msg.peer());
uint8_t cnt = msg.counter();
uint8_t value = msg.value();
OUList3 l3 = BaseChannel::getList3(p);
if( l3.valid() == true ) {
typename OUList3::PeerList pl = lg ? l3.lg() : l3.sh();
if (lg == false || pl.multiExec() == false ) {
DPRINTLN("SensorEventMsg");
sensorPl(pl,cnt,value);
}
return true;
}
return false;
}
bool process (const RemoteEventMsg& msg) {
bool lg = msg.isLong();
Peer p(msg.peer());
uint8_t cnt = msg.counter();
OUList3 l3 = BaseChannel::getList3(p);
if ( l3.valid() == true ) {
typename OUList3::PeerList pl = lg ? l3.lg() : l3.sh();
if (lg == false || pl.multiExec() == false ) {
switch (pl.actionType()) {
case AS_CM_ACTIONTYPE_JUMP_TO_TARGET:
runPl(pl, cnt, AS_CM_JT_ON);
break;
case AS_CM_ACTIONTYPE_TOGGLE_TO_COUNTER:
runPl(pl, cnt, (cnt & 0x01) == 0x01 ? AS_CM_JT_ON : AS_CM_JT_OFF);
break;
case AS_CM_ACTIONTYPE_TOGGLE_INVERSE_TO_COUNTER:
runPl(pl, cnt, (cnt & 0x00) == 0x01 ? AS_CM_JT_ON : AS_CM_JT_OFF);
break;
}
}
return true;
}
return false;
}
void init() {
ledOff(true);
//this->changed(true);
first = false;
}
uint8_t flags () const {
return 0;
}
void configChanged() { }
virtual void switchState(__attribute__((unused)) uint8_t oldstate, __attribute__((unused)) uint8_t newstate, __attribute__((unused)) uint32_t delay) {
if ( newstate == AS_CM_JT_OFF ) {
if (first == false ) {
this->ledOff(false);
}
}
if ( newstate == AS_CM_JT_ON ) {
this->ledOn(false);
}
this->changed(true);
}
};
typedef RemoteChannel<Hal, PEERS_PER_RC_CHANNEL, OUList0> RemChannel;
class OUDevice : public ChannelDevice<Hal, VirtBaseChannel<Hal, OUList0>, 12, OUList0> {
public:
VirtChannel<Hal, RemChannel, OUList0> rc[6];
VirtChannel<Hal, LEDChannel, OUList0> ledc[6];
public:
typedef ChannelDevice<Hal, VirtBaseChannel<Hal, OUList0>, 12, OUList0> DeviceType;
OUDevice (const DeviceInfo& info, uint16_t addr) : DeviceType(info, addr) {
for (uint8_t i = 0; i < 6; i++) DeviceType::registerChannel( rc[i], i+1);
for (uint8_t i = 0; i < 6; i++) DeviceType::registerChannel(ledc[i], i+7);
}
virtual ~OUDevice () {}
RemChannel& remoteChannel (uint8_t num) {
return rc[num-1];
}
LEDChannel& ledChannel(uint8_t num) {
return ledc[num-7];
}
virtual void configChanged () {}
};
OUDevice sdev(devinfo, 0x20);
ConfigButton<OUDevice> cfgBtn(sdev);
void setup () {
DINIT(57600, ASKSIN_PLUS_PLUS_IDENTIFIER);
pixels.init();
sdev.init(hal);
buttonISR(cfgBtn, CONFIG_BUTTON_PIN);
remoteChannelISR(sdev.remoteChannel(1), BTN1_PIN);
remoteChannelISR(sdev.remoteChannel(2), BTN2_PIN);
remoteChannelISR(sdev.remoteChannel(3), BTN3_PIN);
remoteChannelISR(sdev.remoteChannel(4), BTN4_PIN);
remoteChannelISR(sdev.remoteChannel(5), BTN5_PIN);
remoteChannelISR(sdev.remoteChannel(6), BTN6_PIN);
sdev.ledChannel(7).init();
sdev.ledChannel(8).init();
sdev.ledChannel(9).init();
sdev.ledChannel(10).init();
sdev.ledChannel(11).init();
sdev.ledChannel(12).init();
sdev.initDone();
}
void loop() {
bool worked = hal.runready();
bool poll = sdev.pollRadio();
if ( worked == false && poll == false ) {
hal.activity.savePower<Idle<>>(hal);
}
}