DCCpp
This is the library version of a program for Arduino to control railroading DCC devices.
EEStore.cpp
1 /**********************************************************************
2 
3 EEStore.cpp
4 COPYRIGHT (c) 2013-2016 Gregg E. Berman
5 
6 Part of DCC++ BASE STATION for the Arduino
7 
8 **********************************************************************/
9 
10 #include "EEStore.h"
11 
12 #ifdef USE_EEPROM
13 #ifdef VISUALSTUDIO
14 #include "string.h"
15 #endif
16 #include "DCCpp_Uno.h"
17 #include "Turnout.h"
18 #include "Sensor.h"
19 #include "Outputs.h"
20 #include "EEPROM.h"
21 
23 
24 void EEStore::init(){
25 
26 
27  eeStore=(EEStore *)calloc(1,sizeof(EEStore));
28 
29  //EEPROM.get(0,eeStore->data); // get eeStore data
30 #ifdef VISUALSTUDIO
31  EEPROM.get(0, (void *)&eeStore->data, sizeof(EEStoreData));
32 #else
33  EEPROM.get(0, eeStore->data);
34 #endif
35 
36  if(strncmp(eeStore->data.id,EESTORE_ID,sizeof(EESTORE_ID))!=0){ // check to see that eeStore contains valid DCC++ ID
37  sprintf(eeStore->data.id,EESTORE_ID); // if not, create blank eeStore structure (no turnouts, no sensors) and save it back to EEPROM
38 #ifdef USE_TURNOUT
39  eeStore->data.nTurnouts=0;
40 #endif
41 #ifdef USE_SENSOR
42  eeStore->data.nSensors=0;
43 #endif
44 #ifdef USE_OUTPUT
45  eeStore->data.nOutputs=0;
46 #endif
47 #ifdef VISUALSTUDIO
48  EEPROM.put(0, (void *)&eeStore->data, sizeof(EEStoreData));
49 #else
50  EEPROM.put(0, eeStore->data);
51 #endif
52  }
53 
54  reset(); // set memory pointer to first free EEPROM space
55 #ifdef USE_TURNOUT
56  Turnout::load(); // load turnout definitions
57 #endif
58 #ifdef USE_SENSOR
59  Sensor::load(); // load sensor definitions
60 #endif
61 #ifdef USE_OUTPUT
62  Output::load(); // load output definitions
63 #endif
64 }
65 
67 
68 void EEStore::clear(){
69 
70  sprintf(eeStore->data.id,EESTORE_ID); // create blank eeStore structure (no turnouts, no sensors) and save it back to EEPROM
71 #ifdef USE_TURNOUT
72  eeStore->data.nTurnouts=0;
73 #endif
74 #ifdef USE_SENSOR
75  eeStore->data.nSensors=0;
76 #endif
77 #ifdef USE_OUTPUT
78  eeStore->data.nOutputs=0;
79 #endif
80 #ifdef VISUALSTUDIO
81  EEPROM.put(0, (void *)&eeStore->data, sizeof(EEStoreData));
82 #else
83  EEPROM.put(0, eeStore->data);
84 #endif
85 
86 }
87 
89 
90 void EEStore::store() {
91  reset();
92 #ifdef USE_TURNOUT
93  Turnout::store();
94 #endif
95 #ifdef USE_SENSOR
96  Sensor::store();
97 #endif
98 #ifdef USE_SENSOR
99  Output::store();
100 #endif
101 #ifdef VISUALSTUDIO
102  EEPROM.put(0, (void *)&eeStore->data, sizeof(EEStoreData));
103 #else
104  EEPROM.put(0, eeStore->data);
105 #endif
106 }
107 
109 
110 bool EEStore::needsRefreshing() {
111 #ifdef USE_TURNOUT
112  if (eeStore->data.nTurnouts != Turnout::count())
113  return true;
114 #endif
115 #ifdef USE_SENSOR
116  Sensor::store();
117 #endif
118 #ifdef USE_SENSOR
119  Output::store();
120 #endif
121  return false;
122 }
123 
125 
126 void EEStore::advance(int n){
127  eeAddress+=n;
128 }
129 
131 
132 void EEStore::reset(){
133  eeAddress=sizeof(EEStore);
134 }
136 
137 int EEStore::pointer(){
138  return(eeAddress);
139 }
141 
142 EEStore *EEStore::eeStore=NULL;
143 int EEStore::eeAddress=0;
144 
145 #endif