-
Notifications
You must be signed in to change notification settings - Fork 23
/
params.c
357 lines (311 loc) · 8.44 KB
/
params.c
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
/*
* This file is part of the W1209 firmware replacement project
* (https://github.com/mister-grumbler/w1209-firmware).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Control functions for EEPROM storage of persistent application parameters.
*
* The list of aplication parameters with default values:
* Name |Def| Description
* -----+---+---------------------------------------------
* P0 - | C | Cooling/Heating
* (relay ON when temperature is over(C)/below(H) threshold value)
* P1 - | 2 | 0.1 ... 15.0 - Hysteresis
* P2 - |110| 110 ... -45 - Maximum allowed temperature value
* P3 - |-50| -50 ... 105 Minimum allowed temperature value
* P4 - | 0 | 7.0 ... -7.0 Correction of temperature value
* P5 - | 0 | 0 ... 10 Relay switching delay in minutes
* P6 - |Off| On/Off Indication of overheating
* TH - | 28| Threshold value
*/
#include "params.h"
#include "stm8s003/prom.h"
#include "buttons.h"
/* Definitions for EEPROM */
#define EEPROM_BASE_ADDR 0x4000
#define EEPROM_PARAMS_OFFSET 100
static unsigned char paramId;
static int paramCache[10];
const int paramMin[] = {0, 1, -45, -50, -70, 0, 0, 0, 0, -500};
const int paramMax[] = {1, 150, 110, 105, 70, 10, 1, 0, 0, 1100};
const int paramDefault[] = {0, 20, 110, -50, 0, 0, 0, 0, 0, 280};
/**
* @brief Check values in the EEPROM to be correct then load them into
* parameters' cache.
*/
void initParamsEEPROM()
{
if (getButton2() && getButton3() ) {
// Restore parameters to default values
for (paramId = 0; paramId < 10; paramId++) {
paramCache[paramId] = paramDefault[paramId];
}
storeParams();
} else {
// Load parameters from EEPROM
for (paramId = 0; paramId < 10; paramId++) {
paramCache[paramId] = * (int*) (EEPROM_BASE_ADDR + EEPROM_PARAMS_OFFSET
+ (paramId * sizeof paramCache[0]) );
}
}
paramId = 0;
}
/**
* @brief
* @param id
* @return
*/
int getParamById (unsigned char id)
{
if (id < 10) {
return paramCache[id];
}
return -1;
}
/**
* @brief
* @param id
* @param val
*/
void setParamById (unsigned char id, int val)
{
if (id < 10) {
paramCache[id] = val;
}
}
/**
* @brief
* @return
*/
int getParam()
{
return paramCache[paramId];
}
/**
* @brief
* @param val
*/
void setParam (int val)
{
paramCache[paramId] = val;
}
/**
* @brief Incrementing the value of the currently selected parameter.
*/
void incParam()
{
if (paramId == PARAM_RELAY_MODE || paramId == PARAM_OVERHEAT_INDICATION) {
paramCache[paramId] = ~paramCache[paramId] & 0x0001;
} else if (paramCache[paramId] < paramMax[paramId]) {
paramCache[paramId]++;
}
}
/**
* @brief Decrementing the value of the currently selected parameter.
*/
void decParam()
{
if (paramId == PARAM_RELAY_MODE || paramId == PARAM_OVERHEAT_INDICATION) {
paramCache[paramId] = ~paramCache[paramId] & 0x0001;
} else if (paramCache[paramId] > paramMin[paramId]) {
paramCache[paramId]--;
}
}
/**
* @brief
* @return
*/
unsigned char getParamId()
{
return paramId;
}
/**
* @brief
* @param val
*/
void setParamId (unsigned char val)
{
if (val < 10) {
paramId = val;
}
}
/**
* @brief
*/
void incParamId()
{
if (paramId < 6) {
paramId++;
} else {
paramId = 0;
}
}
/**
* @brief
*/
void decParamId()
{
if (paramId > 0) {
paramId--;
} else {
paramId = 6;
}
}
/**
* @brief Converts the current value of the selected parameter to a string.
* @param id
* The identifier of the parameter to be processed.
* @param strBuff
* A pointer to a string buffer where the result should be placed.
*/
void paramToString (unsigned char id, unsigned char* strBuff)
{
switch (id) {
case PARAM_RELAY_MODE:
if (paramCache[id]) {
( (unsigned char*) strBuff) [0] = 'H';
} else {
( (unsigned char*) strBuff) [0] = 'C';
}
( (unsigned char*) strBuff) [1] = 0;
break;
case PARAM_RELAY_HYSTERESIS:
itofpa (paramCache[id], strBuff, 0);
break;
case PARAM_MAX_TEMPERATURE:
itofpa (paramCache[id], strBuff, 6);
break;
case PARAM_MIN_TEMPERATURE:
itofpa (paramCache[id], strBuff, 6);
break;
case PARAM_TEMPERATURE_CORRECTION:
itofpa (paramCache[id], strBuff, 0);
break;
case PARAM_RELAY_DELAY:
itofpa (paramCache[id], strBuff, 6);
break;
case PARAM_OVERHEAT_INDICATION:
( (unsigned char*) strBuff) [0] = 'O';
if (paramCache[id]) {
( (unsigned char*) strBuff) [1] = 'N';
( (unsigned char*) strBuff) [2] = ' ';
} else {
( (unsigned char*) strBuff) [1] = 'F';
( (unsigned char*) strBuff) [2] = 'F';
}
( (unsigned char*) strBuff) [3] = 0;
break;
case PARAM_THRESHOLD:
itofpa (paramCache[id], strBuff, 0);
break;
default: // Display "OFF" to all unknown ID
( (unsigned char*) strBuff) [0] = 'O';
( (unsigned char*) strBuff) [1] = 'F';
( (unsigned char*) strBuff) [2] = 'F';
( (unsigned char*) strBuff) [3] = 0;
}
}
/**
* @brief Stores updated parameters from paramCache into EEPROM.
*/
void storeParams()
{
unsigned char i;
// Check if the EEPROM is write-protected. If it is then unlock the EEPROM.
if ( (FLASH_IAPSR & 0x08) == 0) {
FLASH_DUKR = 0xAE;
FLASH_DUKR = 0x56;
}
// Write to the EEPROM parameters which value is changed.
for (i = 0; i < 10; i++) {
if (paramCache[i] != (* (int*) (EEPROM_BASE_ADDR + EEPROM_PARAMS_OFFSET
+ (i * sizeof paramCache[0]) ) ) ) {
* (int*) (EEPROM_BASE_ADDR + EEPROM_PARAMS_OFFSET
+ (i * sizeof paramCache[0]) ) = paramCache[i];
}
}
// Now write protect the EEPROM.
FLASH_IAPSR &= ~0x08;
}
/**
* @brief
* @param val
* @param offset
*/
static void writeEEPROM (unsigned char val, unsigned char offset)
{
// Check if the EEPROM is write-protected. If it is then unlock the EEPROM.
if ( (FLASH_IAPSR & 0x08) == 0) {
FLASH_DUKR = 0xAE;
FLASH_DUKR = 0x56;
}
// Write the data to the EEPROM.
(* (unsigned char*) (EEPROM_BASE_ADDR + offset) ) = val;
// Now write protect the EEPROM.
FLASH_IAPSR &= ~0x08;
}
/**
* @brief Construction of a string representation of the given value.
* To emulate a floating-point value, a decimal point can be inserted
* before a certain digit.
* When the decimal point is not needed, set pointPosition to 6 or more.
* @param val
* the value to be processed.
* @param str
* pointer to buffer for constructed string.
* @param pointPosition
* put the decimal point in front of specified digit.
*/
void itofpa (int val, unsigned char* str, unsigned char pointPosition)
{
unsigned char i, l, buffer[] = {0, 0, 0, 0, 0, 0};
bool minus = false;
// No calculation is required for zero value
if (val == 0) {
( (unsigned char*) str) [0] = '0';
( (unsigned char*) str) [1] = 0;
return;
}
// Correction for processing of negative value
if (val < 0) {
minus = true;
val = -val;
}
// Forming the reverse string
for (i = 0; val != 0; i++) {
buffer[i] = '0' + (val % 10);
if (i == pointPosition) {
i++;
buffer[i] = '.';
}
val /= 10;
}
// Add leading '0' in case of ".x" result
if (buffer[i - 1] == '.') {
buffer[i] = '0';
i++;
}
// Add '-' sign for negative values
if (minus) {
buffer[i] = '-';
i++;
}
// Reversing to get the result string
for (l = i; i > 0; i--) {
( (unsigned char*) str) [l - i] = buffer[i - 1];
}
// Put null at the end of string
( (unsigned char*) str) [l] = 0;
}