forked from ardyesp/DLO-138
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zconfig.ino
160 lines (115 loc) · 3.9 KB
/
zconfig.ino
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
// zconfig: since we are referencing variables defined in other files
#define PREAMBLE_VALUE 2859
// ------------------------
void loadConfig(boolean reset) {
// ------------------------
DBG_PRINTLN("Loading stored config...");
if(EEPROM.init() != EEPROM_OK) {
loadDefaults();
return;
}
// read preamble
if(reset || (EEPROM.read(PARAM_PREAMBLE) != PREAMBLE_VALUE)) {
loadDefaults();
formatSaveConfig();
return;
}
// load all the parameters from EEPROM
uint16_t data;
data = EEPROM.read(PARAM_TIMEBASE);
setTimeBase(data);
// trigger type is not persisted
setTriggerType(TRIGGER_AUTO);
data = EEPROM.read(PARAM_TRIGDIR);
setTriggerRising(data == 1);
data = EEPROM.read(PARAM_XCURSOR);
xCursor = data;
data = EEPROM.read(PARAM_YCURSOR);
yCursors[0] = data;
data = EEPROM.read(PARAM_YCURSOR + 1);
yCursors[1] = data;
data = EEPROM.read(PARAM_YCURSOR + 2);
yCursors[2] = data;
data = EEPROM.read(PARAM_YCURSOR + 3);
yCursors[3] = data;
data = EEPROM.read(PARAM_WAVES);
waves[0] = data;
data = EEPROM.read(PARAM_WAVES + 1);
waves[1] = data;
data = EEPROM.read(PARAM_WAVES + 2);
waves[2] = data;
data = EEPROM.read(PARAM_WAVES + 3);
waves[3] = data;
data = EEPROM.read(PARAM_TLEVEL);
setTriggerLevel(data);
printStats = EEPROM.read(PARAM_STATS);
zeroVoltageA1 = EEPROM.read(PARAM_ZERO1);
zeroVoltageA2 = EEPROM.read(PARAM_ZERO2);
DBG_PRINTLN("Loaded config:");
DBG_PRINT("Timebase: ");DBG_PRINTLN(currentTimeBase);
DBG_PRINT("Trigger Rising: ");DBG_PRINTLN(triggerRising);
DBG_PRINT("Trigger Type: ");DBG_PRINTLN(triggerType);
DBG_PRINT("X Cursor Pos: ");DBG_PRINTLN(xCursor);
DBG_PRINT("Y Cursors: ");DBG_PRINT(yCursors[0]);DBG_PRINT(", ");DBG_PRINT(yCursors[1]);DBG_PRINT(", ");DBG_PRINT(yCursors[2]);DBG_PRINT(", ");DBG_PRINTLN(yCursors[3]);
DBG_PRINT("Waves: ");DBG_PRINT(waves[0]);DBG_PRINT(", ");DBG_PRINT(waves[1]);DBG_PRINT(", ");DBG_PRINT(waves[2]);DBG_PRINT(", ");DBG_PRINTLN(waves[3]);
DBG_PRINT("Trigger Level: ");DBG_PRINTLN(data);
DBG_PRINT("Print Stats: ");DBG_PRINTLN(printStats);
DBG_PRINT("Wave1 Zero: ");DBG_PRINTLN(zeroVoltageA1);
DBG_PRINT("Wave2 Zero: ");DBG_PRINTLN(zeroVoltageA2);
// check if EEPROM left enough space, or else invoke formatSaveConfig
}
// ------------------------
void loadDefaults() {
// ------------------------
DBG_PRINTLN("Loading defaults");
setTimeBase(T30US);
setTriggerRising(true);
setTriggerType(TRIGGER_AUTO);
setTriggerLevel(0);
// set x in the middle
xCursor = (NUM_SAMPLES - GRID_WIDTH)/2;
// set y in the middle
yCursors[0] = -70;
yCursors[1] = -90;
yCursors[2] = -110;
yCursors[3] = -130;
// show all waves
waves[0] = true;
waves[1] = true;
waves[2] = true;
waves[3] = true;
printStats = false;
zeroVoltageA1 = 1985;
zeroVoltageA2 = 1985;
}
// ------------------------
void formatSaveConfig() {
// ------------------------
DBG_PRINTLN("Formatting EEPROM");
EEPROM.format();
DBG_PRINTLN("Saving all config params....");
saveParameter(PARAM_PREAMBLE, PREAMBLE_VALUE);
saveParameter(PARAM_TIMEBASE, currentTimeBase);
saveParameter(PARAM_TRIGDIR, triggerRising);
saveParameter(PARAM_XCURSOR, xCursor);
saveParameter(PARAM_YCURSOR, yCursors[0]);
saveParameter(PARAM_YCURSOR + 1, yCursors[1]);
saveParameter(PARAM_YCURSOR + 2, yCursors[2]);
saveParameter(PARAM_YCURSOR + 3, yCursors[3]);
saveParameter(PARAM_WAVES, waves[0]);
saveParameter(PARAM_WAVES + 1, waves[1]);
saveParameter(PARAM_WAVES + 2, waves[2]);
saveParameter(PARAM_WAVES + 3, waves[3]);
saveParameter(PARAM_TLEVEL, trigLevel);
saveParameter(PARAM_STATS, printStats);
saveParameter(PARAM_ZERO1, zeroVoltageA1);
saveParameter(PARAM_ZERO2, zeroVoltageA2);
}
// ------------------------
void saveParameter(uint16_t param, uint16_t data) {
// ------------------------
uint16 status = EEPROM.write(param, data);
if(status != EEPROM_OK) {
DBG_PRINT("Unable to save param in EEPROM, code: ");DBG_PRINTLN(status);
}
}