uses BBC micro:bit to measure and display indoor air quality using Bosch BME680 and/or Sensirion SGP30

Dependencies:   microbit

uses Bosch BME680 and/or Sensirion SGP30 sensors to measure indor air quality

sensors should be connected to BBC micro:bit using i2c

commands are received and data is being sent using uBit / nordic radio protocol

display ---

last line always indicates: - first dot: bme680 detected - second dot: sgp30 detected - third dot: sgp 30 setting humidity/temperature - fourth dor: sgp30 measuring - fith dot: bme680 measuring

the detect dots should be in a stable state (not blinking) the measuring dots should be blinking (constant light means: measurement failed)

if only one bme680 is present: - first 3 lines indicate gas resistence (air quality / more dots == worse quality) - fourth line indicates humidity level

if only sgp30 is present: - first two lines indicate SGP30 VOC level - third and fourth line indicate sgp30 CO2 level

if both sensors are present: - first line indicates SGP30 VOC level - second line line indicates sgp30 CO2 level - third line indicates bme680 gas resistence (air quality) - fourth line indicates bme 680 humidity level

buttons - B display state, switches betweeen - full bright - low light - display off

AB reset sgp30 baseline in non volatile storage

data logging -- during measurements the minimum and mximum values for each measured value (temperature, air pressure, humidity,gas resistance, VOC, CO2) are being stored in non volatile storage those (and the last measurement results) are being shown when btn A has been pressed

Revision:
42:ce269f988ac8
Parent:
40:a67a880cb538
Child:
43:f968ca84d4ed
diff -r 8f891ea56b0d -r ce269f988ac8 i2c/i2c_callbacks.cpp
--- a/i2c/i2c_callbacks.cpp	Wed Feb 13 08:05:33 2019 +0000
+++ b/i2c/i2c_callbacks.cpp	Wed Feb 13 17:29:30 2019 +0000
@@ -2,6 +2,7 @@
 
 I2cCallbacks::I2cCallbacks(MicroBit* uBit, MicroBitI2C* i2c){
     _uBit = uBit;
+    _locked = false;
     if (i2c == NULL) {
         _i2c = &(_uBit->i2c);
         }
@@ -15,16 +16,14 @@
 
     if (buffer == NULL || length <= 0)
         return MICROBIT_INVALID_PARAMETER;
-
+        
+    acquireLock();
     result = _i2c->write(address, (const char *)&reg, 1, true);
-    if (result !=0)
-        return MICROBIT_I2C_ERROR;
-
-    result = _i2c->read(address, (char *)buffer, length);
-    if (result !=0)
-        return MICROBIT_I2C_ERROR;
-
-    return MICROBIT_OK;
+    if (result == 0)
+        result = _i2c->read(address, (char *)buffer, length);
+    _locked = false;
+    
+    return result != 0 ? MICROBIT_I2C_ERROR : MICROBIT_OK;
 }
 
 int I2cCallbacks::read(const uint8_t address, uint8_t *buffer, const uint16_t length){
@@ -33,7 +32,10 @@
     if (buffer == NULL || length <= 0)
         return MICROBIT_INVALID_PARAMETER;
 
+    acquireLock();
     result = _i2c->read(address, (char *)buffer, length);
+    _locked = true;
+    
     if (result !=0)
         return MICROBIT_I2C_ERROR;
 
@@ -47,13 +49,33 @@
         tmpBuf[j] = data[i];
     }
     
-    return _i2c->write(dev_id, tmpBuf, length+1);
+    acquireLock();   
+    int result = _i2c->write(dev_id, tmpBuf, length+1);
+    _locked = false;
+    
+    return result;
 }
 
 int I2cCallbacks::write(const uint8_t dev_id, const uint8_t *data, const uint16_t length){
-    return _i2c->write(dev_id, (char*)data, length);
+    acquireLock();
+    int result = _i2c->write(dev_id, (char*)data, length);
+    _locked = false;
+    return result;
+}
+
+void I2cCallbacks::delay_ms_relaxed(const uint32_t period) {
+    _uBit->sleep(period);
 }
 
-void I2cCallbacks::delay_ms(const uint32_t period) {
-    _uBit->sleep(period);
+void I2cCallbacks::delay_ms_strict(const uint32_t period) {
+    unsigned long endTime = _uBit->systemTime() + period;
+    acquireLock();
+    while (_uBit->systemTime() < endTime) {
+        }
+    _locked = false;
 }
+
+void I2cCallbacks::acquireLock() {
+    while (_locked) _uBit->sleep(1);
+    _locked = true;
+    }
\ No newline at end of file