-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensor.cpp
More file actions
61 lines (54 loc) · 1.55 KB
/
Sensor.cpp
File metadata and controls
61 lines (54 loc) · 1.55 KB
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
#include <Arduino.h>
#include <pocketBME280.h>
#include "Sensor.h"
// BME280: read pressure, temperature & humidity
// Developed with the Adafruit library, but I've made an effort to keep this all integer-based, fast and compact (eg anti-aliasing)
// So it bugged me that it dragged in the floating point library. A bit of googling and I found pocketBME280.
// https://github.com/angrest/pocketBME280
// Saves ~4k of program storage space
namespace Sensor
{
pocketBME280 bme;
void Init()
{
Wire.begin();
bme.begin();
}
int32_t Round(int32_t value, int divisor)
{
// return value/divisor, rounded
int32_t result = value/divisor;
if ((abs(value) % divisor) >= (divisor / 2))
result += (result < 0)?-1:+1;
return result;
}
int Round(uint32_t value, int divisor)
{
// return value/divisor, rounded
int result = value/divisor;
if ((value % divisor) >= (uint32_t)(divisor / 2))
result++;
return result;
}
bool Read(int& pressurehPa, int& temperatureC, int& humidityPercent)
{
// return true if read all three
bme.startMeasurement();
int ctr = 100; // avoid infinite loop
while (!bme.isMeasuring() && ctr--)
delay(1);
while (bme.isMeasuring() && ctr--)
delay(1);
if (!ctr)
return false;
temperatureC = Round(bme.getTemperature(), 100);
pressurehPa = Round(bme.getPressure(), 100);
humidityPercent = Round(bme.getHumidity(), 1024);
#if defined(DEMO) && defined(DEBUG)
temperatureC = 25;
pressurehPa = 1015;
humidityPercent = 32;
#endif
return true;
}
};