forked from pvvx/ATC_MiThermometer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger.c
98 lines (93 loc) · 2.55 KB
/
trigger.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
/*
* trigger.c
*
* Created on: 02.01.2021
* Author: pvvx
*/
#include <stdint.h>
#include "tl_common.h"
#include "stack/ble/ble.h"
#include "app.h"
#if USE_TRIGGER_OUT
#include "drivers.h"
#include "sensor.h"
#include "trigger.h"
#include "rds_count.h"
const trigger_t def_trg = {
.temp_threshold = 2100, // 21 °C
.humi_threshold = 5000, // 50 %
.temp_hysteresis = -55, // enable, -0.55 °C
.humi_hysteresis = 0 // disable
#if USE_WK_RDS_COUNTER
,.rds_time_report = 3600 // 1 hours
#ifdef GPIO_KEY2
,.rds.type = RDS_SWITCH
#else
,.rds.type = RDS_CONNECT
#endif
#endif
};
RAM trigger_t trg;
_attribute_ram_code_ void test_trg_on(void) {
if (trg.temp_hysteresis || trg.humi_hysteresis) {
trg.flg.trigger_on = true;
trg.flg.trg_output = (trg.flg.humi_out_on || trg.flg.temp_out_on);
} else {
trg.flg.trigger_on = false;
}
gpio_setup_up_down_resistor(GPIO_TRG, trg.flg.trg_output ? PM_PIN_PULLUP_10K : PM_PIN_PULLDOWN_100K);
}
_attribute_ram_code_ __attribute__((optimize("-Os"))) void set_trigger_out(void) {
if (trg.temp_hysteresis) {
if (trg.flg.temp_out_on) { // temp_out on
if (trg.temp_hysteresis < 0) {
if (measured_data.temp > trg.temp_threshold - trg.temp_hysteresis) {
trg.flg.temp_out_on = false;
}
} else {
if (measured_data.temp < trg.temp_threshold - trg.temp_hysteresis) {
trg.flg.temp_out_on = false;
}
}
} else { // temp_out off
if (trg.temp_hysteresis < 0) {
if (measured_data.temp < trg.temp_threshold + trg.temp_hysteresis) {
trg.flg.temp_out_on = true;
}
} else {
if (measured_data.temp > trg.temp_threshold + trg.temp_hysteresis) {
trg.flg.temp_out_on = true;
}
}
}
} else trg.flg.temp_out_on = false;
if (trg.humi_hysteresis) {
if (trg.flg.humi_out_on) { // humi_out on
if (trg.humi_hysteresis < 0) {
if (measured_data.humi > trg.humi_threshold - trg.humi_hysteresis) {
// humi > threshold
trg.flg.humi_out_on = false;
}
} else { // hysteresis > 0
if (measured_data.humi < trg.humi_threshold - trg.humi_hysteresis) {
// humi < threshold
trg.flg.humi_out_on = false;
}
}
} else { // humi_out off
if (trg.humi_hysteresis < 0) {
if (measured_data.humi < trg.humi_threshold + trg.humi_hysteresis) {
// humi < threshold
trg.flg.humi_out_on = true;
}
} else { // hysteresis > 0
if (measured_data.humi > trg.humi_threshold + trg.humi_hysteresis) {
// humi > threshold
trg.flg.humi_out_on = true;
}
}
}
} else trg.flg.humi_out_on = false;
test_trg_on();
}
#endif // USE_TRIGGER_OUT