This is motivated by the measure period by the css811 sensor, which is 1 minute and cannot be set to 2. Hence, we adverstise every minute to avoid trashing half of the measurements
76 lines
2.4 KiB
C
76 lines
2.4 KiB
C
#include "supervisor.h"
|
|
|
|
const int SLEEP_GRANULARITY = 1; // [min]
|
|
const int SLEEP_MIN_DURATION = SLEEP_GRANULARITY; // [min]
|
|
const int SLEEP_MAX_DURATION = 30; // [min]
|
|
|
|
// Zephyr related stuff
|
|
|
|
void thread_supervisor(){
|
|
supervisor_init();
|
|
supervisor_run();
|
|
//should never return
|
|
}
|
|
|
|
// supervisor stuff
|
|
|
|
enum error_code supervisor_init(){
|
|
enum error_code ret = init_failed;
|
|
if(
|
|
success == ble_init() &&
|
|
success == co2_lvl_init() &&
|
|
success == hygro_init() &&
|
|
success == thermo_init() &&
|
|
success == window_init() &&
|
|
success == battery_init()
|
|
){
|
|
ret = success;
|
|
}else{}
|
|
return ret;
|
|
}
|
|
|
|
enum error_code supervisor_run(){
|
|
int co2_lvl_value = -1;
|
|
int hygro_value = -1;
|
|
int thermo_value = -1;
|
|
int batt_value = -1;
|
|
enum window_status window_value = unknown;
|
|
enum error_code co2_lvl_status, hygro_status, thermo_status, window_status, batt_status;
|
|
int current_sleep_time = SLEEP_MIN_DURATION;
|
|
while(1){
|
|
co2_lvl_status = co2_lvl_get_value(&co2_lvl_value);
|
|
hygro_status = hygro_get_value(&hygro_value);
|
|
thermo_status = thermo_get_value(&thermo_value);
|
|
window_status = window_get_value(&window_value);
|
|
batt_status = battery_get_value(&batt_value);
|
|
if(success != window_status){
|
|
window_value = unknown;
|
|
}else{}
|
|
if(success != hygro_status){
|
|
hygro_value = -1;
|
|
}else{}
|
|
if(success != thermo_status){
|
|
thermo_value = -1;
|
|
}else{}
|
|
if(success != co2_lvl_status){
|
|
co2_lvl_value = -1;
|
|
}else{}
|
|
if(success != batt_status){
|
|
batt_value = -1;
|
|
}else{}
|
|
ble_advertise(window_value, hygro_value, thermo_value, co2_lvl_value, batt_value);
|
|
if((co2_lvl_value > CO2_LEVEL_EMPTY_ROOM) || (window_value == open)){
|
|
// there are people in the room, or someone forgot to close the window
|
|
current_sleep_time = SLEEP_MIN_DURATION;
|
|
}else{
|
|
// no one is in the room, we can wait a litle bit longer before getting the next data point
|
|
current_sleep_time += SLEEP_GRANULARITY;
|
|
if(current_sleep_time > SLEEP_MAX_DURATION){
|
|
current_sleep_time = SLEEP_MAX_DURATION;
|
|
}else{}
|
|
}
|
|
k_sleep(K_MINUTES(current_sleep_time));
|
|
}
|
|
return error_unknown; // should never return
|
|
}
|