aconno acnsensa project for iOS devices with iBeacon packets support.

Dependencies:   LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Mon Aug 06 12:35:48 2018 +0200
Revision:
30:eadbeb23fe95
Parent:
29:b65cab4fa70f
Child:
33:c93573163958
Data structure fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 30:eadbeb23fe95 1 /*
jurica238814 30:eadbeb23fe95 2 * aconno.de
jurica238814 30:eadbeb23fe95 3 * Made by Jurica Resetar
jurica238814 30:eadbeb23fe95 4 * Edited by Karlo Milicevic
jurica238814 30:eadbeb23fe95 5 * Edited by Dominik Bartolovic
jurica238814 30:eadbeb23fe95 6 * All right reserved
jurica238814 30:eadbeb23fe95 7 *
jurica238814 30:eadbeb23fe95 8 */
jurica238814 30:eadbeb23fe95 9
jurica238814 30:eadbeb23fe95 10 #include "mbed.h"
jurica238814 30:eadbeb23fe95 11 #include "ble/BLE.h"
jurica238814 30:eadbeb23fe95 12 #include "acd52832_bsp.h"
jurica238814 30:eadbeb23fe95 13 #include "GapAdvertisingData.h"
jurica238814 30:eadbeb23fe95 14 #include "Si7006A20.h"
jurica238814 30:eadbeb23fe95 15 #include "LSM9DS1.h"
jurica238814 30:eadbeb23fe95 16 #include "math.h"
jurica238814 30:eadbeb23fe95 17 #include "nrf52_digital.h"
jurica238814 30:eadbeb23fe95 18 #include "adc52832_common/utilities.h"
jurica238814 30:eadbeb23fe95 19 #include "MPL115A1.h"
jurica238814 30:eadbeb23fe95 20 #include "acd_nrf52_saadc.h"
jurica238814 30:eadbeb23fe95 21 #include "service.h"
jurica238814 30:eadbeb23fe95 22 #include <events/mbed_events.h>
jurica238814 30:eadbeb23fe95 23 #include "aconnoConfig.h"
jurica238814 30:eadbeb23fe95 24
jurica238814 30:eadbeb23fe95 25 #define V0 0.47 /* In volts */
jurica238814 30:eadbeb23fe95 26 #define TC 0.01 /* In volts */
jurica238814 30:eadbeb23fe95 27 #define VCC (3.6)
jurica238814 30:eadbeb23fe95 28 #define VALUE_TO_PERCENTAGE (100)
jurica238814 30:eadbeb23fe95 29 #define WAKEUP_TIME_DELAY_MS (150)
jurica238814 30:eadbeb23fe95 30 #define APPLICATION_ID (0xCF170059)
jurica238814 30:eadbeb23fe95 31
jurica238814 30:eadbeb23fe95 32 #define ADC_REFERENCE (3.6f) /* adc reference voltage */
jurica238814 30:eadbeb23fe95 33 #define ADC_RESOLUTION (1024) /* 10-bit adc */
jurica238814 30:eadbeb23fe95 34
jurica238814 30:eadbeb23fe95 35 #define I2C_DATA (p19)
jurica238814 30:eadbeb23fe95 36 #define I2C_CLK (p20)
jurica238814 30:eadbeb23fe95 37 #define SPI_MISO (p5)
jurica238814 30:eadbeb23fe95 38 #define SPI_MOSI (p3)
jurica238814 30:eadbeb23fe95 39 #define SPI_SCLK (p4)
jurica238814 30:eadbeb23fe95 40
jurica238814 30:eadbeb23fe95 41 #define DEBUG (0)
jurica238814 30:eadbeb23fe95 42 #define DEBUG_PRINT (1)
jurica238814 30:eadbeb23fe95 43 #define SLEEP_TIME (0.150) /* Sleep time in seconds */
jurica238814 30:eadbeb23fe95 44 #define WAKE_UP_TIME (0.150) /* Awake time in ms */
jurica238814 30:eadbeb23fe95 45 #define ADV_INTERVAL (1000) /* Advertising interval in ms */
jurica238814 30:eadbeb23fe95 46 #define GO_TO_SLEEP (0)
jurica238814 30:eadbeb23fe95 47 /* Sleep flag: 0 -> Device will not go to sleep, 1 -> Will go to sleep mode */
jurica238814 30:eadbeb23fe95 48 #define CALIBRATION_STEPS (20)
jurica238814 30:eadbeb23fe95 49 #define TX_POWER_DB (4)
jurica238814 30:eadbeb23fe95 50 #define INVERT_AXES (0)
jurica238814 30:eadbeb23fe95 51
jurica238814 30:eadbeb23fe95 52 uint8_t gConnected = 0;
jurica238814 30:eadbeb23fe95 53
jurica238814 30:eadbeb23fe95 54 static NRF52_SAADC analogIn;
jurica238814 30:eadbeb23fe95 55 static NRF52_DigitalOut lightPower(p28);
jurica238814 30:eadbeb23fe95 56 static NRF52_DigitalOut temperaturePower(p31);
jurica238814 30:eadbeb23fe95 57 static NRF52_DigitalOut shdn(p6);
jurica238814 30:eadbeb23fe95 58 static NRF52_DigitalOut power(p2);
jurica238814 30:eadbeb23fe95 59 static NRF52_DigitalOut cs(p7);
jurica238814 30:eadbeb23fe95 60 static Si7006 *si;
jurica238814 30:eadbeb23fe95 61 static LSM9DS1 *mems;
jurica238814 30:eadbeb23fe95 62 static SPI *spi;
jurica238814 30:eadbeb23fe95 63 static MPL115A1 *mpl115a1;
jurica238814 30:eadbeb23fe95 64
jurica238814 30:eadbeb23fe95 65 static EventQueue eventQueue(32 * EVENTS_EVENT_SIZE);
jurica238814 30:eadbeb23fe95 66 uint8_t myMacAddress[6] = {};
jurica238814 30:eadbeb23fe95 67 MACService *macServicePtr;
jurica238814 30:eadbeb23fe95 68
jurica238814 30:eadbeb23fe95 69 #if DEBUG_PRINT
jurica238814 30:eadbeb23fe95 70 #include "SEGGER_RTT.h"
jurica238814 30:eadbeb23fe95 71 #define printf(...) SEGGER_RTT_printf(0, __VA_ARGS__)
jurica238814 30:eadbeb23fe95 72 #else
jurica238814 30:eadbeb23fe95 73 #define printf(...)
jurica238814 30:eadbeb23fe95 74 #endif
jurica238814 30:eadbeb23fe95 75
jurica238814 30:eadbeb23fe95 76 static vector3_s memsAccelerometerInit;
jurica238814 30:eadbeb23fe95 77 static vector3_s memsGyroscopeInit;
jurica238814 30:eadbeb23fe95 78 static vector3_s memsMagnetometerInit;
jurica238814 30:eadbeb23fe95 79
jurica238814 30:eadbeb23fe95 80 static GapAdvertisingData adv_data = GapAdvertisingData();
jurica238814 30:eadbeb23fe95 81
jurica238814 30:eadbeb23fe95 82 struct __attribute__((packed, aligned(1))) iBeaconMSD_t
jurica238814 30:eadbeb23fe95 83 {
jurica238814 30:eadbeb23fe95 84 // AppleID is constant
jurica238814 30:eadbeb23fe95 85 uint16_t appleID;
jurica238814 30:eadbeb23fe95 86 // secondID is constant
jurica238814 30:eadbeb23fe95 87 uint8_t secondID;
jurica238814 30:eadbeb23fe95 88 // DataSize is constant
jurica238814 30:eadbeb23fe95 89 uint8_t dataSize;
jurica238814 30:eadbeb23fe95 90 uint8_t UUID[16];
jurica238814 30:eadbeb23fe95 91 uint16_t major;
jurica238814 30:eadbeb23fe95 92 uint16_t minor;
jurica238814 30:eadbeb23fe95 93 int8_t RSSI;
jurica238814 30:eadbeb23fe95 94 }static iBeaconMSD = {.appleID = 0x004C,
jurica238814 30:eadbeb23fe95 95 .secondID = 0x02,
jurica238814 30:eadbeb23fe95 96 .dataSize = 0x15,
jurica238814 30:eadbeb23fe95 97 .UUID = {UUID_INIT},
jurica238814 30:eadbeb23fe95 98 .major = MAJOR,
jurica238814 30:eadbeb23fe95 99 .minor = MINOR,
jurica238814 30:eadbeb23fe95 100 .RSSI = RSSI_INIT};
jurica238814 30:eadbeb23fe95 101
jurica238814 30:eadbeb23fe95 102 struct __attribute__((packed, aligned(1))) advertising_packet
jurica238814 30:eadbeb23fe95 103 {
jurica238814 30:eadbeb23fe95 104 uint32_t header;
jurica238814 30:eadbeb23fe95 105 uint8_t type;
jurica238814 30:eadbeb23fe95 106 union{
jurica238814 30:eadbeb23fe95 107 struct{
jurica238814 30:eadbeb23fe95 108 int16_t gyroscope[3];
jurica238814 30:eadbeb23fe95 109 int16_t accelerometer[3];
jurica238814 30:eadbeb23fe95 110 int16_t magnetometer[3];
jurica238814 30:eadbeb23fe95 111 uint16_t acc_lsb_value;
jurica238814 30:eadbeb23fe95 112 };
jurica238814 30:eadbeb23fe95 113 struct{
jurica238814 30:eadbeb23fe95 114 float temperature;
jurica238814 30:eadbeb23fe95 115 float humidity;
jurica238814 30:eadbeb23fe95 116 float pressure;
jurica238814 30:eadbeb23fe95 117 float light;
jurica238814 30:eadbeb23fe95 118 uint8_t battery;
jurica238814 30:eadbeb23fe95 119 };
jurica238814 30:eadbeb23fe95 120 };
jurica238814 30:eadbeb23fe95 121 };
jurica238814 30:eadbeb23fe95 122
jurica238814 30:eadbeb23fe95 123 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context)
jurica238814 30:eadbeb23fe95 124 {
jurica238814 30:eadbeb23fe95 125 BLE &ble = context->ble;
jurica238814 30:eadbeb23fe95 126 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
jurica238814 30:eadbeb23fe95 127 }
jurica238814 30:eadbeb23fe95 128
jurica238814 30:eadbeb23fe95 129 static advertising_packet advertisementPacket;
jurica238814 30:eadbeb23fe95 130
jurica238814 30:eadbeb23fe95 131 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
jurica238814 30:eadbeb23fe95 132 {
jurica238814 30:eadbeb23fe95 133 // Restart Advertising on disconnection
jurica238814 30:eadbeb23fe95 134 gConnected = 0;
jurica238814 30:eadbeb23fe95 135 BLE::Instance().gap().startAdvertising();
jurica238814 30:eadbeb23fe95 136 }
jurica238814 30:eadbeb23fe95 137
jurica238814 30:eadbeb23fe95 138 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params)
jurica238814 30:eadbeb23fe95 139 {
jurica238814 30:eadbeb23fe95 140 printf("Connection callback.\n");
jurica238814 30:eadbeb23fe95 141 gConnected = 1;
jurica238814 30:eadbeb23fe95 142 }
jurica238814 30:eadbeb23fe95 143
jurica238814 30:eadbeb23fe95 144 /**
jurica238814 30:eadbeb23fe95 145 * Callback triggered when the ble initialization process has finished
jurica238814 30:eadbeb23fe95 146 */
jurica238814 30:eadbeb23fe95 147 void bleInitCompleteSensors(BLE::InitializationCompleteCallbackContext *params)
jurica238814 30:eadbeb23fe95 148 {
jurica238814 30:eadbeb23fe95 149 BLE& ble = params->ble;
jurica238814 30:eadbeb23fe95 150 ble_error_t error = params->error;
jurica238814 30:eadbeb23fe95 151
jurica238814 30:eadbeb23fe95 152 if (error != BLE_ERROR_NONE){
jurica238814 30:eadbeb23fe95 153 return;
jurica238814 30:eadbeb23fe95 154 }
jurica238814 30:eadbeb23fe95 155
jurica238814 30:eadbeb23fe95 156 /* Ensure that it is the default instance of BLE */
jurica238814 30:eadbeb23fe95 157 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE){
jurica238814 30:eadbeb23fe95 158 return;
jurica238814 30:eadbeb23fe95 159 }
jurica238814 30:eadbeb23fe95 160
jurica238814 30:eadbeb23fe95 161 uint8_t mac[6] = {0,0,0,0,0,0};
jurica238814 30:eadbeb23fe95 162 BLEProtocol::AddressType_t temp_address_type;
jurica238814 30:eadbeb23fe95 163 ble.gap().getAddress(&temp_address_type, myMacAddress);
jurica238814 30:eadbeb23fe95 164 macServicePtr = new MACService(ble, mac);
jurica238814 30:eadbeb23fe95 165 macServicePtr->updateMacAddress(myMacAddress); // Update MAC address
jurica238814 30:eadbeb23fe95 166
jurica238814 30:eadbeb23fe95 167 ble.gap().onConnection(onConnectionCallback);
jurica238814 30:eadbeb23fe95 168 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 30:eadbeb23fe95 169
jurica238814 30:eadbeb23fe95 170 /* setup advertising */
jurica238814 30:eadbeb23fe95 171 ble.gap().accumulateAdvertisingPayload(
jurica238814 30:eadbeb23fe95 172 GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 30:eadbeb23fe95 173 ble.gap().accumulateAdvertisingPayload(
jurica238814 30:eadbeb23fe95 174 GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
jurica238814 30:eadbeb23fe95 175 (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
jurica238814 30:eadbeb23fe95 176 ble.gap().setAdvertisingType(
jurica238814 30:eadbeb23fe95 177 GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jurica238814 30:eadbeb23fe95 178 ble.gap().setAdvertisingInterval(ADV_INTERVAL);
jurica238814 30:eadbeb23fe95 179 printf("Init started....\t\t");
jurica238814 30:eadbeb23fe95 180 ble.gap().setTxPower(TX_POWER_DB); // Set TX power to TX_POWER_DB
jurica238814 30:eadbeb23fe95 181 ble.gap().startAdvertising();
jurica238814 30:eadbeb23fe95 182 printf("Init done.\n");
jurica238814 30:eadbeb23fe95 183 }
jurica238814 30:eadbeb23fe95 184
jurica238814 30:eadbeb23fe95 185 float getLight()
jurica238814 30:eadbeb23fe95 186 {
jurica238814 30:eadbeb23fe95 187 return ((float)analogIn.getData()[1])/ADC_RESOLUTION * VALUE_TO_PERCENTAGE;
jurica238814 30:eadbeb23fe95 188 }
jurica238814 30:eadbeb23fe95 189
jurica238814 30:eadbeb23fe95 190 float voltage2temp(float vOut)
jurica238814 30:eadbeb23fe95 191 {
jurica238814 30:eadbeb23fe95 192 return ((float)vOut - (float)V0)/((float)TC);
jurica238814 30:eadbeb23fe95 193 }
jurica238814 30:eadbeb23fe95 194
jurica238814 30:eadbeb23fe95 195 float getTemperature()
jurica238814 30:eadbeb23fe95 196 {
jurica238814 30:eadbeb23fe95 197 return voltage2temp(((float)analogIn.getData()[2])/ADC_RESOLUTION * (float)VCC);
jurica238814 30:eadbeb23fe95 198 }
jurica238814 30:eadbeb23fe95 199
jurica238814 30:eadbeb23fe95 200 uint8_t getBattery()
jurica238814 30:eadbeb23fe95 201 {
jurica238814 30:eadbeb23fe95 202 uint16_t batteryVoltage = analogIn.getData()[0];
jurica238814 30:eadbeb23fe95 203 const uint16_t zero_percent_limit = 739;
jurica238814 30:eadbeb23fe95 204 const uint16_t onehundred_percent_limit = 810;
jurica238814 30:eadbeb23fe95 205 const uint16_t percentage_increments = 5;
jurica238814 30:eadbeb23fe95 206 uint8_t percentage;
jurica238814 30:eadbeb23fe95 207
jurica238814 30:eadbeb23fe95 208 if (batteryVoltage < zero_percent_limit)
jurica238814 30:eadbeb23fe95 209 {
jurica238814 30:eadbeb23fe95 210 percentage = 0;
jurica238814 30:eadbeb23fe95 211 }
jurica238814 30:eadbeb23fe95 212 else if(batteryVoltage > onehundred_percent_limit)
jurica238814 30:eadbeb23fe95 213 {
jurica238814 30:eadbeb23fe95 214 percentage = 100;
jurica238814 30:eadbeb23fe95 215 }
jurica238814 30:eadbeb23fe95 216 else
jurica238814 30:eadbeb23fe95 217 {
jurica238814 30:eadbeb23fe95 218 batteryVoltage -= zero_percent_limit;
jurica238814 30:eadbeb23fe95 219 percentage = (batteryVoltage*100)/(onehundred_percent_limit - zero_percent_limit);
jurica238814 30:eadbeb23fe95 220 percentage = percentage/percentage_increments*percentage_increments;
jurica238814 30:eadbeb23fe95 221 }
jurica238814 30:eadbeb23fe95 222
jurica238814 30:eadbeb23fe95 223 return percentage;
jurica238814 30:eadbeb23fe95 224 }
jurica238814 30:eadbeb23fe95 225
jurica238814 30:eadbeb23fe95 226 float getHumidity()
jurica238814 30:eadbeb23fe95 227 {
jurica238814 30:eadbeb23fe95 228 float result;
jurica238814 30:eadbeb23fe95 229 si->getHumidity(&result);
jurica238814 30:eadbeb23fe95 230 return result;
jurica238814 30:eadbeb23fe95 231 }
jurica238814 30:eadbeb23fe95 232
jurica238814 30:eadbeb23fe95 233 void readGyroscope(vector3_s *gyroscopeData)
jurica238814 30:eadbeb23fe95 234 {
jurica238814 30:eadbeb23fe95 235 mems->readGyroscope((int16_t *)gyroscopeData);
jurica238814 30:eadbeb23fe95 236 *gyroscopeData -= memsGyroscopeInit;
jurica238814 30:eadbeb23fe95 237 }
jurica238814 30:eadbeb23fe95 238
jurica238814 30:eadbeb23fe95 239 void readAccelerometer(vector3_s *accelerometerData)
jurica238814 30:eadbeb23fe95 240 {
jurica238814 30:eadbeb23fe95 241 mems->readAccelerometer((int16_t *)accelerometerData);
jurica238814 30:eadbeb23fe95 242 *accelerometerData -= memsAccelerometerInit;
jurica238814 30:eadbeb23fe95 243 }
jurica238814 30:eadbeb23fe95 244
jurica238814 30:eadbeb23fe95 245 void readMagnetometer(vector3_s *magnetometerData){
jurica238814 30:eadbeb23fe95 246 mems->readMagnetometer((int16_t *)magnetometerData);
jurica238814 30:eadbeb23fe95 247 *magnetometerData -= memsMagnetometerInit;
jurica238814 30:eadbeb23fe95 248 }
jurica238814 30:eadbeb23fe95 249
jurica238814 30:eadbeb23fe95 250 void calibrateAccelerometer(){
jurica238814 30:eadbeb23fe95 251 vector3_s accelerometerData;
jurica238814 30:eadbeb23fe95 252 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter)
jurica238814 30:eadbeb23fe95 253 {
jurica238814 30:eadbeb23fe95 254 readAccelerometer(&accelerometerData);
jurica238814 30:eadbeb23fe95 255 memsAccelerometerInit += accelerometerData;
jurica238814 30:eadbeb23fe95 256 }
jurica238814 30:eadbeb23fe95 257 memsAccelerometerInit /= CALIBRATION_STEPS;
jurica238814 30:eadbeb23fe95 258 }
jurica238814 30:eadbeb23fe95 259
jurica238814 30:eadbeb23fe95 260 void calibrateGyroscope(){
jurica238814 30:eadbeb23fe95 261 vector3_s gyroscopeData;
jurica238814 30:eadbeb23fe95 262 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter)
jurica238814 30:eadbeb23fe95 263 {
jurica238814 30:eadbeb23fe95 264 readGyroscope(&gyroscopeData);
jurica238814 30:eadbeb23fe95 265 memsGyroscopeInit += gyroscopeData;
jurica238814 30:eadbeb23fe95 266 }
jurica238814 30:eadbeb23fe95 267 memsGyroscopeInit /= CALIBRATION_STEPS;
jurica238814 30:eadbeb23fe95 268 }
jurica238814 30:eadbeb23fe95 269
jurica238814 30:eadbeb23fe95 270 void calibrateMag(){
jurica238814 30:eadbeb23fe95 271 vector3_s magnetometerData;
jurica238814 30:eadbeb23fe95 272 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter)
jurica238814 30:eadbeb23fe95 273 {
jurica238814 30:eadbeb23fe95 274 readMagnetometer(&magnetometerData);
jurica238814 30:eadbeb23fe95 275 memsMagnetometerInit += magnetometerData;
jurica238814 30:eadbeb23fe95 276 }
jurica238814 30:eadbeb23fe95 277 memsMagnetometerInit /= CALIBRATION_STEPS;
jurica238814 30:eadbeb23fe95 278 }
jurica238814 30:eadbeb23fe95 279
jurica238814 30:eadbeb23fe95 280 void updateData(){
jurica238814 30:eadbeb23fe95 281 static uint8_t advertisementType = 0;
jurica238814 30:eadbeb23fe95 282 int16_t temp_acc[3];
jurica238814 30:eadbeb23fe95 283 BLE &ble = BLE::Instance();
jurica238814 30:eadbeb23fe95 284
jurica238814 30:eadbeb23fe95 285 if(!advertisementType && !gConnected)
jurica238814 30:eadbeb23fe95 286 {
jurica238814 30:eadbeb23fe95 287 printf("Sensor format 1.\n");
jurica238814 30:eadbeb23fe95 288 ble.gap().clearAdvertisingPayload();
jurica238814 30:eadbeb23fe95 289 /* setup advertising */
jurica238814 30:eadbeb23fe95 290 ble.gap().accumulateAdvertisingPayload(
jurica238814 30:eadbeb23fe95 291 GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 30:eadbeb23fe95 292 ble.gap().accumulateAdvertisingPayload(
jurica238814 30:eadbeb23fe95 293 GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
jurica238814 30:eadbeb23fe95 294 (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
jurica238814 30:eadbeb23fe95 295 ble.gap().setAdvertisingType(
jurica238814 30:eadbeb23fe95 296 GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jurica238814 30:eadbeb23fe95 297
jurica238814 30:eadbeb23fe95 298 adv_data = ble.getAdvertisingData();
jurica238814 30:eadbeb23fe95 299 advertisementPacket.type = 0x00;
jurica238814 30:eadbeb23fe95 300 readGyroscope((vector3_s *)advertisementPacket.gyroscope);
jurica238814 30:eadbeb23fe95 301 readAccelerometer((vector3_s *)temp_acc);
jurica238814 30:eadbeb23fe95 302 readMagnetometer((vector3_s *)advertisementPacket.magnetometer);
jurica238814 30:eadbeb23fe95 303 advertisementPacket.acc_lsb_value = (0xF9E);
jurica238814 30:eadbeb23fe95 304 // ^--- That's in ug cuz MSB is 1
jurica238814 30:eadbeb23fe95 305 #if INVERT_AXES
jurica238814 30:eadbeb23fe95 306 advertisementPacket.accelerometer[0] = temp_acc[1];
jurica238814 30:eadbeb23fe95 307 advertisementPacket.accelerometer[1] = temp_acc[0];
jurica238814 30:eadbeb23fe95 308 advertisementPacket.accelerometer[2] = temp_acc[2];
jurica238814 30:eadbeb23fe95 309 #endif
jurica238814 30:eadbeb23fe95 310
jurica238814 30:eadbeb23fe95 311 adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA,
jurica238814 30:eadbeb23fe95 312 (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
jurica238814 30:eadbeb23fe95 313 ble.setAdvertisingData(adv_data);
jurica238814 30:eadbeb23fe95 314 }
jurica238814 30:eadbeb23fe95 315 else if (advertisementType == 1 && !gConnected)
jurica238814 30:eadbeb23fe95 316 {
jurica238814 30:eadbeb23fe95 317 printf("Sensor format 2.\n");
jurica238814 30:eadbeb23fe95 318 analogIn.updateData();
jurica238814 30:eadbeb23fe95 319 adv_data = ble.getAdvertisingData();
jurica238814 30:eadbeb23fe95 320 advertisementPacket.type = 0x01;
jurica238814 30:eadbeb23fe95 321 advertisementPacket.temperature = getTemperature();
jurica238814 30:eadbeb23fe95 322 advertisementPacket.light = getLight();
jurica238814 30:eadbeb23fe95 323 advertisementPacket.humidity = getHumidity();
jurica238814 30:eadbeb23fe95 324 advertisementPacket.pressure = mpl115a1->getPressure();
jurica238814 30:eadbeb23fe95 325 advertisementPacket.battery = getBattery();
jurica238814 30:eadbeb23fe95 326
jurica238814 30:eadbeb23fe95 327 adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA,
jurica238814 30:eadbeb23fe95 328 (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
jurica238814 30:eadbeb23fe95 329 ble.setAdvertisingData(adv_data);
jurica238814 30:eadbeb23fe95 330 }
jurica238814 30:eadbeb23fe95 331
jurica238814 30:eadbeb23fe95 332 else if (!gConnected)
jurica238814 30:eadbeb23fe95 333 {
jurica238814 30:eadbeb23fe95 334 printf("Beacon format!\n");
jurica238814 30:eadbeb23fe95 335 ble.gap().clearAdvertisingPayload();
jurica238814 30:eadbeb23fe95 336 ble.gap().accumulateAdvertisingPayload(
jurica238814 30:eadbeb23fe95 337 GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 30:eadbeb23fe95 338 ble.gap().accumulateAdvertisingPayload(
jurica238814 30:eadbeb23fe95 339 GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
jurica238814 30:eadbeb23fe95 340 (uint8_t*)&iBeaconMSD, sizeof(iBeaconMSD_t));
jurica238814 30:eadbeb23fe95 341 ble.gap().startAdvertising();
jurica238814 30:eadbeb23fe95 342 }
jurica238814 30:eadbeb23fe95 343 if(++advertisementType > 2) advertisementType = 0;
jurica238814 30:eadbeb23fe95 344 }
jurica238814 30:eadbeb23fe95 345
jurica238814 30:eadbeb23fe95 346 int main()
jurica238814 30:eadbeb23fe95 347 {
jurica238814 30:eadbeb23fe95 348 printf("Main started.\n");
jurica238814 30:eadbeb23fe95 349
jurica238814 30:eadbeb23fe95 350 Thread bleT;
jurica238814 30:eadbeb23fe95 351
jurica238814 30:eadbeb23fe95 352 power = 1;
jurica238814 30:eadbeb23fe95 353 wait_ms(WAKEUP_TIME_DELAY_MS);
jurica238814 30:eadbeb23fe95 354 temperaturePower = 1;
jurica238814 30:eadbeb23fe95 355 lightPower = 1;
jurica238814 30:eadbeb23fe95 356 shdn = 1; // Wake up the pressure sensor
jurica238814 30:eadbeb23fe95 357 analogIn.addChannel(9); // Set VDD as source to SAADC
jurica238814 30:eadbeb23fe95 358 analogIn.addChannel(6); // Light
jurica238814 30:eadbeb23fe95 359 analogIn.addChannel(7); // Temp
jurica238814 30:eadbeb23fe95 360 analogIn.calibrate();
jurica238814 30:eadbeb23fe95 361
jurica238814 30:eadbeb23fe95 362 BLE &ble = BLE::Instance();
jurica238814 30:eadbeb23fe95 363 ble.init(bleInitCompleteSensors);
jurica238814 30:eadbeb23fe95 364 while(ble.hasInitialized() == false){
jurica238814 30:eadbeb23fe95 365 /* spin loop */
jurica238814 30:eadbeb23fe95 366 }
jurica238814 30:eadbeb23fe95 367 ble.onEventsToProcess(scheduleBleEventsProcessing);
jurica238814 30:eadbeb23fe95 368 advertisementPacket.header = APPLICATION_ID;
jurica238814 30:eadbeb23fe95 369
jurica238814 30:eadbeb23fe95 370 I2C i2c(I2C_DATA, I2C_CLK);
jurica238814 30:eadbeb23fe95 371 si = new Si7006(&i2c);
jurica238814 30:eadbeb23fe95 372 mems = new LSM9DS1(&i2c);
jurica238814 30:eadbeb23fe95 373 spi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCLK);
jurica238814 30:eadbeb23fe95 374 mpl115a1 = new MPL115A1(*spi, cs);
jurica238814 30:eadbeb23fe95 375
jurica238814 30:eadbeb23fe95 376 mems->startAccelerometer();
jurica238814 30:eadbeb23fe95 377 mems->startGyroscope();
jurica238814 30:eadbeb23fe95 378 mems->startMagnetometer();
jurica238814 30:eadbeb23fe95 379
jurica238814 30:eadbeb23fe95 380 eventQueue.call_every(500, updateData);
jurica238814 30:eadbeb23fe95 381
jurica238814 30:eadbeb23fe95 382 // This call stops main thread
jurica238814 30:eadbeb23fe95 383 eventQueue.dispatch_forever();
jurica238814 30:eadbeb23fe95 384
jurica238814 30:eadbeb23fe95 385 }