I'm having a hard time with this 2D array in CPP. I tried following this link, but it did not solve my problem.
In the saveApToEeprom()
function I save two strings.
This works fine. Then i want to read from the EEPROM using getConfigForEeprom()
. This is where things go wrong.
The function getConfigForEeprom()
prints 2 times 32 bytes, that matches with what I see in saveApToEeprom()
. But when the program goes into dumbDataEeprom()
all the data in valueEeprom[1]
seem to be gone, except for valueEeprom[0]
is there.
Does anyone know how to solve this issue?
See below for my code
main.cpp
#include <eeprom_handler.h>
void setup(){
initEeprom();
String ssid = "test";
String psw = "tset";
saveApToEeprom(ssid, psw);
uint8_t** valuesOutEeprom = getConfigForEeprom();
dumbDataEeprom(valuesOutEeprom);
}
void loop() {
}
eeprom_handler.h
#ifndef eeprom_handler_H
#define eeprom_handler_H
#include <Arduino.h>
#include <String.h>
#include <EEPROM.h>
/* PARAMETERS EEPROM*/
#define PARAMETER_SIZE_IN_BYTES 32
#define PARAMETERS_EEPROM 2
#define BYTES_NEEDED_FROM_EEPROM (PARAMETER_SIZE_IN_BYTES*PARAMETERS_EEPROM)
/* FUNCTIONS EEPROM */
void initEeprom(uint8_t nmrOfBytes = BYTES_NEEDED_FROM_EEPROM);
uint8_t saveApToEeprom(String apSsid, String ApPsw);
uint8_t** getConfigForEeprom(uint8_t nmrOfBytes = BYTES_NEEDED_FROM_EEPROM);
void dumbDataEeprom(uint8_t** valuesOutEeprom);
void clearEeprom(uint8_t nmrOfBytes = BYTES_NEEDED_FROM_EEPROM);
#endif // eeprom_handler_H
eeprom_handler.cpp
/**************************************************************************/
/*!
@brief Read data from the EEPROM
@param nmrOfBytes uint8_t: total number of bytes of the EEPROM to read
@return valueEeprom uint8_t[][]:
[0-32][]) 32 bytes, should contain SSID
[][0-32]) 32 bytes, should contain SSID
*/
/**************************************************************************/
// TODO reconstruct uint8_t to chars
uint8_t** getConfigForEeprom(uint8_t nmrOfBytes){
if (Serial){
Serial.println("Class eeprom_handler, function: getConfigForEeprom");
}
// init the 2D array
uint8_t** valueEeprom = new uint8_t* [PARAMETERS_EEPROM];
for (uint8_t m = 0; m < PARAMETERS_EEPROM; m++){
valueEeprom[m] = new uint8_t[BYTES_NEEDED_FROM_EEPROM];
}
for(uint8_t i = 0; i < PARAMETER_SIZE_IN_BYTES; i ++ ) {
valueEeprom[0][i] = EEPROM.read(i);
Serial.println(valueEeprom[0][i]);
}
for(uint8_t j = PARAMETER_SIZE_IN_BYTES; j < BYTES_NEEDED_FROM_EEPROM; j ++ ) {
valueEeprom[1][j] = EEPROM.read(j);
Serial.println(valueEeprom[1][j]);
}
return valueEeprom;
}
/**************************************************************************/
/*!
@brief Serial print the data read from the EEPROM
@param valuesOutEeprom uint8_t**: pointer to a uint8_t 2D array
*/
/**************************************************************************/
void dumbDataEeprom(uint8_t** valuesOutEeprom){
if (Serial){
Serial.println("Class eeprom_handler, function: dumbDataEeprom");
for (uint8_t i = 0; i < PARAMETERS_EEPROM; i++){
for (uint8_t j = 0; j < PARAMETER_SIZE_IN_BYTES; j++){
Serial.print(valuesOutEeprom[i][j]);
}
Serial.println();
}
}
}
output (Serial)
Class eeprom_handler, function: initEeprom
Class eeprom_handler, function: saveApToEeprom
Class eeprom_handler, function: clearEeprom
0
116
1
101
2
115
3
116
32
116
33
115
34
101
35
116
Class eeprom_handler, function: getConfigForEeprom
116
101
115
116
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
116
115
101
116
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Class eeprom_handler, function: dumbDataEeprom
1161011151160000000000000000000000000000
00000000000000000000000000000000