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

Committer:
jsa1969
Date:
Tue Feb 12 20:10:42 2019 +0000
Revision:
38:0f29a0ea64ca
Parent:
34:069c4f6d5b2c
Child:
39:efe22f143e47
attempt to attack on the parrallel influence problem

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jsa1969 0:cef60cc92da0 1 /*
jsa1969 0:cef60cc92da0 2 The MIT License (MIT)
jsa1969 0:cef60cc92da0 3
jsa1969 0:cef60cc92da0 4 Copyright (c) 2016 British Broadcasting Corporation.
jsa1969 0:cef60cc92da0 5 This software is provided by Lancaster University by arrangement with the BBC.
jsa1969 0:cef60cc92da0 6
jsa1969 0:cef60cc92da0 7 Permission is hereby granted, free of charge, to any person obtaining a
jsa1969 0:cef60cc92da0 8 copy of this software and associated documentation files (the "Software"),
jsa1969 0:cef60cc92da0 9 to deal in the Software without restriction, including without limitation
jsa1969 0:cef60cc92da0 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
jsa1969 0:cef60cc92da0 11 and/or sell copies of the Software, and to permit persons to whom the
jsa1969 0:cef60cc92da0 12 Software is furnished to do so, subject to the following conditions:
jsa1969 0:cef60cc92da0 13
jsa1969 0:cef60cc92da0 14 The above copyright notice and this permission notice shall be included in
jsa1969 0:cef60cc92da0 15 all copies or substantial portions of the Software.
jsa1969 0:cef60cc92da0 16
jsa1969 0:cef60cc92da0 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jsa1969 0:cef60cc92da0 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jsa1969 0:cef60cc92da0 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
jsa1969 0:cef60cc92da0 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jsa1969 0:cef60cc92da0 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
jsa1969 0:cef60cc92da0 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
jsa1969 0:cef60cc92da0 23 DEALINGS IN THE SOFTWARE.
jsa1969 0:cef60cc92da0 24 */
jsa1969 0:cef60cc92da0 25
jsa1969 0:cef60cc92da0 26 #include "MicroBit.h"
jsa1969 0:cef60cc92da0 27
jsa1969 0:cef60cc92da0 28 #include "bme680.h"
jsa1969 2:544117df8c65 29 #include "sgp30.h"
jsa1969 2:544117df8c65 30 #include "dotmath.h"
jsa1969 11:6844a5578b4f 31 #include "nvstore.h"
jsa1969 2:544117df8c65 32
jsa1969 1:f9245fb53737 33 #include "physics.h"
jsa1969 0:cef60cc92da0 34
jsa1969 33:af2dfab24ca9 35 #include "AppSpecificTestrunner.h"
jsa1969 33:af2dfab24ca9 36
jsa1969 0:cef60cc92da0 37 MicroBit uBit;
jsa1969 2:544117df8c65 38 Bme680* bme680 = NULL;
jsa1969 2:544117df8c65 39 struct bme680_field_data* bme680Data = NULL;
jsa1969 2:544117df8c65 40 Sgp30* sgp30 = NULL;
jsa1969 12:96eb06e837f4 41 I2cCallbacks* callbacks = NULL;
jsa1969 12:96eb06e837f4 42 NvStore* nvStore = NULL;
jsa1969 38:0f29a0ea64ca 43 bool i2cLocked = false;
jsa1969 12:96eb06e837f4 44
jsa1969 12:96eb06e837f4 45 bool cancelMeasureLoops = false;
jsa1969 12:96eb06e837f4 46 int runningLoops = 0;
jsa1969 20:2f11b93f4cbf 47 int displayState = 0;
jsa1969 0:cef60cc92da0 48
jsa1969 38:0f29a0ea64ca 49 void acquireLock() {
jsa1969 38:0f29a0ea64ca 50 while (i2cLocked) uBit.sleep(10);
jsa1969 38:0f29a0ea64ca 51 i2cLocked = true;
jsa1969 38:0f29a0ea64ca 52 }
jsa1969 38:0f29a0ea64ca 53
jsa1969 12:96eb06e837f4 54 void waitForLooppsToFinish() {
jsa1969 12:96eb06e837f4 55 cancelMeasureLoops=true;
jsa1969 12:96eb06e837f4 56 while (runningLoops>0) {
jsa1969 12:96eb06e837f4 57 uBit.sleep(10);
jsa1969 12:96eb06e837f4 58 }
jsa1969 12:96eb06e837f4 59 cancelMeasureLoops = false;
jsa1969 12:96eb06e837f4 60 }
jsa1969 0:cef60cc92da0 61
jsa1969 11:6844a5578b4f 62 void displayPixels(int ystart, int pixels, int maxpixels) {
jsa1969 11:6844a5578b4f 63 int y = ystart;
jsa1969 11:6844a5578b4f 64 for (int i=0; i+((y-ystart)*5)<maxpixels; ++i) {
jsa1969 11:6844a5578b4f 65 if (i==5) {
jsa1969 11:6844a5578b4f 66 ++y;
jsa1969 11:6844a5578b4f 67 i=0;
jsa1969 11:6844a5578b4f 68 }
jsa1969 11:6844a5578b4f 69 int pixelsused = i+((y-ystart)*5);
jsa1969 11:6844a5578b4f 70 uBit.display.image.setPixelValue(i, y, pixelsused<pixels? 255 : 0);
jsa1969 10:8e6b71a46871 71 }
jsa1969 10:8e6b71a46871 72 }
jsa1969 10:8e6b71a46871 73
jsa1969 1:f9245fb53737 74 int measureBme680(){
jsa1969 38:0f29a0ea64ca 75 int result = BME680_E_DEV_NOT_FOUND;
jsa1969 1:f9245fb53737 76 if (bme680!=NULL && bme680Data!=NULL) {
jsa1969 38:0f29a0ea64ca 77 acquireLock();
jsa1969 38:0f29a0ea64ca 78 result = bme680->measure(
jsa1969 1:f9245fb53737 79 bme680Data,
jsa1969 2:544117df8c65 80 bme680Data->temperature==0 ?
jsa1969 2:544117df8c65 81 uBit.thermometer.getTemperature()
jsa1969 2:544117df8c65 82 :bme680Data->temperature,
jsa1969 38:0f29a0ea64ca 83 2000, 350);
jsa1969 38:0f29a0ea64ca 84 i2cLocked = false;
jsa1969 1:f9245fb53737 85 }
jsa1969 38:0f29a0ea64ca 86 return result;
jsa1969 0:cef60cc92da0 87 }
jsa1969 0:cef60cc92da0 88
jsa1969 11:6844a5578b4f 89 void measureAndDisplayBme680() {
jsa1969 11:6844a5578b4f 90 if (bme680!=NULL) {
jsa1969 11:6844a5578b4f 91 uBit.display.image.setPixelValue(4, 4, 255);
jsa1969 11:6844a5578b4f 92 if (measureBme680()==MICROBIT_OK) {
jsa1969 27:cb75a0c5c52a 93 nvStore->updateGas(bme680Data->gas_resistance, bme680Data->humidity);
jsa1969 17:a2c4dd192146 94 nvStore->updateTemp(bme680Data->temperature);
jsa1969 17:a2c4dd192146 95 nvStore->updatePress(bme680Data->pressure);
jsa1969 17:a2c4dd192146 96 nvStore->updateHumidity(bme680Data->humidity);
jsa1969 17:a2c4dd192146 97
jsa1969 14:71060505061e 98 uBit.display.image.setPixelValue(0, 4, 255);
jsa1969 11:6844a5578b4f 99 uBit.display.image.setPixelValue(4, 4, 0);
jsa1969 11:6844a5578b4f 100 int bmeY = sgp30!=NULL ? 2 : 0;
jsa1969 11:6844a5578b4f 101 int bmeMaxPixels = sgp30!=NULL ? 5 : 15;
jsa1969 27:cb75a0c5c52a 102 displayPixels(bmeY, DotMath::pixels(bme680Data->gas_resistance, nvStore->getGasMax(bme680Data->humidity), bmeMaxPixels),
jsa1969 11:6844a5578b4f 103 bmeMaxPixels);
jsa1969 11:6844a5578b4f 104 displayPixels(3, 5*bme680Data->humidity/100000, 5);
jsa1969 14:71060505061e 105 } else {
jsa1969 14:71060505061e 106 uBit.display.image.setPixelValue(0, 4, 0);
jsa1969 2:544117df8c65 107 }
jsa1969 12:96eb06e837f4 108 uBit.display.image.setPixelValue(4, 4, 0);
jsa1969 11:6844a5578b4f 109 }
jsa1969 11:6844a5578b4f 110 }
jsa1969 11:6844a5578b4f 111
jsa1969 11:6844a5578b4f 112 void measureAndDisplaySgp30() {
jsa1969 11:6844a5578b4f 113 if (sgp30!=NULL) {
jsa1969 11:6844a5578b4f 114 int sgpMaxPixels = 10;
jsa1969 11:6844a5578b4f 115 int secondLineStart = 2;
jsa1969 11:6844a5578b4f 116 if (bme680!=NULL) {
jsa1969 11:6844a5578b4f 117 sgpMaxPixels = 5;
jsa1969 11:6844a5578b4f 118 secondLineStart = 1;
jsa1969 11:6844a5578b4f 119 uBit.display.image.setPixelValue(2, 4, 255);
jsa1969 38:0f29a0ea64ca 120 acquireLock();
jsa1969 11:6844a5578b4f 121 if (sgp30->setHumidity(bme680Data->humidity, bme680Data->temperature)) {
jsa1969 11:6844a5578b4f 122 uBit.display.image.setPixelValue(2, 4, 0);
jsa1969 11:6844a5578b4f 123 }
jsa1969 11:6844a5578b4f 124 uBit.sleep(10);
jsa1969 38:0f29a0ea64ca 125 i2cLocked = false;
jsa1969 11:6844a5578b4f 126 }
jsa1969 11:6844a5578b4f 127 uBit.display.image.setPixelValue(3, 4, 255);
jsa1969 38:0f29a0ea64ca 128 acquireLock();
jsa1969 38:0f29a0ea64ca 129 bool measureOK = sgp30->IAQmeasure();
jsa1969 38:0f29a0ea64ca 130 i2cLocked = false;
jsa1969 38:0f29a0ea64ca 131 if (measureOK) {
jsa1969 13:996026828be7 132 uBit.display.image.setPixelValue(1, 4, 255);
jsa1969 11:6844a5578b4f 133 uBit.display.image.setPixelValue(3, 4, 0);
jsa1969 18:c4ac93e01027 134
jsa1969 18:c4ac93e01027 135 nvStore->updateVoc(sgp30->TVOC);
jsa1969 18:c4ac93e01027 136 nvStore->updateCo(sgp30->eCO2);
jsa1969 18:c4ac93e01027 137
jsa1969 12:96eb06e837f4 138 int co2Dots = min (5, sgp30->eCO2 /1500);
jsa1969 11:6844a5578b4f 139 displayPixels(0, 5 - DotMath::pixels(sgp30->TVOC, 20000, sgpMaxPixels), sgpMaxPixels);
jsa1969 11:6844a5578b4f 140 displayPixels(secondLineStart, co2Dots, sgpMaxPixels);
jsa1969 13:996026828be7 141 } else {
jsa1969 13:996026828be7 142 uBit.display.image.setPixelValue(1, 4, 0);
jsa1969 11:6844a5578b4f 143 }
jsa1969 2:544117df8c65 144 }
jsa1969 2:544117df8c65 145 }
jsa1969 2:544117df8c65 146
jsa1969 12:96eb06e837f4 147 void bmeMeasureLoop() {
jsa1969 12:96eb06e837f4 148 if (bme680!=NULL) {
jsa1969 12:96eb06e837f4 149 ++runningLoops;
jsa1969 12:96eb06e837f4 150 while (!cancelMeasureLoops){
jsa1969 11:6844a5578b4f 151 measureAndDisplayBme680();
jsa1969 13:996026828be7 152 uBit.sleep(500);
jsa1969 12:96eb06e837f4 153 }
jsa1969 12:96eb06e837f4 154 --runningLoops;
jsa1969 12:96eb06e837f4 155 }
jsa1969 24:07a1d2e6914e 156 release_fiber();
jsa1969 12:96eb06e837f4 157 }
jsa1969 12:96eb06e837f4 158
jsa1969 12:96eb06e837f4 159 void sgpMeasureLoop() {
jsa1969 12:96eb06e837f4 160 if (sgp30!=NULL) {
jsa1969 12:96eb06e837f4 161 ++runningLoops;
jsa1969 12:96eb06e837f4 162 while (!cancelMeasureLoops){
jsa1969 11:6844a5578b4f 163 measureAndDisplaySgp30();
jsa1969 12:96eb06e837f4 164 uBit.sleep(950);
jsa1969 2:544117df8c65 165 }
jsa1969 12:96eb06e837f4 166 --runningLoops;
jsa1969 2:544117df8c65 167 }
jsa1969 24:07a1d2e6914e 168 release_fiber();
jsa1969 12:96eb06e837f4 169 }
jsa1969 12:96eb06e837f4 170
jsa1969 34:069c4f6d5b2c 171 void startMeasureLoops() {
jsa1969 12:96eb06e837f4 172 if (runningLoops>0) {
jsa1969 12:96eb06e837f4 173 uBit.display.scroll("already running");
jsa1969 12:96eb06e837f4 174 return;
jsa1969 12:96eb06e837f4 175 }
jsa1969 14:71060505061e 176 create_fiber(sgpMeasureLoop);
jsa1969 12:96eb06e837f4 177 create_fiber(bmeMeasureLoop);
jsa1969 2:544117df8c65 178 }
jsa1969 2:544117df8c65 179
jsa1969 11:6844a5578b4f 180 void init680(){
jsa1969 9:5150afa50eb6 181 if (bme680!=NULL){
jsa1969 9:5150afa50eb6 182 delete bme680;
jsa1969 9:5150afa50eb6 183 }
jsa1969 10:8e6b71a46871 184
jsa1969 27:cb75a0c5c52a 185 uint32_t gasMax = nvStore->getGasMax(0);
jsa1969 11:6844a5578b4f 186 if (gasMax>0) {
jsa1969 11:6844a5578b4f 187 uBit.display.scroll((int)gasMax);
jsa1969 11:6844a5578b4f 188 }
jsa1969 10:8e6b71a46871 189
jsa1969 14:71060505061e 190 bme680 = new Bme680(callbacks);
jsa1969 9:5150afa50eb6 191 int code = bme680->init();
jsa1969 9:5150afa50eb6 192 if (code == MICROBIT_OK){
jsa1969 9:5150afa50eb6 193 if (bme680Data==NULL) {
jsa1969 9:5150afa50eb6 194 bme680Data = new struct bme680_field_data;
jsa1969 9:5150afa50eb6 195 }
jsa1969 9:5150afa50eb6 196 code = bme680->measure(
jsa1969 9:5150afa50eb6 197 bme680Data,
jsa1969 9:5150afa50eb6 198 uBit.thermometer.getTemperature(),
jsa1969 9:5150afa50eb6 199 100, 100);
jsa1969 9:5150afa50eb6 200 }
jsa1969 9:5150afa50eb6 201 if (code != MICROBIT_OK){
jsa1969 9:5150afa50eb6 202 delete bme680;
jsa1969 9:5150afa50eb6 203 bme680 = NULL;
jsa1969 9:5150afa50eb6 204 delete bme680Data;
jsa1969 9:5150afa50eb6 205 bme680Data = NULL;
jsa1969 9:5150afa50eb6 206 uBit.display.scroll(code);
jsa1969 9:5150afa50eb6 207 } else {
jsa1969 9:5150afa50eb6 208 uBit.display.image.setPixelValue(0, 4, 255);
jsa1969 9:5150afa50eb6 209 }
jsa1969 11:6844a5578b4f 210 }
jsa1969 9:5150afa50eb6 211
jsa1969 11:6844a5578b4f 212 void initSgp30(){
jsa1969 9:5150afa50eb6 213 if (sgp30!=NULL){
jsa1969 9:5150afa50eb6 214 delete sgp30;
jsa1969 9:5150afa50eb6 215 }
jsa1969 9:5150afa50eb6 216 sgp30 = new Sgp30(callbacks);
jsa1969 9:5150afa50eb6 217 if (sgp30->test() && sgp30->begin()) {
jsa1969 9:5150afa50eb6 218 uBit.display.image.setPixelValue(1, 4, 255);
jsa1969 9:5150afa50eb6 219 } else {
jsa1969 9:5150afa50eb6 220 delete sgp30;
jsa1969 9:5150afa50eb6 221 sgp30 = NULL;
jsa1969 11:6844a5578b4f 222 }}
jsa1969 11:6844a5578b4f 223
jsa1969 11:6844a5578b4f 224 void initSensors() {
jsa1969 12:96eb06e837f4 225 waitForLooppsToFinish();
jsa1969 11:6844a5578b4f 226 init680();
jsa1969 11:6844a5578b4f 227 initSgp30();
jsa1969 9:5150afa50eb6 228 }
jsa1969 9:5150afa50eb6 229
jsa1969 12:96eb06e837f4 230 void displayValuesTxt() {
jsa1969 12:96eb06e837f4 231 waitForLooppsToFinish();
jsa1969 8:1bdb50e03d39 232 if (bme680Data!=NULL) {
jsa1969 8:1bdb50e03d39 233 uBit.display.scroll("g");
jsa1969 8:1bdb50e03d39 234 uBit.display.scroll((int)bme680Data->gas_resistance);
jsa1969 8:1bdb50e03d39 235 uBit.display.scroll("+");
jsa1969 38:0f29a0ea64ca 236 const int highGas = (int)nvStore->getGasMax(bme680Data->humidity);
jsa1969 38:0f29a0ea64ca 237 uBit.display.scroll(highGas);
jsa1969 38:0f29a0ea64ca 238 if (highGas==0) {
jsa1969 38:0f29a0ea64ca 239 for (int i=0; i<10; ++i) {
jsa1969 38:0f29a0ea64ca 240 uBit.display.scroll(":");
jsa1969 38:0f29a0ea64ca 241 uBit.display.scroll((int)nvStore->debugInfo()[i]);
jsa1969 38:0f29a0ea64ca 242 }
jsa1969 38:0f29a0ea64ca 243 }
jsa1969 14:71060505061e 244 uBit.display.scroll("-");
jsa1969 14:71060505061e 245 uBit.display.scroll((int)nvStore->getGasMin());
jsa1969 17:a2c4dd192146 246 uBit.display.scroll("t");
jsa1969 17:a2c4dd192146 247 uBit.display.scroll((int)bme680Data->temperature);
jsa1969 17:a2c4dd192146 248 uBit.display.scroll("+");
jsa1969 17:a2c4dd192146 249 uBit.display.scroll((int)nvStore->getTempMax());
jsa1969 17:a2c4dd192146 250 uBit.display.scroll("-");
jsa1969 17:a2c4dd192146 251 uBit.display.scroll((int)nvStore->getTempMin());
jsa1969 17:a2c4dd192146 252 uBit.display.scroll("p");
jsa1969 17:a2c4dd192146 253 uBit.display.scroll((int)bme680Data->pressure);
jsa1969 17:a2c4dd192146 254 uBit.display.scroll("+");
jsa1969 17:a2c4dd192146 255 uBit.display.scroll((int)nvStore->getPressMax());
jsa1969 17:a2c4dd192146 256 uBit.display.scroll("-");
jsa1969 17:a2c4dd192146 257 uBit.display.scroll((int)nvStore->getPressMin());
jsa1969 17:a2c4dd192146 258 uBit.display.scroll("h");
jsa1969 17:a2c4dd192146 259 uBit.display.scroll((int)bme680Data->humidity);
jsa1969 17:a2c4dd192146 260 uBit.display.scroll("+");
jsa1969 17:a2c4dd192146 261 uBit.display.scroll((int)nvStore->getHumMax());
jsa1969 17:a2c4dd192146 262 uBit.display.scroll("-");
jsa1969 17:a2c4dd192146 263 uBit.display.scroll((int)nvStore->getHumMin());
jsa1969 8:1bdb50e03d39 264 }
jsa1969 17:a2c4dd192146 265
jsa1969 8:1bdb50e03d39 266 if (sgp30!=NULL) {
jsa1969 8:1bdb50e03d39 267 uBit.display.scroll("v");
jsa1969 8:1bdb50e03d39 268 uBit.display.scroll((int)sgp30->TVOC);
jsa1969 18:c4ac93e01027 269 uBit.display.scroll("+");
jsa1969 18:c4ac93e01027 270 uBit.display.scroll((int)nvStore->getVocMax());
jsa1969 8:1bdb50e03d39 271 uBit.display.scroll("c");
jsa1969 8:1bdb50e03d39 272 uBit.display.scroll((int)sgp30->eCO2);
jsa1969 18:c4ac93e01027 273 uBit.display.scroll("+");
jsa1969 18:c4ac93e01027 274 uBit.display.scroll((int)nvStore->getCoMax());
jsa1969 8:1bdb50e03d39 275 }
jsa1969 0:cef60cc92da0 276 }
jsa1969 0:cef60cc92da0 277
jsa1969 12:96eb06e837f4 278 void onButtonA(MicroBitEvent evt)
jsa1969 12:96eb06e837f4 279 {
jsa1969 12:96eb06e837f4 280 if (runningLoops>0) {
jsa1969 12:96eb06e837f4 281 displayValuesTxt();
jsa1969 12:96eb06e837f4 282 } else {
jsa1969 34:069c4f6d5b2c 283 startMeasureLoops();
jsa1969 12:96eb06e837f4 284 }
jsa1969 12:96eb06e837f4 285 }
jsa1969 12:96eb06e837f4 286
jsa1969 12:96eb06e837f4 287 void onButtonB(MicroBitEvent evt)
jsa1969 12:96eb06e837f4 288 {
jsa1969 20:2f11b93f4cbf 289 //initSensors();
jsa1969 20:2f11b93f4cbf 290 switch (++displayState) {
jsa1969 20:2f11b93f4cbf 291 case 1:
jsa1969 20:2f11b93f4cbf 292 uBit.display.setBrightness(5);
jsa1969 20:2f11b93f4cbf 293 break;
jsa1969 20:2f11b93f4cbf 294 case 2:
jsa1969 20:2f11b93f4cbf 295 uBit.display.disable();
jsa1969 20:2f11b93f4cbf 296 break;
jsa1969 20:2f11b93f4cbf 297 default:
jsa1969 20:2f11b93f4cbf 298 uBit.display.setBrightness(255);
jsa1969 20:2f11b93f4cbf 299 uBit.display.enable();
jsa1969 20:2f11b93f4cbf 300 displayState = 0;
jsa1969 20:2f11b93f4cbf 301 }
jsa1969 12:96eb06e837f4 302 }
jsa1969 12:96eb06e837f4 303
jsa1969 9:5150afa50eb6 304 void onButtonAB(MicroBitEvent evt)
jsa1969 9:5150afa50eb6 305 {
jsa1969 19:52e19461e867 306 waitForLooppsToFinish();
jsa1969 12:96eb06e837f4 307 nvStore->resetNvStore();
jsa1969 19:52e19461e867 308 uBit.display.scroll("clear");
jsa1969 9:5150afa50eb6 309 }
jsa1969 9:5150afa50eb6 310
jsa1969 34:069c4f6d5b2c 311 const char* runSofwareModuleTests() {
jsa1969 34:069c4f6d5b2c 312 // heap we've got planty, stack is rare
jsa1969 33:af2dfab24ca9 313 AppSpecificTestrunner* runner = new AppSpecificTestrunner();
jsa1969 33:af2dfab24ca9 314 const char* result = runner->runAll();
jsa1969 33:af2dfab24ca9 315 delete runner;
jsa1969 33:af2dfab24ca9 316 return result;
jsa1969 33:af2dfab24ca9 317 }
jsa1969 33:af2dfab24ca9 318
jsa1969 0:cef60cc92da0 319 int main()
jsa1969 0:cef60cc92da0 320 {
jsa1969 0:cef60cc92da0 321 uBit.init();
jsa1969 0:cef60cc92da0 322
jsa1969 0:cef60cc92da0 323 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
jsa1969 0:cef60cc92da0 324 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
jsa1969 9:5150afa50eb6 325 uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
jsa1969 2:544117df8c65 326
jsa1969 9:5150afa50eb6 327 callbacks = new I2cCallbacks(&uBit);
jsa1969 12:96eb06e837f4 328 nvStore = new NvStore(&uBit);
jsa1969 33:af2dfab24ca9 329
jsa1969 34:069c4f6d5b2c 330 const char* testResults = runSofwareModuleTests();
jsa1969 33:af2dfab24ca9 331 if (! Testrunner::messageOK(testResults)) {
jsa1969 33:af2dfab24ca9 332 uBit.display.scroll(testResults);
jsa1969 33:af2dfab24ca9 333 return -1;
jsa1969 33:af2dfab24ca9 334 }
jsa1969 33:af2dfab24ca9 335
jsa1969 34:069c4f6d5b2c 336 // includes hardware tests
jsa1969 9:5150afa50eb6 337 initSensors();
jsa1969 9:5150afa50eb6 338
jsa1969 34:069c4f6d5b2c 339 startMeasureLoops();
jsa1969 2:544117df8c65 340
jsa1969 0:cef60cc92da0 341 release_fiber();
jsa1969 0:cef60cc92da0 342 }
jsa1969 0:cef60cc92da0 343