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 "Arduino.h"
11 
12 #include "EEStore.h"
13 
14 #ifdef USE_EEPROM
15 #ifdef VISUALSTUDIO
16 #include "string.h"
17 #endif
18 #include "DCCpp_Uno.h"
19 #include "Turnout.h"
20 #include "Sensor.h"
21 #include "Outputs.h"
22 #include "EEPROM.h"
23 
25 
26 void EEStore::init(){
27 
28 
29  //eeStore=(EEStore *)calloc(1,sizeof(EEStore));
30 
31  //EEPROM.get(0,eeStore->data); // get eeStore data
32 #ifdef VISUALSTUDIO
33  EEPROM.get(0, (void *)&data, sizeof(EEStoreData));
34 #else
35  EEPROM.get(0, data);
36 #endif
37 
38  if(strncmp(data.id,EESTORE_ID,sizeof(EESTORE_ID))!=0){ // check to see that eeStore contains valid DCC++ ID
39  sprintf(data.id,EESTORE_ID); // if not, create blank eeStore structure (no turnouts, no sensors) and save it back to EEPROM
40 #ifdef USE_TURNOUT
41  data.nTurnouts=0;
42 #endif
43 #ifdef USE_SENSOR
44  data.nSensors=0;
45 #endif
46 #ifdef USE_OUTPUT
47  data.nOutputs=0;
48 #endif
49 #ifdef VISUALSTUDIO
50  EEPROM.put(0, (void *)&data, sizeof(EEStoreData));
51 #else
52  EEPROM.put(0, data);
53 #endif
54  }
55 
56  reset(); // set memory pointer to first free EEPROM space
57 #ifdef USE_TURNOUT
58  Turnout::load(); // load turnout definitions
59 #endif
60 #ifdef USE_SENSOR
61  Sensor::load(); // load sensor definitions
62 #endif
63 #ifdef USE_OUTPUT
64  Output::load(); // load output definitions
65 #endif
66 }
67 
69 
70 void EEStore::clear(){
71 
72  sprintf(data.id,EESTORE_ID); // create blank eeStore structure (no turnouts, no sensors) and save it back to EEPROM
73 #ifdef USE_TURNOUT
74  data.nTurnouts=0;
75 #endif
76 #ifdef USE_SENSOR
77  data.nSensors=0;
78 #endif
79 #ifdef USE_OUTPUT
80  data.nOutputs=0;
81 #endif
82 #ifdef VISUALSTUDIO
83  EEPROM.put(0, (void *)&data, sizeof(EEStoreData));
84 #else
85  EEPROM.put(0, data);
86 #endif
87 
88 }
89 
91 
92 void EEStore::store() {
93  reset();
94 #ifdef USE_TURNOUT
95  Turnout::store();
96 #endif
97 #ifdef USE_SENSOR
98  Sensor::store();
99 #endif
100 #ifdef USE_OUTPUT
101  Output::store();
102 #endif
103 #ifdef VISUALSTUDIO
104  EEPROM.put(0, (void *)&data, sizeof(EEStoreData));
105 #else
106  EEPROM.put(0, data);
107 #endif
108 }
109 
111 
112 bool EEStore::needsRefreshing() {
113 #ifdef USE_TURNOUT
114  if (data.nTurnouts != Turnout::count())
115  return true;
116 #endif
117 #ifdef USE_SENSOR
118  if (data.nSensors != Sensor::count())
119  return true;
120 #endif
121 #ifdef USE_OUTPUT
122  if (data.nOutputs!= Output::count())
123  return true;
124 #endif
125  return false;
126 }
127 
129 
130 void EEStore::advance(int n){
131  eeAddress+=n;
132 }
133 
135 
136 void EEStore::reset(){
137  eeAddress=sizeof(EEStoreData);
138 }
140 
141 int EEStore::pointer(){
142  return(eeAddress);
143 }
145 
146 //EEStore *EEStore::eeStore=NULL;
147 EEStoreData EEStore::data;
148 
149 int EEStore::eeAddress=0;
150 
151 #endif