/
supla_esp_devconn.c
1947 lines (1500 loc) · 54.4 KB
/
supla_esp_devconn.c
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
/*
Copyright (C) AC SOFTWARE SP. Z O.O.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include <stdio.h>
#include <user_interface.h>
#include <espconn.h>
#include <spi_flash.h>
#include <osapi.h>
#include <mem.h>
#include <stdlib.h>
#include "supla_esp_devconn.h"
#include "supla_esp_cfgmode.h"
#include "supla_esp_gpio.h"
#include "supla_esp_cfg.h"
#include "supla_esp_pwm.h"
#include "supla_esp_hw_timer.h"
#include "supla_ds18b20.h"
#include "supla_dht.h"
#include "supla-dev/srpc.h"
#include "supla-dev/log.h"
#include "supla_esp_countdown_timer.h"
#include "supla_esp_dns_client.h"
#include "supla_esp_wifi.h"
#include "supla_esp_state.h"
#include "supla_esp_input.h"
#ifdef ELECTRICITY_METER_COUNT
#include "supla_esp_electricity_meter.h"
#endif
#ifdef IMPULSE_COUNTER_COUNT
#include "supla_esp_impulse_counter.h"
#endif
#ifdef __FOTA
#include "supla_update.h"
#endif
#ifndef CVD_MAX_COUNT
#define CVD_MAX_COUNT 4
#endif /*CVD_MAX_COUNT*/
#if CVD_MAX_COUNT == 0
#undef CVD_MAX_COUNT
#endif
typedef struct {
int channel_number;
ETSTimer timer;
char value[SUPLA_CHANNELVALUE_SIZE];
}channel_value_delayed;
typedef struct {
ETSTimer supla_devconn_timer1;
ETSTimer supla_iterate_timer;
ETSTimer supla_watchdog_timer;
ETSTimer supla_value_timer;
ETSTimer reconnect_delay_timer;
ETSTimer stop_delay_timer;
// ESPCONN_INPROGRESS fix
char esp_send_buffer[SEND_BUFFER_SIZE];
int esp_send_buffer_len;
// ---------------------------------------------
struct espconn ESPConn;
esp_tcp ESPTCP;
ip_addr_t ipaddr;
void *srpc;
uint8 started;
char registered;
uint32 register_time_sec;
char recvbuff[RECVBUFF_MAXSIZE];
unsigned int recvbuff_size;
unsigned int last_response;
unsigned int last_sent;
unsigned int next_wd_soft_timeout_challenge;
int server_activity_timeout;
#ifdef CVD_MAX_COUNT
channel_value_delayed cvd[CVD_MAX_COUNT];
#endif /*CVD_MAX_COUNT*/
bool resolving_started;
}devconn_params;
static devconn_params *devconn = NULL;
#ifndef SUPLA_SMOOTH_DISABLED
#if defined(RGB_CONTROLLER_CHANNEL) \
|| defined(RGBW_CONTROLLER_CHANNEL) \
|| defined(RGBWW_CONTROLLER_CHANNEL) \
|| defined(DIMMER_CHANNEL)
typedef struct {
char active;
int counter;
int ChannelNumber;
char smoothly;
int color;
int dest_color;
float color_brightness;
float color_brightness_step;
float dest_color_brightness;
float brightness;
float brightness_step;
float dest_brightness;
char turn_onoff;
}devconn_smooth;
devconn_smooth smooth[SMOOTH_MAX_COUNT] = {};
#endif
#endif /*SUPLA_SMOOTH_DISABLED*/
#if NOSSL == 1
#define supla_espconn_sent espconn_sent
#define _supla_espconn_disconnect espconn_disconnect
#define supla_espconn_connect espconn_connect
#else
#define supla_espconn_sent espconn_secure_sent
#define _supla_espconn_disconnect espconn_secure_disconnect
#define supla_espconn_connect espconn_secure_connect
#endif
void DEVCONN_ICACHE_FLASH supla_esp_devconn_timer1_cb(void *timer_arg);
void DEVCONN_ICACHE_FLASH supla_esp_devconn_iterate(void *timer_arg);
void DEVCONN_ICACHE_FLASH supla_esp_devconn_reconnect(void);
void DEVCONN_ICACHE_FLASH supla_esp_devconn_reconnect_with_delay(uint32 time_ms);
void DEVCONN_ICACHE_FLASH supla_esp_devconn_stop_with_delay(void);
void DEVCONN_ICACHE_FLASH
supla_esp_devconn_before_system_restart(void) {
if ( supla_esp_cfgmode_started() == 0 ) {
os_timer_disarm(&devconn->supla_watchdog_timer);
os_timer_disarm(&devconn->supla_devconn_timer1);
os_timer_disarm(&devconn->supla_iterate_timer);
os_timer_disarm(&devconn->supla_value_timer);
#ifdef IMPULSE_COUNTER_COUNT
supla_esp_ic_stop();
#endif /*IMPULSE_COUNTER_COUNT*/
#ifdef ELECTRICITY_METER_COUNT
supla_esp_em_stop();
#endif /*ELECTRICITY_METER_COUNT*/
}
}
uint8 DEVCONN_ICACHE_FLASH supla_esp_devconn_any_outgoingdata_exists(void) {
if (devconn) {
if (devconn->esp_send_buffer_len > 0) {
return 1;
}
if (devconn->srpc && (srpc_out_queue_item_count(devconn->srpc) > 0 ||
srpc_output_dataexists(devconn->srpc) > 0)) {
return 1;
}
}
return 0;
}
#pragma GCC diagnostic pop
int DEVCONN_ICACHE_FLASH
supla_esp_data_read(void *buf, int count, void *dcd) {
if ( devconn->recvbuff_size > 0 ) {
count = devconn->recvbuff_size > count ? count : devconn->recvbuff_size;
os_memcpy(buf, devconn->recvbuff, count);
if ( count == devconn->recvbuff_size ) {
devconn->recvbuff_size = 0;
} else {
unsigned int a;
for(a=0;a<devconn->recvbuff_size-count;a++)
devconn->recvbuff[a] = devconn->recvbuff[count+a];
devconn->recvbuff_size-=count;
}
return count;
}
return -1;
}
void DEVCONN_ICACHE_FLASH
supla_esp_devconn_recv_cb (void *arg, char *pdata, unsigned short len) {
if ( len == 0 || pdata == NULL )
return;
//supla_log(LOG_DEBUG, "sproto recv %i bytes, heap_size: %i", len, system_get_free_heap_size());
if ( len <= RECVBUFF_MAXSIZE-devconn->recvbuff_size ) {
os_memcpy(&devconn->recvbuff[devconn->recvbuff_size], pdata, len);
devconn->recvbuff_size += len;
supla_esp_devconn_iterate(NULL);
} else {
supla_log(LOG_ERR, "Recv buffer size exceeded");
}
}
int DEVCONN_ICACHE_FLASH
supla_esp_data_write_append_buffer(void *buf, int count) {
if ( count > 0 ) {
if ( devconn->esp_send_buffer_len+count > SEND_BUFFER_SIZE ) {
supla_log(LOG_ERR, "Send buffer size exceeded");
return -1;
} else {
memcpy(&devconn->esp_send_buffer[devconn->esp_send_buffer_len], buf, count);
devconn->esp_send_buffer_len+=count;
return 0;
}
}
return 0;
}
int DEVCONN_ICACHE_FLASH
supla_esp_data_write(void *buf, int count, void *dcd) {
int r;
if ( devconn->esp_send_buffer_len > 0 ) {
if ((r = supla_espconn_sent(&devconn->ESPConn,
(unsigned char*)devconn->esp_send_buffer, devconn->esp_send_buffer_len)) == 0) {
devconn->esp_send_buffer_len = 0;
devconn->last_sent = uptime_sec();
}
//supla_log(LOG_DEBUG, "sproto send count: %i result: %i", count, r);
};
if ( devconn->esp_send_buffer_len > 0 ) {
return supla_esp_data_write_append_buffer(buf, count);
}
if ( count > 0 ) {
r = supla_espconn_sent(&devconn->ESPConn, buf, count);
//supla_log(LOG_DEBUG, "sproto send count: %i result: %i", count, r);
if ( ESPCONN_INPROGRESS == r || ESPCONN_MAXNUM == r ) {
return supla_esp_data_write_append_buffer(buf, count);
} else {
if ( r == 0 )
devconn->last_sent = uptime_sec();
return r == 0 ? count : -1;
}
}
return 0;
}
void DEVCONN_ICACHE_FLASH
supla_esp_on_version_error(TSDC_SuplaVersionError *version_error) {
supla_esp_set_state(LOG_ERR, "Protocol version error");
supla_esp_devconn_stop_with_delay();
}
void DEVCONN_ICACHE_FLASH
supla_esp_devconn_send_channel_values_cb(void *ptr) {
if ( supla_esp_devconn_is_registered() == 1 ) {
#ifdef _ROLLERSHUTTER_SUPPORT
int a;
for(a=0; a<RS_MAX_COUNT; a++) {
if ( supla_rs_cfg[a].up != NULL
&& supla_rs_cfg[a].down != NULL
&& supla_rs_cfg[a].up->channel != 255 ) {
supla_esp_channel_value_changed(supla_rs_cfg[a].up->channel, supla_esp_gpio_rs_get_current_position(&supla_rs_cfg[a]));
}
}
#endif /*_ROLLERSHUTTER_SUPPORT*/
supla_esp_board_send_channel_values_with_delay(devconn->srpc);
}
}
void DEVCONN_ICACHE_FLASH
supla_esp_devconn_send_channel_values_with__delay(int time_ms) {
if (devconn) {
os_timer_disarm(&devconn->supla_value_timer);
os_timer_setfn(&devconn->supla_value_timer, (os_timer_func_t *)supla_esp_devconn_send_channel_values_cb, NULL);
os_timer_arm(&devconn->supla_value_timer, time_ms, 0);
}
}
void DEVCONN_ICACHE_FLASH
supla_esp_devconn_send_channel_values_with_delay(void) {
supla_esp_devconn_send_channel_values_with__delay(1500);
}
void DEVCONN_ICACHE_FLASH supla_esp_on_register_result(
TSD_SuplaRegisterDeviceResult *register_device_result) {
char *buff = NULL;
switch (register_device_result->result_code) {
case SUPLA_RESULTCODE_BAD_CREDENTIALS:
supla_esp_set_state(LOG_ERR, "Bad credentials!");
break;
case SUPLA_RESULTCODE_TEMPORARILY_UNAVAILABLE:
supla_esp_set_state(LOG_NOTICE, "Temporarily unavailable!");
break;
case SUPLA_RESULTCODE_LOCATION_CONFLICT:
supla_esp_set_state(LOG_ERR, "Location conflict!");
break;
case SUPLA_RESULTCODE_CHANNEL_CONFLICT:
supla_esp_set_state(LOG_ERR, "Channel conflict!");
break;
case SUPLA_RESULTCODE_REGISTRATION_DISABLED:
supla_esp_set_state(LOG_ERR, "Registration disabled!");
break;
case SUPLA_RESULTCODE_AUTHKEY_ERROR:
supla_esp_set_state(LOG_NOTICE, "Incorrect device AuthKey!");
break;
case SUPLA_RESULTCODE_NO_LOCATION_AVAILABLE:
supla_esp_set_state(LOG_ERR, "No location available!");
break;
case SUPLA_RESULTCODE_USER_CONFLICT:
supla_esp_set_state(LOG_ERR, "User conflict!");
break;
case SUPLA_RESULTCODE_TRUE:
devconn->server_activity_timeout =
register_device_result->activity_timeout;
devconn->registered = 1;
devconn->register_time_sec = uptime_sec();
// supla_esp_gpio_state_connected()
// should be called after setting
// devconn->registered to 1
supla_esp_gpio_state_connected();
supla_esp_set_state(LOG_DEBUG, "Registered and ready.");
supla_log(LOG_DEBUG, "Free heap size: %i", system_get_free_heap_size());
if (devconn->server_activity_timeout != ACTIVITY_TIMEOUT) {
TDCS_SuplaSetActivityTimeout at;
at.activity_timeout = ACTIVITY_TIMEOUT;
srpc_dcs_async_set_activity_timeout(devconn->srpc, &at);
}
#ifdef __FOTA
supla_esp_check_updates(devconn->srpc);
#endif
#if defined(RETREIVE_CHANNEL_CONFIG) && ESP8266_SUPLA_PROTO_VERSION >= 16
{
TDS_GetChannelConfigRequest request = {};
for (uint8 a = 0; a < CHANNEL_CONFIG_LIMIT; a++) {
if (RETREIVE_CHANNEL_CONFIG & (1 << a)) {
request.ChannelNumber = a;
srpc_ds_async_get_channel_config(devconn->srpc, &request);
}
}
}
#endif /*defined(RETREIVE_CHANNEL_CONFIG)*/
#ifndef COUNTDOWN_TIMER_DISABLED
#if ESP8266_SUPLA_PROTO_VERSION >= 12
for (uint8 a = 0; a < RELAY_MAX_COUNT; a++)
if (supla_relay_cfg[a].gpio_id != 255 &&
supla_relay_cfg[a].channel_flags &
SUPLA_CHANNEL_FLAG_COUNTDOWN_TIMER_SUPPORTED) {
TSuplaChannelExtendedValue *ev = (TSuplaChannelExtendedValue *)malloc(
sizeof(TSuplaChannelExtendedValue));
if (ev != NULL) {
supla_esp_countdown_get_state_ev(supla_relay_cfg[a].channel, ev);
supla_esp_channel_extendedvalue_changed(supla_relay_cfg[a].channel,
ev);
free(ev);
}
}
#endif /*ESP8266_SUPLA_PROTO_VERSION >= 12*/
#endif /*COUNTDOWN_TIMER_DISABLED*/
supla_esp_devconn_send_channel_values_with_delay();
#ifdef ELECTRICITY_METER_COUNT
supla_esp_em_device_registered();
#endif
#ifdef IMPULSE_COUNTER_COUNT
supla_esp_ic_device_registered();
#endif
#ifdef BOARD_ON_DEVICE_REGISTERED
BOARD_ON_DEVICE_REGISTERED;
#endif
return;
case SUPLA_RESULTCODE_DEVICE_DISABLED:
supla_esp_set_state(LOG_NOTICE, "Device is disabled!");
break;
case SUPLA_RESULTCODE_LOCATION_DISABLED:
supla_esp_set_state(LOG_NOTICE, "Location is disabled!");
break;
case SUPLA_RESULTCODE_DEVICE_LIMITEXCEEDED:
supla_esp_set_state(LOG_NOTICE, "Device limit exceeded!");
break;
case SUPLA_RESULTCODE_GUID_ERROR:
supla_esp_set_state(LOG_NOTICE, "Incorrect device GUID!");
break;
default:
buff = os_malloc(30);
ets_snprintf(buff, 30, "Unknown code %i",
register_device_result->result_code);
supla_esp_set_state(LOG_NOTICE, buff);
os_free(buff);
buff = NULL;
break;
}
supla_esp_devconn_stop_with_delay();
}
void DEVCONN_ICACHE_FLASH
supla_esp_channel_set_activity_timeout_result(TSDC_SuplaSetActivityTimeoutResult *result) {
devconn->server_activity_timeout = result->activity_timeout;
}
char DEVCONN_ICACHE_FLASH supla_esp_devconn_is_registered(void) {
return devconn != NULL
&& devconn->srpc != NULL
&& devconn->registered == 1 ? 1 : 0;
}
void DEVCONN_ICACHE_FLASH
supla_esp_channel_value__changed(int channel_number, char value[SUPLA_CHANNELVALUE_SIZE]) {
if ( supla_esp_devconn_is_registered() ) {
srpc_ds_async_channel_value_changed(devconn->srpc, channel_number, value);
}
}
void DEVCONN_ICACHE_FLASH
supla_esp_channel_value_changed(int channel_number, char v) {
if ( supla_esp_devconn_is_registered() == 1 ) {
//supla_log(LOG_DEBUG, "supla_esp_channel_value_changed(%i, %i)", channel_number, v);
char value[SUPLA_CHANNELVALUE_SIZE];
memset(value, 0, SUPLA_CHANNELVALUE_SIZE);
value[0] = v;
srpc_ds_async_channel_value_changed(devconn->srpc, channel_number, value);
}
}
void DEVCONN_ICACHE_FLASH
supla_esp_channel_extendedvalue_changed(unsigned char channel_number, TSuplaChannelExtendedValue *value) {
if ( supla_esp_devconn_is_registered() == 1 ) {
#ifdef BOARD_BEFORE_SENDING_THE_EXTENDEDVALUE
BOARD_BEFORE_SENDING_THE_EXTENDEDVALUE;
#endif /*BOARD_BEFORE_SENDING_THE_EXTENDEDVALUE*/
srpc_ds_async_channel_extendedvalue_changed(devconn->srpc, channel_number, value);
}
}
#if defined(RGBW_CONTROLLER_CHANNEL) \
|| defined(RGBWW_CONTROLLER_CHANNEL) \
|| defined(RGB_CONTROLLER_CHANNEL) \
|| defined(DIMMER_CHANNEL)
void DEVCONN_ICACHE_FLASH
supla_esp_channel_rgbw_to_value(char value[SUPLA_CHANNELVALUE_SIZE], int color, char color_brightness, char brightness) {
memset(value, 0, SUPLA_CHANNELVALUE_SIZE);
value[0] = brightness;
value[1] = color_brightness;
value[2] = (char)((color & 0x000000FF)); // BLUE
value[3] = (char)((color & 0x0000FF00) >> 8); // GREEN
value[4] = (char)((color & 0x00FF0000) >> 16); // RED
}
void DEVCONN_ICACHE_FLASH
supla_esp_channel_value_changed_delayed_cb(void *timer_arg) {
if ( supla_esp_devconn_is_registered() ) {
srpc_ds_async_channel_value_changed(devconn->srpc, ((channel_value_delayed*)timer_arg)->channel_number, ((channel_value_delayed*)timer_arg)->value);
}
}
#ifdef CVD_MAX_COUNT
void DEVCONN_ICACHE_FLASH
supla_esp_channel_rgbw_value_changed(int channel_number, int color, char color_brightness, char brightness) {
if ( !devconn || channel_number >= CVD_MAX_COUNT )
return;
devconn->cvd[channel_number].channel_number = channel_number;
supla_esp_channel_rgbw_to_value(devconn->cvd[channel_number].value, color, color_brightness, brightness);
os_timer_disarm(&devconn->cvd[channel_number].timer);
os_timer_setfn(&devconn->cvd[channel_number].timer, (os_timer_func_t *)supla_esp_channel_value_changed_delayed_cb, &devconn->cvd[channel_number]);
os_timer_arm(&devconn->cvd[channel_number].timer, 750, 0);
}
#endif /*CVD_MAX_COUNT*/
#endif
char DEVCONN_ICACHE_FLASH
_supla_esp_channel_set_value(int port, char v, int channel_number) {
char _v = v == 1 ? HI_VALUE : LO_VALUE;
supla_esp_gpio_relay_hi(port, _v, 1);
_v = supla_esp_gpio_relay_is_hi(port);
supla_esp_channel_value_changed(channel_number, _v == HI_VALUE ? 1 : 0);
return (v == 1 ? HI_VALUE : LO_VALUE) == _v;
}
#ifndef COUNTDOWN_TIMER_DISABLED
void DEVCONN_ICACHE_FLASH supla_esp_devconn_on_countdown_on_disarm(uint8 channel_number) {
for(uint8 a=0;a<RELAY_MAX_COUNT;a++)
if ( supla_relay_cfg[a].gpio_id != 255
&& channel_number == supla_relay_cfg[a].channel ) {
if (supla_relay_cfg[a].channel_flags & SUPLA_CHANNEL_FLAG_COUNTDOWN_TIMER_SUPPORTED) {
TSuplaChannelExtendedValue *ev =
(TSuplaChannelExtendedValue *)malloc(sizeof(TSuplaChannelExtendedValue));
if (ev != NULL) {
supla_esp_countdown_get_state_ev(channel_number, ev);
supla_esp_channel_extendedvalue_changed(channel_number, ev);
free(ev);
}
}
break;
}
}
#endif /*COUNTDOWN_TIMER_DISABLED*/
void DEVCONN_ICACHE_FLASH supla_esp_devconn_on_countdown_timer_finish(uint8 gpio_id,
uint8 channel_number, char target_value[SUPLA_CHANNELVALUE_SIZE]) {
_supla_esp_channel_set_value(gpio_id, target_value[0] == 0 ? LO_VALUE : HI_VALUE, channel_number);
}
#if defined(RGB_CONTROLLER_CHANNEL) \
|| defined(RGBW_CONTROLLER_CHANNEL) \
|| defined(RGBWW_CONTROLLER_CHANNEL) \
|| defined(DIMMER_CHANNEL)
hsv DEVCONN_ICACHE_FLASH rgb2hsv(int rgb)
{
hsv out;
double min, max, delta;
unsigned char r = (unsigned char)((rgb & 0x00FF0000) >> 16);
unsigned char g = (unsigned char)((rgb & 0x0000FF00) >> 8);
unsigned char b = (unsigned char)(rgb & 0x000000FF);
min = r < g ? r : g;
min = min < b ? min : b;
max = r > g ? r : g;
max = max > b ? max : b;
out.v = max;
delta = max - min;
if (delta < 0.00001)
{
out.s = 0;
out.h = 0;
return out;
}
if( max > 0.0 ) {
out.s = (delta / max);
} else {
out.s = 0.0;
out.h = -1;
return out;
}
if( r >= max )
out.h = ( g - b ) / delta;
else
if( g >= max )
out.h = 2.0 + ( b - r ) / delta;
else
out.h = 4.0 + ( r - g ) / delta;
out.h *= 60.0;
if( out.h < 0.0 )
out.h += 360.0;
return out;
}
int DEVCONN_ICACHE_FLASH hsv2rgb(hsv in)
{
double hh, p, q, t, ff;
long i;
unsigned char r,g,b;
int rgb = 0;
if(in.s <= 0.0) { // < is bogus, just shuts up warnings
r = in.v;
g = in.v;
b = in.v;
rgb = r & 0xFF; rgb<<=8;
rgb |= g & 0xFF; rgb<<=8;
rgb |= b & 0xFF;
return rgb;
}
hh = in.h;
if(hh >= 360.0) hh = 0.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = in.v * (1.0 - in.s);
q = in.v * (1.0 - (in.s * ff));
t = in.v * (1.0 - (in.s * (1.0 - ff)));
switch(i) {
case 0:
r = in.v; g = t; b = p;
break;
case 1:
r = q; g = in.v; b = p;
break;
case 2:
r = p; g = in.v; b = t;
break;
case 3:
r = p; g = q; b = in.v;
break;
case 4:
r = t; g = p; b = in.v;
break;
case 5:
default:
r = in.v; g = p; b = q;
break;
}
rgb = r & 0xFF; rgb<<=8;
rgb |= g & 0xFF; rgb<<=8;
rgb |= b & 0xFF;
return rgb;
}
#ifndef SUPLA_SMOOTH_DISABLED
void DEVCONN_ICACHE_FLASH supla_esp_devconn_smooth_brightness(float *brightness, float *dest_brightness, float *step) {
if (*step < (100.0 / 255.0)) {
*step = (100.0 / 255.0);
}
if ( *brightness > *dest_brightness ) {
*brightness=*brightness - *step;
if ( *brightness < 0 )
*brightness = 0;
if ( *brightness < *dest_brightness )
*brightness = *dest_brightness;
} else if ( *brightness < *dest_brightness ) {
*brightness=*brightness + *step;
if ( *brightness > 100 )
*brightness = 100;
if ( *brightness > *dest_brightness )
*brightness = *dest_brightness;
}
*step = (*step) * 1.05;
}
void DEVCONN_ICACHE_FLASH supla_esp_devconn_smooth_cb(devconn_smooth *_smooth) {
if ( _smooth->smoothly == 0 || _smooth->counter >= 100 ) {
_smooth->color = _smooth->dest_color;
_smooth->color_brightness = _smooth->dest_color_brightness;
_smooth->brightness = _smooth->dest_brightness;
}
supla_esp_devconn_smooth_brightness(&_smooth->color_brightness, &_smooth->dest_color_brightness, &_smooth->color_brightness_step);
supla_esp_devconn_smooth_brightness(&_smooth->brightness, &_smooth->dest_brightness, &_smooth->brightness_step);
if ( _smooth->color != _smooth->dest_color ) {
hsv c = rgb2hsv(_smooth->color);
hsv dc = rgb2hsv(_smooth->dest_color);
c.s = dc.s;
c.v = dc.v;
if ( c.h < dc.h ) {
if ( 360-dc.h+c.h < dc.h-c.h ) {
c.h -= 3.6;
if ( c.h < 0 )
c.h = 359;
} else {
c.h += 3.6;
if ( c.h >= dc.h )
c.h = dc.h;
}
} else if ( c.h > dc.h ) {
if ( 360-c.h+dc.h < c.h-dc.h ) {
c.h += 3.6;
if ( c.h > 359 )
c.h = 0;
} else {
c.h -= 3.6;
if ( c.h < dc.h )
c.h = dc.h;
}
}
if ( c.h == dc.h ) {
_smooth->color = _smooth->dest_color;
} else {
_smooth->color = hsv2rgb(c);
}
}
#ifdef RGBW_ONOFF_SUPPORT
supla_esp_board_set_rgbw_value(_smooth->ChannelNumber, &_smooth->color, &_smooth->color_brightness, &_smooth->brightness, _smooth->turn_onoff);
#else
supla_esp_board_set_rgbw_value(_smooth->ChannelNumber, &_smooth->color, &_smooth->color_brightness, &_smooth->brightness);
#endif /*RGBW_ONOFF_SUPPORT*/
_smooth->counter++;
if ( _smooth->color == _smooth->dest_color
&& _smooth->color_brightness == _smooth->dest_color_brightness
&& _smooth->brightness == _smooth->dest_brightness ) {
_smooth->active = 0;
}
}
void
_supla_esp_devconn_smooth_cb(void *ptr) {
int a;
char job = 0;
for(a=0;a<SMOOTH_MAX_COUNT;a++)
if ( smooth[a].active == 1 ) {
job = 1;
supla_esp_devconn_smooth_cb(&smooth[a]);
}
if ( job == 0 ) {
supla_esp_hw_timer_disarm();
}
}
#endif /*SUPLA_SMOOTH_DISABLED*/
#ifdef RGBW_ONOFF_SUPPORT
void DEVCONN_ICACHE_FLASH
supla_esp_channel_set_rgbw_value(int ChannelNumber, int Color,
char ColorBrightness, char Brightness,
char TurnOnOff, char smoothly,
char send_value_changed) {
#else
void DEVCONN_ICACHE_FLASH
supla_esp_channel_set_rgbw_value(int ChannelNumber, int Color,
char ColorBrightness, char Brightness,
char smoothly, char send_value_changed) {
#endif /*RGBW_ONOFF_SUPPORT*/
RGBW_CHANNEL_LIMIT
if ( ColorBrightness < 0 ) {
ColorBrightness = 0;
} else if ( ColorBrightness > 100 ) {
ColorBrightness = 100;
}
if ( Brightness < 0 ) {
Brightness = 0;
} else if ( Brightness > 100 ) {
Brightness = 100;
}
#if defined(SUPLA_PWM_COUNT) || defined(SUPLA_SMOOTH_DISABLED)
float _ColorBrightness = ColorBrightness;
float _Brightness = Brightness;
#ifdef RGBW_ONOFF_SUPPORT
supla_esp_board_set_rgbw_value(ChannelNumber, &Color, &_ColorBrightness,
&_Brightness, TurnOnOff);
#else
supla_esp_board_set_rgbw_value(ChannelNumber, &Color, &_ColorBrightness,
&_Brightness);
#endif /*RGBW_ONOFF_SUPPORT*/
#ifdef CVD_MAX_COUNT
if ( send_value_changed ) {
supla_esp_channel_rgbw_value_changed(ChannelNumber, Color, ColorBrightness,
Brightness);
}
#endif /*CVD_MAX_COUNT*/
#else
supla_esp_hw_timer_disarm();
devconn_smooth *_smooth = &smooth[ChannelNumber];
_smooth->active = 1;
_smooth->counter = 0;
_smooth->ChannelNumber = ChannelNumber;
_smooth->smoothly = smoothly;
supla_esp_board_get_rgbw_value(ChannelNumber, &_smooth->color, &_smooth->color_brightness, &_smooth->brightness);
_smooth->color_brightness_step = abs(_smooth->color_brightness-ColorBrightness)/100.00;
_smooth->brightness_step = abs(_smooth->brightness-Brightness)/100.00;
_smooth->dest_color = Color;
_smooth->dest_color_brightness = ColorBrightness;
_smooth->dest_brightness = Brightness;
#ifdef RGBW_ONOFF_SUPPORT
_smooth->turn_onoff = TurnOnOff;
#endif /*RGBW_ONOFF_SUPPORT*/
#ifdef CVD_MAX_COUNT
if ( send_value_changed ) {
supla_esp_channel_rgbw_value_changed(ChannelNumber, Color, ColorBrightness, Brightness);
}
#endif /*CVD_MAX_COUNT*/
supla_esp_hw_timer_init(FRC1_SOURCE, 1, _supla_esp_devconn_smooth_cb, NULL);
supla_esp_hw_timer_arm(10000);
#endif
}
#endif
void DEVCONN_ICACHE_FLASH
supla_esp_channel_set_value(TSD_SuplaChannelNewValue *new_value) {
#ifdef SUPLA_DEBUG
supla_log(LOG_DEBUG,
"supla_esp_channel_set_value: sender %d, channel %d, duration %d"
", value[7-0] %2X %2X %2X %2X %2X %2X %2X %2X",
new_value->SenderID, new_value->ChannelNumber, new_value->DurationMS,
new_value->value[7],
new_value->value[6],
new_value->value[5],
new_value->value[4],
new_value->value[3],
new_value->value[2],
new_value->value[1],
new_value->value[0]);
#endif /*SUPLA_DEBUG*/
#ifdef BOARD_ON_CHANNEL_VALUE_SET
BOARD_ON_CHANNEL_VALUE_SET
#endif /*BOARD_ON_CHANNEL_VALUE_SET*/
#if defined(RGBW_CONTROLLER_CHANNEL) \
|| defined(RGBWW_CONTROLLER_CHANNEL) \
|| defined(RGB_CONTROLLER_CHANNEL) \
|| defined(DIMMER_CHANNEL)
unsigned char rgb_cn = 255;
unsigned char dimmer_cn = 255;
#ifdef RGBW_CONTROLLER_CHANNEL
rgb_cn = RGBW_CONTROLLER_CHANNEL;
#endif
#ifdef RGBWW_CONTROLLER_CHANNEL
rgb_cn = RGBWW_CONTROLLER_CHANNEL;
dimmer_cn = RGBWW_CONTROLLER_CHANNEL+1;
#endif
#ifdef RGB_CONTROLLER_CHANNEL