Ok I hope someone is still looking here.
I had a project written on a Nano using the EEPROMAnything.h library for help store multiple array's in the EEPROM. Ultimately I ran out of RAM space so I got a shinny new Due. After some hours I was able to get a working sketch on the Due of my project. Now that there is no EEPROM I needed a way to store some values between power cycles. I tried to use the DueFlashStorage but I'm having trouble. BTW this is the first 'real' coding I've done so my knowledge is limited. I'm kinda learning as I go here. This project is an LED controller that uses a Nextion Display as the control interface.
Here is the structure I'm working with.
struct LEDAttributes {
byte onHour;
byte onMinute;
int onTimeMins;
byte offHour;
byte offMinute;
int offTimeMins;
byte rampMinutes;
byte lightIntensity;
byte LEDState; //0-ON, 1-RAMPING UP, 2-RAMPING DOWN, 3-OFF, 4-Bypass
byte Lightning;
byte CurrentPWM;
byte Debug;
};
LEDAttributes LEDChannel[4] =
{
{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,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0}
};
This reads from an address derived by the structure size as far as I know and writes it to the appropriate structure.
for (int i = 0; i <= 3; i++)
{
EEPROM_readAnything((i * sizeof LEDChannel[i]), LEDChannel[i]);
}
This pulls the values from the Nextion display and writes them to the correct structure. Then an EEPROM_writeAnything command saves the data.
void ESPushCallback(void *ptr)
{
uint32_t hrON = 0;
n0.getValue(&hrON);
uint32_t hrOFF = 0;
n2.getValue(&hrOFF);
uint32_t minON = 0;
n1.getValue(&minON);
uint32_t minOFF = 0;
n3.getValue(&minOFF);
uint32_t LEDramp = 0;
n4.getValue(&LEDramp);
uint32_t LEDIntensity = 0;
h0.getValue(&LEDIntensity);
uint32_t Lightning = 0;
if(LEDChannel[LEDch - 1].LEDState == 4)
{
LEDChannel[LEDch - 1].onHour = hrON;
LEDChannel[LEDch - 1].onMinute = minON;
LEDChannel[LEDch - 1].onTimeMins = ((LEDChannel[LEDch - 1].onHour * 60) + LEDChannel[LEDch - 1].onMinute);
LEDChannel[LEDch - 1].offHour = hrOFF;
LEDChannel[LEDch - 1].offMinute = minOFF;
LEDChannel[LEDch - 1].offTimeMins = ((LEDChannel[LEDch - 1].offHour * 60) + LEDChannel[LEDch - 1].offMinute);
LEDChannel[LEDch - 1].rampMinutes = LEDramp;
LEDChannel[LEDch - 1].lightIntensity = LEDIntensity;
LEDChannel[LEDch - 1].Lightning = Lightning;
EEPROM_writeAnything(((LEDch -1) * sizeof LEDChannel[LEDch -1]), LEDChannel[LEDch -1]);
}
}
This allows me to read a single structure into memory for use in the code.
EEPROM_readAnything(((LEDch -1) * sizeof LEDChannel[LEDch -1]), LEDChannel[LEDch -1]);
This was a simple test to see how this thing works but I can't seem to make it work. I can get one structure to write but not two. I'm not sure how to write something that will function similar to the EEPROMeverything library. Also if I change the address in the "dueFlashStorage.write(4, b2, sizeof(LEDChannel[0]));" command from 4 to 5 I get an error on the serial terminal that says "Flash start address must be on four byte boundary". I have no idea what that means and my brains are fried because I'm writing this at about 6am after trying all night to make this work.
Any help is great appreciated! Thx.
#include <DueFlashStorage.h>
DueFlashStorage dueFlashStorage;
struct LEDAttributes {
byte onHour;
byte onMinute;
int onTimeMins;
byte offHour;
byte offMinute;
int offTimeMins;
byte rampMinutes;
byte lightIntensity;
byte LEDState; //0-ON, 1-RAMPING UP, 2-RAMPING DOWN, 3-OFF, 4-Bypass
byte Lightning;
byte CurrentPWM;
byte Debug;
};
LEDAttributes LEDChannel[4] =
{
{12,0,9999,0,0,0,0,0,0,0,0,0},
{23,23,23,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,0,0,0,0,0}
};
void setup()
{
Serial.begin(115200);
delay(500);
byte b2[sizeof(LEDChannel[0])]; // create byte array to store the struct
memcpy(b2, &LEDChannel[0], sizeof(LEDChannel[0])); // copy the struct to the byte array
dueFlashStorage.write(4, b2, sizeof(LEDChannel[0])); // write byte array to flash
}
void loop() {
// put your main code here, to run repeatedly:
byte* b = dueFlashStorage.readAddress(4); // byte array which is read from flash at adress 4
LEDAttributes configurationFromFlash; // create a temporary struct
memcpy(&configurationFromFlash, b, sizeof(LEDChannel[0])); // copy byte array to temporary struct
Serial.print(configurationFromFlash.onHour);
Serial.print(",");
Serial.print(configurationFromFlash.onMinute);
Serial.print(",");
Serial.print(configurationFromFlash.onTimeMins);
Serial.println();
delay(1000);
}