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