-
Notifications
You must be signed in to change notification settings - Fork 1
/
clair.cpp
191 lines (152 loc) · 6.38 KB
/
clair.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
#include "clair.h"
#include "sensor.h"
#include "debug.h"
#include <algorithm>
typedef struct {
uint16_t samplingPeriodSeconds;
uint8_t samplesPerMessage;
} transmission_config_t;
// the order of this array corresponds to the LMIC DR_XX enums
static transmission_config_t transmission_configs[] = {
{ .samplingPeriodSeconds = 855, .samplesPerMessage = 5 }, // SF12+
{ .samplingPeriodSeconds = 534, .samplesPerMessage = 4 }, // SF11+
{ .samplingPeriodSeconds = 214, .samplesPerMessage = 5 }, // SF10+
{ .samplingPeriodSeconds = 178, .samplesPerMessage = 3 }, // SF9+
{ .samplingPeriodSeconds = 134, .samplesPerMessage = 2 }, // SF8
{ .samplingPeriodSeconds = 75, .samplesPerMessage = 2 }, // SF7
{ .samplingPeriodSeconds = 38, .samplesPerMessage = 2 } // SF7/B
};
#define NROF_TRANSMISSION_CONFIGS (sizeof(transmission_configs) / sizeof(transmission_configs[0]))
Clair::Clair(Sensor *sensorArg) {
sensor = sensorArg;
currentDatarate = 0; // SF12
secondsSinceLastSample = 0;
numberOfSamplesInBuffer = 0;
indexOfNextSampleInMinuteBuffer = 0;
}
bool Clair::setup() {
return sensor->setup();
}
#define NROF_SAMPLES_IN_MINUTE_BUFFER (sizeof(minuteBuffer) / sizeof(minuteBuffer[0]))
void Clair::addSampleToMinuteBuffer(clair_sample_t sample) {
minuteBuffer[indexOfNextSampleInMinuteBuffer] = sample;
indexOfNextSampleInMinuteBuffer = (indexOfNextSampleInMinuteBuffer + 1) % NROF_SAMPLES_IN_MINUTE_BUFFER;
}
clair_sample_t Clair::getAverageSampleOfLastMinute() {
uint32_t sumOfCo2ppms = 0;
float sumOfTemperatures = 0;
float sumOfHumidities = 0;
for (uint16_t i = 0; i < NROF_SAMPLES_IN_MINUTE_BUFFER; i++) {
sumOfCo2ppms += minuteBuffer[i].co2ppm;
sumOfTemperatures += minuteBuffer[i].temperature;
sumOfHumidities += minuteBuffer[i].humidity;
}
clair_sample_t averageSample;
averageSample.co2ppm = (sumOfCo2ppms + (NROF_SAMPLES_IN_MINUTE_BUFFER / 2)) / NROF_SAMPLES_IN_MINUTE_BUFFER;
averageSample.temperature = sumOfTemperatures / NROF_SAMPLES_IN_MINUTE_BUFFER;
averageSample.humidity = sumOfHumidities / NROF_SAMPLES_IN_MINUTE_BUFFER;
return averageSample;
}
#define PRINT_SAMPLE(SAMPLE) do { \
PRINT(F("CO2: ")); \
PRINT(SAMPLE.co2ppm); \
PRINT(F(" ppm, temperature: ")); \
PRINT(SAMPLE.temperature); \
PRINT(F(" °C, humidity: ")); \
PRINT(SAMPLE.humidity); \
PRINTLN(F(" %")); \
} while (0)
int16_t Clair::getCO2Concentration() {
if (sensor->measurementFailed()) return -1;
clair_sample_t sample = sensor->sampleMeasurements();
PRINT(F("sample: "));
PRINT_SAMPLE(sample);
addSampleToMinuteBuffer(sample);
secondsSinceLastSample += CLAIR_MEASURING_PERIOD_SECS;
if (secondsSinceLastSample >= transmission_configs[currentDatarate].samplingPeriodSeconds) {
PRINTLN(F("adding average sample of last minute to message buffer"));
if (numberOfSamplesInBuffer == transmission_configs[currentDatarate].samplesPerMessage) {
PRINTLN(F("message overdue, discarding oldest sample"));
for (int i = 0; i < numberOfSamplesInBuffer - 1; i++) {
sampleBuffer[i] = sampleBuffer[i + 1];
}
numberOfSamplesInBuffer -= 1;
}
clair_sample_t averageSample = getAverageSampleOfLastMinute();
PRINT(F("average sample of last minute: "));
PRINT_SAMPLE(averageSample);
sampleBuffer[numberOfSamplesInBuffer] = averageSample;
numberOfSamplesInBuffer += 1;
PRINT(F("number of samples in buffer: "));
PRINTLN(numberOfSamplesInBuffer);
secondsSinceLastSample = 0;
}
return static_cast<int16_t>(sample.co2ppm);
}
void Clair::setCurrentDatarate(int datarate) {
if (datarate < 0 || datarate > (int) NROF_TRANSMISSION_CONFIGS) {
PRINT(F("WARNING: invalid datarate: ")); PRINTLN(datarate);
datarate = 0;
}
currentDatarate = datarate;
PRINT(F("current datarate: "));
PRINT_DATARATE(currentDatarate);
PRINTLN("");
PRINT(F("current sampling period [s]: ")); PRINTLN(transmission_configs[currentDatarate].samplingPeriodSeconds);
PRINT(F("current # of samples in message: ")); PRINTLN(transmission_configs[currentDatarate].samplesPerMessage);
}
bool Clair::isMessageDue() {
return numberOfSamplesInBuffer >= transmission_configs[currentDatarate].samplesPerMessage;
}
static uint8_t encodeCO2ppmByte(uint16_t co2ppm) {
co2ppm = std::min(co2ppm, static_cast<uint16_t>(5100));
return (co2ppm + 10) / 20; // apply proper rounding
}
static uint8_t encodeTemperatureByte(float temperature) {
temperature = std::max(temperature, static_cast<float>(.0));
temperature = std::min(temperature, static_cast<float>(31.0));
uint8_t quantizedTemperature = ((uint8_t) (temperature + 0.5)) & 0x1F;
return quantizedTemperature;
}
static uint8_t encodeHumidityByte(float humidity) {
humidity = std::max(humidity, static_cast<float>(10.0));
humidity = std::min(humidity, static_cast<float>(80.0));
uint8_t quantizedHumidity = (uint8_t) (humidity + 5) / 10; // quantize to 10 °K steps
quantizedHumidity -= 1; // start at 0
return quantizedHumidity & 0x7;
}
static void encodeSample(clair_sample_t sample, uint8_t *messageBuffer) {
messageBuffer[0] = encodeCO2ppmByte(sample.co2ppm);
messageBuffer[1] = 0;
messageBuffer[1] |= encodeTemperatureByte(sample.temperature) << 3; // 5 bits
messageBuffer[1] |= encodeHumidityByte(sample.humidity); // 3 bits
}
#define CLAIR_PROTOCOL_VERSION 0
#define CLAIR_MESSAGE_ID_SAMPLE_LIST 0
uint8_t Clair::encodeMessage(uint8_t *messageBuffer, uint16_t messageBufferSize) {
if (!isMessageDue()) return 0;
if (messageBufferSize < 1) return 0;
// encode header
uint8_t *bufferPosition = messageBuffer;
*bufferPosition = 0;
*bufferPosition |= CLAIR_PROTOCOL_VERSION << 6; // protocol version (2 bits)
*bufferPosition |= CLAIR_MESSAGE_ID_SAMPLE_LIST << 3; // message identifier (3 bits)
// NOTE: to use all bits of the message header to full capacity we transmit the number of samples - 1!
*bufferPosition |= transmission_configs[currentDatarate].samplesPerMessage - 1; // message header (3 bits)
bufferPosition += 1;
uint8_t messageLength = 1;
// encode samples
for (int i = 0; i < numberOfSamplesInBuffer; i++) {
messageLength += 2;
if (messageLength > messageBufferSize) {
PRINT(F("WARNING: message buffer size too small:"));
PRINTLN(messageBufferSize);
return 0;
}
encodeSample(sampleBuffer[i], bufferPosition);
bufferPosition += 2;
}
// reset sample buffer
numberOfSamplesInBuffer = 0;
return messageLength;
}