forked from dennisMe2/CarbOnBal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
332 lines (292 loc) · 9.96 KB
/
utils.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
// This software, known as CarbOnBal is
// Copyright, 2017 L.L.M. (Dennis) Meulensteen. dennis@meulensteen.nl
//
// This file is part of CarbOnBal. A combination of software and hardware.
// I hope it may be of some help to you in balancing your carburetors and throttle bodies.
// Always be careful when working on a vehicle or electronic project like this.
// Your life and health are your sole responsibility, use wisely.
//
// CarbOnBal hardware is covered by the Cern Open Hardware License v1.2
// a copy of the text is incuded with the source code.
//
// CarbOnBal 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.
//
// CarbOnBal 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 CarbOnBal. If not, see <http://www.gnu.org/licenses/>.
#include "utils.h"
#include <Arduino.h>
#include "globals.h"
#include LANGUAGE
#include "lcdWrapper.h"
extern settings_t settings;
float millibarFactor = (P5VSENSOR - P0VSENSOR) / 1024.00; //conversion factor to convert the arduino readings to millibars
byte buttonState[NUM_BUTTONS] = { HIGH, HIGH, HIGH, HIGH }; //array for recording the state of buttons
byte buttonCount[NUM_BUTTONS] = { 0, 0, 0, 0 }; //array for recording the state of buttons
byte lastButtonState[NUM_BUTTONS] = { HIGH, HIGH, HIGH, HIGH }; //array for recording the previous state of buttons
unsigned long lastDebounceTime[NUM_BUTTONS]; //array for recording when the button press was first seen
unsigned long lastEntry = 0;
uint8_t debounceDelay = 200; //allow 200ms for switches to settle before they register
void setInterrupt(bool enabled) {
if (enabled) {
TIMSK1 |= (1 << OCIE1A);
} else {
TIMSK1 |= (0 << OCIE1A);
}
}
float convertToPreferredUnits(int value, int ambient) {
if (0 == settings.units)
return value;
if (1 == settings.units)
return ambient - value;
if (2 == settings.units)
return convertToMillibar(value);
if (3 == settings.units)
return convertToMillibar(ambient) - convertToMillibar(value);
if (4 == settings.units)
return convertToCmHg(value);
if (5 == settings.units)
return convertToCmHg(ambient) - convertToCmHg(value);
if (6 == settings.units)
return convertToInHg(value);
if (7 == settings.units)
return convertToInHg(ambient) - convertToInHg(value);
return 0; //error
}
float differenceToPreferredUnits(int value) {
if (0 == settings.units)
return value;
if (1 == settings.units)
return value;
if (2 == settings.units)
return differenceToMillibar(value);
if (3 == settings.units)
return differenceToMillibar(value);
if (4 == settings.units)
return differenceToCmHg(value);
if (5 == settings.units)
return differenceToCmHg(value);
if (6 == settings.units)
return differenceToInHg(value);
if (7 == settings.units)
return differenceToInHg(value);
return 0; //error
}
const char* unitsAsText() {
if (0 == settings.units)
return txtRawValues;
if (1 == settings.units)
return txtRawDescending;
if (2 == settings.units)
return txtMillibarHpa;
if (3 == settings.units)
return txtMillibarHpaDesc;
if (4 == settings.units)
return txtCmMercury;
if (5 == settings.units)
return txtCmMercuryDesc;
if (6 == settings.units)
return txtInchMercury;
if (7 == settings.units)
return txtInchMercuryDesc;
return 0;
}
//convert the arduino reading to millibars for display
float convertToMillibar(int value) {
return value * millibarFactor + P0VSENSOR; //convert reading and add the sensor's minimum pressure
}
float differenceToMillibar(int value) {
return value * millibarFactor; //convert reading and add the sensor's minimum pressure
}
//convert the arduino readings to centimeters of mercury
float convertToCmHg(int value) {
return convertToMillibar(value) * 0.075;
}
float differenceToCmHg(int value) {
return differenceToMillibar(value) * 0.075;
}
//convert the arduino readings to inches of mercury
float convertToInHg(int value) {
return convertToMillibar(value) * 0.02953;
}
float differenceToInHg(int value) {
return differenceToMillibar(value) * 0.02953;
}
//reset to factory defaults
settings_t fetchFactoryDefaultSettings() {
settings_t settings;
settings.silent = false;
settings.advanced = false;
settings.splashScreen = true;
settings.cylinders = 4;
settings.master = 1;
settings.button1 = 0;
settings.button2 = 0;
settings.button3 = 0;
settings.contrast = 10;
settings.brightness = 255;
settings.graphType = 0;
settings.rpmDamping = 10;
settings.units = 0;
settings.zoom = 0;
settings.calibrationMax = 32;
settings.damping = 8;
return settings;
}
void doContrast(int value) {
analogWrite(contrastPin, value);
}
void doBrightness(int value) {
analogWrite(brightnessPin, value);
}
void doHeldButtonAction(int button) {
switch (button) {
case CANCEL:
settings = fetchFactoryDefaultSettings();
doContrast(settings.contrast);
doBrightness(settings.brightness);
break;
}
}
// tests if a button was pressed and applies debounce logic
// this function assumes all buttons are input_pullup, active LOW, and contiguous pin numbers!
// this function does not use wait loops or other blocking functions which delay processing
int buttonPressed() {
int pressedButton = 0;
if (millis() - lastEntry < 50)
return 0; //checking more often that every 50ms is nonsense, just return
lastEntry = millis();
for (uint8_t button = SELECT; button <= CANCEL; button++) {
uint8_t buttonIndex = button - SELECT;
buttonState[buttonIndex] = digitalRead(button);
if ((millis() - lastDebounceTime[buttonIndex]) < debounceDelay)
return 0; //return if this button hasn't settled yet
lastDebounceTime[buttonIndex] = millis();
if (buttonState[buttonIndex] == RELEASED
&& lastButtonState[buttonIndex] == PRESSED) {
buttonCount[buttonIndex] = 0;
pressedButton = button;
} else if (buttonState[buttonIndex] == PRESSED
&& lastButtonState[buttonIndex] == PRESSED) {
buttonCount[buttonIndex]++;
if (button == LEFT)
pressedButton = LEFT;
if (button == RIGHT)
pressedButton = RIGHT;
if (buttonCount[buttonIndex] > 10) {
buttonCount[buttonIndex] = 0;
doHeldButtonAction(button);
}
}
lastButtonState[buttonIndex] = buttonState[buttonIndex];
}
return pressedButton; //just don't try to connect a button to pin 0
}
//creates a special character which is stored in the display's memory
void createWaitKeyPressChar() {
byte customChar[8] = { 0b00100, 0b00100, 0b10101, 0b01110, 0b00100, 0b00000,
0b01110, 0b11111 };
lcd_createChar(0, customChar);
}
void displayKeyPressPrompt() {
createWaitKeyPressChar();
lcd_setCursor(19, 0);
lcd_write(byte((byte) 0));
}
void waitForAnyKey() {
displayKeyPressPrompt();
while (!buttonPressed()) {
delay(50);
}
}
// used by switches which "short" the pin to ground, saves wiring a resistor per switch
void setInputActiveLow(int i) {
pinMode(i, INPUT);
digitalWrite(i, HIGH); // turn on internal pullups
}
// sets a pin to output, with internal pull-up resistors
void setOutputHigh(int i) {
pinMode(i, OUTPUT);
digitalWrite(i, HIGH); // turn on internal pullups
}
// calculate Extremely Fast Integer Exponentially weighted moving average for smoothing.
// factor is how much weight is given to new values vs the stored average as a power of 2.
// ie: 0 = 1:1 and 4 = 1/16th
// shift is used to get n bits of accuracy 'below zero' as it were 0 means no smoothing, more is exponentially (1/2^n) more smoothing
// average is a value in which to store the moving average;
// NOTE that this value is stored shifted 'shift' bits to the left and must be unshifted before use
// NOTE2 the shift WILL truncate if you overdo it, best used on 8-bit Bytes etc.
int intExponentialMovingAverage(int shift, int factor, int average, int input) {
average += ((input << shift) - average) >> factor;
return (average);
}
//slower than the int version but extremely accurate / sensitive
long longExponentialMovingAverage(int factor, long average, int input) {
longAverages longValue; //this insane union is used to save CPU cycles, instead of shifting bits 16x we just load the upper int in one go
longValue.intVal[0] = 0;
longValue.intVal[1] = input;
average += (longValue.longVal - average) >> factor;
return (average);
}
long mulExponentialMovingAverage(long average, int input) {
long weight = 1000;
average += (((long) input * 1000) - average) / weight;
return (average);
}
//need a performance benchmark
float floatExponentialMovingAverage(float weight, float average, int input) {
average += ((float) input - average) / weight;
return (average);
}
// calculate the absolute difference between two integers
int delta(int first, int second) {
if (first >= second) {
return first - second;
} else {
return second - first;
}
}
// return the highest value from a given array
unsigned int maxVal(unsigned int value[]) {
unsigned int maxValue = 0;
for (int index = 0; index < NUM_SENSORS; index++) {
if (value[index] > maxValue) {
maxValue = value[index];
}
}
return maxValue;
}
// return the lowest value from a given array
unsigned int minVal(unsigned int value[]) {
unsigned int minValue = 20000;
for (int index = 0; index < NUM_SENSORS; index++) {
if (value[index] < minValue) {
minValue = value[index];
}
}
return minValue;
}
//Free memory routine from the Arduino playground
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else // __ARM__
extern char *__brkval;
#endif // __arm__
int freeMemory() {
char top;
#ifdef __arm__
return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
return &top - __brkval;
#else // __arm__
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif // __arm__
}