-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathEPROMStore.cpp
More file actions
950 lines (805 loc) · 33.4 KB
/
EPROMStore.cpp
File metadata and controls
950 lines (805 loc) · 33.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
#include "inc/Globals.hpp"
PUSH_NO_WARNINGS
#include <EEPROM.h>
POP_NO_WARNINGS
#include "../Configuration.hpp"
#include "Utility.hpp"
#include "EPROMStore.hpp"
// The platform-independant EEPROM class
// Steps/deg are normalized to this value and stored.
const float SteppingStorageNormalized = 25600.0;
///////////////////////////////////////
// PLATFORM-SPECIFIC IMPLEMENTATIONS
#if USE_DUMMY_EEPROM == true
static uint8_t dummyEepromStorage[EEPROMStore::STORE_SIZE];
// Initialize the EEPROM object for ESP boards, setting aside storage
void EEPROMStore::initialize()
{
LOG(DEBUG_EEPROM, "[EEPROM]: Dummy: Startup with %d bytes", EEPROMStore::STORE_SIZE);
memset(dummyEepromStorage, 0, sizeof(dummyEepromStorage));
displayContents(); // Will always be empty at restart
}
// Update the given location with the given value
void EEPROMStore::update(uint8_t location, uint8_t value)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Dummy: Writing %x to %d", value, location);
dummyEepromStorage[location] = value;
}
// Complete the transaction
void EEPROMStore::commit()
{
// Nothing to do
}
// Read the value at the given location
uint8_t EEPROMStore::read(uint8_t location)
{
uint8_t value;
value = dummyEepromStorage[location];
LOG(DEBUG_EEPROM, "[EEPROM]: Dummy: Read %x from %d", value, location);
return value;
}
#elif defined(ESP32)
// Initialize the EEPROM object for ESP boards, setting aside space for storage
void EEPROMStore::initialize()
{
LOG(DEBUG_EEPROM, "[EEPROM]: ESP32: Startup with %d bytes", STORE_SIZE);
EEPROM.begin(STORE_SIZE);
displayContents();
}
// Update the given location with the given value
void EEPROMStore::update(uint8_t location, uint8_t value)
{
LOG(DEBUG_EEPROM, "[EEPROM]: ESP32: Writing %x to %d", value, location);
EEPROM.write(location, value);
}
// Complete the transaction
void EEPROMStore::commit()
{
LOG(DEBUG_EEPROM, "[EEPROM]: ESP32: Committing");
EEPROM.commit();
}
// Read the value at the given location
uint8_t EEPROMStore::read(uint8_t location)
{
uint8_t value;
value = EEPROM.read(location);
LOG(DEBUG_EEPROM, "[EEPROM]: ESP32: Read %x from %d", value, location);
return value;
}
#else
// Initialize the EEPROM storage in a platform-independent abstraction
void EEPROMStore::initialize()
{
LOG(DEBUG_EEPROM, "[EEPROM]: ATMega: Startup");
displayContents();
}
// Update the given location with the given value
void EEPROMStore::update(uint8_t location, uint8_t value)
{
LOG(DEBUG_EEPROM, "[EEPROM]: ATMega: Writing8 %x to %d", value, location);
EEPROM.write(location, value);
}
// Complete the transaction
void EEPROMStore::commit()
{
// Nothing to do
}
// Read the value at the given location
uint8_t EEPROMStore::read(uint8_t location)
{
uint8_t value = EEPROM.read(location);
LOG(DEBUG_EEPROM, "[EEPROM]: ATMega: Read8 %x from %d", value, location);
return value;
}
#endif
void EEPROMStore::displayContents()
{
#if (DEBUG_LEVEL & (DEBUG_INFO | DEBUG_EEPROM))
// Read the magic marker byte and state
uint16_t marker = readUint16(MAGIC_MARKER_AND_FLAGS_ADDR);
LOG(DEBUG_EEPROM, "[EEPROM]: Magic Marker: %x", marker);
LOG(DEBUG_INFO, "[EEPROM]: Values? %s", ((marker & MAGIC_MARKER_MASK) == MAGIC_MARKER_VALUE) ? "Yes" : "No");
LOG(DEBUG_INFO, "[EEPROM]: Extended values? %s", ((marker & EXTENDED_FLAG) == EXTENDED_FLAG) ? "Yes" : "No");
LOG(DEBUG_INFO, "[EEPROM]: IsPresent(EXTENDED)? %s", (EEPROMStore::isPresent(EXTENDED_FLAG) ? "Yes" : "No"));
LOG(DEBUG_INFO, "[EEPROM]: Stored HATime: %s", getHATime().ToString());
LOG(DEBUG_INFO, "[EEPROM]: Stored UTC Offset: %d", getUtcOffset());
LOG(DEBUG_INFO, "[EEPROM]: Stored Brightness: %d", getBrightness());
LOG(DEBUG_INFO, "[EEPROM]: Stored RA Steps per Degree: %f", getRAStepsPerDegree());
LOG(DEBUG_INFO, "[EEPROM]: Stored DEC Steps per Degree: %f", getDECStepsPerDegree());
LOG(DEBUG_INFO, "[EEPROM]: Stored Speed Factor: %f", getSpeedFactor());
LOG(DEBUG_INFO, "[EEPROM]: Stored Backlash Correction Steps: %d", getBacklashCorrectionSteps());
LOG(DEBUG_INFO, "[EEPROM]: Stored Latitude: %s", getLatitude().ToString());
LOG(DEBUG_INFO, "[EEPROM]: Stored Longitude: %s", getLongitude().ToString());
LOG(DEBUG_INFO, "[EEPROM]: Stored Pitch Calibration Angle: %f", getPitchCalibrationAngle());
LOG(DEBUG_INFO, "[EEPROM]: Stored Roll Calibration Angle: %f", getRollCalibrationAngle());
LOG(DEBUG_INFO, "[EEPROM]: Stored RA Homing Offset: %l", getRAHomingOffset());
LOG(DEBUG_INFO, "[EEPROM]: Stored AZ Position: %l", getAZPosition());
LOG(DEBUG_INFO, "[EEPROM]: Stored ALT Position: %l", getALTPosition());
LOG(DEBUG_INFO, "[EEPROM]: Stored AZ Steps per Degree: %f", getAZStepsPerDegree());
LOG(DEBUG_INFO, "[EEPROM]: Stored ALT Steps per Degree: %f", getALTStepsPerDegree());
LOG(DEBUG_INFO, "[EEPROM]: Stored DEC Homing Offset : %l", getDECHomingOffset());
LOG(DEBUG_INFO, "[EEPROM]: Stored DEC Lower Limit: %l", getDECLowerLimit());
LOG(DEBUG_INFO, "[EEPROM]: Stored DEC Upper Limit: %l", getDECUpperLimit());
LOG(DEBUG_INFO, "[EEPROM]: Stored Last Flashed Version: %d", getLastFlashedVersion());
#endif
}
///////////////////////////////////////
// HELPER FUNCTIONS
// Helper to update the given location with the given 8-bit value
void EEPROMStore::updateUint8(EEPROMStore::ItemAddress location, uint8_t value)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Writing8 %x (%d) to %d", value, value, location);
update(location, value);
}
// Helper to read an 8-bit value from the given location
uint8_t EEPROMStore::readUint8(EEPROMStore::ItemAddress location)
{
uint8_t value;
value = read(location);
LOG(DEBUG_EEPROM, "[EEPROM]: Read8 %x (%d) from %d", value, value, location);
return value;
}
// Helper to update the given location with the given 8-bit value
void EEPROMStore::updateInt8(EEPROMStore::ItemAddress location, int8_t value)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Writing8 %x (%d) to %d", value, value, location);
update(location, static_cast<uint8_t>(value));
}
// Helper to read an 8-bit value from the given location
int8_t EEPROMStore::readInt8(EEPROMStore::ItemAddress location)
{
int8_t value;
value = static_cast<int8_t>(read(location));
LOG(DEBUG_EEPROM, "[EEPROM]: Read8 %x (%d) from %d", value, value, location);
return value;
}
// Helper to update the given location with the given 16-bit value
void EEPROMStore::updateInt16(EEPROMStore::ItemAddress location, int16_t value)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Writing16 %x (%d) to %d", value, value, location);
update(location, value & 0x00FF);
update(location + 1, (value >> 8) & 0x00FF);
}
// Helper to read a 16-bit value from the given location
int16_t EEPROMStore::readInt16(EEPROMStore::ItemAddress location)
{
uint8_t valLo = read(location);
uint8_t valHi = read(location + 1);
uint16_t uValue = (uint16_t) valLo + (uint16_t) valHi * 256;
int16_t value = static_cast<int16_t>(uValue);
LOG(DEBUG_EEPROM, "[EEPROM]: Read16 %x (%d) from %d", value, value, location);
return value;
}
// Helper to update the given location with the given 16-bit value
void EEPROMStore::updateUint16(EEPROMStore::ItemAddress location, uint16_t value)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Writing16 %x (%d) to %d", value, value, location);
update(location, value & 0x00FF);
update(location + 1, (value >> 8) & 0x00FF);
}
// Helper to read a 16-bit value from the given location
uint16_t EEPROMStore::readUint16(EEPROMStore::ItemAddress location)
{
uint8_t valLo = read(location);
uint8_t valHi = read(location + 1);
uint16_t value = (uint16_t) valLo + (uint16_t) valHi * 256;
LOG(DEBUG_EEPROM, "[EEPROM]: Read16 %x (%d) from %d", value, value, location);
return value;
}
// Helper to update the given location with the given 32-bit value
void EEPROMStore::updateInt32(EEPROMStore::ItemAddress location, int32_t value)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Writing32 %x (%l) to %d", value, value, location);
update(location, value & 0x00FF);
update(location + 1, (value >> 8) & 0x00FF);
update(location + 2, (value >> 16) & 0x00FF);
update(location + 3, (value >> 24) & 0x00FF);
}
// Helper to read a 32-bit value from the given location
int32_t EEPROMStore::readInt32(EEPROMStore::ItemAddress location)
{
uint8_t val1 = read(location);
uint8_t val2 = read(location + 1);
uint8_t val3 = read(location + 2);
uint8_t val4 = read(location + 3);
uint32_t uValue = (uint32_t) val1 + (uint32_t) val2 * 256 + (uint32_t) val3 * 256 * 256 + (uint32_t) val4 * 256 * 256 * 256;
int32_t value = static_cast<int32_t>(uValue);
LOG(DEBUG_EEPROM, "[EEPROM]: Read32 %x (%l) from %d", value, value, location);
return value;
}
// Check if the specified item is present in the core set.
// Returns: true - if marker is present and item flag is set
// false - otherwise
bool EEPROMStore::isPresent(ItemFlag item)
{
uint16_t marker = readUint16(MAGIC_MARKER_AND_FLAGS_ADDR);
unsigned check = (MAGIC_MARKER_MASK | item);
unsigned result = (MAGIC_MARKER_VALUE | item);
bool res = ((marker & check) == result);
LOG(DEBUG_EEPROM, "[EEPROM]: IsDataPresent (%x). Checking (%x & %x) == %x => %d", item, marker, check, result, res);
// Data is only present if both magic marker and item flag are present
//return ((marker & (MAGIC_MARKER_MASK | item)) == (MAGIC_MARKER_VALUE | item));
return res;
}
// Check if the specified item is present in the extended set.
// Returns: true - if marker is present, estended values are present, and the extended item flag is set
// false - otherwise
bool EEPROMStore::isPresentExtended(ExtendedItemFlag item)
{
// Check if any extended data is present
if (!isPresent(EXTENDED_FLAG))
return false; // No extended data present
// Have extended data, now see if required item is available
uint16_t marker = readUint16(EXTENDED_FLAGS_ADDR);
bool result = (marker & item);
LOG(DEBUG_EEPROM, "[EEPROM]: IsExtendedDataPresent (%x). Checking (%x & %x) => %d", item, marker, item, result);
return result;
}
// Updates the core flags for the specified item to indicate it is present.
// Note it is only possible to add items, not to remove them.
void EEPROMStore::updateFlags(ItemFlag item)
{
uint16_t newMarkerAndFlags(MAGIC_MARKER_VALUE | item);
// Grab any existing flags, if they're there
uint16_t existingMarkerAndFlags = readUint16(MAGIC_MARKER_AND_FLAGS_ADDR);
if ((existingMarkerAndFlags & MAGIC_MARKER_MASK) == MAGIC_MARKER_VALUE)
newMarkerAndFlags |= (existingMarkerAndFlags & FLAGS_MASK); // Accumulate flags
updateUint16(MAGIC_MARKER_AND_FLAGS_ADDR, newMarkerAndFlags);
// We will not commit until the actual item value has been written
LOG(DEBUG_EEPROM, "[EEPROM]: Marker & flags updated from %x to %x", existingMarkerAndFlags, newMarkerAndFlags);
}
// Updates the core & extended flags for the specified extended item to indicate it is present.
// Note it is only possible to add items, not to remove them.
void EEPROMStore::updateFlagsExtended(ExtendedItemFlag item)
{
uint16_t extendedFlags(0);
// Grab any existing flags, if they're there
if (isPresent(EXTENDED_FLAG))
extendedFlags = readUint16(EXTENDED_FLAGS_ADDR);
updateFlags(EXTENDED_FLAG);
updateUint16(EXTENDED_FLAGS_ADDR, extendedFlags | item);
// We will not commit until the actual item value has been written
LOG(DEBUG_EEPROM, "[EEPROM]: Extended flags updated from %x to %x", extendedFlags, extendedFlags | item);
}
///////////////////////////////////////
// APPLICATION INTERFACE
// Erase all data in the store.
void EEPROMStore::clearConfiguration()
{
for (int i = 0; i < STORE_SIZE; i++)
{
update(i, 0);
}
commit(); // Complete the transaction
}
// Return the saved Hour Angle (HA)
DayTime EEPROMStore::getHATime()
{
// There is no item flag for HA - it is assumed to always be present
return DayTime(readUint8(HA_HOUR_ADDR), readUint8(HA_MINUTE_ADDR), 0);
}
// Store the Hour Angle (HA)
void EEPROMStore::storeHATime(DayTime const &ha)
{
// There is no item flag for HA - it is assumed to always be present
updateUint8(HA_HOUR_ADDR, ha.getHours());
updateUint8(HA_MINUTE_ADDR, ha.getMinutes());
commit(); // Complete the transaction
}
int EEPROMStore::getUtcOffset()
{
int utcOffset = 0;
if (isPresentExtended(UTC_OFFSET_MARKER_FLAG))
{
utcOffset = readInt8(UTC_OFFSET_ADDR);
LOG(DEBUG_EEPROM, "[EEPROM]: UTC OFfset Marker OK! UTC Offset is %d", utcOffset);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for UTC Offset");
}
return utcOffset;
}
void EEPROMStore::storeUtcOffset(int utcOffset)
{
updateInt8(UTC_OFFSET_ADDR, utcOffset);
updateFlagsExtended(UTC_OFFSET_MARKER_FLAG);
commit(); // Complete the transaction
}
// Return the dimensionless brightness value for the display.
byte EEPROMStore::getBrightness()
{
// There is no item flag for brightness - it is assumed to always be present
byte brightness = readUint8(LCD_BRIGHTNESS_ADDR);
if (brightness == 0)
brightness = 10; // Have a reasonable minimum in case nothing is stored
return brightness; // dimensionless scalar
}
// Store the dimensionless brightness value for the display.
void EEPROMStore::storeBrightness(byte brightness)
{
// There is no item flag for brightness - it is assumed to always be present
updateUint8(LCD_BRIGHTNESS_ADDR, brightness);
commit(); // Complete the transaction
}
// Return the RA steps per degree (actually microsteps per degree).
// If it is not present then the default uncalibrated RA_STEPS_PER_DEGREE value is returned.
float EEPROMStore::getRAStepsPerDegree()
{
float raStepsPerDegree(RA_STEPS_PER_DEGREE); // Default value
if (isPresentExtended(RA_NORM_STEPS_MARKER_FLAG))
{
// Latest version stores 100x steps/deg for 256 MS
const float factor = SteppingStorageNormalized / RA_SLEW_MICROSTEPPING;
raStepsPerDegree = readInt32(RA_NORM_STEPS_DEGREE_ADDR) / factor;
LOG(DEBUG_EEPROM, "[EEPROM]: RA Normed Marker Present! RA steps/deg is %f", raStepsPerDegree);
}
else if (isPresent(RA_STEPS_FLAG))
{
// Previous versions stored 10x steps/deg for the specific MS setting
raStepsPerDegree = 0.1 * readInt16(RA_STEPS_DEGREE_ADDR);
LOG(DEBUG_EEPROM, "[EEPROM]: RA Marker OK! RA steps/deg is %f", raStepsPerDegree);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for RA steps");
}
return raStepsPerDegree; // microsteps per degree
}
// Store the RA steps per degree (actually microsteps per degree).
void EEPROMStore::storeRAStepsPerDegree(float raStepsPerDegree)
{
// Store steps as 100x steps/deg at 256 MS.
const float factor = SteppingStorageNormalized / RA_SLEW_MICROSTEPPING;
int32_t val = raStepsPerDegree * factor;
LOG(DEBUG_EEPROM, "[EEPROM]: Storing RA steps to %l (%f)", val, raStepsPerDegree);
updateInt32(RA_NORM_STEPS_DEGREE_ADDR, val);
updateFlagsExtended(RA_NORM_STEPS_MARKER_FLAG);
commit(); // Complete the transaction
}
// Return the DEC steps per degree for guiding (actually microsteps per degree).
// If it is not present then the default uncalibrated DEC_STEPS_PER_DEGREE value is returned.
float EEPROMStore::getDECStepsPerDegree()
{
float decStepsPerDegree(DEC_STEPS_PER_DEGREE); // Default value
if (isPresentExtended(DEC_NORM_STEPS_MARKER_FLAG))
{
// This version stored 100x steps/deg for 256 MS
const float factor = SteppingStorageNormalized / DEC_SLEW_MICROSTEPPING;
decStepsPerDegree = readInt32(DEC_NORM_STEPS_DEGREE_ADDR) / factor;
LOG(DEBUG_EEPROM, "[EEPROM]: DEC Normed Marker Present! DEC steps/deg is %f", decStepsPerDegree);
}
else if (isPresent(DEC_STEPS_FLAG))
{
// Previous versions stored 10x steps/deg for the specific MS setting
decStepsPerDegree = 0.1 * readInt16(DEC_STEPS_DEGREE_ADDR);
LOG(DEBUG_EEPROM, "[EEPROM]: DEC Marker OK! DEC steps/deg is %f", decStepsPerDegree);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for DEC steps");
}
return decStepsPerDegree; // microsteps per degree
}
// Store the DEC steps per degree for guiding (actually microsteps per degree).
void EEPROMStore::storeDECStepsPerDegree(float decStepsPerDegree)
{
const float factor = SteppingStorageNormalized / DEC_SLEW_MICROSTEPPING;
int32_t val = decStepsPerDegree * factor;
LOG(DEBUG_EEPROM, "[EEPROM]: Storing DEC steps to %l (%f)", val, decStepsPerDegree);
updateInt32(DEC_NORM_STEPS_DEGREE_ADDR, val);
updateFlagsExtended(DEC_NORM_STEPS_MARKER_FLAG);
commit(); // Complete the transaction
}
// Return the AZ steps per degree (actually microsteps per degree).
// If it is not present then the default uncalibrated AZ_STEPS_PER_DEGREE value is returned.
float EEPROMStore::getAZStepsPerDegree()
{
#if AZ_STEPPER_TYPE != STEPPER_TYPE_NONE
float azStepsPerDegree(AZIMUTH_STEPS_PER_REV / 360); // Default value
#else
float azStepsPerDegree(1); // Default value
#endif
if (isPresentExtended(AZ_NORM_STEPS_MARKER_FLAG))
{
#if (AZ_STEPPER_TYPE != STEPPER_TYPE_NONE)
// Latest version stores 100x steps/deg for 256 MS
const float factor = SteppingStorageNormalized / AZ_MICROSTEPPING;
azStepsPerDegree = readInt32(AZ_NORM_STEPS_DEGREE_ADDR) / factor;
LOG(DEBUG_EEPROM, "[EEPROM]: AZ Normed Marker Present! AZ steps/deg is %f", azStepsPerDegree);
#else
LOG(DEBUG_EEPROM, "[EEPROM]: AZ marker present but AZ axis disabled; ignoring stored value");
#endif
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for AZ steps");
}
return azStepsPerDegree; // microsteps per degree
}
// Store the AZ steps per degree (actually microsteps per degree).
void EEPROMStore::storeAZStepsPerDegree(float azStepsPerDegree)
{
// Store steps as 100x steps/deg at 256 MS.
#if (AZ_STEPPER_TYPE != STEPPER_TYPE_NONE)
const float factor = SteppingStorageNormalized / AZ_MICROSTEPPING;
int32_t val = azStepsPerDegree * factor;
LOG(DEBUG_EEPROM, "[EEPROM]: Storing AZ steps to %l (%f)", val, azStepsPerDegree);
updateInt32(AZ_NORM_STEPS_DEGREE_ADDR, val);
updateFlagsExtended(AZ_NORM_STEPS_MARKER_FLAG);
commit(); // Complete the transaction
#else
LOG(DEBUG_EEPROM, "[EEPROM]: Skipping AZ steps store; AZ axis disabled");
(void) azStepsPerDegree;
#endif
}
// Return the ALT steps per degree (actually microsteps per degree).
// If it is not present then the default uncalibrated ALT_STEPS_PER_DEGREE value is returned.
float EEPROMStore::getALTStepsPerDegree()
{
#if (ALT_STEPPER_TYPE != STEPPER_TYPE_NONE)
float azStepsPerDegree(ALTITUDE_STEPS_PER_REV / 360); // Default value
#else
float azStepsPerDegree(1); // Default value
#endif
if (isPresentExtended(ALT_NORM_STEPS_MARKER_FLAG))
{
#if (ALT_STEPPER_TYPE != STEPPER_TYPE_NONE)
// Latest version stores 100x steps/deg for 256 MS
const float factor = SteppingStorageNormalized / ALT_MICROSTEPPING;
azStepsPerDegree = readInt32(ALT_NORM_STEPS_DEGREE_ADDR) / factor;
LOG(DEBUG_EEPROM, "[EEPROM]: ALT Normed Marker Present! ALT steps/deg is %f", azStepsPerDegree);
#else
LOG(DEBUG_EEPROM, "[EEPROM]: ALT marker present but ALT axis disabled; ignoring stored value");
#endif
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for ALT steps");
}
return azStepsPerDegree; // microsteps per degree
}
// Store the ALT steps per degree (actually microsteps per degree).
void EEPROMStore::storeALTStepsPerDegree(float azStepsPerDegree)
{
// Store steps as 100x steps/deg at 256 MS.
#if (ALT_STEPPER_TYPE != STEPPER_TYPE_NONE)
const float factor = SteppingStorageNormalized / ALT_MICROSTEPPING;
int32_t val = azStepsPerDegree * factor;
LOG(DEBUG_EEPROM, "[EEPROM]: Storing ALT steps to %l (%f)", val, azStepsPerDegree);
updateInt32(ALT_NORM_STEPS_DEGREE_ADDR, val);
updateFlagsExtended(ALT_NORM_STEPS_MARKER_FLAG);
commit(); // Complete the transaction
#else
LOG(DEBUG_EEPROM, "[EEPROM]: Skipping ALT steps store; ALT axis disabled");
(void) azStepsPerDegree;
#endif
}
int16_t EEPROMStore::getLastFlashedVersion()
{
if (isPresentExtended(LAST_FLASHED_MARKER_FLAG))
{
// This version stored 100x steps/deg for 256 MS
int16_t version = readInt16(LAST_FLASHED_VERSION);
LOG(DEBUG_EEPROM, "[EEPROM]: Last Flashed version Marker Present! last version %d", version);
return version;
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for Last Flashed Version");
}
return 0;
}
void EEPROMStore::storeLastFlashedVersion(int16_t version)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Storing Last flashed version (%d)", version);
updateInt16(LAST_FLASHED_VERSION, version);
updateFlagsExtended(LAST_FLASHED_MARKER_FLAG);
commit(); // Complete the transaction
}
// Return the Speed Factor scalar (dimensionless).
// If it is not present then the default uncalibrated value of 1.0 is returned.
float EEPROMStore::getSpeedFactor()
{
float speedFactor(1.0); // Default uncalibrated value
if (isPresent(SPEED_FACTOR_FLAG))
{
// Speed factor bytes are in split locations :-(
int val = readUint8(SPEED_FACTOR_LOW_ADDR) + (int) readUint8(SPEED_FACTOR_HIGH_ADDR) * 256;
speedFactor = 1.0 + val / 10000.0;
LOG(DEBUG_EEPROM, "[EEPROM]: Speed Marker OK! Speed adjust is %d, speedFactor is %f", val, speedFactor);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for speed factor");
}
return speedFactor;
}
// Store the Speed Factor (dimensionless).
void EEPROMStore::storeSpeedFactor(float speedFactor)
{
// Store the fractional speed factor since it is a number very close to 1
int32_t val = (speedFactor - 1.0f) * 10000.0f;
val = clamp(val, (int32_t) INT16_MIN, (int32_t) INT16_MAX);
LOG(DEBUG_EEPROM, "[EEPROM]: Storing Speed Factor to %l (%f)", val, speedFactor);
// Speed factor bytes are in split locations :-(
updateUint8(SPEED_FACTOR_LOW_ADDR, val & 0xFF);
updateUint8(SPEED_FACTOR_HIGH_ADDR, (val >> 8) & 0xFF);
updateFlags(SPEED_FACTOR_FLAG);
commit(); // Complete the transaction
}
// Return the Backlash Correction step count (microsteps).
// If it is not present then the default value from the configuration is returned.
int16_t EEPROMStore::getBacklashCorrectionSteps()
{
// Use nominal default values
int16_t backlashCorrectionSteps(BACKLASH_STEPS);
if (isPresent(BACKLASH_STEPS_FLAG))
{
backlashCorrectionSteps = readInt16(BACKLASH_STEPS_ADDR);
LOG(DEBUG_EEPROM, "[EEPROM]: Backlash Steps Marker OK! Backlash correction is %d", backlashCorrectionSteps);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for backlash correction");
}
return backlashCorrectionSteps; // Microsteps (slew)
}
// Store the Backlash Correction step count (microsteps).
void EEPROMStore::storeBacklashCorrectionSteps(int16_t backlashCorrectionSteps)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Write: Updating Backlash to %d", backlashCorrectionSteps);
updateInt16(BACKLASH_STEPS_ADDR, backlashCorrectionSteps);
updateFlags(BACKLASH_STEPS_FLAG);
commit(); // Complete the transaction
}
// Return the stored location Latitude.
// If it is not present then the default value of 45 degrees North is returned.
Latitude EEPROMStore::getLatitude()
{
Latitude latitude(45.0); // Default value (degrees, +ve is North)
if (isPresent(LATITUDE_FLAG))
{
latitude = Latitude(1.0f * readInt16(LATITUDE_ADDR) / 100.0f);
LOG(DEBUG_EEPROM, "[EEPROM]: Latitude Marker OK! Latitude is %s", latitude.ToString());
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for latitude");
}
return latitude; // Object
}
// Store the configured location Latitude.
void EEPROMStore::storeLatitude(Latitude const &latitude)
{
int32_t val = static_cast<int32_t>(roundf(latitude.getTotalHours() * 100.0f));
val = clamp(val, (int32_t) INT16_MIN, (int32_t) INT16_MAX);
LOG(DEBUG_EEPROM, "[EEPROM]: Storing Latitude as %l (%f)", val, latitude.getTotalHours());
updateInt16(LATITUDE_ADDR, val);
updateFlags(LATITUDE_FLAG);
commit(); // Complete the transaction
}
// Return the stored location Longitude.
// If it is not present then the default value of 100 degrees East is returned.
Longitude EEPROMStore::getLongitude()
{
Longitude longitude(100.0); // Default value (degrees, +ve is East)
if (isPresent(LONGITUDE_FLAG))
{
longitude = Longitude(1.0f * readInt16(LONGITUDE_ADDR) / 100.0f);
LOG(DEBUG_EEPROM, "[EEPROM]: Longitude Marker OK! Longitude is %s", longitude.ToString());
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for longitude");
}
return longitude; // Object
}
// Store the configured location Longitude.
void EEPROMStore::storeLongitude(Longitude const &longitude)
{
int32_t val = static_cast<int32_t>(roundf(longitude.getTotalHours() * 100.0f));
val = clamp(val, (int32_t) INT16_MIN, (int32_t) INT16_MAX);
LOG(DEBUG_EEPROM, "[EEPROM]: Storing Longitude as %l (%f)", val, longitude.getTotalHours());
updateInt16(LONGITUDE_ADDR, val);
updateFlags(LONGITUDE_FLAG);
commit(); // Complete the transaction
}
// Return the stored Pitch Calibration Angle (degrees).
// If it is not present then the default value of 0 degrees.
float EEPROMStore::getPitchCalibrationAngle()
{
float pitchCalibrationAngle(0); // degrees
if (isPresent(PITCH_OFFSET_FLAG))
{
int32_t val = readUint16(PITCH_OFFSET_ADDR);
pitchCalibrationAngle = (val - 16384) / 100.0;
LOG(DEBUG_EEPROM, "[EEPROM]: Pitch Offset Marker OK! Pitch Offset is %l (%f)", val, pitchCalibrationAngle);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for Pitch Offset");
}
return pitchCalibrationAngle; // degrees
}
// Store the configured Pitch Calibration Angle (degrees).
void EEPROMStore::storePitchCalibrationAngle(float pitchCalibrationAngle)
{
int32_t val = (pitchCalibrationAngle * 100) + 16384;
val = clamp(val, (int32_t) INT16_MIN, (int32_t) INT16_MAX);
LOG(DEBUG_EEPROM, "[EEPROM]: Storing Pitch calibration %l (%f)", val, pitchCalibrationAngle);
updateInt16(PITCH_OFFSET_ADDR, val);
updateFlags(PITCH_OFFSET_FLAG);
commit(); // Complete the transaction
}
// Return the stored Roll Calibration Angle (degrees).
// If it is not present then the default value of 0 degrees.
float EEPROMStore::getRollCalibrationAngle()
{
float rollCalibrationAngle(0); // degrees
if (isPresent(ROLL_OFFSET_FLAG))
{
int32_t val = readUint16(ROLL_OFFSET_ADDR);
rollCalibrationAngle = (val - 16384) / 100.0;
LOG(DEBUG_EEPROM, "[EEPROM]: Roll Offset Marker OK! Roll Offset is %l (%f)", val, rollCalibrationAngle);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored value for Roll Offset");
}
return rollCalibrationAngle; // degrees
}
// Store the configured Roll Calibration Angle (degrees).
void EEPROMStore::storeRollCalibrationAngle(float rollCalibrationAngle)
{
int32_t val = (rollCalibrationAngle * 100) + 16384;
val = clamp(val, (int32_t) INT16_MIN, (int32_t) INT16_MAX);
LOG(DEBUG_EEPROM, "[EEPROM]: Storing Roll calibration %l (%f)", val, rollCalibrationAngle);
updateInt16(ROLL_OFFSET_ADDR, val);
updateFlags(ROLL_OFFSET_FLAG);
commit(); // Complete the transaction
}
// Return the stored DEC Lower Limit (slew microsteps relative to home).
// If it is not present then the default value of 0 steps (limits are disabled).
float EEPROMStore::getDECLowerLimit()
{
float decLowerLimit(0); // limit angle (deg)
// Note that flags doesn't verify that _both_ DEC limits have been written - these should always be stored as a pair
if (isPresentExtended(DEC_LIMIT_MARKER_FLAG))
{
decLowerLimit = readInt32(DEC_LOWER_LIMIT_ADDR) / 100.0f;
LOG(DEBUG_EEPROM, "[EEPROM]: DEC lower limit read as %f", decLowerLimit);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored values for DEC limits");
}
return decLowerLimit; // limit angle (deg)
}
// Store the configured DEC Lower Limit Pos (slew microsteps relative to home).
void EEPROMStore::storeDECLowerLimit(float decLowerLimit)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Write: Updating DEC Lower Limit to %l", decLowerLimit);
// Note that flags doesn't verify that _both_ DEC limits have been written - these should always be stored as a pair
updateInt32(DEC_LOWER_LIMIT_ADDR, static_cast<int32_t>(roundf(decLowerLimit * 100.0f)));
updateFlagsExtended(DEC_LIMIT_MARKER_FLAG);
commit(); // Complete the transaction
}
// Return the stored DEC Upper Limit (slew microsteps relative to home).
// If it is not present then the default value of 0 steps (limits are disabled).
float EEPROMStore::getDECUpperLimit()
{
float decUpperLimit(0); // limit angle (deg)
// Note that flags doesn't verify that _both_ DEC limits have been written - these should always be stored as a pair
if (isPresentExtended(DEC_LIMIT_MARKER_FLAG))
{
decUpperLimit = readInt32(DEC_UPPER_LIMIT_ADDR) / 100.0f;
LOG(DEBUG_EEPROM, "[EEPROM]: DEC upper limit read as %f", decUpperLimit);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored values for DEC limits");
}
return decUpperLimit; // limit angle (deg)
}
// Store the configured DEC Upper Limit Pos (slew microsteps relative to home).
void EEPROMStore::storeDECUpperLimit(float decUpperLimit)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Write: Updating DEC Upper Limit to %l", decUpperLimit);
// Note that flags doesn't verify that _both_ DEC limits have been written - these should always be stored as a pair
updateInt32(DEC_UPPER_LIMIT_ADDR, static_cast<int32_t>(roundf(decUpperLimit * 100.0f)));
updateFlagsExtended(DEC_LIMIT_MARKER_FLAG);
commit(); // Complete the transaction
}
// Get the configured RA Homing offset for Hall sensor homing (slew microsteps relative to home).
int32_t EEPROMStore::getRAHomingOffset()
{
int32_t raHomingOffset(0); // microsteps (slew)
if (isPresentExtended(RA_HOMING_MARKER_FLAG))
{
raHomingOffset = readInt32(RA_HOMING_OFFSET_ADDR);
LOG(DEBUG_EEPROM, "[EEPROM]: RA Homing offset read as %l", raHomingOffset);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored values for RA Homing offset");
}
return raHomingOffset; // microsteps (slew)
}
// Get the configured DEC Homing offset for Hall sensor homing (slew microsteps relative to home).
int32_t EEPROMStore::getDECHomingOffset()
{
int32_t decHomingOffset(0); // microsteps (slew)
if (isPresentExtended(DEC_HOMING_MARKER_FLAG))
{
decHomingOffset = readInt32(DEC_HOMING_OFFSET_ADDR);
LOG(DEBUG_EEPROM, "[EEPROM]: DEC Homing offset read as %l", decHomingOffset);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored values for DEC Homing offset");
}
return decHomingOffset; // microsteps (slew)
}
// Store the configured RA Homing offset for Hall sensor homing (slew microsteps relative to home).
void EEPROMStore::storeRAHomingOffset(int32_t raHomingOffset)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Write: Updating RA Homing offset to %l", raHomingOffset);
updateInt32(RA_HOMING_OFFSET_ADDR, raHomingOffset);
updateFlagsExtended(RA_HOMING_MARKER_FLAG);
commit(); // Complete the transaction
}
// Store the configured DEC Homing offset for Hall sensor homing (slew microsteps relative to home).
void EEPROMStore::storeDECHomingOffset(int32_t decHomingOffset)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Write: Updating DEC Homing offset to %l", decHomingOffset);
updateInt32(DEC_HOMING_OFFSET_ADDR, decHomingOffset);
updateFlagsExtended(DEC_HOMING_MARKER_FLAG);
commit(); // Complete the transaction
}
// Get the current AZ position from home (in steps)
int32_t EEPROMStore::getAZPosition()
{
int32_t azPosition(0); // microsteps (slew)
if (isPresentExtended(AZ_POSITION_MARKER_FLAG))
{
azPosition = readInt32(AZ_POSITION_ADDR);
LOG(DEBUG_EEPROM, "[EEPROM]: AZ position read as %l", azPosition);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored values for AZ position");
}
return azPosition; // microsteps (slew)
}
// Store the current AZ position (in steps)
void EEPROMStore::storeAZPosition(int32_t azPosition)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Write: Updating AZ Position to %l", azPosition);
updateInt32(AZ_POSITION_ADDR, azPosition);
updateFlagsExtended(AZ_POSITION_MARKER_FLAG);
commit(); // Complete the transaction
}
// Get the current ALT position from home (in steps)
int32_t EEPROMStore::getALTPosition()
{
int32_t altPosition(0); // microsteps (slew)
if (isPresentExtended(ALT_POSITION_MARKER_FLAG))
{
altPosition = readInt32(ALT_POSITION_ADDR);
LOG(DEBUG_EEPROM, "[EEPROM]: ALT position read as %l", altPosition);
}
else
{
LOG(DEBUG_EEPROM, "[EEPROM]: No stored values for ALT position");
}
return altPosition; // microsteps (slew)
}
// Store the current ALT position in steps
void EEPROMStore::storeALTPosition(int32_t altPosition)
{
LOG(DEBUG_EEPROM, "[EEPROM]: Write: Updating ALT Position to %l", altPosition);
updateInt32(ALT_POSITION_ADDR, altPosition);
updateFlagsExtended(ALT_POSITION_MARKER_FLAG);
commit(); // Complete the transaction
}