arduino-photometrics/lib/storage/storage_interface.h

55 lines
2.6 KiB
C++

#ifndef STORAGE_INTERFACE_H
#define STORAGE_INTERFACE_H
#include <Arduino.h>
#include <EEPROM.h>
// based on https://docs.arduino.cc/learn/programming/eeprom-guide/
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 uint16_t MEGA_2560_EEPROM_SIZE = 4096;
void clear_eeprom_at(uint16_t idx);
uint16_t get_last_header_nbpackage(uint16_t* last_header_idx);
public:
Storage_interface();
~Storage_interface();
uint16_t get_nb_package();
void clear_eeprom();
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);
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);
void get_struct(uint16_t offset, 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* p_next_package, uint16_t* nb_measures);
void set_struct(uint16_t offset, 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);
// Dont check if the stored measure structure match with the header
template< typename T, typename TT>
void add_measure(T* photo_values, TT* temp_values, 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);
free_space = EEPROM.read(p_last_header + OFFSET_NEXT_PACKAGE);
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 + sizeof(uint32_t);
for(int i = 0; i < nb_photo; i++, idx += sizeof(T)){
EEPROM.put(idx, photo_values[i]);
}
for(int i = 0; i < nb_temp; i++, idx += sizeof(TT)){
EEPROM.put(idx, temp_values[i]);
}
}
};
#endif