From 9268d1ab118323d7ad11e992ea41386810dd8c2b Mon Sep 17 00:00:00 2001 From: aurel Date: Thu, 27 Nov 2025 13:53:54 +0100 Subject: [PATCH] Implement EEPROM measure retrieval and add debug printing --- lib/debug_config.h | 29 ++++++ lib/sensor/photoresistance_ohm_retrieval.cpp | 7 +- lib/sensor/sensormanager.cpp | 5 +- lib/storage/storage_interface.cpp | 98 ++++++++++---------- lib/storage/storage_interface.h | 10 +- platformio.ini | 6 +- src/main.cpp | 32 ++----- 7 files changed, 101 insertions(+), 86 deletions(-) create mode 100644 lib/debug_config.h diff --git a/lib/debug_config.h b/lib/debug_config.h new file mode 100644 index 0000000..f22d21e --- /dev/null +++ b/lib/debug_config.h @@ -0,0 +1,29 @@ +#ifndef DEBUG_CONFIG_H +#define DEBUG_CONFIG_H + +#define DEBUG + +#ifdef DEBUG + #define DLOG_INIT() Serial.begin(9600) + #define DLOG(msg) Serial.print(msg); + #define DLOGLN(msg) Serial.println(msg); + #define WARN_IF(cond, msg) if(cond) Serial.print(msg); + + #define ASSERT(condition, msg) \ + if (!(condition)) { \ + Serial.print("!!! ASSERTION FAILED: "); \ + Serial.print(msg); \ + Serial.print(" [F:"); Serial.print(__FILE__); \ + Serial.print(" L:"); Serial.print(__LINE__); \ + Serial.println("]"); \ + while (true); \ + } +#else + #define DLOG_INIT() + #define DLOG(msg) + #define DLOGLN(msg) + #define WARN_IF(cond, msg) + #define ASSERT(condition, msg) +#endif + +#endif // DEBUG_CONFIG_H \ No newline at end of file diff --git a/lib/sensor/photoresistance_ohm_retrieval.cpp b/lib/sensor/photoresistance_ohm_retrieval.cpp index f24c50b..db214c6 100755 --- a/lib/sensor/photoresistance_ohm_retrieval.cpp +++ b/lib/sensor/photoresistance_ohm_retrieval.cpp @@ -1,15 +1,16 @@ #include #include "photoresistance_ohm_retrieval.h" +#include "debug_config.h" SensorOhm::SensorOhm(uint8_t pin_analog_read, float vcc, int32_t rFixed, float current) : pin_analog_read(pin_analog_read), vcc(vcc), rFixed(rFixed), current(current) { if (vcc > mega2560_max_vcc){ - Serial.println("FATAL: VCC too high !"); - while (true); // bloque le programme + DLOGLN("FATAL: VCC too high !") + while (true); } if (current > mega2560_max_current){ - Serial.println("FATAL: Intensity too high !"); + DLOGLN("FATAL: Intensity too high !") while (true); } } diff --git a/lib/sensor/sensormanager.cpp b/lib/sensor/sensormanager.cpp index 08c4ff2..6ae359d 100644 --- a/lib/sensor/sensormanager.cpp +++ b/lib/sensor/sensormanager.cpp @@ -1,5 +1,6 @@ #include #include "sensormanager.h" +#include "debug_config.h" SensorManager::SensorManager(){} @@ -41,7 +42,7 @@ void SensorManager::setup(uint8_t nb_sensor, uint8_t *analog_in){ */ void SensorManager::get_resistances(int32_t* res_tab, uint8_t tab_lenght){ if (tab_lenght < nbPhotoSensor){ - Serial.print("Error bad tab size"); + DLOGLN("Error bad tab size") return; } @@ -70,7 +71,7 @@ void SensorManager::updateMinMax(uint8_t index, int32_t current_res) { */ void SensorManager::get_min_max(int32_t* min_res_tab, int32_t* max_res_tab, uint8_t tab_lenght){ if (tab_lenght < nbPhotoSensor){ - Serial.print("Error bad tab size"); + DLOGLN("Error bad tab size") return; } diff --git a/lib/storage/storage_interface.cpp b/lib/storage/storage_interface.cpp index 762f564..87fdbf1 100644 --- a/lib/storage/storage_interface.cpp +++ b/lib/storage/storage_interface.cpp @@ -1,6 +1,7 @@ #include "storage_interface.h" #include #include +#include "debug_config.h" // Manual implements of EEPROM.put() for few uint type @@ -65,41 +66,6 @@ void Storage_interface::clear_eeprom_at(uint16_t idx){ } } -// legacy function (don't finished and never tested) -/* -uint16_t get_nb_package_legacy(){ - uint8_t flags, nb_photo_sensor, nb_temp_sensor, temp_size_t, photo_size_t, schedule, tmp, timestamp_size_t; - uint16_t nb_package = 0, nb_measure, start_package = 0, timestamp_sum_size, next_position; - bool timestamp; - flags = EEPROM.read(start_package); - - while ((flags & 0b1) != 0){ - nb_package++; - nb_photo_sensor = EEPROM.read(start_package + OFFSET_NB_PHOTO_SENSOR); - photo_size_t = EEPROM.read(start_package + OFFSET_PHOTO_MEASURES_SIZE); - - nb_temp_sensor = EEPROM.read(start_package + OFFSET_NB_TEMP_SENSOR); - temp_size_t = EEPROM.read(start_package + OFFSET_TEMP_MEASURES_SIZE); - - tmp = EEPROM.read(start_package + OFFSET_NB_TEMP_SENSOR); // read high byte - nb_measure = EEPROM.read(start_package + OFFSET_NB_TEMP_SENSOR + 1) | (tmp << 8); // read low byte - - timestamp = false; - timestamp_size_t = EEPROM.read(start_package + OFFSET_timestamp_SIZE); - if ((flags & 0b001) != 0 ){ - timestamp = true; - schedule = EEPROM.read(start_package + OFFSET_MEASURES_SCH); - timestamp_sum_size = (nb_measure/schedule) * timestamp_size_t; - }else if ((flags & 0b01) != 0){ - timestamp = true; - timestamp_sum_size = nb_measure * timestamp_size_t; - }else{ - timestamp_sum_size = 0; - } - } - return nb_package; -}*/ - /** * @brief get_last_header_nbpackage * * @@ -136,8 +102,8 @@ void Storage_interface::get_store_struct(uint16_t offset, bool* timestamp, bool* // Checking there if a struct is possibly at the index if ((flags & 0b1) == 0){ - Serial.println("Missing struct index or bad index"); - return; + DLOGLN("Missing struct index or bad index") + while(true); } // timestamps reads @@ -149,8 +115,7 @@ void Storage_interface::get_store_struct(uint16_t offset, bool* timestamp, bool* *timestamp_schedule = 0; if (flags & 0b01){ *timestamp = true; - if (EEPROM.read(offset + OFFSET_MEASURES_SCH) != 0) - Serial.println("Incoherent timestamp parameter in the header."); + WARN_IF(EEPROM.read(offset + OFFSET_MEASURES_SCH) != 0, "Incoherent timestamp parameter in the header.") } // photo res reads @@ -184,8 +149,7 @@ void Storage_interface::set_store_struct(uint16_t offset, bool timestamp, bool i if(timestamp_schedule != 0){ flags = flags | 0b00100000; EEPROM.write(offset + OFFSET_MEASURES_SCH, timestamp_schedule); - if(!timestamp) - Serial.println("Bad timestamp parameter for header writer."); + WARN_IF(!timestamp, "Bad timestamp parameter for header writer.") }else{ EEPROM.write(offset + OFFSET_MEASURES_SCH, 0); if (timestamp) @@ -198,8 +162,7 @@ void Storage_interface::set_store_struct(uint16_t offset, bool timestamp, bool i EEPROM.write(offset + OFFSET_NB_PHOTO_SENSOR, nb_photo_sensor); }else{ EEPROM.write(offset + OFFSET_NB_PHOTO_SENSOR, 0); - if (nb_photo_sensor != 0) - Serial.println("Bad photo sensor parameter for header writer."); + WARN_IF(nb_photo_sensor != 0, "Bad photo sensor parameter for header writer.") } if (temp_sensor){ @@ -207,8 +170,7 @@ void Storage_interface::set_store_struct(uint16_t offset, bool timestamp, bool i EEPROM.write(offset + OFFSET_NB_TEMP_SENSOR, nb_temp_sensor); }else{ EEPROM.write(offset + OFFSET_NB_TEMP_SENSOR, 0); - if (nb_temp_sensor != 0) - Serial.println("Bad temperature sensor parameter for header writer."); + WARN_IF(nb_temp_sensor != 0, "Bad temperature sensor parameter for header writer.") } // TODO Analyse if is_final_set just be remove from parameter @@ -244,7 +206,7 @@ void Storage_interface::add_last_package(bool timestamp, bool is_final_set, bool } // Dont check if the stored measure structure match with the header -void Storage_interface::add_measure(uint8_t* photo_values, uint8_t* temp_values, uint32_t timestamp, uint8_t nb_photo, uint8_t nb_temp){ +void Storage_interface::add_measure(uint8_t* array_photo_val, uint8_t* array_temp_val, uint32_t timestamp, uint8_t nb_photo, uint8_t nb_temp){ uint16_t p_last_header, free_space, idx; get_last_header_nbpackage(&p_last_header); @@ -253,11 +215,45 @@ void Storage_interface::add_measure(uint8_t* photo_values, uint8_t* temp_values, EEPROM.write(p_last_header + OFFSET_NB_MEASURES, EEPROM.read(p_last_header + OFFSET_NB_MEASURES) + 1); EEPROM.put(free_space, timestamp); - idx = p_last_header + int32_size; - for(int i = 0; i < nb_photo; i++, idx += int8_size){ - EEPROM.put(idx, photo_values[i]); + idx = p_last_header + sizeof(uint32_t); + for(int i = 0; i < nb_photo; i++, idx += sizeof(uint8_t)){ + EEPROM.put(idx, array_photo_val[i]); } - for(int i = 0; i < nb_temp; i++, idx += int8_size){ - EEPROM.put(idx, temp_values[i]); + for(int i = 0; i < nb_temp; i++, idx += sizeof(uint8_t)){ + EEPROM.put(idx, array_temp_val[i]); + } +} + +void Storage_interface::get_measure(uint8_t* array_photo_val, uint8_t* array_temp_val, uint32_t* timestamp, uint8_t* nb_photo, uint8_t* nb_temp, uint16_t idx_measure){ + uint16_t p_last_header, idx; + uint8_t flags; + + get_last_header_nbpackage(&p_last_header); + idx = p_last_header + OFFSET_START_DATA_MEASURES; + + *nb_photo = EEPROM.read(p_last_header + OFFSET_NB_PHOTO_SENSOR); + *nb_temp = EEPROM.read(p_last_header + OFFSET_NB_TEMP_SENSOR); + + flags = EEPROM.read(p_last_header); + if((flags & 0b010) != 0){ + EEPROM.get(p_last_header + OFFSET_START_DATA_MEASURES, timestamp); + idx += sizeof(uint32_t) * idx_measure; + }else if ((flags & 0b001) != 0){ + uint8_t schedule; + schedule = EEPROM.read(p_last_header + OFFSET_MEASURES_SCH); + WARN_IF(schedule == 0, "Struct error for timestamp scheduling.") + + if(idx_measure % schedule != 0) + idx += sizeof(uint32_t); + idx += sizeof(uint32_t) * (idx_measure / schedule); + } + + idx += ((*nb_photo) + (*nb_temp)) * idx_measure * sizeof(uint8_t); + + for(int i = 0; i < *nb_photo; i++, idx += sizeof(uint8_t)){ + array_photo_val[i] = EEPROM.read(idx); + } + for(int i = 0; i < *nb_temp; i++, idx += sizeof(uint8_t)){ + array_temp_val[i] = EEPROM.read(idx); } } \ No newline at end of file diff --git a/lib/storage/storage_interface.h b/lib/storage/storage_interface.h index 0b2813d..642e3b7 100644 --- a/lib/storage/storage_interface.h +++ b/lib/storage/storage_interface.h @@ -10,7 +10,6 @@ class Storage_interface private: static constexpr uint8_t OFFSET_MEASURES_SCH = 1, OFFSET_NB_PHOTO_SENSOR = 2, OFFSET_NB_TEMP_SENSOR = 3, OFFSET_PHOTO_MEASURES_SIZE = 4, OFFSET_TEMP_MEASURES_SIZE = 5, OFFSET_NB_MEASURES = 6, OFFSET_NEXT_PACKAGE = 8, OFFSET_START_DATA_MEASURES = 10; // unit in byte - static constexpr uint8_t int32_size = sizeof(int32_t), int16_size = sizeof(int16_t), int8_size = sizeof(int8_t); static constexpr uint16_t MEGA_2560_EEPROM_SIZE = 4096; void clear_eeprom(); @@ -23,11 +22,14 @@ private: public: + Storage_interface(); + ~Storage_interface(); + uint16_t get_nb_package(); void add_last_package(bool timestamp, bool is_final_set, bool photo_sensor, bool temp_sensor, uint8_t timestamp_schedule, uint8_t nb_photo_sensor, uint8_t nb_temp_sensor, uint8_t photo_size, uint8_t temp_size, uint16_t nb_measures); void add_measure(uint8_t* photo_values, uint8_t* temp_values, uint32_t timestamp, uint8_t nb_photo, uint8_t nb_temp); - Storage_interface(); - ~Storage_interface(); + void get_measure(uint8_t* photo_values, uint8_t* temp_values, uint32_t* timestamp, uint8_t* nb_photo, uint8_t* nb_temp, uint16_t idx_measure); + // Dont check if the stored measure structure match with the header template< typename T, typename TT> @@ -40,7 +42,7 @@ public: EEPROM.write(p_last_header + OFFSET_NB_MEASURES, EEPROM.read(p_last_header + OFFSET_NB_MEASURES) + 1); EEPROM.put(free_space, timestamp); - idx = p_last_header + int32_size; + idx = p_last_header + sizeof(uint32_t); for(int i = 0; i < nb_photo; i++, idx += sizeof(T)){ EEPROM.put(idx, photo_values[i]); } diff --git a/platformio.ini b/platformio.ini index c2f1ac1..9a78dce 100755 --- a/platformio.ini +++ b/platformio.ini @@ -13,10 +13,12 @@ platform = atmelavr board = megaADK framework = arduino test_framework = unity -build_flags = -std=gnu++17 +build_flags = + -std=gnu++17 + -I lib/ lib_deps = northernwidget/DS3231@^1.1.2 arduino-libraries/Ethernet robtillaart/CRC -lib_ldf_mode = deep+ \ No newline at end of file +lib_ldf_mode = deep+ \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 550ca56..e264ad1 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,31 +22,9 @@ Traitement tr; RTClib myRTC; DS3231 Clock; -// void setup() { -// Wire.begin(); -// Serial.begin(9600); -// Serial.println("\nScanning I2C bus..."); - -// for (byte addr = 1; addr < 127; addr++) { -// Wire.beginTransmission(addr); -// if (Wire.endTransmission() == 0) { -// Serial.print("I2C device found at address 0x"); -// Serial.println(addr, HEX); -// } -// } -// Serial.println("Scan complete."); -// constexpr time_t tstmp {1660644000UL}; - -// Clock.setEpoch(tstmp, false); -// // set to 24h -// Clock.setClockMode(false); -// } - void setup() { Wire.begin(); Serial.begin(9600); - //pinMode(ledPin, OUTPUT); - s_manager.setup(nbPhotoSensor, analogPin); } @@ -61,11 +39,14 @@ void loop() { epoch += 3600; } Clock.setEpoch(epoch); +#ifdef DEBUG Serial.print("RTC mis à jour avec epoch: "); Serial.println(epoch); +#endif } while (Serial.available()) Serial.read(); // clean buffer +#ifdef DEBUG // Just for verification of DS3231 Data // check now the data from ESP8266 and DS3231 // get year @@ -89,20 +70,23 @@ void loop() { Serial.print(" - weekday "); Serial.print(Clock.getDoW(), DEC); Serial.println(); +#endif } +#ifdef DEBUG printDate(Clock, decTemp); - s_manager.print_current_res(); s_manager.print_min_max_res(); +#endif int32_t res_array[nbPhotoSensor]/*, min_array[nbPhotoSensor], max_array[nbPhotoSensor]*/; int16_t result[nbPhotoSensor]; s_manager.get_resistances(res_array, nbPhotoSensor); tr.map_r(min_res, max_res, res_array, result, nbPhotoSensor); +#ifdef DEBUG print_named_tab(result, nbPhotoSensor, "int32_t normalised",(uint8_t) 18); - +#endif delay(2000); }