doc(nodes): adding comments in the nodes code

close #62
This commit is contained in:
adrien balleyguier
2026-05-06 17:23:41 +02:00
committed by Klagarge
parent 78167ceb38
commit c32728ccbe
12 changed files with 118 additions and 32 deletions

View File

@@ -2,6 +2,8 @@
static const int BT_MS_ADV_UP = 500; // [ms] time during which the advertising is active
// keys as defined in the specs
static const char BT_KEY_SIZE = 1; // [B]
static const char BT_KEY_WINDOW = 0x01;
static const char BT_KEY_HUMIDITY = 0x02;
static const char BT_KEY_TEMP = 0x03;
@@ -14,15 +16,19 @@ static const char BT_VALUE_SIZE_HUMIDITY = 1;
static const char BT_VALUE_SIZE_TEMP = 2;
static const char BT_VALUE_SIZE_CO2_LVL = 4;
static const int BT_AD_DATA_INDEX_WINDOW = BT_PREAMBLE_SIZE + 1;
static const int BT_AD_DATA_INDEX_HUMIDITY = BT_AD_DATA_INDEX_WINDOW + 2;
static const int BT_AD_DATA_INDEX_TEMP = BT_AD_DATA_INDEX_HUMIDITY + 2;
static const int BT_AD_DATA_INDEX_CO2_LVL = BT_AD_DATA_INDEX_TEMP + 3;
// value start index in the frame - equal to {prior field index + prior value size + key size}
static const int BT_AD_DATA_INDEX_WINDOW = BT_PREAMBLE_SIZE + BT_KEY_SIZE;
static const int BT_AD_DATA_INDEX_HUMIDITY = BT_AD_DATA_INDEX_WINDOW + BT_VALUE_SIZE_WINDOW + BT_KEY_SIZE;
static const int BT_AD_DATA_INDEX_TEMP = BT_AD_DATA_INDEX_HUMIDITY + BT_VALUE_SIZE_HUMIDITY + BT_KEY_SIZE;
static const int BT_AD_DATA_INDEX_CO2_LVL = BT_AD_DATA_INDEX_TEMP + BT_VALUE_SIZE_TEMP + BT_KEY_SIZE;
// sum of all value size + size for all keys
// sum of all value size + size for all keys + size preamble
static const char BT_AD_TOTAL_SIZE =
BT_VALUE_SIZE_WINDOW + BT_VALUE_SIZE_HUMIDITY + BT_VALUE_SIZE_TEMP + BT_VALUE_SIZE_CO2_LVL + 4 + 2;
BT_PREAMBLE_SIZE + (4 * BT_KEY_SIZE) +
BT_VALUE_SIZE_WINDOW + BT_VALUE_SIZE_HUMIDITY + BT_VALUE_SIZE_TEMP + BT_VALUE_SIZE_CO2_LVL;
// data field in the broadcasted frame
// all values are set to 0 and the "company id" field is set to 0xffff
static uint8_t ad_data[] = {
0xff, 0xff,
BT_KEY_WINDOW, 0x00,
@@ -30,6 +36,7 @@ static uint8_t ad_data[] = {
BT_KEY_TEMP, 0x00, 0x00,
BT_KEY_CO2_LVL, 0x00, 0x00, 0x00, 0x00
};
// frame that will be broadcasted
static const struct bt_data ad[] = {
BT_DATA(BT_DATA_MANUFACTURER_DATA, ad_data, BT_AD_TOTAL_SIZE),
};
@@ -46,7 +53,6 @@ enum error_code ble_advertise(
){
enum error_code ret = write_failed;
int shift = 0;
// set values here
ad_data[BT_AD_DATA_INDEX_WINDOW] = (uint8_t)(window_value == open ? 0x1 : 0x0);
ad_data[BT_AD_DATA_INDEX_HUMIDITY] = (uint8_t)(hygro_value & 0xff);
for(int i=0;i<BT_VALUE_SIZE_TEMP;i++){

View File

@@ -8,7 +8,23 @@
#include "error_code.h"
#include "window_status.h"
/**
* @brief Initialise the ble module
* @return init_failed upon any error occuring during init
* @return success otherwise
*/
enum error_code ble_init();
/**
* @brief Sets the given values as data and broadcast the BLE frame
* @param window_value : window opening status in {open/closed}
* @param hygro_value : humidity percentage [%]
* @param thermo_value : temperature in [d°C]
* @param co2_lvl_value : co2 level in [ppm]
* @return write_failed if any problem occur during the BLE broadcast
* @return success otherwise
* @note The broadcast is stopped at the return time
*/
enum error_code ble_advertise(
enum window_status window_value,
int hygro_value,

View File

@@ -22,9 +22,10 @@ enum error_code co2_lvl_get_value(int* holder){
temp.val2 = temp_value%10;
// humidity conversion is straight away
humidity.val1 = humidity_value;
if(
// co2 measurement requires temperature and humidity
(0 == ccs811_envdata_update(dev, &temp, &humidity)) &&
// fetch is required to update sensor read data
(0 == sensor_sample_fetch(dev)) &&
(0 == sensor_channel_get(dev, SENSOR_CHAN_CO2, &co2))
){

View File

@@ -10,7 +10,21 @@
extern const int CO2_LEVEL_EMPTY_ROOM; // [ppm]
/**
* @brief init the co2 level measurement module
* @return init_failed upon any error during init
* @return success otherwise
*/
enum error_code co2_lvl_init();
/**
* @brief Retrieve the CO2 level measurement and stores it in the given parameter
* @param[out] holder : pointer where the measurement will be stored
* @return read_failed upon any error occuring during measurement
* @return success otherwise
* @note the content of holder should not be trusted upon returning something else than success
* @note for the first measurements after a reboot, holder will contain the value 0xffffffff
*/
enum error_code co2_lvl_get_value(int* holder);
#endif //CO2_LEVEL_H

View File

@@ -3,12 +3,13 @@
#include <limits.h>
// enum of all error code that may occur during the plein_de_eeeeeeeeeeeeeee application runtime
enum error_code{
success = 0,
init_failed,
read_failed,
write_failed,
error_unknown,
success = 0, // any kind of success
init_failed, // error related to initialization
read_failed, // error related to reading action
write_failed, // error related to writing action
error_unknown, // any error not linked to other code
error_code_last, // iteration purpose
};

View File

@@ -13,7 +13,9 @@ enum error_code hygro_init(){
enum error_code hygro_get_value(int* holder){
enum error_code ret = read_failed;
struct sensor_value humidity;
if( (sensor_sample_fetch(dev) >= 0) &&
if(
// fetch is required to update sensor read data
(sensor_sample_fetch(dev) >= 0) &&
(sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &humidity) >= 0)
){
*holder = humidity.val1; //taking only the integer part

View File

@@ -7,7 +7,20 @@
#include "error_code.h"
/**
* @brief init the hygrometer module
* @return init_failed upon any error during init
* @return success otherwise
*/
enum error_code hygro_init();
/**
* @brief Retrieve the humidity level and stores it in the given parameter
* @param[out] holder : pointer where the measurement will be stored
* @return read_failed upon any error occuring during measurement
* @return success otherwise
* @note the content of holder should not be trusted upon returning something else than success
*/
enum error_code hygro_get_value(int* holder);
#endif //HYGROMETER_H

View File

@@ -12,6 +12,7 @@
int main(void){
supervisor_init();
// todo : sohuld init fail, activate led ?
supervisor_run();
// should never come here
return 0;

View File

@@ -6,18 +6,14 @@ const int SLEEP_MAX_DURATION = 30; // [min]
enum error_code supervisor_init(){
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{}
if(
success == ble_init() &&
success == co2_lvl_init() &&
success == hygro_init() &&
success == thermo_init() &&
success == window_init()
){
ret = success;
}else{}
return ret;
}
@@ -34,14 +30,13 @@ enum error_code supervisor_run(){
hygro_status = hygro_get_value(&hygro_value);
thermo_status = thermo_get_value(&thermo_value);
window_status = window_get_value(&window_value);
// maybe change arguments order
// todo : manage special values
// todo : manage non-success error codes
ble_advertise(window_value, hygro_value, thermo_value, co2_lvl_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
// 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;

View File

@@ -15,7 +15,18 @@ extern const int SLEEP_GRANULARITY; // [min]
extern const int SLEEP_MIN_DURATION; // [min]
extern const int SLEEP_MAX_DURATION; // [min]
/**
* @brief init the supervisor, and thus the complete plein_de_ee application
* @return init_failed upon any error occurring during the initialisation of any module
* @return success otherwise
*/
enum error_code supervisor_init();
/**
* @brief exexcute the plein_de_eeeeeee application
* @return error_unknown
* @note should never return
*/
enum error_code supervisor_run();
#endif //SUPERVISOR_H

View File

@@ -7,7 +7,20 @@
#include "error_code.h"
/**
* @brief init the thermometer module
* @return init_failed upon any error during init
* @return success otherwise
*/
enum error_code thermo_init();
/**
* @brief Retrieve the temperature and stores it in the given parameter
* @param[out] holder : pointer where the measurement will be stored
* @return read_failed upon any error occuring during measurement
* @return success otherwise
* @note the content of holder should not be trusted upon returning something else than success
*/
enum error_code thermo_get_value(int* holder);
#endif //THERMOMETER_H

View File

@@ -6,13 +6,26 @@
#include "error_code.h"
enum window_status{
closed = 0,
open,
unknown,
closed = 0, // window is closed or sensor is not connected
open, // window is opened
unknown, // window opening status could not be read
windows_status_last, // iteration purpose
};
/**
* @brief init the window opening status module
* @return init_failed upon any error during init
* @return success otherwise
*/
enum error_code window_init();
/**
* @brief Retrieve the window opening status and stores it in the given parameter
* @param[out] holder : pointer where the measurement will be stored
* @return read_failed upon any error occuring during measurement
* @return success otherwise
* @note the content of holder should not be trusted upon returning something else than success
*/
enum error_code window_get_value(enum window_status* holder);
#endif //WINDOW_STATUS_