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