EEPROM file storage, and argparsing that actually works
This commit is contained in:
parent
3b2f0fa4cf
commit
829c08c031
5 changed files with 4385 additions and 492 deletions
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
EEPROMClass::EEPROMClass() {
|
||||
memset(someFakeEEPROM_memory, 0xff, sizeof(someFakeEEPROM_memory));
|
||||
storage = NULL;
|
||||
autoUpdate = false;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -22,15 +24,98 @@ void EEPROMClass::write( int idx, uint8_t val )
|
|||
{
|
||||
printf("Writing to EEPROM address %u = %u\n", idx, val);
|
||||
someFakeEEPROM_memory[idx] = val;
|
||||
|
||||
if(autoUpdate && storage)
|
||||
{
|
||||
fseek(storage, idx, SEEK_SET);
|
||||
fputc(val, storage);
|
||||
fflush(storage);
|
||||
}
|
||||
}
|
||||
|
||||
void EEPROMClass::update( int idx, uint8_t val )
|
||||
{
|
||||
write(idx, val);
|
||||
}
|
||||
|
||||
uint16_t EEPROMClass::length()
|
||||
{
|
||||
return sizeof(someFakeEEPROM_memory);
|
||||
}
|
||||
|
||||
// TODO: Add missing functioality..
|
||||
int16_t EEPROMClass::setStorage(const char* filename, bool write)
|
||||
{
|
||||
|
||||
//Close any open storage file
|
||||
if(storage)
|
||||
{
|
||||
fclose(storage);
|
||||
storage = NULL;
|
||||
}
|
||||
|
||||
autoUpdate = write;
|
||||
|
||||
storage = fopen(filename, "rb");
|
||||
|
||||
|
||||
//If only reading, fail if file does not exist (makes no sense otherwise)
|
||||
if(!storage && !autoUpdate) {
|
||||
printf("Could not open EEPROM storage file: '%s'\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(storage)
|
||||
{
|
||||
printf("Reading EEPROM storage file: '%s'\n", filename);
|
||||
rewind(storage);
|
||||
fread(someFakeEEPROM_memory, sizeof(someFakeEEPROM_memory), 1, storage);
|
||||
}
|
||||
|
||||
if(!autoUpdate)
|
||||
{
|
||||
//No need for the file anymore, close it
|
||||
fclose(storage);
|
||||
storage = NULL;
|
||||
}
|
||||
|
||||
//Create file if it doesn't exist (so we can write to it)
|
||||
if(!storage && autoUpdate)
|
||||
{
|
||||
storage = fopen(filename, "wb");
|
||||
if(!storage)
|
||||
{
|
||||
printf("Could not create EEPROM storage file: '%s'\n", filename);
|
||||
autoUpdate = false;
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
||||
if(storage && autoUpdate)
|
||||
{
|
||||
//Reopen file for writing without overwriting it
|
||||
storage = freopen(filename, "r+b", storage);
|
||||
|
||||
if(!storage)
|
||||
{
|
||||
printf("Could not access EEPROM storage file for writing: '%s'\n", filename);
|
||||
autoUpdate = false;
|
||||
return -3;
|
||||
}
|
||||
|
||||
printf("Writing any EEPROM changes to '%s'\n", filename);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void EEPROMClass::closeStorage() {
|
||||
if(storage==NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Closing EEPROM storage\n");
|
||||
|
||||
fclose(storage);
|
||||
storage=NULL;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue