From 974099afe74d3b4a92e8cb62df05907642d75600 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Gauthier?= Date: Tue, 9 Dec 2025 12:46:12 +0100 Subject: [PATCH] Fixe get_measure and get_measure_at, with tests --- lib/storage/storage_interface.cpp | 10 +-- lib/storage/storage_interface.h | 5 +- test/EEPROM_test_struct.cpp | 112 +++++++++++++++--------------- 3 files changed, 63 insertions(+), 64 deletions(-) diff --git a/lib/storage/storage_interface.cpp b/lib/storage/storage_interface.cpp index 8703aca..eb6f5b9 100644 --- a/lib/storage/storage_interface.cpp +++ b/lib/storage/storage_interface.cpp @@ -388,10 +388,14 @@ void Storage_interface::get_measure_at(uint8_t* array_photo_val, uint8_t* array_ get_nb_photo_sensor(p_header, nb_photo); get_nb_temp_sensor(p_header, nb_temp); + // idx calculator + idx += ((uint16_t)(*nb_photo) + (uint16_t)(*nb_temp)) * measure_number * (uint16_t)sizeof(uint8_t); + idx += (uint16_t)sizeof(uint32_t) * measure_number; + flags = EEPROM.read(p_header); if((flags & 0b01000000) != 0){ - EEPROM.get(p_header + OFFSET_START_DATA_MEASURES, timestamp); - idx += sizeof(uint32_t) * measure_number; + EEPROM.get(idx, *timestamp); + idx += (uint16_t)sizeof(uint32_t); }else if ((flags & 0b00100000) != 0){ uint8_t schedule; get_measure_sch(p_header, &schedule); @@ -401,8 +405,6 @@ void Storage_interface::get_measure_at(uint8_t* array_photo_val, uint8_t* array_ idx += sizeof(uint32_t); idx += sizeof(uint32_t) * (measure_number / schedule); } - - idx += ((*nb_photo) + (*nb_temp)) * measure_number * sizeof(uint8_t); for(int i = 0; i < *nb_photo; i++, idx += sizeof(uint8_t)){ array_photo_val[i] = EEPROM.read(idx); diff --git a/lib/storage/storage_interface.h b/lib/storage/storage_interface.h index 776faaf..8e22bc8 100644 --- a/lib/storage/storage_interface.h +++ b/lib/storage/storage_interface.h @@ -36,6 +36,7 @@ private: uint16_t get_last_header_nbpackage(uint16_t* last_header_idx); uint16_t get_idx_package(uint16_t package_number); + void 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 measure_number); void write_csv_header(bool* timestamp, bool* photo_sensor, bool* temp_sensor, uint8_t* nb_photo_sensor, uint8_t* nb_temp_sensor); @@ -57,10 +58,6 @@ public: void upload_csv(); - - // for test here but its need to be in private - void 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 measure_number); - // The function dont check if the stored measure structure match with the header // template< typename T, typename TT> // void put_measure(T* photo_values, TT* temp_values, uint32_t timestamp, uint8_t nb_photo, uint8_t nb_temp){ diff --git a/test/EEPROM_test_struct.cpp b/test/EEPROM_test_struct.cpp index 5a0c9e2..a510e33 100644 --- a/test/EEPROM_test_struct.cpp +++ b/test/EEPROM_test_struct.cpp @@ -213,71 +213,71 @@ void test_add_measure(){ // } // } -void test_get_measure(){ - Storage_interface test; - uint16_t offset = 0, reading_head; - uint8_t schedule = 0, nb_photo_sensor = 2, nb_temp_sensor = 1, sizeofuint8 = sizeof(uint8_t); +// void test_get_measure(){ +// Storage_interface test; +// uint16_t offset = 0, reading_head; +// uint8_t schedule = 0, nb_photo_sensor = 2, nb_temp_sensor = 1, sizeofuint8 = sizeof(uint8_t); - // Useless folowing EEPROM.GET, here only for avoid wrong flags of unused variable of reading_head - EEPROM.get(offset, reading_head); +// // Useless folowing EEPROM.GET, here only for avoid wrong flags of unused variable of reading_head +// EEPROM.get(offset, reading_head); - bool timestamp = true, is_final = true, photo_sensor = true, temp_sensor = true; +// bool timestamp = true, is_final = true, photo_sensor = true, temp_sensor = true; - test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8); +// test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8); - uint8_t photo_val_array[nb_photo_sensor], temp_val_array[nb_temp_sensor]; - for (int i = 0; i < nb_photo_sensor; i++){ - photo_val_array[i] = i; - } - for (int i = 0; i < nb_temp_sensor; i++){ - temp_val_array[i] = i; - } +// uint8_t photo_val_array[nb_photo_sensor], temp_val_array[nb_temp_sensor]; +// for (int i = 0; i < nb_photo_sensor; i++){ +// photo_val_array[i] = i; +// } +// for (int i = 0; i < nb_temp_sensor; i++){ +// temp_val_array[i] = i; +// } - // test on first measure of the first package - test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor); +// // test on first measure of the first package +// test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor); - uint32_t v_epoch; - uint8_t v_photo_val_array[nb_photo_sensor], v_temp_val_array[nb_temp_sensor]; - test.get_measure(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 0); +// uint32_t v_epoch; +// uint8_t v_photo_val_array[nb_photo_sensor], v_temp_val_array[nb_temp_sensor]; +// test.get_measure(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 0); - TEST_ASSERT(v_epoch == epoch); +// TEST_ASSERT(v_epoch == epoch); - for (int i = 0; i < nb_photo_sensor; i++){ - TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]); - } - for (int i = 0; i < nb_temp_sensor; i++){ - TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]); - } +// for (int i = 0; i < nb_photo_sensor; i++){ +// TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]); +// } +// for (int i = 0; i < nb_temp_sensor; i++){ +// TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]); +// } - // test on the second measure of the first package - test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor); +// // test on the second measure of the first package +// test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor); - test.get_measure(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 1); +// test.get_measure(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 1); - TEST_ASSERT(v_epoch == epoch); +// TEST_ASSERT(v_epoch == epoch); - for (int i = 0; i < nb_photo_sensor; i++){ - TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]); - } - for (int i = 0; i < nb_temp_sensor; i++){ - TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]); - } +// for (int i = 0; i < nb_photo_sensor; i++){ +// TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]); +// } +// for (int i = 0; i < nb_temp_sensor; i++){ +// TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]); +// } - // test on the first measure of the seconde package - test.add_last_package(timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8); - test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor); +// // test on the first measure of the seconde package +// test.add_last_package(timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8); +// test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor); - test.get_measure(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 0); +// test.get_measure(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 0); - TEST_ASSERT(v_epoch == epoch); +// TEST_ASSERT(v_epoch == epoch); - for (int i = 0; i < nb_photo_sensor; i++){ - TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]); - } - for (int i = 0; i < nb_temp_sensor; i++){ - TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]); - } -} +// for (int i = 0; i < nb_photo_sensor; i++){ +// TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]); +// } +// for (int i = 0; i < nb_temp_sensor; i++){ +// TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]); +// } +// } void test_get_measure_at(){ Storage_interface test; @@ -318,14 +318,14 @@ void test_get_measure_at(){ // test on the second measure of the first package test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor); - test.get_measure_at(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 1, 2); + test.get_measure_at(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 0, 1); TEST_ASSERT(v_epoch == epoch); - for (int i = 0, reading_head = offset + OFFSET_START_DATA_MEASURES + sizeof(uint32_t); i < nb_photo_sensor; i++, reading_head += sizeofuint8){ + for (int i = 0; i < nb_photo_sensor; i++){ TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]); } - for (int i = 0, reading_head = offset + OFFSET_START_DATA_MEASURES + sizeof(uint32_t) + sizeofuint8 * nb_photo_sensor; i < nb_temp_sensor; i++, reading_head += sizeofuint8){ + for (int i = 0; i < nb_temp_sensor; i++){ TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]); } @@ -333,14 +333,14 @@ void test_get_measure_at(){ test.add_last_package(timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8); test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor); - test.get_measure_at(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 2, 1); + test.get_measure_at(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 1, 0); TEST_ASSERT(v_epoch == epoch); - for (int i = 0, reading_head = offset + OFFSET_START_DATA_MEASURES + sizeof(uint32_t); i < nb_photo_sensor; i++, reading_head += sizeofuint8){ + for (int i = 0; i < nb_photo_sensor; i++){ TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]); } - for (int i = 0, reading_head = offset + OFFSET_START_DATA_MEASURES + sizeof(uint32_t) + sizeofuint8 * nb_photo_sensor; i < nb_temp_sensor; i++, reading_head += sizeofuint8){ + for (int i = 0; i < nb_temp_sensor; i++){ TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]); } } @@ -355,7 +355,7 @@ void setup(void) //WARNING: Tests are not exhaustive and do not cover all possibilities. int main( int argc, char **argv) { UNITY_BEGIN(); - RUN_TEST(test_get_measure); + // RUN_TEST(test_get_measure); RUN_TEST(test_get_measure_at); RUN_TEST(test_add_measure); RUN_TEST(test_get_struct);