-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathSomfy.cpp
5132 lines (5060 loc) · 206 KB
/
Somfy.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Preferences.h>
#include <ELECHOUSE_CC1101_SRC_DRV.h>
#include <SPI.h>
#include <WebServer.h>
#include <esp_task_wdt.h>
#include "Utils.h"
#include "ConfigSettings.h"
#include "Somfy.h"
#include "Sockets.h"
#include "MQTT.h"
#include "ConfigFile.h"
#include "GitOTA.h"
extern Preferences pref;
extern SomfyShadeController somfy;
extern SocketEmitter sockEmit;
extern ConfigSettings settings;
extern MQTTClass mqtt;
extern GitUpdater git;
uint8_t rxmode = 0; // Indicates whether the radio is in receive mode. Just to ensure there isn't more than one interrupt hooked.
#define SYMBOL 640
#if defined(ESP8266)
#define RECEIVE_ATTR ICACHE_RAM_ATTR
#elif defined(ESP32)
#define RECEIVE_ATTR IRAM_ATTR
#else
#define RECEIVE_ATTR
#endif
#define SETMY_REPEATS 35
#define TILT_REPEATS 15
#define TX_QUEUE_DELAY 100
int sort_asc(const void *cmp1, const void *cmp2) {
int a = *((uint8_t *)cmp1);
int b = *((uint8_t *)cmp2);
if(a == b) return 0;
else if(a < b) return -1;
return 1;
}
static int interruptPin = 0;
static uint8_t bit_length = 56;
somfy_commands translateSomfyCommand(const String& string) {
if (string.equalsIgnoreCase("My")) return somfy_commands::My;
else if (string.equalsIgnoreCase("Up")) return somfy_commands::Up;
else if (string.equalsIgnoreCase("MyUp")) return somfy_commands::MyUp;
else if (string.equalsIgnoreCase("Down")) return somfy_commands::Down;
else if (string.equalsIgnoreCase("MyDown")) return somfy_commands::MyDown;
else if (string.equalsIgnoreCase("UpDown")) return somfy_commands::UpDown;
else if (string.equalsIgnoreCase("MyUpDown")) return somfy_commands::MyUpDown;
else if (string.equalsIgnoreCase("Prog")) return somfy_commands::Prog;
else if (string.equalsIgnoreCase("SunFlag")) return somfy_commands::SunFlag;
else if (string.equalsIgnoreCase("StepUp")) return somfy_commands::StepUp;
else if (string.equalsIgnoreCase("StepDown")) return somfy_commands::StepDown;
else if (string.equalsIgnoreCase("Flag")) return somfy_commands::Flag;
else if (string.equalsIgnoreCase("Sensor")) return somfy_commands::Sensor;
else if (string.equalsIgnoreCase("Toggle")) return somfy_commands::Toggle;
else if (string.equalsIgnoreCase("Favorite")) return somfy_commands::Favorite;
else if (string.equalsIgnoreCase("Stop")) return somfy_commands::Stop;
else if (string.startsWith("fav") || string.startsWith("FAV")) return somfy_commands::Favorite;
else if (string.startsWith("mud") || string.startsWith("MUD")) return somfy_commands::MyUpDown;
else if (string.startsWith("md") || string.startsWith("MD")) return somfy_commands::MyDown;
else if (string.startsWith("ud") || string.startsWith("UD")) return somfy_commands::UpDown;
else if (string.startsWith("mu") || string.startsWith("MU")) return somfy_commands::MyUp;
else if (string.startsWith("su") || string.startsWith("SU")) return somfy_commands::StepUp;
else if (string.startsWith("sd") || string.startsWith("SD")) return somfy_commands::StepDown;
else if (string.startsWith("sen") || string.startsWith("SEN")) return somfy_commands::Sensor;
else if (string.startsWith("p") || string.startsWith("P")) return somfy_commands::Prog;
else if (string.startsWith("u") || string.startsWith("U")) return somfy_commands::Up;
else if (string.startsWith("d") || string.startsWith("D")) return somfy_commands::Down;
else if (string.startsWith("m") || string.startsWith("M")) return somfy_commands::My;
else if (string.startsWith("f") || string.startsWith("F")) return somfy_commands::Flag;
else if (string.startsWith("s") || string.startsWith("S")) return somfy_commands::SunFlag;
else if (string.startsWith("t") || string.startsWith("T")) return somfy_commands::Toggle;
else if (string.length() == 1) return static_cast<somfy_commands>(strtol(string.c_str(), nullptr, 16));
else return somfy_commands::My;
}
String translateSomfyCommand(const somfy_commands cmd) {
switch (cmd) {
case somfy_commands::Up:
return "Up";
case somfy_commands::Down:
return "Down";
case somfy_commands::My:
return "My";
case somfy_commands::MyUp:
return "My+Up";
case somfy_commands::UpDown:
return "Up+Down";
case somfy_commands::MyDown:
return "My+Down";
case somfy_commands::MyUpDown:
return "My+Up+Down";
case somfy_commands::Prog:
return "Prog";
case somfy_commands::SunFlag:
return "Sun Flag";
case somfy_commands::Flag:
return "Flag";
case somfy_commands::StepUp:
return "Step Up";
case somfy_commands::StepDown:
return "Step Down";
case somfy_commands::Sensor:
return "Sensor";
case somfy_commands::Toggle:
return "Toggle";
case somfy_commands::Favorite:
return "Favorite";
case somfy_commands::Stop:
return "Stop";
default:
return "Unknown(" + String((uint8_t)cmd) + ")";
}
}
byte somfy_frame_t::calc80Checksum(byte b0, byte b1, byte b2) {
byte cs80 = 0;
cs80 = (((b0 & 0xF0) >> 4) ^ ((b1 & 0xF0) >> 4));
cs80 ^= ((b2 & 0xF0) >> 4);
cs80 ^= (b0 & 0x0F);
cs80 ^= (b1 & 0x0F);
return cs80;
}
void somfy_frame_t::decodeFrame(byte* frame) {
byte decoded[10];
decoded[0] = frame[0];
// The last 3 bytes are not encoded even on 80-bits. Go figure.
decoded[7] = frame[7];
decoded[8] = frame[8];
decoded[9] = frame[9];
for (byte i = 1; i < 7; i++) {
decoded[i] = frame[i] ^ frame[i - 1];
}
byte checksum = 0;
// We only want the upper nibble for the command byte.
for (byte i = 0; i < 7; i++) {
if (i == 1) checksum = checksum ^ (decoded[i] >> 4);
else checksum = checksum ^ decoded[i] ^ (decoded[i] >> 4);
}
checksum &= 0b1111; // We keep the last 4 bits only
this->checksum = decoded[1] & 0b1111;
this->encKey = decoded[0];
// Lets first determine the protocol.
this->cmd = (somfy_commands)((decoded[1] >> 4));
if(this->cmd == somfy_commands::RTWProto) {
if(this->encKey >= 160) {
this->proto = radio_proto::RTS;
if(this->encKey == 164) this->cmd = somfy_commands::Toggle;
}
else if(this->encKey > 148) {
this->proto = radio_proto::RTV;
this->cmd = (somfy_commands)(this->encKey - 148);
}
else if(this->encKey > 133) {
this->proto = radio_proto::RTW;
this->cmd = (somfy_commands)(this->encKey - 133);
}
}
else this->proto = radio_proto::RTS;
// We reuse this memory address so we must reset the processed
// flag. This will ensure we can see frames on the first beat.
this->processed = false;
this->rollingCode = decoded[3] + (decoded[2] << 8);
this->remoteAddress = (decoded[6] + (decoded[5] << 8) + (decoded[4] << 16));
this->valid = this->checksum == checksum && this->remoteAddress > 0 && this->remoteAddress < 16777215;
if (this->cmd != somfy_commands::Sensor && this->valid) this->valid = (this->rollingCode > 0);
// Next lets process some of the RTS extensions for 80-bit frames
if(this->valid && this->proto == radio_proto::RTS && this->bitLength == 80) {
// Do a parity checksum on the 80 bit data.
if((decoded[9] & 0x0F) != this->calc80Checksum(decoded[7], decoded[8], decoded[9])) this->valid = false;
if(this->valid) {
// Translate extensions for stop and favorite.
if(this->cmd == somfy_commands::My) this->cmd = (somfy_commands)((decoded[1] >> 4) | ((decoded[8] & 0x0F) << 4));
// Bit packing to get the step size prohibits translation on the byte.
else if(this->cmd == somfy_commands::StepDown) this->cmd = (somfy_commands)((decoded[1] >> 4) | ((decoded[8] & 0x08) << 4));
}
}
if (this->valid) {
// Check for valid command.
switch (this->cmd) {
//case somfy_commands::Unknown0:
case somfy_commands::My:
case somfy_commands::Up:
case somfy_commands::MyUp:
case somfy_commands::Down:
case somfy_commands::MyDown:
case somfy_commands::UpDown:
case somfy_commands::MyUpDown:
case somfy_commands::Prog:
case somfy_commands::Flag:
case somfy_commands::SunFlag:
case somfy_commands::Sensor:
break;
case somfy_commands::UnknownD:
case somfy_commands::RTWProto:
this->valid = false;
break;
case somfy_commands::StepUp:
case somfy_commands::StepDown:
// Decode the step size.
this->stepSize = ((decoded[8] & 0x07) << 4) | ((decoded[9] & 0xF0) >> 4);
break;
case somfy_commands::Toggle:
case somfy_commands::Favorite:
case somfy_commands::Stop:
break;
default:
this->valid = false;
break;
}
}
if(this->valid && this->encKey == 0) this->valid = false;
if (!this->valid) {
Serial.print("INVALID FRAME ");
Serial.print("KEY:");
Serial.print(this->encKey);
Serial.print(" ADDR:");
Serial.print(this->remoteAddress);
Serial.print(" CMD:");
Serial.print(translateSomfyCommand(this->cmd));
Serial.print(" RCODE:");
Serial.println(this->rollingCode);
Serial.println(" KEY 1 2 3 4 5 6 ");
Serial.println("--------------------------------");
Serial.print("ENC ");
for (byte i = 0; i < 10; i++) {
if (frame[i] < 10)
Serial.print(" ");
else if (frame[i] < 100)
Serial.print(" ");
Serial.print(frame[i]);
Serial.print(" ");
}
Serial.println();
Serial.print("DEC ");
for (byte i = 0; i < 10; i++) {
if (decoded[i] < 10)
Serial.print(" ");
else if (decoded[i] < 100)
Serial.print(" ");
Serial.print(decoded[i]);
Serial.print(" ");
}
Serial.println();
}
}
void somfy_frame_t::decodeFrame(somfy_rx_t *rx) {
this->hwsync = rx->cpt_synchro_hw;
this->pulseCount = rx->pulseCount;
this->bitLength = rx->bit_length;
this->rssi = ELECHOUSE_cc1101.getRssi();
this->decodeFrame(rx->payload);
}
byte somfy_frame_t::encode80Byte7(byte start, uint8_t repeat) {
while((repeat * 4) + start > 255) repeat -= 15;
return start + (repeat * 4);
}
void somfy_frame_t::encode80BitFrame(byte *frame, uint8_t repeat) {
switch(this->cmd) {
// Step up and down commands encode the step size into the last 3 bytes.
case somfy_commands::StepUp:
if(repeat == 0) frame[1] = (static_cast<byte>(somfy_commands::StepDown) << 4) | (frame[1] & 0x0F);
if(this->stepSize == 0) this->stepSize = 1;
frame[7] = 132; // For simplicity this appears to be constant.
frame[8] = ((this->stepSize & 0x70) >> 4) | 0x38;
frame[9] = ((this->stepSize & 0x0F) << 4);
frame[9] |= this->calc80Checksum(frame[7], frame[8], frame[9]);
break;
case somfy_commands::StepDown:
if(repeat == 0) frame[1] = (static_cast<byte>(somfy_commands::StepDown) << 4) | (frame[1] & 0x0F);
if(this->stepSize == 0) this->stepSize = 1;
frame[7] = 132; // For simplicity this appears to be constant.
frame[8] = ((this->stepSize & 0x70) >> 4) | 0x30;
frame[9] = ((this->stepSize & 0x0F) << 4);
frame[9] |= this->calc80Checksum(frame[7], frame[8], frame[9]);
break;
case somfy_commands::Favorite:
if(repeat == 0) frame[1] = (static_cast<byte>(somfy_commands::My) << 4) | (frame[1] & 0x0F);
frame[7] = repeat > 0 ? 132 : 196;
frame[8] = 44;
frame[9] = 0x90;
frame[9] |= this->calc80Checksum(frame[7], frame[8], frame[9]);
break;
case somfy_commands::Stop:
if(repeat == 0) frame[1] = (static_cast<byte>(somfy_commands::My) << 4) | (frame[1] & 0x0F);
frame[7] = repeat > 0 ? 132 : 196;
frame[8] = 47;
frame[9] = 0xF0;
frame[9] |= this->calc80Checksum(frame[7], frame[8], frame[9]);
break;
case somfy_commands::Toggle:
frame[0] = 164;
frame[1] |= 0xF0;
frame[7] = this->encode80Byte7(196, repeat);
frame[8] = 0;
frame[9] = 0x10;
frame[9] |= this->calc80Checksum(frame[7], frame[8], frame[9]);
break;
case somfy_commands::Up:
frame[7] = this->encode80Byte7(196, repeat);
frame[8] = 32;
frame[9] = 0x00;
frame[9] |= this->calc80Checksum(frame[7], frame[8], frame[9]);
break;
case somfy_commands::Down:
frame[7] = this->encode80Byte7(196, repeat);
frame[8] = 44;
frame[9] = 0x80;
frame[9] |= this->calc80Checksum(frame[7], frame[8], frame[9]);
break;
case somfy_commands::Prog:
case somfy_commands::UpDown:
case somfy_commands::MyDown:
case somfy_commands::MyUp:
case somfy_commands::MyUpDown:
case somfy_commands::My:
frame[7] = this->encode80Byte7(196, repeat);
frame[8] = 0x00;
frame[9] = 0x10;
frame[9] |= this->calc80Checksum(frame[7], frame[8], frame[9]);
break;
default:
break;
}
}
void somfy_frame_t::encodeFrame(byte *frame) {
const byte btn = static_cast<byte>(cmd);
this->valid = true;
frame[0] = this->encKey; // Encryption key. Doesn't matter much
frame[1] = (btn & 0x0F) << 4; // Which button did you press? The 4 LSB will be the checksum
frame[2] = this->rollingCode >> 8; // Rolling code (big endian)
frame[3] = this->rollingCode; // Rolling code
frame[4] = this->remoteAddress >> 16; // Remote address
frame[5] = this->remoteAddress >> 8; // Remote address
frame[6] = this->remoteAddress; // Remote address
frame[7] = 132;
frame[8] = 0;
frame[9] = 29;
// Ok so if this is an RTW things are a bit different.
if(this->proto == radio_proto::RTW) {
frame[1] = 0xF0;
switch(this->cmd) {
case somfy_commands::My:
frame[0] = 133;
break;
case somfy_commands::Up:
frame[0] = 134;
break;
case somfy_commands::MyUp:
frame[0] = 135;
break;
case somfy_commands::Down:
frame[0] = 136;
break;
case somfy_commands::MyDown:
frame[0] = 137;
break;
case somfy_commands::UpDown:
frame[0] = 138;
break;
case somfy_commands::MyUpDown:
frame[0] = 139;
break;
case somfy_commands::Prog:
frame[0] = 140;
break;
case somfy_commands::SunFlag:
frame[0] = 141;
break;
case somfy_commands::Flag:
frame[0] = 142;
break;
default:
break;
}
}
else if(this->proto == radio_proto::RTV) {
frame[1] = 0xF0;
switch(this->cmd) {
case somfy_commands::My:
frame[0] = 149;
break;
case somfy_commands::Up:
frame[0] = 150;
break;
case somfy_commands::MyUp:
frame[0] = 151;
break;
case somfy_commands::Down:
frame[0] = 152;
break;
case somfy_commands::MyDown:
frame[0] = 153;
break;
case somfy_commands::UpDown:
frame[0] = 154;
break;
case somfy_commands::MyUpDown:
frame[0] = 155;
break;
case somfy_commands::Prog:
frame[0] = 156;
break;
case somfy_commands::SunFlag:
frame[0] = 157;
break;
case somfy_commands::Flag:
frame[0] = 158;
break;
default:
break;
}
}
else {
if(this->bitLength == 80) this->encode80BitFrame(&frame[0], this->repeats);
}
byte checksum = 0;
for (byte i = 0; i < 7; i++) {
checksum = checksum ^ frame[i] ^ (frame[i] >> 4);
}
checksum &= 0b1111; // We keep the last 4 bits only
// Checksum integration
frame[1] |= checksum;
// Obfuscation: a XOR of all the bytes
for (byte i = 1; i < 7; i++) {
frame[i] ^= frame[i - 1];
}
}
void somfy_frame_t::print() {
Serial.println("----------- Receiving -------------");
Serial.print("RSSI:");
Serial.print(this->rssi);
Serial.print(" LQI:");
Serial.println(this->lqi);
Serial.print("CMD:");
Serial.print(translateSomfyCommand(this->cmd));
Serial.print(" ADDR:");
Serial.print(this->remoteAddress);
Serial.print(" RCODE:");
Serial.println(this->rollingCode);
Serial.print("KEY:");
Serial.print(this->encKey, HEX);
Serial.print(" CS:");
Serial.println(this->checksum);
}
bool somfy_frame_t::isSynonym(somfy_frame_t &frame) { return this->remoteAddress == frame.remoteAddress && this->cmd != frame.cmd && this->rollingCode == frame.rollingCode; }
bool somfy_frame_t::isRepeat(somfy_frame_t &frame) { return this->remoteAddress == frame.remoteAddress && this->cmd == frame.cmd && this->rollingCode == frame.rollingCode; }
void somfy_frame_t::copy(somfy_frame_t &frame) {
if(this->isRepeat(frame)) {
this->repeats++;
this->rssi = frame.rssi;
this->lqi = frame.lqi;
}
else {
this->synonym = this->isSynonym(frame);
this->valid = frame.valid;
if(!this->synonym) this->processed = frame.processed;
this->rssi = frame.rssi;
this->lqi = frame.lqi;
this->cmd = frame.cmd;
this->remoteAddress = frame.remoteAddress;
this->rollingCode = frame.rollingCode;
this->encKey = frame.encKey;
this->checksum = frame.checksum;
this->hwsync = frame.hwsync;
this->repeats = frame.repeats;
}
}
void SomfyShadeController::end() { this->transceiver.disableReceive(); }
SomfyShadeController::SomfyShadeController() {
memset(this->m_shadeIds, 255, sizeof(this->m_shadeIds));
uint64_t mac = ESP.getEfuseMac();
this->startingAddress = mac & 0x0FFFFF;
}
bool SomfyShadeController::useNVS() { return !(settings.appVersion.major > 1 || settings.appVersion.minor >= 4); };
SomfyShade *SomfyShadeController::findShadeByRemoteAddress(uint32_t address) {
for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) {
SomfyShade &shade = this->shades[i];
if(shade.getRemoteAddress() == address) return &shade;
else {
for(uint8_t j = 0; j < SOMFY_MAX_LINKED_REMOTES; j++) {
if(shade.linkedRemotes[j].getRemoteAddress() == address) return &shade;
}
}
}
return nullptr;
}
SomfyGroup *SomfyShadeController::findGroupByRemoteAddress(uint32_t address) {
for(uint8_t i = 0; i < SOMFY_MAX_GROUPS; i++) {
SomfyGroup &group = this->groups[i];
if(group.getRemoteAddress() == address) return &group;
}
return nullptr;
}
void SomfyShadeController::updateGroupFlags() {
for(uint8_t i = 0; i < SOMFY_MAX_GROUPS; i++) {
SomfyGroup *group = &this->groups[i];
if(group && group->getGroupId() != 255) {
uint8_t flags = group->flags;
group->updateFlags();
if(flags != group->flags)
group->emitState();
}
}
}
#ifdef USE_NVS
bool SomfyShadeController::loadLegacy() {
Serial.println("Loading Legacy shades using NVS");
pref.begin("Shades", true);
pref.getBytes("shadeIds", this->m_shadeIds, sizeof(this->m_shadeIds));
pref.end();
for(uint8_t i = 0; i < sizeof(this->m_shadeIds); i++) {
if(i != 0) DEBUG_SOMFY.print(",");
DEBUG_SOMFY.print(this->m_shadeIds[i]);
}
DEBUG_SOMFY.println();
sortArray<uint8_t>(this->m_shadeIds, sizeof(this->m_shadeIds));
#ifdef DEBUG_SOMFY
for(uint8_t i = 0; i < sizeof(this->m_shadeIds); i++) {
if(i != 0) DEBUG_SOMFY.print(",");
DEBUG_SOMFY.print(this->m_shadeIds[i]);
}
DEBUG_SOMFY.println();
#endif
uint8_t id = 0;
for(uint8_t i = 0; i < sizeof(this->m_shadeIds); i++) {
if(this->m_shadeIds[i] == id) this->m_shadeIds[i] = 255;
id = this->m_shadeIds[i];
SomfyShade *shade = &this->shades[i];
shade->setShadeId(id);
if(id == 255) {
continue;
}
shade->load();
}
#ifdef DEBUG_SOMFY
for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) {
DEBUG_SOMFY.print(this->shades[i].getShadeId());
DEBUG_SOMFY.print(":");
DEBUG_SOMFY.print(this->m_shadeIds[i]);
if(i < SOMFY_MAX_SHADES - 1) DEBUG_SOMFY.print(",");
}
Serial.println();
#endif
#ifdef USE_NVS
if(!this->useNVS()) {
pref.begin("Shades");
pref.putBytes("shadeIds", this->m_shadeIds, sizeof(this->m_shadeIds));
pref.end();
}
#endif
this->commit();
return true;
}
#endif
bool SomfyShadeController::begin() {
// Load up all the configuration data.
//ShadeConfigFile::getAppVersion(this->appVersion);
Serial.printf("App Version:%u.%u.%u\n", settings.appVersion.major, settings.appVersion.minor, settings.appVersion.build);
#ifdef USE_NVS
if(!this->useNVS()) { // At 1.4 we started using the configuration file. If the file doesn't exist then booh.
// We need to remove all the extraeneous data from NVS for the shades. From here on out we
// will rely on the shade configuration.
Serial.println("No longer using NVS");
if(ShadeConfigFile::exists()) {
ShadeConfigFile::load(this);
}
else {
this->loadLegacy();
}
pref.begin("Shades");
if(pref.isKey("shadeIds")) {
pref.getBytes("shadeIds", this->m_shadeIds, sizeof(this->m_shadeIds));
pref.clear(); // Delete all the keys.
}
pref.end();
for(uint8_t i = 0; i < sizeof(this->m_shadeIds); i++) {
// Start deleting the keys for the shades.
if(this->m_shadeIds[i] == 255) continue;
char shadeKey[15];
sprintf(shadeKey, "SomfyShade%u", this->m_shadeIds[i]);
pref.begin(shadeKey);
pref.clear();
pref.end();
}
}
#endif
if(ShadeConfigFile::exists()) {
Serial.println("shades.cfg exists so we are using that");
ShadeConfigFile::load(this);
}
else {
Serial.println("Starting clean");
#ifdef USE_NVS
this->loadLegacy();
#endif
}
this->transceiver.begin();
// Set the radio type for shades that have yet to be specified.
bool saveFlag = false;
for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) {
SomfyShade *shade = &this->shades[i];
if(shade->getShadeId() != 255 && shade->bitLength == 0) {
//Serial.printf("Setting bit length to %d\n", this->transceiver.config.type);
shade->bitLength = this->transceiver.config.type;
saveFlag = true;
}
}
if(saveFlag) somfy.commit();
return true;
}
void SomfyShadeController::commit() {
if(git.lockFS) return;
esp_task_wdt_reset(); // Make sure we don't reset inadvertently.
ShadeConfigFile file;
file.begin();
file.save(this);
file.end();
this->isDirty = false;
this->lastCommit = millis();
}
void SomfyShadeController::writeBackup() {
if(git.lockFS) return;
esp_task_wdt_reset(); // Make sure we don't reset inadvertently.
ShadeConfigFile file;
file.begin("/controller.backup", false);
file.backup(this);
file.end();
}
SomfyRoom * SomfyShadeController::getRoomById(uint8_t roomId) {
for(uint8_t i = 0; i < SOMFY_MAX_ROOMS; i++) {
if(this->rooms[i].roomId == roomId) return &this->rooms[i];
}
return nullptr;
}
SomfyShade * SomfyShadeController::getShadeById(uint8_t shadeId) {
for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) {
if(this->shades[i].getShadeId() == shadeId) return &this->shades[i];
}
return nullptr;
}
SomfyGroup * SomfyShadeController::getGroupById(uint8_t groupId) {
for(uint8_t i = 0; i < SOMFY_MAX_GROUPS; i++) {
if(this->groups[i].getGroupId() == groupId) return &this->groups[i];
}
return nullptr;
}
void SomfyShade::clear() {
this->setShadeId(255);
this->setRemoteAddress(0);
this->moveStart = 0;
this->tiltStart = 0;
this->noSunStart = 0;
this->sunStart = 0;
this->windStart = 0;
this->windLast = 0;
this->noWindStart = 0;
this->noSunDone = true;
this->sunDone = true;
this->windDone = true;
this->noWindDone = true;
this->startPos = 0.0f;
this->startTiltPos = 0.0f;
this->settingMyPos = false;
this->settingPos = false;
this->settingTiltPos = false;
this->awaitMy = 0;
this->flipPosition = false;
this->flipCommands = false;
this->lastRollingCode = 0;
this->shadeType = shade_types::roller;
this->tiltType = tilt_types::none;
//this->txQueue.clear();
this->currentPos = 0.0f;
this->currentTiltPos = 0.0f;
this->direction = 0;
this->tiltDirection = 0;
this->target = 0.0f;
this->tiltTarget = 0.0f;
this->myPos = -1.0f;
this->myTiltPos = -1.0f;
this->bitLength = somfy.transceiver.config.type;
this->proto = somfy.transceiver.config.proto;
for(uint8_t i = 0; i < SOMFY_MAX_LINKED_REMOTES; i++)
this->linkedRemotes[i].setRemoteAddress(0);
this->paired = false;
this->name[0] = 0x00;
this->upTime = 10000;
this->downTime = 10000;
this->tiltTime = 7000;
this->stepSize = 100;
this->repeats = 1;
this->sortOrder = 255;
}
void SomfyRoom::clear() {
this->roomId = 0;
strcpy(this->name, "");
}
void SomfyGroup::clear() {
this->setGroupId(255);
this->setRemoteAddress(0);
this->repeats = 0;
this->roomId = 0;
this->name[0] = 0x00;
memset(&this->linkedShades, 0x00, sizeof(this->linkedShades));
}
bool SomfyShade::linkRemote(uint32_t address, uint16_t rollingCode) {
// Check to see if the remote is already linked. If it is
// just return true after setting the rolling code
for(uint8_t i = 0; i < SOMFY_MAX_LINKED_REMOTES; i++) {
if(this->linkedRemotes[i].getRemoteAddress() == address) {
this->linkedRemotes[i].setRollingCode(rollingCode);
return true;
}
}
for(uint8_t i = 0; i < SOMFY_MAX_LINKED_REMOTES; i++) {
if(this->linkedRemotes[i].getRemoteAddress() == 0) {
this->linkedRemotes[i].setRemoteAddress(address);
this->linkedRemotes[i].setRollingCode(rollingCode);
#ifdef USE_NVS
if(somfy.useNVS()) {
uint32_t linkedAddresses[SOMFY_MAX_LINKED_REMOTES];
memset(linkedAddresses, 0x00, sizeof(linkedAddresses));
uint8_t j = 0;
for(uint8_t i = 0; i < SOMFY_MAX_LINKED_REMOTES; i++) {
SomfyLinkedRemote lremote = this->linkedRemotes[i];
if(lremote.getRemoteAddress() != 0) linkedAddresses[j++] = lremote.getRemoteAddress();
}
char shadeKey[15];
snprintf(shadeKey, sizeof(shadeKey), "SomfyShade%u", this->getShadeId());
pref.begin(shadeKey);
pref.putBytes("linkedAddr", linkedAddresses, sizeof(uint32_t) * SOMFY_MAX_LINKED_REMOTES);
pref.end();
}
#endif
this->commit();
return true;
}
}
return false;
}
bool SomfyGroup::linkShade(uint8_t shadeId) {
// Check to see if the shade is already linked. If it is just return true
for(uint8_t i = 0; i < SOMFY_MAX_GROUPED_SHADES; i++) {
if(this->linkedShades[i] == shadeId) {
return true;
}
}
for(uint8_t i = 0; i < SOMFY_MAX_GROUPED_SHADES; i++) {
if(this->linkedShades[i] == 0) {
this->linkedShades[i] = shadeId;
somfy.commit();
return true;
}
}
return false;
}
void SomfyShade::commit() { somfy.commit(); }
void SomfyShade::commitShadePosition() {
somfy.isDirty = true;
#ifdef USE_NVS
char shadeKey[15];
if(somfy.useNVS()) {
snprintf(shadeKey, sizeof(shadeKey), "SomfyShade%u", this->shadeId);
Serial.print("Writing current shade position: ");
Serial.println(this->currentPos, 4);
pref.begin(shadeKey);
pref.putFloat("currentPos", this->currentPos);
pref.end();
}
#endif
}
void SomfyShade::commitMyPosition() {
somfy.isDirty = true;
#ifdef USE_NVS
if(somfy.useNVS()) {
char shadeKey[15];
snprintf(shadeKey, sizeof(shadeKey), "SomfyShade%u", this->shadeId);
Serial.print("Writing my shade position:");
Serial.print(this->myPos);
Serial.println("%");
pref.begin(shadeKey);
pref.putUShort("myPos", this->myPos);
pref.end();
}
#endif
}
void SomfyShade::commitTiltPosition() {
somfy.isDirty = true;
#ifdef USE_NVS
if(somfy.useNVS()) {
char shadeKey[15];
snprintf(shadeKey, sizeof(shadeKey), "SomfyShade%u", this->shadeId);
Serial.print("Writing current shade tilt position: ");
Serial.println(this->currentTiltPos, 4);
pref.begin(shadeKey);
pref.putFloat("currentTiltPos", this->currentTiltPos);
pref.end();
}
#endif
}
bool SomfyShade::unlinkRemote(uint32_t address) {
for(uint8_t i = 0; i < SOMFY_MAX_LINKED_REMOTES; i++) {
if(this->linkedRemotes[i].getRemoteAddress() == address) {
this->linkedRemotes[i].setRemoteAddress(0);
#ifdef USE_NVS
if(somfy.useNVS()) {
char shadeKey[15];
snprintf(shadeKey, sizeof(shadeKey), "SomfyShade%u", this->getShadeId());
uint32_t linkedAddresses[SOMFY_MAX_LINKED_REMOTES];
memset(linkedAddresses, 0x00, sizeof(linkedAddresses));
uint8_t j = 0;
for(uint8_t i = 0; i < SOMFY_MAX_LINKED_REMOTES; i++) {
SomfyLinkedRemote lremote = this->linkedRemotes[i];
if(lremote.getRemoteAddress() != 0) linkedAddresses[j++] = lremote.getRemoteAddress();
}
pref.begin(shadeKey);
pref.putBytes("linkedAddr", linkedAddresses, sizeof(uint32_t) * SOMFY_MAX_LINKED_REMOTES);
pref.end();
}
#endif
this->commit();
return true;
}
}
return false;
}
bool SomfyGroup::unlinkShade(uint8_t shadeId) {
bool removed = false;
for(uint8_t i = 0; i < SOMFY_MAX_GROUPED_SHADES; i++) {
if(this->linkedShades[i] == shadeId) {
this->linkedShades[i] = 0;
removed = true;
}
}
// Compress the linked shade ids so we can stop looking on the first 0
if(removed) {
this->compressLinkedShadeIds();
somfy.commit();
}
return removed;
}
void SomfyGroup::compressLinkedShadeIds() {
// [1,0,4,3,0,0,0] i:0,j:0
// [1,0,4,3,0,0,0] i:1,j:1
// [1,4,0,3,0,0,0] i:2,j:1
// [1,4,3,0,0,0,0] i:3,j:2
// [1,4,3,0,0,0,0] i:4,j:2
// [1,2,0,0,3,0,0] i:0,j:0
// [1,2,0,0,3,0,0] i:1,j:1
// [1,2,0,0,3,0,0] i:2,j:2
// [1,2,0,0,3,0,0] i:3,j:2
// [1,2,3,0,0,0,0] i:4,j:2
// [1,2,3,0,0,0,0] i:5,j:3
for(uint8_t i = 0, j = 0; i < SOMFY_MAX_GROUPED_SHADES; i++) {
if(this->linkedShades[i] != 0) {
if(i != j) {
this->linkedShades[j] = this->linkedShades[i];
this->linkedShades[i] = 0;
}
j++;
}
}
}
void SomfyShadeController::compressRepeaters() {
for(uint8_t i = 0, j = 0; i < SOMFY_MAX_REPEATERS; i++) {
if(this->repeaters[i] != 0) {
if(i != j) {
this->repeaters[j] = this->repeaters[i];
this->repeaters[i] = 0;
}
j++;
}
}
}
bool SomfyGroup::hasShadeId(uint8_t shadeId) {
for(uint8_t i = 0; i < SOMFY_MAX_GROUPED_SHADES; i++) {
if(this->linkedShades[i] == 0) break;
if(this->linkedShades[i] == shadeId) return true;
}
return false;
}
bool SomfyShade::isAtTarget() {
float epsilon = .00001;
if(this->tiltType == tilt_types::tiltonly) return fabs(this->currentTiltPos - this->tiltTarget) < epsilon;
else if(this->tiltType == tilt_types::none) return fabs(this->currentPos - this->target) < epsilon;
return fabs(this->currentPos - this->target) < epsilon && fabs(this->currentTiltPos - this->tiltTarget) < epsilon;
}
bool SomfyRemote::simMy() { return (this->flags & static_cast<uint8_t>(somfy_flags_t::SimMy)) > 0; }
void SomfyRemote::setSimMy(bool bSimMy) { bSimMy ? this->flags |= static_cast<uint8_t>(somfy_flags_t::SimMy) : this->flags &= ~(static_cast<uint8_t>(somfy_flags_t::SimMy)); }
bool SomfyRemote::hasSunSensor() { return (this->flags & static_cast<uint8_t>(somfy_flags_t::SunSensor)) > 0;}
bool SomfyRemote::hasLight() { return (this->flags & static_cast<uint8_t>(somfy_flags_t::Light)) > 0; }
void SomfyRemote::setSunSensor(bool bHasSensor ) { bHasSensor ? this->flags |= static_cast<uint8_t>(somfy_flags_t::SunSensor) : this->flags &= ~(static_cast<uint8_t>(somfy_flags_t::SunSensor)); }
void SomfyRemote::setLight(bool bHasLight ) { bHasLight ? this->flags |= static_cast<uint8_t>(somfy_flags_t::Light) : this->flags &= ~(static_cast<uint8_t>(somfy_flags_t::Light)); }
void SomfyGroup::updateFlags() {
uint8_t oldFlags = this->flags;
this->flags = 0;
for(uint8_t i = 0; i < SOMFY_MAX_GROUPED_SHADES; i++) {
if(this->linkedShades[i] != 0) {
SomfyShade *shade = somfy.getShadeById(this->linkedShades[i]);
if(shade) this->flags |= shade->flags;
}
else break;
}
if(oldFlags != this->flags) this->emitState();
}
bool SomfyShade::isInGroup() {
if(this->getShadeId() == 255) return false;
for(uint8_t i = 0; i < SOMFY_MAX_GROUPS; i++) {
if(somfy.groups[i].getGroupId() != 255 && somfy.groups[i].hasShadeId(this->getShadeId())) return true;
}
return false;
}
void SomfyShade::setGPIOs() {
if(this->proto == radio_proto::GP_Relay) {
// Determine whether the direction needs to be set.
uint8_t p_on = (this->gpioFlags & (uint8_t)gpio_flags_t::LowLevelTrigger) == 0x00 ? HIGH : LOW;
uint8_t p_off = (this->gpioFlags & (uint8_t)gpio_flags_t::LowLevelTrigger) == 0x00 ? LOW : HIGH;
int8_t dir = this->direction;
if(dir == 0 && this->tiltType == tilt_types::integrated)
dir = this->tiltDirection;
else if(this->tiltType == tilt_types::tiltonly)
dir = this->tiltDirection;
if(this->shadeType == shade_types::drycontact) {
digitalWrite(this->gpioDown, this->currentPos == 100 ? p_on : p_off);
this->gpioDir = this->currentPos == 100 ? 1 : -1;
}
else if(this->shadeType == shade_types::drycontact2) {
if(this->currentPos == 100) {
digitalWrite(this->gpioDown, p_off);
digitalWrite(this->gpioUp, p_on);
}
else {
digitalWrite(this->gpioUp, p_off);
digitalWrite(this->gpioDown, p_on);
}
this->gpioDir = this->currentPos == 100 ? 1 : -1;
}
else {
switch(dir) {
case -1:
digitalWrite(this->gpioDown, p_off);
digitalWrite(this->gpioUp, p_on);
if(dir != this->gpioDir) Serial.printf("UP: true, DOWN: false\n");
this->gpioDir = dir;
break;
case 1:
digitalWrite(this->gpioUp, p_off);
digitalWrite(this->gpioDown, p_on);
if(dir != this->gpioDir) Serial.printf("UP: false, DOWN: true\n");
this->gpioDir = dir;
break;
default:
digitalWrite(this->gpioUp, p_off);
digitalWrite(this->gpioDown, p_off);
if(dir != this->gpioDir) Serial.printf("UP: false, DOWN: false\n");
this->gpioDir = dir;
break;
}
}
}
else if(this->proto == radio_proto::GP_Remote) {
if(millis() > this->gpioRelease) {
//uint8_t p_on = (this->gpioFlags & (uint8_t)gpio_flags_t::LowLevelTrigger) == 0x00 ? HIGH : LOW;
uint8_t p_off = (this->gpioFlags & (uint8_t)gpio_flags_t::LowLevelTrigger) == 0x00 ? LOW : HIGH;
digitalWrite(this->gpioUp, p_off);
digitalWrite(this->gpioDown, p_off);
digitalWrite(this->gpioMy, p_off);
this->gpioRelease = 0;
}
}
}
void SomfyRemote::triggerGPIOs(somfy_frame_t &frame) { }
void SomfyShade::triggerGPIOs(somfy_frame_t &frame) {
if(this->proto == radio_proto::GP_Remote) {
uint8_t p_on = (this->gpioFlags & (uint8_t)gpio_flags_t::LowLevelTrigger) == 0x00 ? HIGH : LOW;
uint8_t p_off = (this->gpioFlags & (uint8_t)gpio_flags_t::LowLevelTrigger) == 0x00 ? LOW : HIGH;
int8_t dir = 0;
switch(frame.cmd) {
case somfy_commands::My:
if(this->shadeType != shade_types::drycontact && !this->isToggle()) {
digitalWrite(this->gpioUp, p_off);
digitalWrite(this->gpioDown, p_off);
digitalWrite(this->gpioMy, p_on);
dir = 0;
if(dir != this->gpioDir) Serial.printf("UP: false, DOWN: false, MY: true\n");