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 14:11:51 2018 +0200
Revision:
33:c93573163958
Parent:
32:168317e16c69
Parent:
30:eadbeb23fe95
Child:
34:714dfddf2cd9
Heads merged

Who changed what in which revision?

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