/
supla_esp.h
444 lines (344 loc) · 11.3 KB
/
supla_esp.h
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
/*
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.
*/
#ifndef SUPLA_ESP_H_
#define SUPLA_ESP_H_
#include "board/supla_esp_board.h"
#include "espmissingincludes.h"
#define SUPLA_ESP_SOFTVER "2.8.59"
#define STATE_UNKNOWN 0
#define STATE_DISCONNECTED 1
#define STATE_IPRECEIVED 2
#define STATE_CONNECTED 4
#define STATE_CFGMODE 5
#define STATE_UPDATE 6
#ifndef ESP8266_SUPLA_PROTO_VERSION
#define ESP8266_SUPLA_PROTO_VERSION SUPLA_PROTO_VERSION
#endif /* ESP8266_SUPLA_PROTO_VERSION */
#ifndef STATE_SECTOR_OFFSET
#define STATE_SECTOR_OFFSET 1
#endif /*STATE_SECTOR_OFFSET*/
#define LO_VALUE 0
#define HI_VALUE 1
#define RELAY_INIT_VALUE LO_VALUE
#ifndef SAVE_STATE_DELAY
#define SAVE_STATE_DELAY 1000
#endif /*SAVE_STATE_DELAY*/
#ifndef SEND_BUFFER_SIZE
#define SEND_BUFFER_SIZE 500
#endif /*SEND_BUFFER_SIZE*/
#ifndef CFG_BTN_PRESS_TIME
#define CFG_BTN_PRESS_TIME 5000
#endif /*CFG_BTN_PRESS_TIME*/
#ifndef GET_CFG_PRESS_TIME
#define GET_CFG_PRESS_TIME supla_esp_input_get_cfg_press_time
#endif /*GET_CFG_PRESS_TIME*/
// minimum button press time after which ON_HOLD action trigger is send
#ifndef BTN_HOLD_TIME_MS
#define BTN_HOLD_TIME_MS 700
#endif /*BTN_HOLD_TIME_MS*/
// maximum delay between button press time to count it as multiclick
#ifndef BTN_MULTICLICK_TIME_MS
#define BTN_MULTICLICK_TIME_MS 300
#endif /*BTN_MULTICLICK_TIME_MS*/
#ifndef INPUT_MAX_COUNT
#define INPUT_MAX_COUNT 7
#endif /*INPUT_MAX_COUNT*/
#ifndef RELAY_MAX_COUNT
#define RELAY_MAX_COUNT 4
#endif /*RELAY_MAX_COUNT*/
#ifndef RS_MAX_COUNT
#define RS_MAX_COUNT 8
#endif /*RS_MAX_COUNT*/
#ifndef RS_SAVE_STATE_DELAY
#define RS_SAVE_STATE_DELAY 0
#endif /*RS_SAVE_STATE_DELAY*/
#ifndef RS_AUTOCAL_FILTERING_TIME_MS
// 300 ms of filtering time since RS start movement and we start to check if
// RS motor is taking current
#define RS_AUTOCAL_FILTERING_TIME_MS 300
#endif /*RS_AUTOCAL_FILTERING_TIME_MS*/
#ifndef RS_AUTOCAL_MIN_TIME_MS
// auto calibration with calculated times below 500 ms will be considered as
// error
#define RS_AUTOCAL_MIN_TIME_MS 500
#endif /*RS_AUTOCAL_MIN_TIME_MS*/
#ifndef RS_AUTOCAL_MAX_TIME_MS
// auto calibration with calculated time above 9 min 50 s will be considered as
// error
#define RS_AUTOCAL_MAX_TIME_MS ((9 * 60 + 50) * 1000)
#endif /*RS_AUTOCAL_MAX_TIME_MS*/
#ifndef CFG_TIME1_COUNT
#define CFG_TIME1_COUNT 8
#endif /*CFG_TIME1_COUNT*/
#ifndef CFG_TIME2_COUNT
#define CFG_TIME2_COUNT 8
#endif /*CFG_TIME2_COUNT*/
#ifndef STATE_CFG_TIME1_COUNT
#define STATE_CFG_TIME1_COUNT CFG_TIME1_COUNT
#endif /*STATE_CFG_TIME1_COUNT*/
#ifndef STATE_CFG_TIME2_COUNT
#define STATE_CFG_TIME2_COUNT CFG_TIME2_COUNT
#endif /*STATE_CFG_TIME2_COUNT*/
#ifndef SMOOTH_MAX_COUNT
#define SMOOTH_MAX_COUNT 1
#endif /*SMOOTH_MAX_COUNT*/
#define INPUT_FLAG_PULLUP 0x01
#define INPUT_FLAG_CFG_BTN 0x02
#define INPUT_FLAG_FACTORY_RESET 0x04
#define INPUT_FLAG_DISABLE_INTR 0x08
#define INPUT_FLAG_TRIGGER_ON_PRESS 0x10 // used for monostable inputs
// If none of below flags are set, then by default "on hold" entry to cfg mode
// is used for monostable button, and 10x toggle for bistable button.
// In case of monostable input, "on toggle" flag enables 10x press to enter cfg
// mode and disables default "on hold".
// If both "on toggle" and "on hold" flags are set for monostable input then
// both methods will be enabled for monostable input.
// Those flags doesn't have any effect on bistable inputs - there is always
// only "on toggle" variant possible.
#define INPUT_FLAG_CFG_ON_TOGGLE 0x20
#define INPUT_FLAG_CFG_ON_HOLD 0x40
#define INPUT_TYPE_SENSOR 1
#define INPUT_TYPE_BTN_MONOSTABLE 2
// Standard BISTABLE button toggles relay on each input state change
#define INPUT_TYPE_BTN_BISTABLE 4
// Motion sensor turns relay on, when sensor is on, and turns off,
// when sensor is off (fixed position-state). It ignores stored relay state
#define INPUT_TYPE_MOTION_SENSOR 8
#define INPUT_TYPE_CUSTOM 200
#ifndef INPUT_MIN_CYCLE_COUNT
#define INPUT_MIN_CYCLE_COUNT 5
#endif /*INPUT_MIN_CYCLE_COUNT*/
#ifndef INPUT_CYCLE_TIME
#define INPUT_CYCLE_TIME 20
#endif /*INPUT_CYCLE_TIME*/
// milliseconds
#ifndef RS_START_DELAY
#define RS_START_DELAY 1000
#endif
// milliseconds
#ifndef RS_STOP_DELAY
#define RS_STOP_DELAY 500
#endif
// microseconds
#ifndef RELAY_MIN_DELAY
#define RELAY_MIN_DELAY 100000
#endif
#ifndef MAIN_ICACHE_FLASH
#define MAIN_ICACHE_FLASH ICACHE_FLASH_ATTR
#endif
#ifndef GPIO_ICACHE_FLASH
#define GPIO_ICACHE_FLASH ICACHE_FLASH_ATTR
#endif
#ifndef DEVCONN_ICACHE_FLASH
#define DEVCONN_ICACHE_FLASH ICACHE_FLASH_ATTR
#endif
#ifndef CFG_ICACHE_FLASH_ATTR
#define CFG_ICACHE_FLASH_ATTR ICACHE_FLASH_ATTR
#endif
#ifndef DHT_ICACHE_FLASH
#define DHT_ICACHE_FLASH ICACHE_FLASH_ATTR
#endif
#ifndef CDT_ICACHE_FLASH_ATTR
#define CDT_ICACHE_FLASH_ATTR ICACHE_FLASH_ATTR
#endif
#ifndef DNS_ICACHE_FLASH_ATTR
#define DNS_ICACHE_FLASH_ATTR ICACHE_FLASH_ATTR
#endif
#ifndef BTN1_DEFAULT
#define BTN1_DEFAULT BTN_TYPE_MONOSTABLE
#endif
#ifndef BTN2_DEFAULT
#define BTN2_DEFAULT BTN_TYPE_BISTABLE
#endif
#ifndef BTN_MAX_COUNT
#define BTN_MAX_COUNT 4
#endif
#ifndef MANUFACTURER_ID
#define MANUFACTURER_ID 0
#endif
#ifndef PRODUCT_ID
#define PRODUCT_ID 0
#endif
#ifndef DEVICE_FLAGS
#define DEVICE_FLAGS SUPLA_DEVICE_FLAG_CALCFG_ENTER_CFG_MODE
#endif
#ifndef MQTT_PREFIX_SIZE
#define MQTT_PREFIX_SIZE 50
#endif /*MQTT_PREFIX_SIZE*/
#ifndef MQTT_RECVBUF_SIZE
#define MQTT_RECVBUF_SIZE 1024
#endif /*MQTT_RECVBUF_SIZE*/
#ifndef MQTT_SENDBUF_SIZE
#define MQTT_SENDBUF_SIZE 4096
#endif /*MQTT_SENDBUF_SIZE*/
#ifndef MQTT_DEVICE_NAME
#define MQTT_DEVICE_NAME AP_SSID
#endif /*MQTT_DEVICE_NAME*/
#ifndef MQTT_POOL_PUBLICATION_MAX_DELAY
#define MQTT_POOL_PUBLICATION_MAX_DELAY 3600
#endif /*MQTT_POOL_PUBLICATION_MAX_DELAY*/
#ifndef MQTT_BOARD_ACTION_TRIGGER_IDX_OFFSET
#define MQTT_BOARD_ACTION_TRIGGER_IDX_OFFSET 0
#endif /*MQTT_BOARD_ACTION_TRIGGER_IDX_OFFSET*/
#ifndef MQTT_BOARD_ACTION_TRIGGER_FIRST_CHANNEL_ID
#define MQTT_BOARD_ACTION_TRIGGER_FIRST_CHANNEL_ID 0
#endif /*MQTT_BOARD_ACTION_TRIGGER_FIRST_CHANNEL_ID*/
#define CFG_FLAG_MQTT_ENABLED 0x01
#define CFG_FLAG_MQTT_NO_RETAIN 0x02
#define CFG_FLAG_MQTT_TLS 0x04
#define CFG_FLAG_MQTT_NO_AUTH 0x08
#define CFG_FLAG_DEVICE_LOCKED 0x10
#ifndef CFG_FLAG_TRIGGER_ON_RELEASE
#define CFG_FLAG_TRIGGER_ON_RELEASE 0
#endif /*CFG_FLAG_TRIGGER_ON_RELEASE*/
#ifndef CFG_FLAG_TRIGGER_ON_PRESS
#define CFG_FLAG_TRIGGER_ON_PRESS 1
#endif /*CFG_FLAG_TRIGGER_ON_PRESS*/
#ifdef MQTT_HA_ROLLERSHUTTER_SUPPORT
#ifndef BOARD_ON_ROLLERSHUTTER_POSITION_CHANGED
#define BOARD_ON_ROLLERSHUTTER_POSITION_CHANGED
#endif
#endif
void supla_esp_board_set_device_name(char *buffer, uint8 buffer_size);
#if ESP8266_SUPLA_PROTO_VERSION >= 10
void supla_esp_board_set_channels(TDS_SuplaDeviceChannel_C *channels,
unsigned char *channel_count);
#else
void supla_esp_board_set_channels(TDS_SuplaDeviceChannel_B *channels,
unsigned char *channel_count);
#endif /*ESP8266_SUPLA_PROTO_VERSION >= 10*/
void supla_esp_board_relay_before_change_state(void);
void supla_esp_board_relay_after_change_state(void);
void supla_esp_board_gpio_init(void);
void ICACHE_FLASH_ATTR supla_system_restart(void);
void ICACHE_FLASH_ATTR supla_system_restart_with_delay(uint32 delay_ms);
#ifdef __FOTA
#ifndef UPDATE_TIMEOUT
// 120 sec.
#define UPDATE_TIMEOUT 120000000
#endif /* UPDATE_TIMEOUT */
#define RSA_NUM_BYTES 512
#define RSA_PUBLIC_EXPONENT 65537
extern const uint8_t rsa_public_key_bytes[RSA_NUM_BYTES];
#ifndef UPDATE_PARAM3
#define UPDATE_PARAM3 0
#endif /*UPDATE_PARAM3*/
#ifndef UPDATE_PARAM4
#define UPDATE_PARAM4 0
#endif /*UPDATE_PARAM4*/
#endif /*__FOTA*/
#ifndef INTR_CLEAR_MASK
#define INTR_CLEAR_MASK 0xFF
#endif
#ifndef GPIO_PORT_INIT
#define GPIO_PORT_INIT \
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); \
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4); \
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5); \
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); \
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13); \
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14)
#endif
// PWM ----------------------------------
#ifndef PWM_0_OUT_IO_MUX
#define PWM_0_OUT_IO_MUX PERIPHS_IO_MUX_GPIO4_U
#define PWM_0_OUT_IO_NUM 4
#define PWM_0_OUT_IO_FUNC FUNC_GPIO4
#endif
#ifndef PWM_1_OUT_IO_MUX
#define PWM_1_OUT_IO_MUX PERIPHS_IO_MUX_GPIO5_U
#define PWM_1_OUT_IO_NUM 5
#define PWM_1_OUT_IO_FUNC FUNC_GPIO5
#endif
#ifndef PWM_2_OUT_IO_MUX
#define PWM_2_OUT_IO_MUX PERIPHS_IO_MUX_MTDI_U
#define PWM_2_OUT_IO_NUM 12
#define PWM_2_OUT_IO_FUNC FUNC_GPIO12
#endif
#ifndef PWM_3_OUT_IO_MUX
#define PWM_3_OUT_IO_MUX PERIPHS_IO_MUX_MTCK_U
#define PWM_3_OUT_IO_NUM 13
#define PWM_3_OUT_IO_FUNC FUNC_GPIO13
#endif
#ifndef PWM_4_OUT_IO_MUX
#define PWM_4_OUT_IO_MUX PERIPHS_IO_MUX_MTMS_U
#define PWM_4_OUT_IO_NUM 14
#define PWM_4_OUT_IO_FUNC FUNC_GPIO14
#endif
#ifndef PWM_PERIOD
#define PWM_PERIOD 1000
#endif
// --------------------------------------
#ifndef AP_SSID
#ifdef ESP8285
#define AP_SSID "SUPLA-ESP8285"
#else
#define AP_SSID "SUPLA-ESP8266"
#endif
#endif
#define SPI_FLASH_SEC_SIZE 4096
#define SERVER_MAXSIZE 100
#define WIFI_SSID_MAXSIZE 32
#define WIFI_PWD_MAXSIZE 64
#define STATE_MAXSIZE 300
#define RECVBUFF_MAXSIZE 1024
#define ACTIVITY_TIMEOUT 10
#ifdef WATCHDOG_TIMEOUT
#error "WATCHDOG_TIMEOUT is deprecated use WATCHDOG_TIMEOUT_SEC"
#endif /*WATCHDOG_TIMEOUT*/
#ifdef WATCHDOG_SOFT_TIMEOUT
#error "WATCHDOG_SOFT_TIMEOUT is deprecated use WATCHDOG_SOFT_TIMEOUT_SEC"
#endif /*WATCHDOG_SOFT_TIMEOUT*/
#ifndef WATCHDOG_TIMEOUT_SEC
#define WATCHDOG_TIMEOUT_SEC 60
#endif /*WATCHDOG_TIMEOUT*/
#ifndef WATCHDOG_SOFT_TIMEOUT_SEC
// WATCHDOG_SOFT_TIMEOUT_SEC > WATCHDOG_TIMEOUT == WATCHDOG_TIMEOUT inactive
#define WATCHDOG_SOFT_TIMEOUT_SEC 65
#endif /*WATCHDOG_SOFT_TIMEOUT_SEC*/
#ifndef RECONNECT_DELAY_MSEC
#define RECONNECT_DELAY_MSEC 2000
#endif /*RECONNECT_DELAY_MSEC*/
#ifndef RELAY_DOUBLE_TRY
#define RELAY_DOUBLE_TRY 10000
#endif
#ifndef RS_MAX_COUNT
#define RS_MAX_COUNT 4
#endif
#ifndef RGBW_CHANNEl_CMP
#define RGBW_CHANNEl_CMP
#endif
#ifndef RGBW_CHANNEL_LIMIT
#define RGBW_CHANNEL_LIMIT \
if (ChannelNumber >= 2) return;
#endif
#ifdef DONT_SAVE_STATE
#define DEVICE_STATE_INACTIVE
#endif
#ifndef CHANNEL_CONFIG_LIMIT
#define CHANNEL_CONFIG_LIMIT 8
#endif /*CHANNEL_CONFIG_LIMIT*/
unsigned _supla_int64_t MAIN_ICACHE_FLASH uptime_usec(void);
unsigned _supla_int64_t MAIN_ICACHE_FLASH uptime_msec(void);
uint32 MAIN_ICACHE_FLASH uptime_sec(void);
#ifdef CFG_TIME_VARIABLES
#ifndef CFG_TIME_VARIABLES_PRECISION
#define CFG_TIME_VARIABLES_PRECISION 0
#endif /*CFG_TIME_VARIABLES_PRECISION*/
#endif /*CFG_TIME_VARIABLES*/
#ifndef INPUT_SILENT_STARTUP_TIME_MS
#define INPUT_SILENT_STARTUP_TIME_MS 400
#endif /*INPUT_SILENT_STARTUP_TIME_MS*/
#endif /* SUPLA_ESP_H_ */