-
Notifications
You must be signed in to change notification settings - Fork 2
/
Catena4430_cMeasurementLoopV1.cpp
284 lines (234 loc) · 7.64 KB
/
Catena4430_cMeasurementLoopV1.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
/*
Module: Catena4430_cMeasurementLoopV1.cpp
Function:
Class for transmitting accumulated measurements.
Copyright:
See accompanying LICENSE file for copyright and license information.
Author:
Dhinesh Kumar Pitchai, MCCI Corporation October 2022
*/
#include "Catena4430_cMeasurementLoopV1.h"
#include <Catena4430.h>
#include <arduino_lmic.h>
#include <Catena4430_Sensor.h>
using namespace McciCatena4430;
using namespace McciCatena;
extern cMeasurementLoop *gpMeasurementLoopConcrete;
void cMeasurementLoopV1::beginSensors(void)
{
if (this->m_BME280.begin(BME280_ADDRESS, Adafruit_BME280::OPERATING_MODE::Sleep))
{
this->m_fBme280 = true;
gCatena.SafePrintf("On-board Temperature Sensor found\n");
}
else
{
this->m_fBme280 = false;
gCatena.SafePrintf("No BME280 found: check wiring\n");
}
if (this->m_si1133.begin())
{
this->m_fSi1133 = true;
gpMeasurementLoopConcrete->m_fLowLight = true;
auto const measConfig = Catena_Si1133::ChannelConfiguration_t()
.setAdcMux(Catena_Si1133::InputLed_t::LargeWhite)
.setSwGainCode(7)
.setHwGainCode(4)
.setPostShift(1)
.set24bit(true);
this->m_si1133.configure(0, measConfig, 0);
gCatena.SafePrintf("On-board Light Sensor found\n");
}
else
{
this->m_fSi1133 = false;
gCatena.SafePrintf("No Si1133 found: check hardware\n");
}
}
bool cMeasurementLoopV1::takeMeasurements(void)
{
// start SI1133 measurement (one-time)
if (this->m_fSi1133)
this->m_si1133.start(true);
if (this->m_fBme280)
{
auto m = this->m_BME280.readTemperaturePressureHumidity();
this->m_data.env.Temperature = m.Temperature;
this->m_data.env.Pressure = m.Pressure;
this->m_data.env.Humidity = m.Humidity;
this->m_data.flags |= FlagsV1::TPH;
}
gpMeasurementLoopConcrete->setTimer(1000);
bool fSi1133Data = true;
while (fSi1133Data)
{
gIwdgTimer.refreshWatchdog();
if (this->m_si1133.isOneTimeReady())
{
fSi1133Data = false;
uint32_t data[1];
this->m_si1133.readMultiChannelData(data, 1);
this->m_si1133.stop();
this->m_data.flags |= FlagsV1::Light;
this->m_data.light.White = (float) data[0];
if (data[0] <= 500)
gpMeasurementLoopConcrete->m_fLowLight = true;
else
gpMeasurementLoopConcrete->m_fLowLight = false;
}
else if (gpMeasurementLoopConcrete->timedOut())
{
fSi1133Data = false;
this->m_si1133.stop();
if (gpMeasurementLoopConcrete->isTraceEnabled(gpMeasurementLoopConcrete->DebugFlags::kError))
gCatena.SafePrintf("S1133 timed out\n");
}
else
fSi1133Data = true;
}
return true;
}
bool cMeasurementLoopV1::clearMeasurements(void)
{
memset((void *) &this->m_data, 0, sizeof(this->m_data));
this->m_data.flags = FlagsV1(0);
}
bool cMeasurementLoopV1::formatMeasurements(
cMeasurementLoop::TxBuffer_t& b,
cMeasurementLoop::Measurement const &mData
)
{
auto const savedLed = gLed.Set(McciCatena::LedPattern::Off);
if (!(this->fDisableLED && this->m_fLowLight))
{
gLed.Set(McciCatena::LedPattern::Measuring);
}
if (this->fData_Vbat)
this->m_data.flags |= FlagsV1::Vbat;
if (this->fData_Version)
this->m_data.flags |= FlagsV1::Version;
if (this->fData_CO2)
this->m_data.flags |= FlagsV1::CO2ppm;
if (this->fData_BootCount)
this->m_data.flags |= FlagsV1::Boot;
if (this->fData_Activity)
this->m_data.flags |= FlagsV1::Activity;
if (this->fData_Pellet)
this->m_data.flags |= FlagsV1::Pellets;
// initialize the message buffer to an empty state
b.begin();
// insert format byte
b.put(this->kMessageFormat);
// insert the timestamp from the data
// stuff zero if time is not valid.
b.put4u(std::uint32_t(mData.DateTime.getGpsTime()));
// the flags in Measurement correspond to the over-the-air flags.
b.put(std::uint8_t(m_data.flags));
// send Vbat
if ((m_data.flags & FlagsV1::Vbat) != FlagsV1(0))
{
float Vbat = mData.Vbat;
gCatena.SafePrintf("Vbat: %d mV\n", (int) (Vbat * 1000.0f));
b.putV(Vbat);
}
// send firmware version
if ((m_data.flags & FlagsV1::Version) != FlagsV1(0))
{
b.put(mData.ver.Major);
b.put(mData.ver.Minor);
b.put(mData.ver.Patch);
b.put(mData.ver.Local);
}
// Vbus is shown as 5000 * v
float Vbus = mData.Vbus;
gCatena.SafePrintf("Vbus: %d mV\n", (int) (Vbus * 1000.0f));
// put co2ppm
if ((m_data.flags & FlagsV1::CO2ppm) != FlagsV1(0))
{
gCatena.SafePrintf(
"SCD30: T(C): %c%d.%02d RH(%%): %d.%02d CO2(ppm): %d.%02d\n",
this->ts, this->tint, this->tfrac,
this->rhint, this->rhfrac,
this->co2int, this->co2frac
);
b.put2u(TxBufferBase_t::f2uflt16(mData.co2ppm.CO2ppm / 40000.0f));
}
// send boot count
if ((m_data.flags & FlagsV1::Boot) != FlagsV1(0))
{
b.putBootCountLsb(mData.BootCount);
}
if ((m_data.flags & FlagsV1::TPH) != FlagsV1(0))
{
gCatena.SafePrintf(
"BME280: T: %d P: %d RH: %d\n",
(int) m_data.env.Temperature,
(int) m_data.env.Pressure,
(int) m_data.env.Humidity
);
b.putT(m_data.env.Temperature);
b.putP(m_data.env.Pressure);
// no method for 2-byte RH, directly encode it.
b.put2uf((m_data.env.Humidity / 100.0f) * 65535.0f);
}
// put light
if ((m_data.flags & FlagsV1::Light) != FlagsV1(0))
{
gCatena.SafePrintf(
"Si1133: %d White\n",
(int) m_data.light.White
);
b.putLux(TxBufferBase_t::f2uflt16(m_data.light.White / pow(2.0, 24)));
}
// put pellets
if ((m_data.flags & FlagsV1::Pellets) != FlagsV1(0))
{
for (unsigned i = 0; i < cMeasurementLoop::kMaxPelletEntries; ++i)
{
b.put2(mData.pellets[i].Total & 0xFFFFu);
b.put(mData.pellets[i].Recent);
}
}
// put activity
if ((m_data.flags & FlagsV1::Activity) != FlagsV1(0))
{
for (unsigned i = 0; i < mData.nActivity; ++i)
{
// scale to 0..1
float aAvg = mData.activity[i].Avg;
gCatena.SafePrintf(
"Activity[%u] [0..1000): %d Avg\n",
i,
500 + int(500 * aAvg)
);
b.put2uf(TxBufferBase_t::f2sflt16(aAvg));
}
}
if (!(this->fDisableLED && this->m_fLowLight))
gLed.Set(savedLed);
}
void cMeasurementLoopV1::writeVersionData(File dataFile)
{
if ((m_data.flags & FlagsV1::TPH) != FlagsV1(0))
{
dataFile.print(m_data.env.Temperature);
dataFile.print(',');
dataFile.print(m_data.env.Humidity);
dataFile.print(',');
dataFile.print(m_data.env.Pressure);
dataFile.print(',');
}
else
{
dataFile.print(",,,");
}
if ((m_data.flags & FlagsV1::Light) != FlagsV1(0))
{
dataFile.print(m_data.light.White);
dataFile.print(',');
}
else
{
dataFile.print(",");
}
}