2020-10-28 22:00:07 +01:00
|
|
|
/*
|
2022-01-07 02:28:35 +01:00
|
|
|
* © 2021 Neil McKechnie
|
|
|
|
* © 2021 Fred Decker
|
2022-07-30 23:08:42 +02:00
|
|
|
* © 2020-2022 Harald Barth
|
2022-01-07 02:28:35 +01:00
|
|
|
* © 2020-2021 Chris Harlow
|
2020-10-28 22:00:07 +01:00
|
|
|
* © 2013-2016 Gregg E. Berman
|
2022-01-07 02:28:35 +01:00
|
|
|
* All rights reserved.
|
2020-10-28 22:00:07 +01:00
|
|
|
*
|
2022-01-07 02:28:35 +01:00
|
|
|
* This file is part of CommandStation-EX
|
2020-10-28 22:00:07 +01:00
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* It is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-11-08 02:07:21 +01:00
|
|
|
|
2021-11-30 19:40:31 +01:00
|
|
|
#include "defines.h"
|
2021-11-08 02:07:21 +01:00
|
|
|
#ifndef DISABLE_EEPROM
|
2020-06-03 15:26:49 +02:00
|
|
|
#include "EEStore.h"
|
2021-08-27 13:56:27 +02:00
|
|
|
|
2020-10-03 14:07:25 +02:00
|
|
|
#include "DIAG.h"
|
2021-08-27 13:56:27 +02:00
|
|
|
#include "Outputs.h"
|
|
|
|
#include "Sensors.h"
|
|
|
|
#include "Turnouts.h"
|
2020-06-03 15:26:49 +02:00
|
|
|
|
2023-10-31 14:47:29 +01:00
|
|
|
#if defined(ARDUINO_ARCH_SAMC) || defined(ARDUINO_GIGA)
|
2020-06-03 15:26:49 +02:00
|
|
|
ExternalEEPROM EEPROM;
|
|
|
|
#endif
|
|
|
|
|
2021-08-27 13:56:27 +02:00
|
|
|
void EEStore::init() {
|
2023-10-31 14:47:29 +01:00
|
|
|
#if defined(ARDUINO_ARCH_SAMC) || defined(ARDUINO_GIGA)
|
2021-08-27 13:56:27 +02:00
|
|
|
EEPROM.begin(0x50); // Address for Microchip 24-series EEPROM with all three
|
|
|
|
// A pins grounded (0b1010000 = 0x50)
|
2020-06-03 15:26:49 +02:00
|
|
|
#endif
|
|
|
|
|
2021-08-27 13:56:27 +02:00
|
|
|
eeStore = (EEStore *)calloc(1, sizeof(EEStore));
|
|
|
|
|
|
|
|
EEPROM.get(0, eeStore->data); // get eeStore data
|
|
|
|
|
|
|
|
// check to see that eeStore contains valid DCC++ ID
|
|
|
|
if (strncmp(eeStore->data.id, EESTORE_ID, sizeof(EESTORE_ID)) != 0) {
|
|
|
|
// if not, create blank eeStore structure (no
|
|
|
|
// turnouts, no sensors) and save it back to EEPROM
|
2022-07-30 21:54:28 +02:00
|
|
|
strncpy(eeStore->data.id, EESTORE_ID, sizeof(EESTORE_ID)+0);
|
2021-08-27 13:56:27 +02:00
|
|
|
eeStore->data.nTurnouts = 0;
|
|
|
|
eeStore->data.nSensors = 0;
|
|
|
|
eeStore->data.nOutputs = 0;
|
|
|
|
EEPROM.put(0, eeStore->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
reset(); // set memory pointer to first free EEPROM space
|
|
|
|
Turnout::load(); // load turnout definitions
|
|
|
|
Sensor::load(); // load sensor definitions
|
|
|
|
Output::load(); // load output definitions
|
2020-06-03 15:26:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-08-27 13:56:27 +02:00
|
|
|
void EEStore::clear() {
|
|
|
|
sprintf(eeStore->data.id,
|
|
|
|
EESTORE_ID); // create blank eeStore structure (no turnouts, no
|
|
|
|
// sensors) and save it back to EEPROM
|
|
|
|
eeStore->data.nTurnouts = 0;
|
|
|
|
eeStore->data.nSensors = 0;
|
|
|
|
eeStore->data.nOutputs = 0;
|
|
|
|
EEPROM.put(0, eeStore->data);
|
2020-06-03 15:26:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-08-27 13:56:27 +02:00
|
|
|
void EEStore::store() {
|
|
|
|
reset();
|
|
|
|
Turnout::store();
|
|
|
|
Sensor::store();
|
|
|
|
Output::store();
|
|
|
|
EEPROM.put(0, eeStore->data);
|
|
|
|
DIAG(F("EEPROM used: %d/%d bytes"), EEStore::pointer(), EEPROM.length());
|
2020-06-03 15:26:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-08-27 13:56:27 +02:00
|
|
|
void EEStore::advance(int n) { eeAddress += n; }
|
2020-06-03 15:26:49 +02:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-08-27 13:56:27 +02:00
|
|
|
void EEStore::reset() { eeAddress = sizeof(EEStore); }
|
2020-06-03 15:26:49 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-08-27 13:56:27 +02:00
|
|
|
int EEStore::pointer() { return (eeAddress); }
|
2020-06-03 15:26:49 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-10-03 14:07:25 +02:00
|
|
|
void EEStore::dump(int num) {
|
2022-07-30 21:54:28 +02:00
|
|
|
byte b = 0;
|
2021-08-27 13:56:27 +02:00
|
|
|
DIAG(F("Addr 0x char"));
|
|
|
|
for (int n = 0; n < num; n++) {
|
|
|
|
EEPROM.get(n, b);
|
|
|
|
DIAG(F("%d %x %c"), n, b, isprint(b) ? b : ' ');
|
|
|
|
}
|
2020-10-03 14:07:25 +02:00
|
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-08-27 13:56:27 +02:00
|
|
|
EEStore *EEStore::eeStore = NULL;
|
|
|
|
int EEStore::eeAddress = 0;
|
2021-11-08 02:07:21 +01:00
|
|
|
#endif
|