Hello,
I'm working on trying to understand how to read/write to the EEPROM and use the memcpy function but am having trouble. I've looked up a lot of examples and tried to read and understand what I'm doing. I can get the code below to compile but it gives a warming that says I have something wrong. I can't seem to get the memcpy to work either which is also in the warning so I assume it's all related. I'd appreciate any help or pointers to get me in the right direction. I ultimately want to incorporate this code into a bigger project I'm making for a temperature controller set. I have the menu code working and this test code is so I can get this memory functions working.
Thanks!
Warning:
In file included from src/main.cpp:2:
In member function 'T& EEPROMClass::get(int, T&) [with T = tempCtrlSets]',
inlined from 'void memSetsLoad()' at src/main.cpp:90:30:
C:/Users/dclap/.platformio/packages/framework-arduinoespressif32/libraries/EEPROM/src/EEPROM.h:57:13: warning: 'void* memcpy(void*, const void*, size_t)' writing 24 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
memcpy((uint8_t*) &t, _data + address, sizeof(T));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
In member function 'const T& EEPROMClass::put(int, const T&) [with T = tempCtrlSets]',
inlined from 'void memSetsSave()' at src/main.cpp:97:30:
C:/Users/dclap/.platformio/packages/framework-arduinoespressif32/libraries/EEPROM/src/EEPROM.h:66:13: warning: 'void* memcpy(void*, const void*, size_t)' reading 24 bytes from a region of size 0 [-Wstringop-overflow=]
memcpy(_data + address, (const uint8_t*) &t, sizeof(T));
Code:
#include <Arduino.h>
#include <LiquidCrystal_I2C.h> // Library for LCD
#include <EEPROM.h>
// ******************************************************************************************************************************
// Defines
// ******************************************************************************************************************************
#define SET_CHECK_VAL 123456789
// ******************************************************************************************************************************
// Project structures
// ******************************************************************************************************************************
struct tempCtrlSets {
float tempF_SP; // Temperature setpoint (F)
float heatHyst; // Hysteresis for heating temperature control (F)
float coolHyst; // Hysteresis for cooling temperature control (F)
boolean heatEnbl; // Enable heating control
boolean coolEnbl; // Enable cooling control
uint8_t heatDelay; // Heating control cycle delay (min)
uint8_t coolDelay; // Cooling control cycle delay (min)
uint8_t switchDelay; // Heating/Cooling switchover delay (min)
uint32_t settingCheckValue; // Setting validation value
};
// ******************************************************************************************************************************
// Global variables
// ******************************************************************************************************************************
tempCtrlSets controlSets[3]; // Controller settings
tempCtrlSets defaultSets[3]; // Default settings
// ******************************************************************************************************************************
// Function forward declarations
// ******************************************************************************************************************************
void memSetsDefaults(); // Set default settings values
void memSetsLoad(); // Load settings values
void memSetsSave(); // Save settings values
// ******************************************************************************************************************************
// Setup loop
// ******************************************************************************************************************************
void setup() {
// Initialize serial port
Serial.begin(115200);
// Default settings
defaultSets[0] = {38.0, 1.0, 1.0, false, true, 1, 10, 1, SET_CHECK_VAL};
defaultSets[1] = {68.0, 1.0, 1.0, true, true, 1, 10, 1, SET_CHECK_VAL};
defaultSets[2] = {68.0, 1.0, 1.0, true, true, 1, 10, 1, SET_CHECK_VAL};
// Load settings from memory
memSetsLoad();
}
// ******************************************************************************************************************************
// Main loop
// ******************************************************************************************************************************
void loop() {
memcpy(&defaultSets[3], &controlSets[3], sizeof(controlSets[3]));
Serial.println(defaultSets[0].tempF_SP);
Serial.println(defaultSets[1].tempF_SP);
Serial.println(defaultSets[2].tempF_SP);
Serial.println(controlSets[0].tempF_SP);
Serial.println(controlSets[1].tempF_SP);
Serial.println(controlSets[2].tempF_SP);
delay(1000);
memSetsSave();
}
// Set default settings values
void memSetsDefaults() {
controlSets[3] = defaultSets[3];
}
// Load settings values
void memSetsLoad() {
EEPROM.get(0,controlSets[3]);
if (controlSets[0].settingCheckValue != SET_CHECK_VAL) {memSetsDefaults();}
}
// Save settings values
void memSetsSave() {
EEPROM.put(0,controlSets[3]);
}