/
TemperatureFormats.cpp
388 lines (348 loc) · 10.9 KB
/
TemperatureFormats.cpp
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
/* Copyright (C) 2019 Lee C. Bussy (@LBussy)
This file is part of LBussy's BrewPi Firmware Remix (BrewPi-Firmware-RMX).
BrewPi Firmware RMX 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, either version 3 of the
License, or (at your option) any later version.
BrewPi Firmware RMX 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 BrewPi Firmware RMX. If not, see <https://www.gnu.org/licenses/>.
These scripts were originally a part of firmware, a part of
the BrewPi project. Legacy support (for the very popular Arduino
controller) seems to have been discontinued in favor of new hardware.
All credit for the original firmware goes to @elcojacobs,
@m-mcgowan, @elnicoCZ, @ntfreak, @Gargy007 and I'm sure many more
contributors around the world. My apologies if I have missed anyone;
those were the names listed as contributors on the Legacy branch.
See: 'original-license.md' for notes about the original project's
license and credits. */
#include "Brewpi.h"
#include "TemperatureFormats.h"
#include "Platform.h"
#include "TempControl.h"
#include <string.h>
#include <stdarg.h>
// See header file for details about the temp format used.
// result can have maximum length of : sign + 3 digits integer part + point + 3 digits fraction part + '\0' = 9 bytes;
// only 1, 2 or 3 decimals allowed.
// returns pointer to the string
// long_temperature is used to prevent overflow
char *tempToString(char *s, long_temperature rawValue, uint8_t numDecimals, uint8_t maxLength)
{
if (isDisabledOrInvalid(rawValue))
{
strcpy_P(s, PSTR("null"));
return s;
}
rawValue = convertFromInternalTemp(rawValue);
return fixedPointToString(s, rawValue, numDecimals, maxLength);
}
char *tempDiffToString(char *s, long_temperature rawValue, uint8_t numDecimals, uint8_t maxLength)
{
rawValue = convertFromInternalTempDiff(rawValue);
return fixedPointToString(s, rawValue, numDecimals, maxLength);
}
char *fixedPointToString(char *s, temperature rawValue, uint8_t numDecimals, uint8_t maxLength)
{
return fixedPointToString(s, long_temperature(rawValue), numDecimals, maxLength);
}
// this gets rid of snprintf_P
void mysnprintf_P(char *buf, int len, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vsnprintf_P(buf, len, fmt, args);
va_end(args);
}
char *fixedPointToString(char *s, long_temperature rawValue, uint8_t numDecimals, uint8_t maxLength)
{
s[0] = ' ';
if (rawValue < 0l)
{
s[0] = '-';
rawValue = -rawValue;
}
int intPart = rawValue >> TEMP_FIXED_POINT_BITS; // do not use longTempDiffToInt because it rounds up
uint16_t fracPart;
const char *fmt;
uint16_t scale;
switch (numDecimals)
{
case 1:
fmt = PSTR("%d.%01d");
scale = 10;
break;
case 2:
fmt = PSTR("%d.%02d");
scale = 100;
break;
default:
fmt = PSTR("%d.%03d");
scale = 1000;
}
fracPart = ((rawValue & TEMP_FIXED_POINT_MASK) * scale + TEMP_FIXED_POINT_SCALE / 2) >> TEMP_FIXED_POINT_BITS; // add 256 for rounding
if (fracPart >= scale)
{
intPart++;
fracPart = 0;
}
mysnprintf_P(&s[1], maxLength - 1, fmt, intPart, fracPart);
return s;
}
bool stringToTemp(temperature *result, const char *numberString)
{
if (0 == strcmp(PSTR("null"), numberString))
{
*result = DISABLED_TEMP;
return true;
}
long_temperature longResult;
if (stringToFixedPoint(&longResult, numberString))
{
*result = constrainTemp16(convertToInternalTemp(longResult));
return true;
}
return false;
}
bool stringToTempDiff(temperature *result, const char *numberString)
{
long_temperature longResult;
if (stringToFixedPoint(&longResult, numberString))
{
longResult = convertToInternalTempDiff(longResult);
*result = constrainTemp16(longResult);
return true;
}
return false;
}
bool invalidStrtolResult(const char *start, const char *end)
{
return ((*end != '\0' && *end != '.' && *end != ' ') // parsing did not end at end of string, space or decimal point
|| start == end); // no number found in string
}
bool stringToFixedPoint(long_temperature *result, const char *numberString)
{
// receive new temperature as null terminated string: "19.20"
long_temperature newValue;
long_temperature decimalValue = 0;
char *decimalPtr;
char *end;
// Check if - is in the string
bool positive = (0 == strchr(numberString, '-'));
newValue = strtol_impl(numberString, &end); // convert string to integer
if (invalidStrtolResult(numberString, end))
{
return false; // string was not valid
}
newValue = newValue << TEMP_FIXED_POINT_BITS; // shift to fixed point
// find the point in the string to know whether we have decimals
decimalPtr = strchr(numberString, '.'); // returns pointer to the point.
if (decimalPtr != 0)
{
decimalPtr++; // skip decimal point
// convert decimals to integer
decimalValue = strtol_impl(decimalPtr, &end) << TEMP_FIXED_POINT_BITS;
if (invalidStrtolResult(decimalPtr, end))
{
return false; // string was not valid
}
uint8_t charsAfterPoint = end - decimalPtr; // actually used # digits after point
while (charsAfterPoint-- > 0)
{
decimalValue = (decimalValue + 5) / 10;
}
}
*result = positive ? newValue + decimalValue : newValue - decimalValue;
return true;
}
bool stringToFixedPoint(temperature *result, const char *numberString)
{
long_temperature longResult;
if (stringToFixedPoint(&longResult, numberString))
{
*result = constrainTemp16(longResult);
return true;
}
return false;
}
bool stringToBool(bool *result, const char *numberString)
{
char *end;
if (0 == strcmp(PSTR("true"), numberString))
{
*result = true;
return true;
}
if (0 == strcmp(PSTR("false"), numberString))
{
*result = false;
return true;
}
uint8_t newValue = strtol_impl(numberString, &end); // only accept 0 and 1
if (invalidStrtolResult(numberString, end))
{ // no number found in string
return false;
}
if (newValue > 1)
{
return false;
}
*result = newValue;
return true;
}
// returns UINT16_MIN on failure
bool stringToUint16(uint16_t *result, const char *numberString)
{
char *end;
long newValue;
newValue = strtol_impl(numberString, &end); // only accept 0 and 1
if (invalidStrtolResult(numberString, end))
{ // no number found in string
return false;
}
if (newValue > UINT16_MAX)
{
*result = UINT16_MAX; // clip to max
}
else if (newValue < 0)
{
return false; // negative values are invalid
}
else
{
*result = newValue;
}
return true;
}
// convertToInternalTemp receives the external temp format in fixed point and converts it to the internal format
// It scales the value for Fahrenheit and adds the offset needed for absolute temperatures. For temperature differences, use no offset.
long_temperature convertToInternalTempImpl(long_temperature rawTemp, bool addOffset)
{
if (tempControl.cc.tempFormat == 'F')
{ // value received is in F, convert to C
if (addOffset)
{
rawTemp = rawTemp - (temperature(32) << TEMP_FIXED_POINT_BITS);
}
int8_t rounder = (rawTemp < 0) ? -45 : 45;
rawTemp = (rawTemp * 50 + rounder) / 90; // rounded result
}
if (addOffset)
{
rawTemp += C_OFFSET;
}
return rawTemp;
}
// convertAndConstrain adds an offset, then scales with *9/5 for Fahrenheit. Use it without the offset argument for temperature differences
long_temperature convertFromInternalTempImpl(long_temperature rawTemp, bool addOffset)
{
if (tempControl.cc.tempFormat == 'F')
{ // value received is in F, convert to C
if (addOffset)
{
rawTemp -= F_OFFSET;
}
int8_t rounder = (rawTemp < 0) ? -25 : 25;
rawTemp = (rawTemp * 90 + rounder) / 50; // round result
}
else if (addOffset)
{
rawTemp -= C_OFFSET;
}
return rawTemp;
}
int fixedToTenths(long_temperature temp)
{
temp = convertFromInternalTemp(temp);
temperature rounder = (temp < 0) ? -TEMP_FIXED_POINT_SCALE / 2 : TEMP_FIXED_POINT_SCALE / 2;
return (10 * temp + rounder) / TEMP_FIXED_POINT_SCALE; // return rounded result in tenth of degrees
}
temperature tenthsToFixed(int temp)
{
long_temperature fixedPointTemp = convertToInternalTemp(((long_temperature)temp * TEMP_FIXED_POINT_SCALE + 5) / 10);
return constrainTemp16(fixedPointTemp);
}
temperature constrainTemp(long_temperature valLong, temperature lower, temperature upper)
{
temperature val = constrainTemp16(valLong);
if (val < lower)
{
return lower;
}
if (val > upper)
{
return upper;
}
return temperature(valLong);
}
temperature constrainTemp16(long_temperature val)
{
if (val < MIN_TEMP)
{
return MIN_TEMP;
}
if (val > MAX_TEMP)
{
return MAX_TEMP;
}
return val;
}
temperature multiplyFactorTemperatureLong(temperature factor, long_temperature b)
{
return constrainTemp16(((long_temperature)factor * (b - C_OFFSET)) >> TEMP_FIXED_POINT_BITS);
}
temperature multiplyFactorTemperatureDiffLong(temperature factor, long_temperature b)
{
return constrainTemp16(((long_temperature)factor * b) >> TEMP_FIXED_POINT_BITS);
}
temperature multiplyFactorTemperature(temperature factor, temperature b)
{
return constrainTemp16(((long_temperature)factor * ((long_temperature)b - C_OFFSET)) >> TEMP_FIXED_POINT_BITS);
}
temperature multiplyFactorTemperatureDiff(temperature factor, temperature b)
{
return constrainTemp16(((long_temperature)factor * (long_temperature)b) >> TEMP_FIXED_POINT_BITS);
}
long int my_strtol(const char *str, char **tail)
{
long val = 0;
bool positive = true;
*tail = (char *)str;
uint16_t read = 0;
while (1)
{
if (**tail == '\0')
{
break;
}
else if (**tail == ' ')
{
if (read != 0)
{
break; // only leading zeros allowed
}
}
else if (**tail == '-')
{
positive = false;
}
else if (**tail >= '0' && **tail <= '9')
{
val = val * 10 + (**tail - '0');
read++;
}
else
{
break;
}
(*tail)++;
}
if (read == 0)
{
*tail = (char *)str;
}
return positive ? val : -val;
}