aconno acnsensa project for iOS devices with iBeacon packets support.
Dependencies: LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common
main.cpp@9:0453d592221b, 2018-04-20 (annotated)
- Committer:
- dbartolovic
- Date:
- Fri Apr 20 09:45:52 2018 +0000
- Revision:
- 9:0453d592221b
- Parent:
- 7:7fa0da697c8d
- Child:
- 12:46fbc77fab33
x=x, y=y, z=z
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jurica238814 | 0:12899fa39f88 | 1 | /* |
jurica238814 | 0:12899fa39f88 | 2 | * aconno.de |
jurica238814 | 0:12899fa39f88 | 3 | * Made by Jurica Resetar |
Dautor | 1:326ce5e200fb | 4 | * Edited by Karlo Milicevic |
Dautor | 6:51745805d8b0 | 5 | * Edited by Dominik Bartolovic |
jurica238814 | 0:12899fa39f88 | 6 | * All right reserved |
jurica238814 | 0:12899fa39f88 | 7 | * |
jurica238814 | 0:12899fa39f88 | 8 | */ |
jurica238814 | 0:12899fa39f88 | 9 | |
jurica238814 | 0:12899fa39f88 | 10 | #include "mbed.h" |
jurica238814 | 0:12899fa39f88 | 11 | #include "ble/BLE.h" |
jurica238814 | 0:12899fa39f88 | 12 | #include "acd52832_bsp.h" |
jurica238814 | 0:12899fa39f88 | 13 | #include "GapAdvertisingData.h" |
jurica238814 | 0:12899fa39f88 | 14 | #include "Si7006A20.h" |
jurica238814 | 0:12899fa39f88 | 15 | #include "LSM9DS1.h" |
jurica238814 | 0:12899fa39f88 | 16 | #include "math.h" |
jurica238814 | 0:12899fa39f88 | 17 | #include "nrf52_digital.h" |
Dautor | 1:326ce5e200fb | 18 | #include "adc52832_common/utilities.h" |
Dautor | 1:326ce5e200fb | 19 | #include "MPL115A1.h" |
Dautor | 4:634796e5b8c3 | 20 | #include "acd_nrf52_saadc.h" |
jurica238814 | 0:12899fa39f88 | 21 | |
jurica238814 | 0:12899fa39f88 | 22 | #define V0 0.47 /* In volts */ |
jurica238814 | 0:12899fa39f88 | 23 | #define TC 0.01 /* In volts */ |
jurica238814 | 0:12899fa39f88 | 24 | #define VCC (3.6) |
Dautor | 1:326ce5e200fb | 25 | #define VALUE_TO_PERCENTAGE (100) |
Dautor | 1:326ce5e200fb | 26 | #define WAKEUP_TIME_DELAY_MS (150) |
Dautor | 1:326ce5e200fb | 27 | #define APPLICATION_ID (0xCF170059) |
Dautor | 1:326ce5e200fb | 28 | |
Dautor | 4:634796e5b8c3 | 29 | #define ADC_REFERENCE (3.6f) /* adc reference voltage */ |
Dautor | 4:634796e5b8c3 | 30 | #define ADC_RESOLUTION (1024) /* 10-bit adc */ |
Dautor | 4:634796e5b8c3 | 31 | |
Dautor | 1:326ce5e200fb | 32 | #define I2C_DATA (p19) |
Dautor | 1:326ce5e200fb | 33 | #define I2C_CLK (p20) |
Dautor | 1:326ce5e200fb | 34 | #define SPI_MISO (p5) |
Dautor | 1:326ce5e200fb | 35 | #define SPI_MOSI (p3) |
Dautor | 1:326ce5e200fb | 36 | #define SPI_SCLK (p4) |
jurica238814 | 0:12899fa39f88 | 37 | |
Dautor | 6:51745805d8b0 | 38 | #define DEBUG (0) |
Dautor | 6:51745805d8b0 | 39 | #define DEBUG_PRINT (1) |
jurica238814 | 0:12899fa39f88 | 40 | #define SLEEP_TIME (0.150) /* Sleep time in seconds */ |
jurica238814 | 0:12899fa39f88 | 41 | #define WAKE_UP_TIME (0.150) /* Awake time in ms */ |
jurica238814 | 0:12899fa39f88 | 42 | #define ADV_INTERVAL (100) /* Advertising interval in ms */ |
jurica238814 | 0:12899fa39f88 | 43 | #define GO_TO_SLEEP (0) /* Sleep flag: 0 -> Device will not go to sleep, 1 -> Will go to sleep mode */ |
Dautor | 1:326ce5e200fb | 44 | #define CALIBRATION_STEPS 20 |
jurica238814 | 0:12899fa39f88 | 45 | |
Dautor | 4:634796e5b8c3 | 46 | static NRF52_SAADC analogIn; |
Dautor | 1:326ce5e200fb | 47 | static NRF52_DigitalOut lightPower(p28); |
Dautor | 1:326ce5e200fb | 48 | static NRF52_DigitalOut temperaturePower(p31); |
Dautor | 1:326ce5e200fb | 49 | static NRF52_DigitalOut shdn(p6); |
Dautor | 1:326ce5e200fb | 50 | static NRF52_DigitalOut led(p23); |
Dautor | 1:326ce5e200fb | 51 | static NRF52_DigitalOut power(p2); |
Dautor | 1:326ce5e200fb | 52 | static NRF52_DigitalOut cs(p7); |
Dautor | 1:326ce5e200fb | 53 | static Si7006 *si; |
Dautor | 1:326ce5e200fb | 54 | static LSM9DS1 *mems; |
Dautor | 1:326ce5e200fb | 55 | static SPI *spi; |
Dautor | 1:326ce5e200fb | 56 | static MPL115A1 *mpl115a1; |
jurica238814 | 0:12899fa39f88 | 57 | |
jurica238814 | 0:12899fa39f88 | 58 | #if DEBUG_PRINT |
jurica238814 | 0:12899fa39f88 | 59 | #include "nrf52_uart.h" |
Dautor | 1:326ce5e200fb | 60 | NRF52_UART serial = NRF52_UART(p25, p26, Baud9600); //Tx, RX BaudRate |
jurica238814 | 0:12899fa39f88 | 61 | char buffer[256] = {0}; |
jurica238814 | 0:12899fa39f88 | 62 | #define SEND(...) {uint8_t len = sprintf(buffer, __VA_ARGS__); serial.send(buffer, len);} |
Dautor | 1:326ce5e200fb | 63 | #define SENDN(...) {uint8_t len = sprintf(buffer "\n\r", __VA_ARGS__); serial.send(buffer, len);} |
jurica238814 | 0:12899fa39f88 | 64 | #else |
jurica238814 | 0:12899fa39f88 | 65 | #define SEND(...); |
Dautor | 1:326ce5e200fb | 66 | #define SENDN(...); |
jurica238814 | 0:12899fa39f88 | 67 | #endif |
jurica238814 | 0:12899fa39f88 | 68 | |
Dautor | 1:326ce5e200fb | 69 | static bool sleepFlag = true; |
jurica238814 | 0:12899fa39f88 | 70 | |
Dautor | 3:78ceda8ef565 | 71 | static vector3_s memsAccelerometerInit; |
Dautor | 3:78ceda8ef565 | 72 | static vector3_s memsGyroscopeInit; |
Dautor | 3:78ceda8ef565 | 73 | static vector3_s memsMagnetometerInit; |
jurica238814 | 0:12899fa39f88 | 74 | |
Dautor | 1:326ce5e200fb | 75 | static BLE &ble = BLE::Instance(); |
Dautor | 1:326ce5e200fb | 76 | static GapAdvertisingData adv_data = GapAdvertisingData(); |
jurica238814 | 0:12899fa39f88 | 77 | |
Dautor | 1:326ce5e200fb | 78 | struct __attribute__((packed, aligned(1))) advertising_packet{ |
Dautor | 1:326ce5e200fb | 79 | uint32_t header; |
Dautor | 1:326ce5e200fb | 80 | uint8_t type; |
Dautor | 1:326ce5e200fb | 81 | union{ |
Dautor | 1:326ce5e200fb | 82 | struct{ |
Dautor | 3:78ceda8ef565 | 83 | int16_t gyroscope[3]; |
Dautor | 3:78ceda8ef565 | 84 | int16_t accelerometer[3]; |
Dautor | 3:78ceda8ef565 | 85 | int16_t magnetometer[3]; |
Dautor | 6:51745805d8b0 | 86 | uint16_t acc_lsb_value; |
Dautor | 1:326ce5e200fb | 87 | }; |
Dautor | 1:326ce5e200fb | 88 | struct{ |
Dautor | 1:326ce5e200fb | 89 | float temperature; |
Dautor | 1:326ce5e200fb | 90 | float humidity; |
Dautor | 1:326ce5e200fb | 91 | float pressure; |
Dautor | 1:326ce5e200fb | 92 | float light; |
Dautor | 4:634796e5b8c3 | 93 | uint8_t battery; |
Dautor | 1:326ce5e200fb | 94 | }; |
Dautor | 1:326ce5e200fb | 95 | }; |
Dautor | 1:326ce5e200fb | 96 | }; |
Dautor | 3:78ceda8ef565 | 97 | static advertising_packet advertisementPacket; |
jurica238814 | 0:12899fa39f88 | 98 | |
jurica238814 | 0:12899fa39f88 | 99 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ |
Dautor | 1:326ce5e200fb | 100 | // Restart Advertising on disconnection |
jurica238814 | 0:12899fa39f88 | 101 | BLE::Instance().gap().startAdvertising(); |
jurica238814 | 0:12899fa39f88 | 102 | } |
jurica238814 | 0:12899fa39f88 | 103 | |
jurica238814 | 0:12899fa39f88 | 104 | /** |
jurica238814 | 0:12899fa39f88 | 105 | * Function for waking the core up |
jurica238814 | 0:12899fa39f88 | 106 | */ |
Dautor | 1:326ce5e200fb | 107 | void wakeMeUp(){ |
Dautor | 1:326ce5e200fb | 108 | sleepFlag = false; |
Dautor | 1:326ce5e200fb | 109 | } |
jurica238814 | 0:12899fa39f88 | 110 | |
jurica238814 | 0:12899fa39f88 | 111 | /** |
jurica238814 | 0:12899fa39f88 | 112 | * Callback triggered when the ble initialization process has finished |
jurica238814 | 0:12899fa39f88 | 113 | */ |
jurica238814 | 0:12899fa39f88 | 114 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){ |
jurica238814 | 0:12899fa39f88 | 115 | BLE& ble = params->ble; |
jurica238814 | 0:12899fa39f88 | 116 | ble_error_t error = params->error; |
jurica238814 | 0:12899fa39f88 | 117 | |
Dautor | 3:78ceda8ef565 | 118 | if (error != BLE_ERROR_NONE){ |
jurica238814 | 0:12899fa39f88 | 119 | return; |
jurica238814 | 0:12899fa39f88 | 120 | } |
jurica238814 | 0:12899fa39f88 | 121 | |
jurica238814 | 0:12899fa39f88 | 122 | /* Ensure that it is the default instance of BLE */ |
Dautor | 3:78ceda8ef565 | 123 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE){ |
jurica238814 | 0:12899fa39f88 | 124 | return; |
jurica238814 | 0:12899fa39f88 | 125 | } |
jurica238814 | 0:12899fa39f88 | 126 | |
jurica238814 | 0:12899fa39f88 | 127 | ble.gap().onDisconnection(disconnectionCallback); |
jurica238814 | 0:12899fa39f88 | 128 | |
jurica238814 | 0:12899fa39f88 | 129 | /* setup advertising */ |
jurica238814 | 0:12899fa39f88 | 130 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Dautor | 3:78ceda8ef565 | 131 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); |
jurica238814 | 0:12899fa39f88 | 132 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
jurica238814 | 0:12899fa39f88 | 133 | ble.gap().setAdvertisingInterval(ADV_INTERVAL); |
jurica238814 | 0:12899fa39f88 | 134 | ble.gap().startAdvertising(); |
jurica238814 | 0:12899fa39f88 | 135 | } |
jurica238814 | 0:12899fa39f88 | 136 | |
Dautor | 1:326ce5e200fb | 137 | float getLight(){ |
Dautor | 4:634796e5b8c3 | 138 | return ((float)analogIn.getData()[1])/ADC_RESOLUTION * VALUE_TO_PERCENTAGE; |
jurica238814 | 0:12899fa39f88 | 139 | } |
jurica238814 | 0:12899fa39f88 | 140 | |
jurica238814 | 0:12899fa39f88 | 141 | float voltage2temp(float vOut){ |
jurica238814 | 0:12899fa39f88 | 142 | return ((float)vOut - (float)V0)/((float)TC); |
jurica238814 | 0:12899fa39f88 | 143 | } |
jurica238814 | 0:12899fa39f88 | 144 | |
Dautor | 1:326ce5e200fb | 145 | float getTemperature(){ |
Dautor | 4:634796e5b8c3 | 146 | return voltage2temp(((float)analogIn.getData()[2])/ADC_RESOLUTION * (float)VCC); |
Dautor | 4:634796e5b8c3 | 147 | } |
Dautor | 4:634796e5b8c3 | 148 | |
Dautor | 4:634796e5b8c3 | 149 | uint8_t getBattery(){ |
Dautor | 6:51745805d8b0 | 150 | uint16_t batteryVoltage = analogIn.getData()[0]; |
dbartolovic | 9:0453d592221b | 151 | |
dbartolovic | 7:7fa0da697c8d | 152 | const uint16_t zero_percent_limit = 739; |
dbartolovic | 7:7fa0da697c8d | 153 | const uint16_t onehundred_percent_limit = 810; |
dbartolovic | 7:7fa0da697c8d | 154 | const uint16_t percentage_increments = 5; |
dbartolovic | 7:7fa0da697c8d | 155 | uint8_t percentage; |
dbartolovic | 7:7fa0da697c8d | 156 | |
dbartolovic | 7:7fa0da697c8d | 157 | if (batteryVoltage < zero_percent_limit) |
dbartolovic | 7:7fa0da697c8d | 158 | { |
dbartolovic | 7:7fa0da697c8d | 159 | percentage = 0; |
dbartolovic | 7:7fa0da697c8d | 160 | } |
dbartolovic | 7:7fa0da697c8d | 161 | else if(batteryVoltage > onehundred_percent_limit) |
dbartolovic | 7:7fa0da697c8d | 162 | { |
dbartolovic | 7:7fa0da697c8d | 163 | percentage = 100; |
dbartolovic | 7:7fa0da697c8d | 164 | } |
dbartolovic | 7:7fa0da697c8d | 165 | else |
dbartolovic | 7:7fa0da697c8d | 166 | { |
dbartolovic | 7:7fa0da697c8d | 167 | batteryVoltage -= zero_percent_limit; |
dbartolovic | 7:7fa0da697c8d | 168 | percentage = (batteryVoltage*100)/(onehundred_percent_limit - zero_percent_limit); |
dbartolovic | 7:7fa0da697c8d | 169 | percentage = percentage/percentage_increments*percentage_increments; |
dbartolovic | 7:7fa0da697c8d | 170 | } |
dbartolovic | 7:7fa0da697c8d | 171 | |
dbartolovic | 7:7fa0da697c8d | 172 | return percentage; |
jurica238814 | 0:12899fa39f88 | 173 | } |
jurica238814 | 0:12899fa39f88 | 174 | |
Dautor | 1:326ce5e200fb | 175 | float getHumidity(){ |
Dautor | 1:326ce5e200fb | 176 | float result; |
Dautor | 1:326ce5e200fb | 177 | si->getHumidity(&result); |
Dautor | 1:326ce5e200fb | 178 | return result; |
jurica238814 | 0:12899fa39f88 | 179 | } |
jurica238814 | 0:12899fa39f88 | 180 | |
Dautor | 3:78ceda8ef565 | 181 | void readGyroscope(vector3_s *gyroscopeData){ |
Dautor | 3:78ceda8ef565 | 182 | mems->readGyroscope((int16_t *)gyroscopeData); |
Dautor | 3:78ceda8ef565 | 183 | *gyroscopeData -= memsGyroscopeInit; |
jurica238814 | 0:12899fa39f88 | 184 | } |
jurica238814 | 0:12899fa39f88 | 185 | |
Dautor | 3:78ceda8ef565 | 186 | void readAccelerometer(vector3_s *accelerometerData){ |
Dautor | 3:78ceda8ef565 | 187 | mems->readAccelerometer((int16_t *)accelerometerData); |
Dautor | 3:78ceda8ef565 | 188 | *accelerometerData -= memsAccelerometerInit; |
jurica238814 | 0:12899fa39f88 | 189 | } |
jurica238814 | 0:12899fa39f88 | 190 | |
Dautor | 3:78ceda8ef565 | 191 | void readMagnetometer(vector3_s *magnetometerData){ |
Dautor | 3:78ceda8ef565 | 192 | mems->readMagnetometer((int16_t *)magnetometerData); |
Dautor | 3:78ceda8ef565 | 193 | *magnetometerData -= memsMagnetometerInit; |
jurica238814 | 0:12899fa39f88 | 194 | } |
jurica238814 | 0:12899fa39f88 | 195 | |
Dautor | 3:78ceda8ef565 | 196 | void calibrateAccelerometer(){ |
Dautor | 3:78ceda8ef565 | 197 | vector3_s accelerometerData; |
Dautor | 1:326ce5e200fb | 198 | for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){ |
Dautor | 3:78ceda8ef565 | 199 | readAccelerometer(&accelerometerData); |
Dautor | 3:78ceda8ef565 | 200 | memsAccelerometerInit += accelerometerData; |
jurica238814 | 0:12899fa39f88 | 201 | } |
Dautor | 3:78ceda8ef565 | 202 | memsAccelerometerInit /= CALIBRATION_STEPS; |
jurica238814 | 0:12899fa39f88 | 203 | } |
jurica238814 | 0:12899fa39f88 | 204 | |
Dautor | 3:78ceda8ef565 | 205 | void calibrateGyroscope(){ |
Dautor | 3:78ceda8ef565 | 206 | vector3_s gyroscopeData; |
Dautor | 1:326ce5e200fb | 207 | for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){ |
Dautor | 3:78ceda8ef565 | 208 | readGyroscope(&gyroscopeData); |
Dautor | 3:78ceda8ef565 | 209 | memsGyroscopeInit += gyroscopeData; |
jurica238814 | 0:12899fa39f88 | 210 | } |
Dautor | 3:78ceda8ef565 | 211 | memsGyroscopeInit /= CALIBRATION_STEPS; |
jurica238814 | 0:12899fa39f88 | 212 | } |
jurica238814 | 0:12899fa39f88 | 213 | |
jurica238814 | 0:12899fa39f88 | 214 | void calibrateMag(){ |
Dautor | 3:78ceda8ef565 | 215 | vector3_s magnetometerData; |
Dautor | 1:326ce5e200fb | 216 | for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){ |
Dautor | 3:78ceda8ef565 | 217 | readMagnetometer(&magnetometerData); |
Dautor | 3:78ceda8ef565 | 218 | memsMagnetometerInit += magnetometerData; |
jurica238814 | 0:12899fa39f88 | 219 | } |
Dautor | 3:78ceda8ef565 | 220 | memsMagnetometerInit /= CALIBRATION_STEPS; |
jurica238814 | 0:12899fa39f88 | 221 | } |
jurica238814 | 0:12899fa39f88 | 222 | |
jurica238814 | 0:12899fa39f88 | 223 | void updateData(){ |
Dautor | 3:78ceda8ef565 | 224 | static uint8_t advertisementType = 0; |
jurica238814 | 0:12899fa39f88 | 225 | |
Dautor | 3:78ceda8ef565 | 226 | if(advertisementType < 1){ |
Dautor | 3:78ceda8ef565 | 227 | advertisementPacket.type = 0x00; |
Dautor | 3:78ceda8ef565 | 228 | readGyroscope((vector3_s *)advertisementPacket.gyroscope); |
Dautor | 3:78ceda8ef565 | 229 | readAccelerometer((vector3_s *)advertisementPacket.accelerometer); |
Dautor | 3:78ceda8ef565 | 230 | readMagnetometer((vector3_s *)advertisementPacket.magnetometer); |
Dautor | 6:51745805d8b0 | 231 | advertisementPacket.acc_lsb_value = (0xF9E);//(0x3D80); |
Dautor | 6:51745805d8b0 | 232 | // ^--- That's in ug cuz MSB is 1 |
jurica238814 | 0:12899fa39f88 | 233 | } |
jurica238814 | 0:12899fa39f88 | 234 | else{ |
Dautor | 6:51745805d8b0 | 235 | analogIn.updateData(); |
Dautor | 6:51745805d8b0 | 236 | |
Dautor | 3:78ceda8ef565 | 237 | advertisementPacket.type = 0x01; |
Dautor | 3:78ceda8ef565 | 238 | advertisementPacket.temperature = getTemperature(); |
Dautor | 3:78ceda8ef565 | 239 | advertisementPacket.light = getLight(); |
Dautor | 3:78ceda8ef565 | 240 | advertisementPacket.humidity = getHumidity(); |
Dautor | 3:78ceda8ef565 | 241 | advertisementPacket.pressure = mpl115a1->getPressure(); |
Dautor | 4:634796e5b8c3 | 242 | advertisementPacket.battery = getBattery(); |
jurica238814 | 0:12899fa39f88 | 243 | } |
Dautor | 6:51745805d8b0 | 244 | #if DEBUG == 0 |
Dautor | 3:78ceda8ef565 | 245 | if(++advertisementType > 2) advertisementType = 0; |
Dautor | 6:51745805d8b0 | 246 | #endif |
jurica238814 | 0:12899fa39f88 | 247 | |
jurica238814 | 0:12899fa39f88 | 248 | adv_data = ble.getAdvertisingData(); |
Dautor | 3:78ceda8ef565 | 249 | adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); |
jurica238814 | 0:12899fa39f88 | 250 | ble.setAdvertisingData(adv_data); |
jurica238814 | 0:12899fa39f88 | 251 | } |
jurica238814 | 0:12899fa39f88 | 252 | |
Dautor | 6:51745805d8b0 | 253 | #if DEBUG |
Dautor | 6:51745805d8b0 | 254 | void do_per() |
Dautor | 6:51745805d8b0 | 255 | { |
Dautor | 6:51745805d8b0 | 256 | /* |
Dautor | 6:51745805d8b0 | 257 | SEND("T: %f\r\nL: %f\r\nH: %f\r\nP: %f\r\nB: %d\r\n", advertisementPacket.temperature, |
Dautor | 6:51745805d8b0 | 258 | advertisementPacket.light, |
Dautor | 6:51745805d8b0 | 259 | advertisementPacket.humidity, |
Dautor | 6:51745805d8b0 | 260 | advertisementPacket.pressure, |
Dautor | 6:51745805d8b0 | 261 | advertisementPacket.battery); |
Dautor | 6:51745805d8b0 | 262 | */ |
Dautor | 6:51745805d8b0 | 263 | |
Dautor | 6:51745805d8b0 | 264 | SEND("G: %6d %6d %6d\r\nA: %6d %6d %6d\r\nM: %6d %6d %6d\r\n", advertisementPacket.gyroscope[0], advertisementPacket.gyroscope[1], advertisementPacket.gyroscope[2], |
Dautor | 6:51745805d8b0 | 265 | advertisementPacket.accelerometer[0], advertisementPacket.accelerometer[1], advertisementPacket.accelerometer[2], |
Dautor | 6:51745805d8b0 | 266 | advertisementPacket.magnetometer[0], advertisementPacket.magnetometer[1], advertisementPacket.magnetometer[2]); |
Dautor | 6:51745805d8b0 | 267 | } |
Dautor | 6:51745805d8b0 | 268 | #endif |
jurica238814 | 0:12899fa39f88 | 269 | |
Dautor | 1:326ce5e200fb | 270 | int main(){ |
jurica238814 | 0:12899fa39f88 | 271 | power = 1; |
Dautor | 1:326ce5e200fb | 272 | wait_ms(WAKEUP_TIME_DELAY_MS); |
jurica238814 | 0:12899fa39f88 | 273 | temperaturePower = 1; |
jurica238814 | 0:12899fa39f88 | 274 | lightPower = 1; |
Dautor | 1:326ce5e200fb | 275 | shdn = 1; // Wake up the pressure sensor |
Dautor | 4:634796e5b8c3 | 276 | analogIn.addChannel(9); // Set VDD as source to SAADC |
Dautor | 4:634796e5b8c3 | 277 | analogIn.addChannel(6); // Light |
Dautor | 4:634796e5b8c3 | 278 | analogIn.addChannel(7); // Temp |
Dautor | 4:634796e5b8c3 | 279 | analogIn.calibrate(); |
Dautor | 4:634796e5b8c3 | 280 | |
Dautor | 3:78ceda8ef565 | 281 | advertisementPacket.header = APPLICATION_ID; |
Dautor | 1:326ce5e200fb | 282 | |
Dautor | 1:326ce5e200fb | 283 | ble.init(bleInitComplete); |
jurica238814 | 0:12899fa39f88 | 284 | |
Dautor | 1:326ce5e200fb | 285 | I2C i2c(I2C_DATA, I2C_CLK); |
Dautor | 1:326ce5e200fb | 286 | si = new Si7006(&i2c); |
Dautor | 2:c0654c5fb771 | 287 | mems = new LSM9DS1(&i2c); |
Dautor | 1:326ce5e200fb | 288 | spi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCLK); |
Dautor | 1:326ce5e200fb | 289 | mpl115a1 = new MPL115A1(*spi, cs); |
jurica238814 | 0:12899fa39f88 | 290 | |
Dautor | 3:78ceda8ef565 | 291 | mems->startAccelerometer(); |
Dautor | 3:78ceda8ef565 | 292 | mems->startGyroscope(); |
Dautor | 3:78ceda8ef565 | 293 | mems->startMagnetometer(); |
jurica238814 | 0:12899fa39f88 | 294 | |
jurica238814 | 0:12899fa39f88 | 295 | led = 1; |
jurica238814 | 0:12899fa39f88 | 296 | |
jurica238814 | 0:12899fa39f88 | 297 | Ticker ticker; |
Dautor | 1:326ce5e200fb | 298 | ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up |
jurica238814 | 0:12899fa39f88 | 299 | |
Dautor | 6:51745805d8b0 | 300 | #if DEBUG |
Dautor | 6:51745805d8b0 | 301 | Ticker per; |
Dautor | 6:51745805d8b0 | 302 | per.attach(do_per, 1); |
Dautor | 6:51745805d8b0 | 303 | #endif |
Dautor | 6:51745805d8b0 | 304 | |
Dautor | 4:634796e5b8c3 | 305 | while(ble.hasInitialized() == false){ |
Dautor | 4:634796e5b8c3 | 306 | /* spin loop */ |
Dautor | 4:634796e5b8c3 | 307 | } |
Dautor | 4:634796e5b8c3 | 308 | |
Dautor | 1:326ce5e200fb | 309 | while(true){ |
Dautor | 1:326ce5e200fb | 310 | if (sleepFlag && GO_TO_SLEEP){ |
Dautor | 1:326ce5e200fb | 311 | ble.gap().stopAdvertising(); |
Dautor | 1:326ce5e200fb | 312 | sleep(); |
Dautor | 1:326ce5e200fb | 313 | ble.waitForEvent(); |
Dautor | 1:326ce5e200fb | 314 | } |
Dautor | 1:326ce5e200fb | 315 | else{ |
Dautor | 1:326ce5e200fb | 316 | // I'm awake |
Dautor | 1:326ce5e200fb | 317 | updateData(); |
Dautor | 1:326ce5e200fb | 318 | ble.gap().startAdvertising(); |
Dautor | 1:326ce5e200fb | 319 | wait_ms(WAKE_UP_TIME); |
Dautor | 1:326ce5e200fb | 320 | sleepFlag = true; |
jurica238814 | 0:12899fa39f88 | 321 | } |
jurica238814 | 0:12899fa39f88 | 322 | } |
jurica238814 | 0:12899fa39f88 | 323 | } |