Skip to content

Commit 66a64a4

Browse files
committed
Port Barometer and fix IMU
1 parent 5e20258 commit 66a64a4

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

examples/Modulino_PlugNPlay/Modulino_PlugNPlay.ino

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ModulinoPixels leds;
66
ModulinoKnob encoder;
77
ModulinoDistance distance;
88
ModulinoMovement imu;
9+
ModulinoAir barometer;
910

1011
void setup() {
1112

@@ -22,7 +23,6 @@ void setup() {
2223

2324
imu.begin();
2425
barometer.begin();
25-
//humidity.begin();
2626
}
2727

2828
int skip = 0;
@@ -52,13 +52,10 @@ void loop() {
5252
Serial.print("\tz ");
5353
Serial.println(imu.getZ(), 3);
5454

55-
Serial.print("Pressure: " + String(barometer.readPressure()));
56-
Serial.println("\tTemperature: " + String(barometer.readTemperature()));
55+
Serial.print("Pressure: " + String(barometer.getPressure()));
56+
Serial.println("\tTemperature: " + String(barometer.getTemperature()));
5757
}
5858

59-
//Serial.print("Humidity: " + String(humidity.readHumidity()));
60-
//Serial.println("\tTemperature: " + String(humidity.readTemperature()));
61-
6259
if (color.available()) {
6360
int r;
6461
int g;

src/Modulino.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#include "Modulino.h"
22

33
// The only singleton that needs to exist
4-
ModulinoClass Modulino;
4+
// Build before other objects to fix the Wire object
5+
ModulinoClass Modulino __attribute__ ((init_priority (101)));
56

67
APDS9999 color(Wire1); // TODO: need to change to APDS9999 https://docs.broadcom.com/doc/APDS-9999-DS
7-
LPS22HBClass barometer(Wire1);
8-
HS300xClass humidity(Wire1);
98

109
ModulinoColor RED(255, 0, 0);
1110
ModulinoColor BLUE(0, 0, 255);

src/Modulino.h

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,13 @@ class ModulinoPixels : public Module {
184184
bool begin() {
185185
Module::begin();
186186
}
187-
void set(int idx, ModulinoColor rgb, uint8_t brightness = 128) {
187+
void set(int idx, ModulinoColor rgb, uint8_t brightness = 5) {
188+
if (brightness > 0x1F) {
189+
brightness = 0x1F;
190+
}
188191
data[idx] = (uint32_t)rgb | brightness | 0xE0;
189192
}
190-
void set(int idx, uint8_t r, uint8_t g, uint8_t b, uint8_t brightness = 128) {
193+
void set(int idx, uint8_t r, uint8_t g, uint8_t b, uint8_t brightness = 5) {
191194
set(idx, ModulinoColor(r,g,b), brightness);
192195
}
193196
void clear(int idx) {
@@ -327,16 +330,24 @@ extern ModulinoClass Modulino;
327330
TODO: These classes need to be ported to Modulino ecosystem, as per the tof_sensor example
328331
*/
329332
extern APDS9999 color; // TODO: need to change to APDS9999 https://docs.broadcom.com/doc/APDS-9999-DS
330-
extern LPS22HBClass barometer;
331333

332334
class ModulinoMovement : public Module {
333335
public:
334336
bool begin() {
335-
imu.begin();
336-
imu.setContinuousMode();
337+
if (_imu == nullptr) {
338+
_imu = new BoschSensorClass(*((TwoWire*)getWire()));
339+
}
340+
initialized = _imu->begin();
341+
if (initialized) {
342+
_imu->setContinuousMode();
343+
}
344+
return initialized != 0;
337345
}
338346
int update() {
339-
return imu.readAcceleration(x, y, z);
347+
if (initialized) {
348+
return _imu->readAcceleration(x, y, z);
349+
}
350+
return 0;
340351
}
341352
float getX() {
342353
return x;
@@ -348,12 +359,39 @@ class ModulinoMovement : public Module {
348359
return z;
349360
}
350361
private:
351-
BoschSensorClass imu = BoschSensorClass(*((TwoWire*)getWire()));
362+
BoschSensorClass* _imu = nullptr;
352363
float x,y,z;
364+
int initialized = 0;
353365
};
354366

355367
class ModulinoAir : public Module {
356-
368+
public:
369+
bool begin() {
370+
if (_barometer == nullptr) {
371+
_barometer = new LPS22HBClass(*((TwoWire*)getWire()));
372+
}
373+
initialized = _barometer->begin();
374+
if (initialized == 0) {
375+
// unfortunately LPS22HBClass calles Wire.end() on failure, restart it
376+
getWire()->begin();
377+
}
378+
return initialized != 0;
379+
}
380+
float getPressure() {
381+
if (initialized) {
382+
return _barometer->readPressure();
383+
}
384+
return 0;
385+
}
386+
float getTemperature() {
387+
if (initialized) {
388+
return _barometer->readTemperature();
389+
}
390+
return 0;
391+
}
392+
private:
393+
LPS22HBClass* _barometer = nullptr;
394+
int initialized = 0;
357395
};
358396

359397
class ModulinoLight : public Module {

0 commit comments

Comments
 (0)