-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsensors.hpp
325 lines (248 loc) · 7.6 KB
/
sensors.hpp
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
// SPDX-FileCopyrightText: 2020-2021 Ivan Ivanov
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef _SENSOR_
#define _SENSOR_
// best case scenario is one file for each sensor and rule by makefiles, but it is arduino ide =)
//#define _USE_MLX90615_ 1 //
//#define _USE_LSM6DS3_ 1
#include "utils.h"
#include <TimeLib.h>
#include <WiFiUdp.h>
#include <WiFi.h>
#include <SoftWire.h>
#include "battery.h"
#include "version.h"
#include "i2c_bme280.h"
#include "i2c_lsm6ds3.h"
#include "i2c_ina219.h"
#include "i2c_mlx90615.h"
#include "i2c_max30100.h"
#define SENSOR_INITED4ULP(X,VAL) ulp_ ## X _inited = VAL
#define SENSOR_ERROR4ULP(X,VAL) ulp_ ## X _error = VAL
class Sensor {
private:
// int i2c_addr;
// virtual void read_data_direct();
protected:
int i2c_addr;
char *name;
// bool inited = false;
SoftWire *i2c;
int16_t *ulp_error;
virtual void init_direct();
bool testAddress();
uint8_t readRegister(uint8_t address);
void writeRegister(uint8_t address, uint8_t data);
void writeRegister(uint8_t address, uint16_t data);
uint8_t updateRegister(uint8_t address, uint8_t mask, uint8_t value);
uint8_t burstRead(uint8_t baseAddress, uint8_t *buffer, uint8_t length);
public:
bool init();
void sleep();
void wake();
void on();
void off();
void read();
};
//============================================================================================
class GyroscopeLSM6DS3 : public Sensor {
private:
int32_t steps;
void printUlpData();
public:
GyroscopeLSM6DS3();
~GyroscopeLSM6DS3();
void init(SoftWire *i2c);
void sleep();
void wake();
void read();
uint16_t getStepCount();
};
typedef GyroscopeLSM6DS3 Gyroscope;
//============================================================================================
class ThermoMeter: public Sensor {
private:
float AmbientTempC;
float ObjectTempC;
float raw_temp_to_C(uint16_t _t);
void printUlpData();
void wakeUp();
public:
ThermoMeter();
void init(SoftWire *i2c);
void sleep();
void wake();
void read();
float getAmbientC();
float getObjectC();
};
//============================================================================================
#define ALPHA 0.85 //dc filter alpha value
#define MEAN_FILTER_SIZE 15
struct dcFilter_t {
float w;
float result;
};
struct meanDiffFilter_t
{
float values[MEAN_FILTER_SIZE];
uint8_t index;
float sum;
uint8_t count;
};
struct butterworthFilter_t
{
float v[2];
float result;
};
class PulseMeter: public Sensor {
private:
// PulseOximeter pulse;
float heartRate;
float spO2;
dcFilter_t dcFilterIR;
meanDiffFilter_t meanDiffIR;
void process_Value(uint16_t rawIR);
float meanDiff(float M, meanDiffFilter_t* filterValues);
dcFilter_t dcRemoval(float x, float prev_w, float alpha);
butterworthFilter_t lpbFilterIR;
void lowPassButterworthFilter( float x, butterworthFilter_t * filterResult );
void printUlpData();
void printRawData();
public:
PulseMeter();
void init(SoftWire *i2c);
void sleep();
void wake();
void read();
float getHeartRate();
float getSpO2();
bool dataIsReady();
void dropData();
};
//============================================================================================
/*
#define BATTERY_ADC_CH ADC1_CHANNEL_4 // GPIO 32
#define BATTERY_ADC_SAMPLE 33
#define BATTERY_ADC_DIV 1
#define ADC_VREF 1128 // ADC calibration data
*/
class CurrentMeter: public Sensor {
private:
Battery *battery;
float vcc;
float current;
uint8_t batLevel;
int32_t tm = 1;
int32_t tb = 60;
float calc_aggr_accum(float mA);
void setCalibration(uint16_t calValue);
void printUlpData();
public:
CurrentMeter();
void init(SoftWire *i2c);
void sleep();
void wake();
// uint32_t get_battery_voltage(void);
void read();
float getVcc();
// float getCurrent();
uint8_t getBatLevel();
};
//============================================================================================
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
class TimeMeter: public Sensor {
private:
uint8_t bcd2dec(uint8_t bcd);
uint8_t dec2bcd(uint8_t n);
void printUlpData();
WiFiUDP Udp;
char *ntpServerName = "europe.pool.ntp.org";
uint32_t localPort = 8888; // local port to listen for UDP packets
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
uint32_t ntpUpdateInterval = 3600;
int timeZone = 3; // Moscow Time
void updateUlpTime(tmElements_t &tm);
void sendNtpPacket(IPAddress &address);
time_t getNtpTime();
bool read_data_to_tm(tmElements_t &tm) ;
public:
TimeMeter();
void init(SoftWire *i2c);
void read();
void sleep();
void wake();
time_t currentTime();
time_t updateNtpTime();
void updateTime(int h, int m, int s);
void updateDate(int d, int m, int y);
void setTimeZone(int8_t tz) { timeZone = tz; }
// DS3231M_Class dateNow() {return DS3231M;}
};
//============================================================================================
#define BME280_TEMP_PRESS_CALIB_DATA_LEN UINT8_C(26)
#define BME280_HUMIDITY_CALIB_DATA_LEN UINT8_C(7)
#define BME280_P_T_H_DATA_LEN UINT8_C(8)
struct bme280CalibData
{
uint16_t dig_t1;
int16_t dig_t2;
int16_t dig_t3;
uint16_t dig_p1;
int16_t dig_p2;
int16_t dig_p3;
int16_t dig_p4;
int16_t dig_p5;
int16_t dig_p6;
int16_t dig_p7;
int16_t dig_p8;
int16_t dig_p9;
uint8_t dig_h1;
uint16_t dig_h2;
uint8_t dig_h3;
int16_t dig_h4;
int16_t dig_h5;
int8_t dig_h6;
int32_t t_fine;
};
// un-compensated data
struct bme280UncompData
{
uint32_t pressure;
uint32_t temperature;
uint32_t humidity;
};
/**\name Macro to combine two 8 bit data's to form a 16 bit data */
#define BME280_SWAP_BYTES(num) ((num>>8) | (num<<8))
#define BME280_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb)
class BME280Meter: public Sensor {
private:
bme280UncompData uData;
// bme280CalibData calibData;
int32_t t_fine;
float temperature;
float pressure;
float humidity;
void printUlpData();
#ifdef MAIN_CORE_INIT_SENSORS
void parseTempPressCalibData(const uint8_t *reg_data);
void parseHumidityCalibData(const uint8_t *reg_data);
void outCalibData();
#else
void parseTempPressCalibDataUlp();
void parseHumidityCalibDataUlp();
#endif
void parseSensorData();
float readTemperature();
float readPressure();
float readHumidity();
public:
BME280Meter();
void init(SoftWire *i2c);
void sleep();
void wake();
void read();
float getPressure();
float getHumidity();
};
#endif