-
Notifications
You must be signed in to change notification settings - Fork 2
/
model4841-production-lorawan-cMeasurementLoop.h
322 lines (282 loc) · 9.84 KB
/
model4841-production-lorawan-cMeasurementLoop.h
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
/*
Module: model4841-production-lorawan-cMeasurementLoop.h
Function:
cMeasurementLoop definitions.
Copyright:
See accompanying LICENSE file for copyright and license information.
Author:
Terry Moore, MCCI Corporation July 2019
*/
#ifndef _model4841_production_lorawan_cMeasurementLoop_h_
# define _model4841_production_lorawan_cMeasurementLoop_h_
#pragma once
#include <Arduino.h>
#include <Wire.h>
#include <Catena.h>
#include <Catena_FSM.h>
#include <Catena_Led.h>
#include <Catena_Log.h>
#include <Catena_Mx25v8035f.h>
#include <Catena_PollableInterface.h>
#include <Catena_Timer.h>
#include <Catena_TxBuffer.h>
#include <Catena-SHT3x.h>
#include <Catena-PMS7003.h>
#include <Catena-PMS7003Hal-4630.h>
#include <mcciadk_baselib.h>
#include <sgpc3.h>
#include <stdlib.h>
#include <cstdint>
#ifndef ARDUINO_MCCI_CATENA_4630
# error "This sketch targets the MCCI Catena 4630"
#endif
extern McciCatena::Catena gCatena;
extern McciCatena::Catena::LoRaWAN gLoRaWAN;
extern McciCatena::StatusLed gLed;
/****************************************************************************\
|
| An object to represent the uplink activity
|
\****************************************************************************/
class cMeasurementLoop : public McciCatena::cPollableObject
{
public:
// constructor
cMeasurementLoop(
McciCatenaPMS7003::cPMS7003& pms7003,
McciCatenaSht3x::cSHT3x& TempRh,
SGPC3& Sgpc3Sensor
)
: m_Pms7003(pms7003)
, m_TempRh(TempRh)
, m_Sgpc3Sensor(Sgpc3Sensor)
, m_txCycleSec_Permanent(6 * 60) // default uplink interval
, m_txCycleSec(30) // initial uplink interval
, m_txCycleCount(10) // initial count of fast uplinks
{};
// neither copyable nor movable
cMeasurementLoop(const cMeasurementLoop&) = delete;
cMeasurementLoop& operator=(const cMeasurementLoop&) = delete;
cMeasurementLoop(const cMeasurementLoop&&) = delete;
cMeasurementLoop& operator=(const cMeasurementLoop&&) = delete;
enum class State : std::uint8_t
{
stNoChange = 0, // this name must be present: indicates "no change of state"
stInitial, // this name must be present: it's the starting state.
stInactive, // parked; not doing anything.
stSleeping, // active; sleeping between measurements
stWakePms, // wake the PM sensor
stMeasurePms, // make the PM measurements
stSleepPms, // sleep the PM sensor
stTransmit, // transmit data
stFinal, // this name must be present, it's the terminal state.
};
static constexpr const char *getStateName(State s)
{
switch (s)
{
case State::stNoChange: return "stNoChange";
case State::stInitial: return "stInitial";
case State::stInactive: return "stInactive";
case State::stSleeping: return "stSleeping";
case State::stWakePms: return "stWakePms";
case State::stMeasurePms: return "stMeasurePms";
case State::stSleepPms: return "stSleepPms";
case State::stTransmit: return "stTransmit";
case State::stFinal: return "stFinal";
default: return "<<unknown>>";
}
}
static constexpr uint8_t kUplinkPort = 1;
static constexpr uint8_t kMessageFormat = 0x21;
enum class Flags : uint8_t
{
Vbat = 1 << 0,
Vcc = 1 << 1,
Vbus = 1 << 2, // Vbus input
Boot = 1 << 3,
TH = 1 << 4, // temperature, humidity
PM = 1 << 5, // Particulate matter
Dust = 1 << 6, // Dust
TVOC = 1 << 7, // TVOC
};
static constexpr size_t kTxBufferSize = 36;
using TxBuffer_t = McciCatena::AbstractTxBuffer_t<kTxBufferSize>;
// initialize measurement FSM.
void begin();
void end();
McciCatenaPMS7003::cPMS7003Hal *getHal() const
{
return this->m_Pms7003.getHal();
}
void setTxCycleTime(
std::uint32_t txCycleSec,
std::uint32_t txCycleCount
)
{
this->m_txCycleSec = txCycleSec;
this->m_txCycleCount = txCycleCount;
this->m_UplinkTimer.setInterval(txCycleSec * 1000);
if (this->m_UplinkTimer.peekTicks() != 0)
this->m_fsm.eval();
}
std::uint32_t getTxCycleTime()
{
return this->m_txCycleSec;
}
virtual void poll() override;
void setTempRh(bool fEnable)
{
this->m_fTempRh = fEnable;
}
void setSgpc3(bool fEnable)
{
this->m_fSgpc3 = fEnable;
}
void setVbus(float Vbus)
{
this->m_fUsbPower = (Vbus > 3.0f);
}
// request that the measurement loop be active/inactive
void requestActive(bool fEnable);
private:
static constexpr unsigned kNumMeasurements = 10;
// evaluate the control FSM.
State fsmDispatch(State currentState, bool fEntry);
// set the timer
void setTimer(std::uint32_t ms)
{
this->m_timer_start = millis();
this->m_timer_delay = ms;
this->m_fTimerActive = true;
this->m_fTimerEvent = false;
}
void clearTimer()
{
this->m_fTimerActive = false;
this->m_fTimerEvent = false;
}
bool timedOut()
{
bool result = this->m_fTimerEvent;
this->m_fTimerEvent = false;
return result;
}
// sleep handling
void sleep();
bool checkDeepSleep();
void doSleepAlert(bool fDeepSleep);
void doDeepSleep();
void deepSleepPrepare();
void deepSleepRecovery();
// measurement handling
void resetMeasurement()
{
this->m_iMeasurement = 0;
this->m_measurement_received = false;
this->m_measurement_valid = false;
}
bool measurementAwake()
{
return this->m_measurement_received;
}
bool measurementComplete()
{
return this->m_iMeasurement >= kNumMeasurements;
}
static void measurementAvailable(
void *pUserData,
const McciCatenaPMS7003::cPMS7003::Measurements<std::uint16_t> *pData,
bool fWarmedUp
);
void processMeasurement(
const McciCatenaPMS7003::cPMS7003::Measurements<std::uint16_t> *pData,
bool fWarmedUp
);
void processOneMeasurement(
float &r,
std::uint16_t *pv
);
bool postProcess(
McciCatenaPMS7003::cPMS7003::Measurements<float> &results
);
void fillTxBuffer(TxBuffer_t &b);
void startTransmission(TxBuffer_t &b);
void sendBufferDone(bool fSuccess);
bool txComplete()
{
return this->m_txcomplete;
}
static std::uint16_t particle2uf(float v)
{
return McciCatena::TxBuffer_t::f2uflt16(v);
}
void updateTxCycleTime();
// instance data
McciCatena::cFSM <cMeasurementLoop, State>
m_fsm;
McciCatenaPMS7003::cPMS7003&
m_Pms7003;
McciCatenaSht3x::cSHT3x& m_TempRh;
SGPC3& m_Sgpc3Sensor;
// true if object is registered for polling.
bool m_registered : 1;
// true if object is running.
bool m_running : 1;
// true to request exit
bool m_exit : 1;
// true if in active uplink mode, false otehrwise.
bool m_active : 1;
// set true to request transition to active uplink mode; cleared by FSM
bool m_rqActive : 1;
// set true to request transition to inactive uplink mode; cleared by FSM
bool m_rqInactive : 1;
// set true if event timer times out
bool m_fTimerEvent : 1;
// set true while evenet timer is active.
bool m_fTimerActive : 1;
// set true when a measurement is received
bool m_measurement_received : 1;
// set true when sufficient valid measurements are received
bool m_measurement_valid : 1;
// set true when first warmed-up measurement is received.
bool m_fWarmedUp : 1;
// set true if USB power is present.
bool m_fUsbPower : 1;
// set true if Temperature/RH sensor (SHT3x) is present
bool m_fTempRh : 1;
// set true if SGPC3 sensor (Air Quality) is present
bool m_fSgpc3 : 1;
// set true while a transmit is pending.
bool m_txpending : 1;
// set true when a transmit completes.
bool m_txcomplete : 1;
// set true when a transmit complete with an error.
bool m_txerr : 1;
// set true when we've printed how we plan to sleep
bool m_fPrintedSleeping : 1;
// index of next measurement in array.
unsigned m_iMeasurement;
// the collection of arrays of PM measurements
McciCatenaPMS7003::cPMS7003::PmBins<std::uint16_t[kNumMeasurements]> m_Pm;
// the collection of arrays of dust measurements
McciCatenaPMS7003::cPMS7003::DustBins<std::uint16_t[kNumMeasurements]> m_Dust;
// uplink time control
McciCatena::cTimer m_UplinkTimer;
std::uint32_t m_txCycleSec;
std::uint32_t m_txCycleCount;
std::uint32_t m_txCycleSec_Permanent;
// for simple internal timer.
std::uint32_t m_timer_start;
std::uint32_t m_timer_delay;
};
static constexpr cMeasurementLoop::Flags operator| (const cMeasurementLoop::Flags lhs, const cMeasurementLoop::Flags rhs)
{
return cMeasurementLoop::Flags(uint8_t(lhs) | uint8_t(rhs));
};
static cMeasurementLoop::Flags operator|= (cMeasurementLoop::Flags &lhs, const cMeasurementLoop::Flags &rhs)
{
lhs = lhs | rhs;
return lhs;
};
#endif /* _model4841_production_lorawan_cMeasurementLoop_h_ */