From e1d274470b1302ba2c43b3dd31509dfe4ec8612e Mon Sep 17 00:00:00 2001 From: adrien balleyguier Date: Wed, 22 Apr 2026 09:55:13 +0200 Subject: [PATCH] feat(nodes): WIP supervisor first implementation wip: untested --- nodes/src/ble_advertiser.c | 9 +++++++ nodes/src/ble_advertiser.h | 5 ++++ nodes/src/co2_level.c | 2 ++ nodes/src/co2_level.h | 2 ++ nodes/src/error_code.h | 2 ++ nodes/src/main.c | 2 ++ nodes/src/supervisor.c | 49 +++++++++++++++++++++++++++++++++++--- nodes/src/supervisor.h | 7 ++++++ nodes/src/window_status.h | 7 ++++++ 9 files changed, 82 insertions(+), 3 deletions(-) diff --git a/nodes/src/ble_advertiser.c b/nodes/src/ble_advertiser.c index e69de29..5406e40 100644 --- a/nodes/src/ble_advertiser.c +++ b/nodes/src/ble_advertiser.c @@ -0,0 +1,9 @@ +#include "ble_advertiser.h" + +enum error_code ble_init(){ + return success; +} + +enum error_code ble_advertise(){ + return success; +} diff --git a/nodes/src/ble_advertiser.h b/nodes/src/ble_advertiser.h index 020a938..32f8d18 100644 --- a/nodes/src/ble_advertiser.h +++ b/nodes/src/ble_advertiser.h @@ -1,4 +1,9 @@ #ifndef BLE_ADVERTISER_H #define BLE_ADVERTISER_H +#include "error_code.h" + +enum error_code ble_init(); +enum error_code ble_advertise(); + #endif //BLE_ADVERTISER_H diff --git a/nodes/src/co2_level.c b/nodes/src/co2_level.c index 56bc09a..ca70455 100644 --- a/nodes/src/co2_level.c +++ b/nodes/src/co2_level.c @@ -1,5 +1,7 @@ #include "co2_level.h" +const int CO2_LEVEL_EMPTY_ROOM = 400; // [ppm] + enum error_code co2_lvl_init(){ return success; } diff --git a/nodes/src/co2_level.h b/nodes/src/co2_level.h index 775650f..b56c9d8 100644 --- a/nodes/src/co2_level.h +++ b/nodes/src/co2_level.h @@ -3,6 +3,8 @@ #include "error_code.h" +extern const int CO2_LEVEL_EMPTY_ROOM; // [ppm] + enum error_code co2_lvl_init(); int co2_lvl_get_value(); diff --git a/nodes/src/error_code.h b/nodes/src/error_code.h index 6b382d7..ba43b4c 100644 --- a/nodes/src/error_code.h +++ b/nodes/src/error_code.h @@ -3,6 +3,8 @@ enum error_code{ success = 0, + init_failed, + error_unknown, error_code_last, // iteration purpose }; diff --git a/nodes/src/main.c b/nodes/src/main.c index aca0509..899afdd 100644 --- a/nodes/src/main.c +++ b/nodes/src/main.c @@ -8,6 +8,8 @@ #include #include +#include "supervisor.h" + int main(void){ supervisor_init(); supervisor_run(); diff --git a/nodes/src/supervisor.c b/nodes/src/supervisor.c index 717c272..34c08f9 100644 --- a/nodes/src/supervisor.c +++ b/nodes/src/supervisor.c @@ -1,10 +1,53 @@ #include "supervisor.h" +const int SLEEP_GRANULARITY = 2; // [min] +const int SLEEP_MIN_DURATION = SLEEP_GRANULARITY; // [min] +const int SLEEP_MAX_DURATION = 30; // [min] + enum error_code supervisor_init(){ - return success; + enum error_code ret = init_failed; + ret = ble_init(); + if(success == ret){ + ret = co2_lvl_init(); + if(success == ret){ + ret = hygro_init(); + if(success == ret){ + ret = thermo_init(); + if(success == ret){ + ret = window_init(); + }else{} + }else{} + }else{} + }else{} + // debug with led set -> init and blink led red + return ret; } enum error_code supervisor_run(){ - while(1); - return success; + int co2_lvl_value = -1; + int hygro_value = -1; + int thermo_value = -1; + enum window_status window_value = unknown; + int current_sleep_time = SLEEP_MIN_DURATION; + while(1){ + co2_lvl_value = co2_lvl_get_value(); + hygro_value = hygro_get_value(); + thermo_value = thermo_get_value(); + window_value = window_get_value(); + // maybe change arguments order + // todo : manage special values + ble_advertise(co2_lvl_value, hygro_value, thermo_value, window_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 liitle 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 } diff --git a/nodes/src/supervisor.h b/nodes/src/supervisor.h index d946ae9..b705c3e 100644 --- a/nodes/src/supervisor.h +++ b/nodes/src/supervisor.h @@ -1,13 +1,20 @@ #ifndef SUPERVISOR_H #define SUPERVISOR_H +#include + #include "error_code.h" +#include "ble_advertiser.h" #include "window_status.h" #include "thermometer.h" #include "hygrometer.h" #include "co2_level.h" +extern const int SLEEP_GRANULARITY; // [min] +extern const int SLEEP_MIN_DURATION; // [min] +extern const int SLEEP_MAX_DURATION; // [min] + enum error_code supervisor_init(); enum error_code supervisor_run(); diff --git a/nodes/src/window_status.h b/nodes/src/window_status.h index 3f5ac83..2e57d81 100644 --- a/nodes/src/window_status.h +++ b/nodes/src/window_status.h @@ -3,6 +3,13 @@ #include "error_code.h" +enum window_status{ + closed = 0, + open, + unknown, + windows_status_last, // iteration purpose +}; + enum error_code window_init(); int window_get_value();