Skip to content

Commit 49a16f6

Browse files
V1.7.15 - Updates
- Fixed the EEPROM persistence failures on ESP boards.
1 parent ef8b4d9 commit 49a16f6

File tree

13 files changed

+272
-124
lines changed

13 files changed

+272
-124
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <EEPROM.h>
2+
#include "EPROMStore.hpp"
3+
#include "Utility.hpp"
4+
5+
// The global instance of the platform-independant EEPROM class
6+
EPROMStore *EPROMStore::_eepromStore = NULL;
7+
8+
// Initialize the EEPROM storage in a platform-independent abstraction
9+
void EPROMStore::initialize()
10+
{
11+
LOGV2(DEBUG_VERBOSE, "EEPROM: Initialize. Instance is %s", _eepromStore == NULL ? "NULL" : "VALID");
12+
if (_eepromStore == NULL)
13+
{
14+
LOGV1(DEBUG_VERBOSE, "EEPROM: Creating single instance");
15+
_eepromStore = new EPROMStore();
16+
}
17+
}
18+
19+
// Get the instance of the EEPROM storage
20+
EPROMStore *EPROMStore::Storage()
21+
{
22+
return _eepromStore;
23+
}
24+
25+
#ifdef ESPBOARD
26+
27+
// Construct the EEPROM object for ESP boards, settign aside 32 bytes for storage
28+
EPROMStore::EPROMStore()
29+
{
30+
LOGV1(DEBUG_VERBOSE, "EEPROM[ESP]: Startup with 32 bytes");
31+
EEPROM.begin(32);
32+
}
33+
34+
// Update the given location with the given value
35+
void EPROMStore::update(int location, uint8_t value)
36+
{
37+
LOGV3(DEBUG_VERBOSE, "EEPROM[ESP]: Writing %x to %d", value, location);
38+
EEPROM.write(location, value);
39+
LOGV1(DEBUG_VERBOSE, "EEPROM[ESP]: Committing");
40+
EEPROM.commit();
41+
}
42+
43+
// Read the value at the given location
44+
uint8_t EPROMStore::read(int location)
45+
{
46+
uint8_t value;
47+
value = EEPROM.read(location);
48+
LOGV3(DEBUG_VERBOSE, "EEPROM[ESP]: Read %x from %d", value, location);
49+
return value;
50+
}
51+
52+
#else
53+
54+
// Construct the EEPROM object for non-ESP boards
55+
EPROMStore::EPROMStore()
56+
{
57+
LOGV1(DEBUG_VERBOSE, "EEPROM[UNO]: Startup ");
58+
}
59+
60+
// Update the given location with the given value
61+
void EPROMStore::update(int location, uint8_t value)
62+
{
63+
LOGV3(DEBUG_VERBOSE, "EEPROM[UNO]: Writing %x to %d", value, location);
64+
EEPROM.write(location, value);
65+
}
66+
67+
// Read the value at the given location
68+
uint8_t EPROMStore::read(int location)
69+
{
70+
uint8_t value = EEPROM.read(location);
71+
LOGV3(DEBUG_VERBOSE, "EEPROM[UNO]: Read %x from %d", value, location);
72+
return value;
73+
}
74+
75+
#endif
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include <Arduino.h>
3+
4+
// Platform independant abstraction of the EEPROM storage capability of the boards.
5+
// This is needed because the ESP boards require two things that the Arduino boards don't:
6+
// 1) It wants to know how many bytes you want to use (at most)
7+
// 2) It wants you to call a commit() function after a write() to actual persist the data.
8+
class EPROMStore {
9+
static EPROMStore *_eepromStore;
10+
public:
11+
EPROMStore();
12+
static void initialize();
13+
14+
void update(int location, uint8_t value);
15+
uint8_t read(int location);
16+
static EPROMStore* Storage();
17+
};
18+
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
11
#include "Globals.hpp"
2-
#include <EEPROM.h>
3-
4-
void EEPROMupdate(int loc, byte val)
5-
{
6-
if (EEPROM.read(loc)!=val)
7-
{
8-
EEPROM.write(loc,val);
9-
}
10-
}
2+
#include "Utility.hpp"

Software/Arduino code/OpenAstroTracker/Globals.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include <Arduino.h>
55
#include <WString.h>
66

7-
void EEPROMupdate(int loc, byte val);
8-
97
// Set to 1 if you are in the northern hemisphere.
108
#define NORTHERN_HEMISPHERE 1
119

@@ -33,7 +31,8 @@ extern byte PolarisRASecond;
3331
#define DEBUG_MOUNT_VERBOSE 0x10
3432
#define DEBUG_GENERAL 0x20
3533
#define DEBUG_MEADE 0x40
36-
#define DEBUG_ANY 0x7F
34+
#define DEBUG_VERBOSE 0x80
35+
#define DEBUG_ANY 0xFF
3736

3837
// Bit Name Output
3938
// 0 DEBUG_INFO General output, like startup variables and status
@@ -48,8 +47,10 @@ extern byte PolarisRASecond;
4847
// Note that if you use an app to control OAT, ANY debug output will likely confuse that app.
4948
// Debug output is useful if you are using Wifi to control the OAT or if you are issuing
5049
// manual commands via a terminal.
50+
//
5151
// #define DEBUG_LEVEL (DEBUG_SERIAL|DEBUG_WIFI|DEBUG_INFO|DEBUG_MOUNT|DEBUG_GENERAL)
5252
// #define DEBUG_LEVEL (DEBUG_ANY)
53+
// #define DEBUG_LEVEL (DEBUG_INFO|DEBUG_MOUNT|DEBUG_GENERAL)
5354
#define DEBUG_LEVEL (DEBUG_NONE)
5455

5556
// Set this to 1 to run a key diagnostic. No tracker functions are on at all.
@@ -73,8 +74,8 @@ extern byte PolarisRASecond;
7374
#undef HEADLESS_CLIENT
7475
#define HEADLESS_CLIENT 1
7576
#define WIFI_ENABLED
76-
#define INFRA_SSID "yourSSID"
77-
#define INFRA_WPAKEY "yourWPAkey"
77+
#define INFRA_SSID "YouSSID"
78+
#define INFRA_WPAKEY "YourWPAKey"
7879
#define OAT_WPAKEY "superSecret"
7980
#define HOSTNAME "OATerScope"
8081
// 0 - Infrastructure Only - Connecting to a Router

Software/Arduino code/OpenAstroTracker/LcdMenu.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <EEPROM.h>
21
#include "Utility.hpp"
2+
#include "EPROMStore.hpp"
33
#include "LcdMenu.hpp"
44

55
#if HEADLESS_CLIENT == 0
@@ -20,7 +20,7 @@ LcdMenu::LcdMenu(byte cols, byte rows, int maxItems) : _lcd(8, 9, 4, 5, 6, 7) {
2020
_lastDisplay[1] = "";
2121
_menuItems = new MenuItem * [maxItems];
2222

23-
_brightness = EEPROM.read(11);
23+
_brightness = EPROMStore::Storage()->read(11);
2424
LOGV2(DEBUG_INFO, "LCD: Brightness from EEPROM is %d", _brightness);
2525
// pinMode(10, OUTPUT);
2626
// analogWrite(10, _brightness);
@@ -88,7 +88,7 @@ void LcdMenu::setBacklightBrightness(int level, bool persist) {
8888
LOGV2(DEBUG_INFO, "LCD: Wrote %d as brightness", _brightness );
8989
if (persist) {
9090
LOGV2(DEBUG_INFO, "LCD: Saving %d as brightness", (_brightness & 0x00FF));
91-
EEPROMupdate(11, (byte)(_brightness & 0x00FF));
91+
EPROMStore::Storage()->update(11, (byte)(_brightness & 0x00FF));
9292
}
9393
}
9494

Software/Arduino code/OpenAstroTracker/Mount.cpp

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#include <EEPROM.h>
2-
31
#include "InterruptCallback.hpp"
42

53
#include "LcdMenu.hpp"
64
#include "Mount.hpp"
75
#include "Utility.hpp"
6+
#include "EPROMStore.hpp"
87

98
//mountstatus
109
#define STATUS_PARKED 0B0000000000000000
@@ -106,6 +105,18 @@ void Mount::startTimerInterrupts()
106105

107106
}
108107

108+
/////////////////////////////////
109+
//
110+
// readConfiguration
111+
//
112+
/////////////////////////////////
113+
void Mount::readConfiguration()
114+
{
115+
LOGV1(DEBUG_INFO, "Mount: Reading configuration data from EEPROM");
116+
readPersistentData();
117+
LOGV1(DEBUG_INFO, "Mount: Done reading configuration data from EEPROM");
118+
}
119+
109120
/////////////////////////////////
110121
//
111122
// readPersistentData
@@ -124,29 +135,44 @@ void Mount::startTimerInterrupts()
124135
void Mount::readPersistentData()
125136
{
126137
// Read the magic marker byte and state
127-
int marker = EEPROM.read(4) + EEPROM.read(5) * 256;
128-
LOGV2(DEBUG_MOUNT_VERBOSE, "EEPROM: Marker: %x ", marker);
138+
uint8_t markerLo=EPROMStore::Storage()->read(4);
139+
uint8_t markerHi=EPROMStore::Storage()->read(5);
140+
141+
uint16_t marker = (uint16_t)markerLo + (uint16_t)markerHi * 256;
142+
LOGV4(DEBUG_INFO, "Mount: EEPROM: Marker: %x (L:%d H:%d)", marker, markerLo, markerHi);
129143

130144
if ((marker & 0xFF01) == 0xBE01) {
131-
_stepsPerRADegree = EEPROM.read(6) + EEPROM.read(7) * 256;
132-
LOGV2(DEBUG_MOUNT,"EEPROM: RA Marker OK! RA steps/deg is %d", _stepsPerRADegree);
145+
_stepsPerRADegree = EPROMStore::Storage()->read(6) + EPROMStore::Storage()->read(7) * 256;
146+
LOGV2(DEBUG_INFO,"Mount: EEPROM: RA Marker OK! RA steps/deg is %d", _stepsPerRADegree);
147+
}
148+
else{
149+
LOGV1(DEBUG_INFO,"Mount: EEPROM: No stored value for RA steps");
133150
}
134151

135152
if ((marker & 0xFF02) == 0xBE02) {
136-
_stepsPerDECDegree = EEPROM.read(8) + EEPROM.read(9) * 256;
137-
LOGV2(DEBUG_MOUNT,"EEPROM: DEC Marker OK! DEC steps/deg is %d", _stepsPerDECDegree);
153+
_stepsPerDECDegree = EPROMStore::Storage()->read(8) + EPROMStore::Storage()->read(9) * 256;
154+
LOGV2(DEBUG_INFO,"Mount: EEPROM: DEC Marker OK! DEC steps/deg is %d", _stepsPerDECDegree);
155+
}
156+
else{
157+
LOGV1(DEBUG_INFO,"Mount: EEPROM: No stored value for DEC steps");
138158
}
139159

140160
float speed = 1.0;
141161
if ((marker & 0xFF04) == 0xBE04) {
142-
int adjust = EEPROM.read(0) + EEPROM.read(3) * 256;
162+
int adjust = EPROMStore::Storage()->read(0) + EPROMStore::Storage()->read(3) * 256;
143163
speed = 1.0 + 1.0 * adjust / 10000.0;
144-
LOGV3(DEBUG_MOUNT,"EEPROM: Speed Marker OK! Speed adjust is %d, speedFactor is %f", adjust, speed);
164+
LOGV3(DEBUG_INFO,"Mount: EEPROM: Speed Marker OK! Speed adjust is %d, speedFactor is %f", adjust, speed);
165+
}
166+
else{
167+
LOGV1(DEBUG_INFO,"Mount: EEPROM: No stored value for speed factor");
145168
}
146169

147170
if ((marker & 0xFF08) == 0xBE08) {
148-
_backlashCorrectionSteps = EEPROM.read(10) + EEPROM.read(11) * 256;
149-
LOGV2(DEBUG_MOUNT,"EEPROM: Backlash Steps Marker OK! Backlash correction is %d", _backlashCorrectionSteps);
171+
_backlashCorrectionSteps = EPROMStore::Storage()->read(10) + EPROMStore::Storage()->read(11) * 256;
172+
LOGV2(DEBUG_INFO,"Mount: EEPROM: Backlash Steps Marker OK! Backlash correction is %d", _backlashCorrectionSteps);
173+
}
174+
else{
175+
LOGV1(DEBUG_INFO,"Mount: EEPROM: No stored value for backlash correction");
150176
}
151177

152178
setSpeedCalibration(speed, false);
@@ -159,15 +185,17 @@ void Mount::readPersistentData()
159185
/////////////////////////////////
160186
void Mount::writePersistentData(int which, int val)
161187
{
162-
int flag = 0x00;
188+
uint8_t flag = 0x00;
163189
int loByteLocation = 0;
164190
int hiByteLocation = 0;
165191

166192
// If we're written something before...
167-
if (EEPROM.read(5) == 0xBE) {
193+
uint8_t magicMarker = EPROMStore::Storage()->read(5);
194+
LOGV4(DEBUG_INFO,"Mount: EEPROM Write: Marker is %x, flag is %x (%d)", magicMarker, flag, flag);
195+
if (magicMarker == 0xBE) {
168196
// ... read the current state ...
169-
flag = EEPROM.read(4);
170-
LOGV3(DEBUG_MOUNT,"EEPROM Write: Marker is 0xBE, flag is %x (%d)", flag, flag);
197+
flag = EPROMStore::Storage()->read(4);
198+
LOGV3(DEBUG_INFO,"Mount: EEPROM Write: Marker is 0xBE, flag is %x (%d)", flag, flag);
171199
}
172200
switch (which) {
173201
case RA_STEPS:
@@ -176,6 +204,7 @@ void Mount::writePersistentData(int which, int val)
176204
flag |= 0x01;
177205
loByteLocation = 6;
178206
hiByteLocation = 7;
207+
LOGV2(DEBUG_INFO,"Mount: EEPROM Write: Updating RA steps to %d", val);
179208
}
180209
break;
181210
case DEC_STEPS:
@@ -184,6 +213,7 @@ void Mount::writePersistentData(int which, int val)
184213
flag |= 0x02;
185214
loByteLocation = 8;
186215
hiByteLocation = 9;
216+
LOGV2(DEBUG_INFO,"Mount: EEPROM Write: Updating DEC steps to %d", val);
187217
}
188218
break;
189219
case SPEED_FACTOR_DECIMALS:
@@ -192,6 +222,7 @@ void Mount::writePersistentData(int which, int val)
192222
flag |= 0x04;
193223
loByteLocation = 0;
194224
hiByteLocation = 3;
225+
LOGV2(DEBUG_INFO,"Mount: EEPROM Write: Updating Speed factor to %d", val);
195226
}
196227
break;
197228
case BACKLASH_CORRECTION:
@@ -200,19 +231,20 @@ void Mount::writePersistentData(int which, int val)
200231
flag |= 0x08;
201232
loByteLocation = 10;
202233
hiByteLocation = 11;
234+
LOGV2(DEBUG_INFO,"Mount: EEPROM Write: Updating Backlash to %d", val);
203235
}
204236
break;
205237
}
206238

207-
LOGV3(DEBUG_MOUNT_VERBOSE,"EEPROM Write: New Marker is 0xBE, flag is %x (%d)", flag, flag);
239+
LOGV3(DEBUG_INFO,"Mount: EEPROM Write: New Marker is 0xBE, flag is %x (%d)", flag, flag);
208240

209-
EEPROMupdate(4, flag);
210-
EEPROMupdate(5, 0xBE);
241+
EPROMStore::Storage()->update(4, flag);
242+
EPROMStore::Storage()->update(5, 0xBE);
211243

212-
EEPROMupdate(loByteLocation, val & 0x00FF);
213-
EEPROMupdate(hiByteLocation, (val >> 8) & 0x00FF);
244+
EPROMStore::Storage()->update(loByteLocation, val & 0x00FF);
245+
EPROMStore::Storage()->update(hiByteLocation, (val >> 8) & 0x00FF);
214246

215-
LOGV5(DEBUG_MOUNT,"EEPROM Write: wrote %x to %d and %x to %d", val & 0x00FF, loByteLocation, (val >> 8) & 0x00FF, hiByteLocation);
247+
LOGV5(DEBUG_INFO,"Mount: EEPROM Write: Wrote %x to %d and %x to %d", val & 0x00FF, loByteLocation, (val >> 8) & 0x00FF, hiByteLocation);
216248
}
217249

218250
/////////////////////////////////

Software/Arduino code/OpenAstroTracker/Mount.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ class Mount {
188188
// Called when startup is complete and the mount needs to start updating steppers.
189189
void startTimerInterrupts();
190190

191+
// Read the saved configuration from persistent storage
192+
void readConfiguration();
193+
191194
private:
192195

193196
// Reads values from EEPROM that configure the mount (if previously stored)

Software/Arduino code/OpenAstroTracker/OpenAstroTracker.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include "Globals.hpp"
2020

21-
String version = "V1.7.14";
21+
String version = "V1.7.15";
2222

2323
///////////////////////////////////////////////////////////////////////////
2424
// Please see the Globals.h file for configuration of the firmware.

0 commit comments

Comments
 (0)