-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor_manager.cpp
More file actions
281 lines (262 loc) · 8.1 KB
/
sensor_manager.cpp
File metadata and controls
281 lines (262 loc) · 8.1 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
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
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/watchdog.h"
#include "pico/multicore.h"
#include "json.hpp"
#include "Tricorder_AS7341.h"
#include "Tricorder_BME688.h"
#include "Tricorder_LTR390.h"
#include "Tricorder_PMSA003I.h"
#include "Tricorder_TLV493D.h"
#include "Tricorder_SCD41.h"
#include "Tricorder_Sensor.h"
#define GPIO_ON 1
#define GPIO_OFF 0
#define LED_PIN 25
#define MAX_BUFFER_SIZE 256
#define MAX_INITIALIZATION_RETRIES 2
#define INITIALIZATION_RETRY_DELAY 10
// Sensor Array
Tricorder_AS7341 _as7341 = Tricorder_AS7341();
Tricorder_BME688 _bme688 = Tricorder_BME688();
Tricorder_LTR390 _ltr390 = Tricorder_LTR390();
Tricorder_PMSA003I _pmsa003i = Tricorder_PMSA003I();
Tricorder_TLV493D _tlv493d = Tricorder_TLV493D();
Tricorder_SCD41 _scd41 = Tricorder_SCD41();
Tricorder_Sensor *sensors[] = {
&_as7341,
&_bme688,
&_ltr390,
&_pmsa003i,
//&_tlv493d,
//&_scd41
};
void send_json(nlohmann::json frame){
printf("<%s>\n", frame.dump().c_str());
}
void async_sensor_update_loop(){
while(true){
for(Tricorder_Sensor *sensor : sensors){
sensor->async_update_sensor();
}
}
}
int main(){
// Global Variables
bool led_state = false;
uint64_t led_stamp = time_us_64();
//char nam_buf[MAX_BUFFER_SIZE], prop_buf[MAX_BUFFER_SIZE], val_buf[MAX_BUFFER_SIZE];
bool read_ready = false;
int read_state = 0;
int read_idx = 0;
char input_buffer[MAX_BUFFER_SIZE];
// Basic Initialization
stdio_init_all();
gpio_set_function(2, GPIO_FUNC_UART);
gpio_set_function(3, GPIO_FUNC_UART);
uart_set_hw_flow(uart0, true, true);
//if(watchdog_caused_reboot()){ printf("<Rebooted By Watchdog>\n"); }
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
// Initialize Sensors
//printf("<Initializing Sensors>\n");
for(Tricorder_Sensor *sensor : sensors){
gpio_put(LED_PIN, GPIO_ON);
led_state = true;
int counter = 0;
bool success = sensor->sensor_init();
while(!success && counter < MAX_INITIALIZATION_RETRIES){
sleep_ms(INITIALIZATION_RETRY_DELAY);
counter++;
success = sensor->sensor_init();
//printf("<Retrying: %s>\n", sensor->sensor_name);
}
if(success){
//printf("<Initialized: %s>\n", sensor->sensor_name);
}else{
//printf("<Failed To Initialize: %s>\n", sensor->sensor_name);
}
gpio_put(LED_PIN, GPIO_OFF);
led_state = false;
sleep_ms(10);
}
// Blinky Show
gpio_put(LED_PIN, GPIO_OFF);
led_state = false;
sleep_ms(500);
for(int i = 0; i < 20; i++){
gpio_put(LED_PIN, led_state ? GPIO_ON : GPIO_OFF);
led_state = !led_state;
sleep_ms(50);
}
//printf("<Finished Initializing Sensors>\n");
// Send Ready Message
printf("<Sensor Manager Ready>\n");
// Launch Asynchronous Sensor Reader
multicore_launch_core1(async_sensor_update_loop);
// Main Loop
watchdog_enable(1000, 1);
while(true){
watchdog_update();
//printf("Start Loop\n");
uint64_t current_time = time_us_64();
// Read Input
while(uart_is_readable(uart0) && !read_ready){
if(read_idx >= MAX_BUFFER_SIZE){
read_ready = false;
read_idx = 0;
read_state = 0;
}
char tmp = uart_getc(uart0);
switch(read_state){
case 0:
if(tmp == '<'){
read_state = 1;
read_idx = 0;
}
break;
case 1:
if(tmp == '>'){
input_buffer[read_idx] = '\0';
read_state = 0;
read_idx = 0;
read_ready = true;
}else{
input_buffer[read_idx] = tmp;
read_idx++;
}
break;
}
}
// Handle Input
if(read_ready){
//printf("\n<RECV: %s>\n\n", input_buffer);
read_ready = false;
auto jinp = nlohmann::json::parse(input_buffer, nullptr, false);
if(!jinp.is_discarded()){
const char *cmd = jinp.at("command").get<std::string>().c_str();
if(strcmp(cmd, "list_sensors") == 0){
//printf("<EXECUTE: List Sensors>\n");
nlohmann::json sensor_list = nlohmann::json::array();
for(Tricorder_Sensor *sensor : sensors){
nlohmann::json entry = {
{"name", sensor->sensor_name},
{"Initialized", sensor->initialized},
{"enabled", sensor->enabled}
};
sensor_list.push_back(entry);
}
nlohmann::json report_frame = {
{"report", "sensor_list"},
{"sensors", sensor_list}
};
send_json(report_frame);
}else if(strcmp(cmd, "set_parameter") == 0){
//printf("<EXECUTE: Set Parameter>\n");
//printf("<Getting target...>\n");
const char *target = jinp.at("sensor").get<std::string>().c_str();
//printf("<Getting parameter...>\n");
const char *param = jinp.at("param").get<std::string>().c_str();
//printf("<Getting value...>\n");
const char *val = jinp.at("value").get<std::string>().c_str();
//printf("<Searching for sensor...>\n");
for(Tricorder_Sensor *sensor : sensors){
if(strcmp(target, sensor->sensor_name) == 0){
//printf("<Setting property...>\n");
sensor->set_property((char*)param, (char*)val);
//printf("<Property set!>\n");
break;
}
}
}else if(strcmp(cmd, "list_options") == 0){
//printf("<EXECUTE: List Options>\n");
//printf("<Getting target...>\n");
const char *target = jinp.at("sensor").get<std::string>().c_str();
//printf("<Searching for sensor...>\n");
for(Tricorder_Sensor *sensor : sensors){
if(strcmp(target, sensor->sensor_name) == 0){
//printf("<Building message...>\n");
nlohmann::json opts_frame = sensor->report_options();
send_json(opts_frame);
//printf("<Message sent!>\n");
break;
}
}
}else if(strcmp(cmd, "reset") == 0){
//printf("<EXECUTE: Reset>\n");
printf("<Rebooting...>\n");
while(true){ sleep_ms(10); }
}else if(strcmp(cmd, "initialize") == 0){
//printf("<EXECUTE: Initialize>\n");
//printf("<Getting target...>\n");
const char *target = jinp.at("sensor").get<std::string>().c_str();
//printf("<Searching for sensor...>\n");
for(Tricorder_Sensor *sensor : sensors){
if(strcmp(target, sensor->sensor_name) == 0){
//printf("<Initializing sensor...>\n");
sensor->sensor_init();
//printf("<Sensor initialized!>\n");
break;
}
}
}else if(strcmp(cmd, "enable") == 0){
//printf("<EXECUTE: Enable>\n");
//printf("<Getting target...>\n");
const char *target = jinp.at("sensor").get<std::string>().c_str();
//printf("<Searching for sensor...>\n");
for(Tricorder_Sensor *sensor : sensors){
if(strcmp(target, sensor->sensor_name) == 0){
//printf("<Enabling sensor...>\n");
sensor->set_property("enabled", "true");
//printf("<Sensor Enabled!>\n");
break;
}
}
}else if(strcmp(cmd, "disable") == 0){
//printf("<EXECUTE: Disable>\n");
//printf("<Getting target...>\n");
const char *target = jinp.at("sensor").get<std::string>().c_str();
//printf("<Searching for sensor...>\n");
for(Tricorder_Sensor *sensor : sensors){
if(strcmp(target, sensor->sensor_name) == 0){
//printf("<Disabling sensor...>\n");
sensor->set_property("enabled", "false");
//printf("<Sensor Disabled!>\n");
break;
}
}
}else if(strcmp(cmd, "enable_all") == 0){
for(Tricorder_Sensor *sensor : sensors){
sensor->set_property("enabled", "true");
}
}else if(strcmp(cmd, "disable_all") == 0){
for(Tricorder_Sensor *sensor : sensors){
sensor->set_property("enabled", "false");
}
}
}
}
// Update LED
//printf("Updating LED...\n");
gpio_put(LED_PIN, led_state ? GPIO_ON : GPIO_OFF);
led_state = !led_state;
//sleep_ms(1000);
// Run through sensors
//printf("Reporting Sensors...\n");
for(Tricorder_Sensor *sensor : sensors){
//printf(" Updating %s...\n", sensor->sensor_name);
//bool sensor_updated = sensor->update_data();
//printf(" Finished updating %s\n", sensor->sensor_name);
//if(sensor_updated){
// //printf(" Reporting %s...\n", sensor->sensor_name);
// nlohmann::json report_frame = sensor->build_json();
// send_json(report_frame);
// //printf(" Finished reporting %s\n", sensor->sensor_name);
//}
//sensor->async_update_sensor();
if(sensor->async_poll_sensor()){
send_json(sensor->async_get_frame());
}
}
}
}