aconno acnsensa project for iOS devices with iBeacon packets support.
Dependencies: LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common
main.cpp@7:7fa0da697c8d, 2018-03-21 (annotated)
- Committer:
- dbartolovic
- Date:
- Wed Mar 21 10:07:24 2018 +0000
- Revision:
- 7:7fa0da697c8d
- Parent:
- 6:51745805d8b0
- Child:
- 9:0453d592221b
- Child:
- 10:a2364dee495f
Updated battery level function.
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 | 7:7fa0da697c8d | 151 | /* // Old code. |
Dautor | 4:634796e5b8c3 | 152 | if(batteryVoltage >= 810) return 100; |
Dautor | 4:634796e5b8c3 | 153 | if(batteryVoltage >= 796) return 85; |
Dautor | 4:634796e5b8c3 | 154 | if(batteryVoltage >= 782) return 75; |
Dautor | 4:634796e5b8c3 | 155 | if(batteryVoltage >= 768) return 50; |
Dautor | 4:634796e5b8c3 | 156 | if(batteryVoltage >= 754) return 25; |
Dautor | 4:634796e5b8c3 | 157 | if(batteryVoltage >= 740) return 10; |
Dautor | 4:634796e5b8c3 | 158 | return 0; |
dbartolovic | 7:7fa0da697c8d | 159 | */ |
dbartolovic | 7:7fa0da697c8d | 160 | // New version. |
dbartolovic | 7:7fa0da697c8d | 161 | const uint16_t zero_percent_limit = 739; |
dbartolovic | 7:7fa0da697c8d | 162 | const uint16_t onehundred_percent_limit = 810; |
dbartolovic | 7:7fa0da697c8d | 163 | const uint16_t percentage_increments = 5; |
dbartolovic | 7:7fa0da697c8d | 164 | uint8_t percentage; |
dbartolovic | 7:7fa0da697c8d | 165 | |
dbartolovic | 7:7fa0da697c8d | 166 | if (batteryVoltage < zero_percent_limit) |
dbartolovic | 7:7fa0da697c8d | 167 | { |
dbartolovic | 7:7fa0da697c8d | 168 | percentage = 0; |
dbartolovic | 7:7fa0da697c8d | 169 | } |
dbartolovic | 7:7fa0da697c8d | 170 | else if(batteryVoltage > onehundred_percent_limit) |
dbartolovic | 7:7fa0da697c8d | 171 | { |
dbartolovic | 7:7fa0da697c8d | 172 | percentage = 100; |
dbartolovic | 7:7fa0da697c8d | 173 | } |
dbartolovic | 7:7fa0da697c8d | 174 | else |
dbartolovic | 7:7fa0da697c8d | 175 | { |
dbartolovic | 7:7fa0da697c8d | 176 | batteryVoltage -= zero_percent_limit; |
dbartolovic | 7:7fa0da697c8d | 177 | percentage = (batteryVoltage*100)/(onehundred_percent_limit - zero_percent_limit); |
dbartolovic | 7:7fa0da697c8d | 178 | percentage = percentage/percentage_increments*percentage_increments; |
dbartolovic | 7:7fa0da697c8d | 179 | } |
dbartolovic | 7:7fa0da697c8d | 180 | |
dbartolovic | 7:7fa0da697c8d | 181 | return percentage; |
jurica238814 | 0:12899fa39f88 | 182 | } |
jurica238814 | 0:12899fa39f88 | 183 | |
Dautor | 1:326ce5e200fb | 184 | float getHumidity(){ |
Dautor | 1:326ce5e200fb | 185 | float result; |
Dautor | 1:326ce5e200fb | 186 | si->getHumidity(&result); |
Dautor | 1:326ce5e200fb | 187 | return result; |
jurica238814 | 0:12899fa39f88 | 188 | } |
jurica238814 | 0:12899fa39f88 | 189 | |
Dautor | 3:78ceda8ef565 | 190 | void readGyroscope(vector3_s *gyroscopeData){ |
Dautor | 3:78ceda8ef565 | 191 | mems->readGyroscope((int16_t *)gyroscopeData); |
Dautor | 3:78ceda8ef565 | 192 | *gyroscopeData -= memsGyroscopeInit; |
jurica238814 | 0:12899fa39f88 | 193 | } |
jurica238814 | 0:12899fa39f88 | 194 | |
Dautor | 3:78ceda8ef565 | 195 | void readAccelerometer(vector3_s *accelerometerData){ |
Dautor | 3:78ceda8ef565 | 196 | mems->readAccelerometer((int16_t *)accelerometerData); |
Dautor | 3:78ceda8ef565 | 197 | *accelerometerData -= memsAccelerometerInit; |
jurica238814 | 0:12899fa39f88 | 198 | } |
jurica238814 | 0:12899fa39f88 | 199 | |
Dautor | 3:78ceda8ef565 | 200 | void readMagnetometer(vector3_s *magnetometerData){ |
Dautor | 3:78ceda8ef565 | 201 | mems->readMagnetometer((int16_t *)magnetometerData); |
Dautor | 3:78ceda8ef565 | 202 | *magnetometerData -= memsMagnetometerInit; |
jurica238814 | 0:12899fa39f88 | 203 | } |
jurica238814 | 0:12899fa39f88 | 204 | |
Dautor | 3:78ceda8ef565 | 205 | void calibrateAccelerometer(){ |
Dautor | 3:78ceda8ef565 | 206 | vector3_s accelerometerData; |
Dautor | 1:326ce5e200fb | 207 | for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){ |
Dautor | 3:78ceda8ef565 | 208 | readAccelerometer(&accelerometerData); |
Dautor | 3:78ceda8ef565 | 209 | memsAccelerometerInit += accelerometerData; |
jurica238814 | 0:12899fa39f88 | 210 | } |
Dautor | 3:78ceda8ef565 | 211 | memsAccelerometerInit /= CALIBRATION_STEPS; |
jurica238814 | 0:12899fa39f88 | 212 | } |
jurica238814 | 0:12899fa39f88 | 213 | |
Dautor | 3:78ceda8ef565 | 214 | void calibrateGyroscope(){ |
Dautor | 3:78ceda8ef565 | 215 | vector3_s gyroscopeData; |
Dautor | 1:326ce5e200fb | 216 | for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){ |
Dautor | 3:78ceda8ef565 | 217 | readGyroscope(&gyroscopeData); |
Dautor | 3:78ceda8ef565 | 218 | memsGyroscopeInit += gyroscopeData; |
jurica238814 | 0:12899fa39f88 | 219 | } |
Dautor | 3:78ceda8ef565 | 220 | memsGyroscopeInit /= CALIBRATION_STEPS; |
jurica238814 | 0:12899fa39f88 | 221 | } |
jurica238814 | 0:12899fa39f88 | 222 | |
jurica238814 | 0:12899fa39f88 | 223 | void calibrateMag(){ |
Dautor | 3:78ceda8ef565 | 224 | vector3_s magnetometerData; |
Dautor | 1:326ce5e200fb | 225 | for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){ |
Dautor | 3:78ceda8ef565 | 226 | readMagnetometer(&magnetometerData); |
Dautor | 3:78ceda8ef565 | 227 | memsMagnetometerInit += magnetometerData; |
jurica238814 | 0:12899fa39f88 | 228 | } |
Dautor | 3:78ceda8ef565 | 229 | memsMagnetometerInit /= CALIBRATION_STEPS; |
jurica238814 | 0:12899fa39f88 | 230 | } |
jurica238814 | 0:12899fa39f88 | 231 | |
jurica238814 | 0:12899fa39f88 | 232 | void updateData(){ |
Dautor | 3:78ceda8ef565 | 233 | static uint8_t advertisementType = 0; |
jurica238814 | 0:12899fa39f88 | 234 | |
Dautor | 3:78ceda8ef565 | 235 | if(advertisementType < 1){ |
Dautor | 3:78ceda8ef565 | 236 | advertisementPacket.type = 0x00; |
Dautor | 3:78ceda8ef565 | 237 | readGyroscope((vector3_s *)advertisementPacket.gyroscope); |
Dautor | 3:78ceda8ef565 | 238 | readAccelerometer((vector3_s *)advertisementPacket.accelerometer); |
Dautor | 3:78ceda8ef565 | 239 | readMagnetometer((vector3_s *)advertisementPacket.magnetometer); |
Dautor | 6:51745805d8b0 | 240 | advertisementPacket.acc_lsb_value = (0xF9E);//(0x3D80); |
Dautor | 6:51745805d8b0 | 241 | // ^--- That's in ug cuz MSB is 1 |
jurica238814 | 0:12899fa39f88 | 242 | } |
jurica238814 | 0:12899fa39f88 | 243 | else{ |
Dautor | 6:51745805d8b0 | 244 | analogIn.updateData(); |
Dautor | 6:51745805d8b0 | 245 | |
Dautor | 3:78ceda8ef565 | 246 | advertisementPacket.type = 0x01; |
Dautor | 3:78ceda8ef565 | 247 | advertisementPacket.temperature = getTemperature(); |
Dautor | 3:78ceda8ef565 | 248 | advertisementPacket.light = getLight(); |
Dautor | 3:78ceda8ef565 | 249 | advertisementPacket.humidity = getHumidity(); |
Dautor | 3:78ceda8ef565 | 250 | advertisementPacket.pressure = mpl115a1->getPressure(); |
Dautor | 4:634796e5b8c3 | 251 | advertisementPacket.battery = getBattery(); |
jurica238814 | 0:12899fa39f88 | 252 | } |
Dautor | 6:51745805d8b0 | 253 | #if DEBUG == 0 |
Dautor | 3:78ceda8ef565 | 254 | if(++advertisementType > 2) advertisementType = 0; |
Dautor | 6:51745805d8b0 | 255 | #endif |
jurica238814 | 0:12899fa39f88 | 256 | |
jurica238814 | 0:12899fa39f88 | 257 | adv_data = ble.getAdvertisingData(); |
Dautor | 3:78ceda8ef565 | 258 | adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); |
jurica238814 | 0:12899fa39f88 | 259 | ble.setAdvertisingData(adv_data); |
jurica238814 | 0:12899fa39f88 | 260 | } |
jurica238814 | 0:12899fa39f88 | 261 | |
Dautor | 6:51745805d8b0 | 262 | #if DEBUG |
Dautor | 6:51745805d8b0 | 263 | void do_per() |
Dautor | 6:51745805d8b0 | 264 | { |
Dautor | 6:51745805d8b0 | 265 | /* |
Dautor | 6:51745805d8b0 | 266 | SEND("T: %f\r\nL: %f\r\nH: %f\r\nP: %f\r\nB: %d\r\n", advertisementPacket.temperature, |
Dautor | 6:51745805d8b0 | 267 | advertisementPacket.light, |
Dautor | 6:51745805d8b0 | 268 | advertisementPacket.humidity, |
Dautor | 6:51745805d8b0 | 269 | advertisementPacket.pressure, |
Dautor | 6:51745805d8b0 | 270 | advertisementPacket.battery); |
Dautor | 6:51745805d8b0 | 271 | */ |
Dautor | 6:51745805d8b0 | 272 | |
Dautor | 6:51745805d8b0 | 273 | 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 | 274 | advertisementPacket.accelerometer[0], advertisementPacket.accelerometer[1], advertisementPacket.accelerometer[2], |
Dautor | 6:51745805d8b0 | 275 | advertisementPacket.magnetometer[0], advertisementPacket.magnetometer[1], advertisementPacket.magnetometer[2]); |
Dautor | 6:51745805d8b0 | 276 | } |
Dautor | 6:51745805d8b0 | 277 | #endif |
jurica238814 | 0:12899fa39f88 | 278 | |
Dautor | 1:326ce5e200fb | 279 | int main(){ |
jurica238814 | 0:12899fa39f88 | 280 | power = 1; |
Dautor | 1:326ce5e200fb | 281 | wait_ms(WAKEUP_TIME_DELAY_MS); |
jurica238814 | 0:12899fa39f88 | 282 | temperaturePower = 1; |
jurica238814 | 0:12899fa39f88 | 283 | lightPower = 1; |
Dautor | 1:326ce5e200fb | 284 | shdn = 1; // Wake up the pressure sensor |
Dautor | 4:634796e5b8c3 | 285 | analogIn.addChannel(9); // Set VDD as source to SAADC |
Dautor | 4:634796e5b8c3 | 286 | analogIn.addChannel(6); // Light |
Dautor | 4:634796e5b8c3 | 287 | analogIn.addChannel(7); // Temp |
Dautor | 4:634796e5b8c3 | 288 | analogIn.calibrate(); |
Dautor | 4:634796e5b8c3 | 289 | |
Dautor | 3:78ceda8ef565 | 290 | advertisementPacket.header = APPLICATION_ID; |
Dautor | 1:326ce5e200fb | 291 | |
Dautor | 1:326ce5e200fb | 292 | ble.init(bleInitComplete); |
jurica238814 | 0:12899fa39f88 | 293 | |
Dautor | 1:326ce5e200fb | 294 | I2C i2c(I2C_DATA, I2C_CLK); |
Dautor | 1:326ce5e200fb | 295 | si = new Si7006(&i2c); |
Dautor | 2:c0654c5fb771 | 296 | mems = new LSM9DS1(&i2c); |
Dautor | 1:326ce5e200fb | 297 | spi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCLK); |
Dautor | 1:326ce5e200fb | 298 | mpl115a1 = new MPL115A1(*spi, cs); |
jurica238814 | 0:12899fa39f88 | 299 | |
Dautor | 3:78ceda8ef565 | 300 | mems->startAccelerometer(); |
Dautor | 3:78ceda8ef565 | 301 | mems->startGyroscope(); |
Dautor | 3:78ceda8ef565 | 302 | mems->startMagnetometer(); |
jurica238814 | 0:12899fa39f88 | 303 | |
jurica238814 | 0:12899fa39f88 | 304 | led = 1; |
jurica238814 | 0:12899fa39f88 | 305 | |
jurica238814 | 0:12899fa39f88 | 306 | Ticker ticker; |
Dautor | 1:326ce5e200fb | 307 | ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up |
jurica238814 | 0:12899fa39f88 | 308 | |
Dautor | 6:51745805d8b0 | 309 | #if DEBUG |
Dautor | 6:51745805d8b0 | 310 | Ticker per; |
Dautor | 6:51745805d8b0 | 311 | per.attach(do_per, 1); |
Dautor | 6:51745805d8b0 | 312 | #endif |
Dautor | 6:51745805d8b0 | 313 | |
Dautor | 4:634796e5b8c3 | 314 | while(ble.hasInitialized() == false){ |
Dautor | 4:634796e5b8c3 | 315 | /* spin loop */ |
Dautor | 4:634796e5b8c3 | 316 | } |
Dautor | 4:634796e5b8c3 | 317 | |
Dautor | 1:326ce5e200fb | 318 | while(true){ |
Dautor | 1:326ce5e200fb | 319 | if (sleepFlag && GO_TO_SLEEP){ |
Dautor | 1:326ce5e200fb | 320 | ble.gap().stopAdvertising(); |
Dautor | 1:326ce5e200fb | 321 | sleep(); |
Dautor | 1:326ce5e200fb | 322 | ble.waitForEvent(); |
Dautor | 1:326ce5e200fb | 323 | } |
Dautor | 1:326ce5e200fb | 324 | else{ |
Dautor | 1:326ce5e200fb | 325 | // I'm awake |
Dautor | 1:326ce5e200fb | 326 | updateData(); |
Dautor | 1:326ce5e200fb | 327 | ble.gap().startAdvertising(); |
Dautor | 1:326ce5e200fb | 328 | wait_ms(WAKE_UP_TIME); |
Dautor | 1:326ce5e200fb | 329 | sleepFlag = true; |
jurica238814 | 0:12899fa39f88 | 330 | } |
jurica238814 | 0:12899fa39f88 | 331 | } |
jurica238814 | 0:12899fa39f88 | 332 | } |