forked from pvvx/ATC_MiThermometer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrds_count.c
242 lines (231 loc) · 6.28 KB
/
rds_count.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
/*
* rds_count.c
*
* Created on: 20.02.2022
* Author: pvvx
*/
#include <stdint.h>
#include "tl_common.h"
#if USE_TRIGGER_OUT && USE_WK_RDS_COUNTER
#include "stack/ble/ble.h"
#include "app.h"
#include "drivers.h"
#include "sensor.h"
#include "trigger.h"
#include "ble.h"
#include "lcd.h"
#include "custom_beacon.h"
#if USE_MIHOME_BEACON
#include "mi_beacon.h"
#endif
#if USE_HA_BLE_BEACON
#include "ha_ble_beacon.h"
#endif
#include "rds_count.h"
#if (BLE_EXT_ADV)
extern u32 blt_advExpectTime;
extern u8 app_adv_set_param[];
#endif
RAM rds_count_t rds; // Reed switch pulse counter
void rds_init(void) {
if (rds.type) {
#ifndef GPIO_KEY2
if(rds.type == RDS_CONNECT)
trg.rds.rs_invert = 1;
#else
if(rds.type == RDS_CONNECT) {
rds.type = RDS_SWITCH;
trg.rds.type = RDS_SWITCH;
}
#endif
rds_input_on();
}
#if USE_WK_RDS_COUNTER32 // save 32 bits?
if ((flash_read_cfg(&rds.count_short[1], EEP_ID_RPC, sizeof(rds.count_short[1])) != sizeof(rds.count_short[1])) {
rds.count = 0;
}
#endif
rds.report_tick = utc_time_sec;
}
//_attribute_ram_code_
__attribute__((optimize("-Os")))
void set_rds_adv_data(void) {
adv_buf.send_count++;
int advertising_type = cfg.flg.advertising_type;
#if USE_SECURITY_BEACON
if (cfg.flg2.adv_crypto) {
if (advertising_type == ADV_TYPE_PVVX) {
#if USE_HA_BLE_BEACON
ha_ble_encrypt_event_beacon(rds.type);
#else
pvvx_encrypt_event_beacon(rds.type);
#endif
#if USE_HA_BLE_BEACON
} else if (advertising_type == ADV_TYPE_HA_BLE) {
ha_ble_encrypt_event_beacon(rds.type);
#endif
#if USE_MIHOME_BEACON
} else if (advertising_type == ADV_TYPE_MI) {
mi_encrypt_event_beacon(rds.type);
#endif
} else {
#if USE_HA_BLE_BEACON
ha_ble_encrypt_event_beacon(rds.type);
#else
atc_encrypt_event_beacon();
#endif
}
} else
#endif // USE_SECURITY_BEACON
{
if (advertising_type == ADV_TYPE_PVVX) {
#if USE_HA_BLE_BEACON
ha_ble_event_beacon(rds.type);
#else
pvvx_event_beacon(rds.type);
#endif
#if USE_HA_BLE_BEACON
} else if (advertising_type == ADV_TYPE_HA_BLE) {
ha_ble_event_beacon(rds.type);
#endif
#if USE_MIHOME_BEACON
} else if (advertising_type == ADV_TYPE_MI) {
mi_event_beacon(rds.type);
#endif
} else {
#if USE_HA_BLE_BEACON
ha_ble_event_beacon(rds.type);
#else
atc_event_beacon();
#endif
}
}
adv_buf.update_count = 0; // refresh adv_buf.data in next set_adv_data()
load_adv_data();
}
//_attribute_ram_code_
static void start_ext_adv(void) {
#if (BLE_EXT_ADV)
if (adv_buf.ext_adv_init) { // support extension advertise
set_rds_adv_data();
blta.adv_duraton_en = EXT_ADV_COUNT;
adv_buf.data_size = 0; // flag adv_buf.send_count++
ll_ext_adv_t *p = (ll_ext_adv_t *)&app_adv_set_param;
// patch: set time next ext.adv = 0
blt_advExpectTime = clock_time() + 250*CLOCK_16M_SYS_TIMER_CLK_1US; // set time next ext.adv
p->adv_event_tick = blt_advExpectTime;
p->advInt_use = EXT_ADV_INTERVAL; // new adv. interval
} else
#endif
{
bls_ll_setAdvEnable(BLC_ADV_DISABLE); // adv disable
bls_ll_setAdvParam(EXT_ADV_INTERVAL, EXT_ADV_INTERVAL,
ADV_TYPE_NONCONNECTABLE_UNDIRECTED, OWN_ADDRESS_PUBLIC, 0, NULL,
BLT_ENABLE_ADV_ALL, ADV_FP_NONE);
set_rds_adv_data();
adv_buf.data_size = 0; // flag adv_buf.send_count++
bls_ll_setAdvDuration(EXT_ADV_INTERVAL*EXT_ADV_COUNT*625+33, 1);
blta.adv_interval = 0; // system tick
bls_ll_setAdvEnable(BLC_ADV_ENABLE); // adv enable
}
}
_attribute_ram_code_ void rds_suspend(void) {
if (!ble_connected) {
/* TODO: if connection mode, gpio wakeup throws errors in sdk libs!
Work options: bls_pm_setSuspendMask(SUSPEND_ADV | DEEPSLEEP_RETENTION_ADV | SUSPEND_CONN);
No DEEPSLEEP_RETENTION_CONN */
cpu_set_gpio_wakeup(GPIO_RDS, BM_IS_SET(reg_gpio_in(GPIO_RDS), GPIO_RDS & 0xff)? Level_Low : Level_High, 1); // pad wakeup deepsleep enable
bls_pm_setWakeupSource(PM_WAKEUP_PAD | PM_WAKEUP_TIMER); // gpio pad wakeup suspend/deepsleep
} else {
cpu_set_gpio_wakeup(GPIO_RDS, Level_Low, 0); // pad wakeup suspend/deepsleep disable
}
}
/* if (rds.type) // rds.type: switch or counter */
_attribute_ram_code_
__attribute__((optimize("-Os")))
void rds_task(void) {
// rds_input_on(); // in "app_config.h" and WakeupLowPowerCb()
if (get_rds_input()) {
if (!trg.flg.rds_input) {
// keypress event
trg.flg.rds_input = 1;
if (rds.type == RDS_SWITCH) { // switch mode
rds.event = rds.type;
}
#ifndef GPIO_KEY2
else if (rds.type == RDS_CONNECT) { // connect mode
// connect keypress event
uint32_t new = clock_time();
ext_key.key_pressed_tik1 = new;
ext_key.key_pressed_tik2 = new;
set_adv_con_time(0); // set connection adv.
SET_LCD_UPDATE();
return;
}
#endif
}
#ifndef GPIO_KEY2
else if (rds.type == RDS_CONNECT) { // connect mode
// connection key held
uint32_t new = clock_time();
if(new - ext_key.key_pressed_tik1 > 1750*CLOCK_16M_SYS_TIMER_CLK_1MS) {
ext_key.key_pressed_tik1 = new;
cfg.flg.temp_F_or_C ^= 1;
if(ext_key.rest_adv_int_tad) {
set_adv_con_time(1); // restore default adv.
ext_key.rest_adv_int_tad = 0;
}
SET_LCD_UPDATE();
}
if(new - ext_key.key_pressed_tik2 > 20*CLOCK_16M_SYS_TIMER_CLK_1S) {
set_default_cfg();
}
return;
}
#endif
} else {
if (trg.flg.rds_input) {
// key released event
trg.flg.rds_input = 0;
rds.count++;
#if USE_WK_RDS_COUNTER32 // save 32 bits?
if (rds.count_short[0] == 0) {
flash_write_cfg(&rds.count_short[1], EEP_ID_RPC, sizeof(rds.count_short[1]));
}
#endif
if (rds.type == RDS_COUNTER) { // counter mode
if ((rds.count & 0xffff) == 0) { // report 'overflow 16 bit count'
rds.event = rds.type;
}
} else if (rds.type == RDS_SWITCH) { // switch mode
rds.event = rds.type;
}
}
#ifndef GPIO_KEY2
if (rds.type == RDS_CONNECT) { // connect mode
// connection key released
uint32_t new = clock_time();
ext_key.key_pressed_tik1 = new;
ext_key.key_pressed_tik2 = new;
/* in app.c: main_loop()
if(ext_key.rest_adv_int_tad < -80) {
set_adv_con_time(1); // restore default adv.
SET_LCD_UPDATE();
}
*/
return;
}
#endif
}
if (trg.rds_time_report
&& utc_time_sec - rds.report_tick > trg.rds_time_report) {
rds.event = rds.type;
}
if ((!ble_connected) && rds.event) {
rds.adv_counter++;
start_ext_adv();
rds.event = RDS_NONE;
rds.report_tick = utc_time_sec;
}
}
#endif // USE_TRIGGER_OUT