-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathaltimetre_oled_bmp180_04.ino
389 lines (318 loc) · 8.94 KB
/
altimetre_oled_bmp180_04.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
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
#include <SFE_BMP180.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Button.h>
#include <EEPROM.h>
#include "EEPROMAnything.h"
// If using software SPI (the default case):
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
#define BUTTON1_PIN 4
#define HPA 0
#define METER 1
#define DEG 2
#define MVOLT 3
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
/* Uncomment this block to use hardware SPI
#define OLED_DC 6
#define OLED_CS 7
#define OLED_RESET 8
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
*/
extern uint8_t Font24x40[];
extern uint8_t Symbol[];
extern uint8_t Splash[];
extern uint8_t Battery[];
SFE_BMP180 pressure;
Button button1 = Button(BUTTON1_PIN,BUTTON_PULLDOWN);
boolean longPush = false;
int value, etat = 0;
double QNH, saveQNH;
double temperature, pression, altitude = 0;
double baseAltitude, saveBaseAltitude = 0;
double altiMin = 9999.0;
double altiMax = 0.0;
double lastValue = 0.0;
long lastVcc, vcc = 0;
int eepromAddr = 10;
#define MAX_SAMPLES 20
double samplesBuffer[MAX_SAMPLES];
int indexBfr = 0;
double averagePressure = 0;
boolean bufferReady = false;
int screen = 0; // numero d'ecran
#define NB_SCREENS 6
String debugMsg;
/* ------------------------------------ setup ------------------------------------------ */
void setup() {
button1.releaseHandler(handleButtonReleaseEvents);
button1.holdHandler(handleButtonHoldEvents,2000);
display.begin(SSD1306_SWITCHCAPVCC);
// init QNH
EEPROM_readAnything(eepromAddr, QNH);
if (isnan(QNH)) {
QNH = 1013.25;
EEPROM_writeAnything(eepromAddr, QNH); // QNH standard
}
saveQNH = QNH;
display.clearDisplay();
display.drawBitmap(0, 0, Splash, 128, 64, 1);
display.display();
delay(1000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
if (!pressure.begin()) {
display.println("Init fail !");
display.println("Turn OFF");
display.display();
while(1); // Pause forever.
}
button1.isPressed();
screen = 1;
}
/* ------------------------------------ loop ------------------------------------------ */
void loop() {
char status;
long vcc;
button1.isPressed();
// get pressure and temperature and calculate altitude
status = pressure.startTemperature();
if (status != 0) {
delay(status);
status = pressure.getTemperature(temperature);
if (status != 0) {
status = pressure.startPressure(0);
if (status != 0) {
delay(status);
status = pressure.getPressure(pression, temperature);
if (status != 0) {
savePressureSample(pression);
averagePressure = getPressureAverage();
if (bufferReady) {
altitude = pressure.altitude(averagePressure*100, QNH*100);
setAltiMinMax();
}
}
}
}
}
if (etat == 0) {
// init baseAltitude
if (baseAltitude == 0) {
baseAltitude = round(altitude);
saveBaseAltitude = baseAltitude;
}
// calculate QNH
if (baseAltitude != saveBaseAltitude) {
QNH = pressure.sealevel(pression, baseAltitude);
saveBaseAltitude = baseAltitude;
}
// Save QNH in EEPROM
if (QNH != saveQNH) {
saveQNH = QNH;
EEPROM_writeAnything(eepromAddr, QNH);
}
switch (screen) {
case 1: // Altitude
if (lastValue != altitude) {
showScreen("ALTITUDE", altitude, METER);
lastValue = altitude;
}
break;
case 2: // Altitude Max
if (lastValue != altiMax) {
showScreen("ALTITUDE MAX", altiMax, METER);
lastValue = altiMax;
}
break;
case 3: // Altitude Min
if (lastValue != altiMin) {
showScreen("ALTITUDE MIN", altiMin, METER);
lastValue = altiMin;
}
break;
case 4: // Pression
if (lastValue != pression) {
showScreen("PRESSION", pression, HPA);
lastValue = pression;
}
break;
case 5: // Temperature
if (lastValue != temperature) {
showScreen("TEMPERATURE", temperature, DEG);
lastValue = temperature;
}
break;
case 6: // Batterie
vcc = readVcc();
if (lastVcc != vcc) {
showScreen("BATTERIE", vcc, MVOLT);
lastVcc = vcc;
}
break;
}
}
else { // Settings
// Settings
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.println("CALIBRATION");
display.setCursor(0,15);
display.print("QNH : ");
display.print(QNH,2);
display.println(" hPa");
//display.print("Debug : ");
//display.println(debugMsg);
//display.print("Etat : ");
//display.println(etat);
if (etat == 1) drawSymbol(100, 40, 4); //UP
if (etat == 2) drawSymbol(100, 40, 5); // DOWN
if (value != 0) {
baseAltitude += value;
value = 0;
// recalcule le QNH
QNH = pressure.sealevel(pression, baseAltitude);
}
display.setCursor(0,45);
display.print("Alti : ");
display.println(baseAltitude, 0);
display.display();
}
delay(50);
}
/* -------------------- fonctions -------------------- */
// Affiche les données d'un ecran
void showScreen(String label, double value, int unit) {
display.clearDisplay();
showBatterylevel(readVcc());
display.setCursor(0,0);
display.println(label);
drawFloatValue(0, 20, value, unit);
display.display();
}
// Enregistre un echantillon de pression
void savePressureSample(float pressure) {
if (indexBfr == MAX_SAMPLES) {
indexBfr = 0;
bufferReady = true;
}
samplesBuffer[indexBfr++] = pressure;
}
// Retourne la moyenne des echantillons de pression
float getPressureAverage() {
double sum = 0;
for (int i =0; i<MAX_SAMPLES; i++) {
sum += samplesBuffer[i];
}
return sum/MAX_SAMPLES;
}
// Enregistre les altitudes Min & Max
void setAltiMinMax() {
if (altitude > altiMax) altiMax = altitude;
if (altitude < altiMin) altiMin = altitude;
}
void resetAltiMinMax() {
altiMax = altiMin = altitude;
}
// Gestion du bouton relaché
void handleButtonReleaseEvents(Button &btn) {
//debugMsg = "Release";
if (!longPush) {
if (etat != 0 ) { // Settings
if (etat == 1) value = 1;
if (etat == 2) value = -1;
}
else { // Change screen
screen++;
if (screen > NB_SCREENS) screen = 1;
lastValue = 0;
}
}
longPush = false;
}
// Gestion de l'appui prolongé sur le bouton
void handleButtonHoldEvents(Button &btn) {
//debugMsg = "Hold";
longPush = true;
//screen = 1;
value = 0;
if (screen == 1 && ++etat > 2) {
etat = 0;
}
else if (screen == 2 || screen == 3) {
resetAltiMinMax();
}
}
// Affiche un caractére en x, y
void drawCar(int sx, int sy, int num, uint8_t *font, int fw, int fh, int color) {
byte row;
for(int y=0; y<fh; y++) {
for(int x=0; x<(fw/8); x++) {
row = pgm_read_byte_near(font+x+y*(fw/8)+(fw/8)*fh*num);
for(int i=0;i<8;i++) {
if (bitRead(row, 7-i) == 1) display.drawPixel(sx+(x*8)+i, sy+y, color);
}
}
}
}
// Affiche un gros caractére en x, y
void drawBigCar(int sx, int sy, int num) {
drawCar(sx, sy, num, Font24x40, 24, 40, WHITE) ;
}
void drawDot(int sx, int sy, int h) {
display.fillRect(sx, sy-h, h, h, WHITE);
}
// Affiche un symbole en x, y
void drawSymbol(int sx, int sy, int num) {
drawCar(sx, sy, num, Symbol, 16, 16, WHITE) ;
}
// Affiche un nombre decimal
void drawFloatValue(int sx, int sy, double val, int unit) {
char charBuf[15];
if (val < 10000) {
dtostrf(val, 3, 1, charBuf);
int nbCar = strlen(charBuf);
if (nbCar > 5) { // pas de decimal
for (int n=0; n<4; n++) drawBigCar(sx+n*26, sy, charBuf[n]- '0');
drawSymbol(108,sy, unit);
}
else {
drawBigCar(sx+86, sy, charBuf[nbCar-1]- '0');
drawDot(78, sy+39, 6);
nbCar--;
if (--nbCar > 0) drawBigCar(sx+52, sy, charBuf[nbCar-1]- '0');
if (--nbCar > 0) drawBigCar(sx+26, sy, charBuf[nbCar-1]- '0');
if (--nbCar > 0) drawBigCar(sx, sy, charBuf[nbCar-1]- '0');
drawSymbol(112,sy, unit);
}
}
}
// Show battery level icon
void showBatterylevel(long vcc) {
if (vcc > 3600) display.drawBitmap(104, 1, Battery, 20, 7, 1);
else if (vcc > 3400) display.drawBitmap(104, 1, Battery+21, 20, 7, 1);
else if (vcc > 3200) display.drawBitmap(104, 1, Battery+42, 20, 7, 1);
else if (vcc > 3000) display.drawBitmap(104, 1, Battery+63, 20, 7, 1);
else display.drawBitmap(104, 1, Battery+84, 20, 7, 1);
}
// Read VCC
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}