-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.cpp
More file actions
112 lines (57 loc) · 2.67 KB
/
Copy pathenvironment.cpp
File metadata and controls
112 lines (57 loc) · 2.67 KB
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
// this is a base input class, this class will not access any of the other
// files, other files will include this file this file doesnt need to include
// any other file in this project
// i estimate this file to take about 1% of the arduino memeory and the
// libraries are taking 2%
#include "Libraries/SparkFun/SparkFunHTU21D.h"
#include "Libraries/SparkFun/SparkFunHTU21D.cpp"
namespace Environment{
float last_temperature = 0;
float last_humidity = 0;
HTU21D sensor; // Create an HTU21D object
int sensor_cooldown_ms = 1000; // we only check the tempreture every second
typedef void (*EnvironmentCallback)(float tempreture, float humidity); // create the call back type
const int MAX_CALLBACKS = 1; // each array can only hold 5 callbacks
EnvironmentCallback environment_callbacks[MAX_CALLBACKS];
int environment_callback_count = 0;
void on_environment_event(float tempreture, float humidity){
for (int i = 0; i < environment_callback_count; i++) {
if (environment_callbacks[i] != nullptr) {
environment_callbacks[i](tempreture,humidity);
}
}
}
bool sensor_cooldown_passed() {
// this avoid internal bounce effect for the buttons
static unsigned long last_event_time = 0;
unsigned long now = millis();
if (now - last_event_time < sensor_cooldown_ms) {
return false; // ignore event
}
last_event_time = now;
return true; // accept event
}
void init(){
sensor.begin(); // initate the humidity sensor
}
void loop(){
if (!sensor_cooldown_passed()) return; // we return if the cool down has not passed
// check the humidity and check if it changed
float current_tempreture = sensor.readTemperature();
float current_humidity = sensor.readHumidity();
if (current_tempreture != last_temperature || // check tempreture
last_humidity != current_humidity) // check humidity
{
// yes i made them into one event am low on memeory other
// wise i would have made them into two event
last_temperature = current_tempreture;
last_humidity = current_humidity;
on_environment_event(current_tempreture, current_humidity);
}
}
void register_environment_callback(EnvironmentCallback callback) {
if (environment_callback_count < MAX_CALLBACKS) {
environment_callbacks[environment_callback_count++] = callback;
}
}
}