aconno acnsensa project for iOS devices with iBeacon packets support.

Dependencies:   LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common

Committer:
Dautor
Date:
Fri Mar 02 12:37:27 2018 +0000
Revision:
3:78ceda8ef565
Parent:
2:c0654c5fb771
Child:
4:634796e5b8c3
Made naming be more consistent

Who changed what in which revision?

UserRevisionLine numberNew 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
jurica238814 0:12899fa39f88 5 * All right reserved
jurica238814 0:12899fa39f88 6 *
jurica238814 0:12899fa39f88 7 */
jurica238814 0:12899fa39f88 8
jurica238814 0:12899fa39f88 9 #include "mbed.h"
jurica238814 0:12899fa39f88 10 #include "ble/BLE.h"
jurica238814 0:12899fa39f88 11 #include "acd52832_bsp.h"
jurica238814 0:12899fa39f88 12 #include "GapAdvertisingData.h"
jurica238814 0:12899fa39f88 13 #include "Si7006A20.h"
jurica238814 0:12899fa39f88 14 #include "LSM9DS1.h"
jurica238814 0:12899fa39f88 15 #include "math.h"
jurica238814 0:12899fa39f88 16 #include "nrf52_digital.h"
Dautor 1:326ce5e200fb 17 #include "adc52832_common/utilities.h"
Dautor 1:326ce5e200fb 18 #include "MPL115A1.h"
jurica238814 0:12899fa39f88 19
jurica238814 0:12899fa39f88 20 #define V0 0.47 /* In volts */
jurica238814 0:12899fa39f88 21 #define TC 0.01 /* In volts */
jurica238814 0:12899fa39f88 22 #define VCC (3.6)
Dautor 1:326ce5e200fb 23 #define VALUE_TO_PERCENTAGE (100)
Dautor 1:326ce5e200fb 24 #define WAKEUP_TIME_DELAY_MS (150)
Dautor 1:326ce5e200fb 25 #define APPLICATION_ID (0xCF170059)
Dautor 1:326ce5e200fb 26
Dautor 1:326ce5e200fb 27 #define I2C_DATA (p19)
Dautor 1:326ce5e200fb 28 #define I2C_CLK (p20)
Dautor 1:326ce5e200fb 29 #define SPI_MISO (p5)
Dautor 1:326ce5e200fb 30 #define SPI_MOSI (p3)
Dautor 1:326ce5e200fb 31 #define SPI_SCLK (p4)
jurica238814 0:12899fa39f88 32
jurica238814 0:12899fa39f88 33 #define DEBUG_PRINT (0)
jurica238814 0:12899fa39f88 34 #define SLEEP_TIME (0.150) /* Sleep time in seconds */
jurica238814 0:12899fa39f88 35 #define WAKE_UP_TIME (0.150) /* Awake time in ms */
jurica238814 0:12899fa39f88 36 #define ADV_INTERVAL (100) /* Advertising interval in ms */
jurica238814 0:12899fa39f88 37 #define GO_TO_SLEEP (0) /* Sleep flag: 0 -> Device will not go to sleep, 1 -> Will go to sleep mode */
Dautor 1:326ce5e200fb 38 #define CALIBRATION_STEPS 20
jurica238814 0:12899fa39f88 39
Dautor 1:326ce5e200fb 40 static AnalogIn temperature(ADC_TEMP);
Dautor 1:326ce5e200fb 41 static AnalogIn light(ADC_LIGHT);
Dautor 1:326ce5e200fb 42 static NRF52_DigitalOut lightPower(p28);
Dautor 1:326ce5e200fb 43 static NRF52_DigitalOut temperaturePower(p31);
Dautor 1:326ce5e200fb 44 static NRF52_DigitalOut shdn(p6);
Dautor 1:326ce5e200fb 45 static NRF52_DigitalOut led(p23);
Dautor 1:326ce5e200fb 46 static NRF52_DigitalOut power(p2);
Dautor 1:326ce5e200fb 47 static NRF52_DigitalOut cs(p7);
Dautor 1:326ce5e200fb 48 static Si7006 *si;
Dautor 1:326ce5e200fb 49 static LSM9DS1 *mems;
Dautor 1:326ce5e200fb 50 static SPI *spi;
Dautor 1:326ce5e200fb 51 static MPL115A1 *mpl115a1;
jurica238814 0:12899fa39f88 52
jurica238814 0:12899fa39f88 53 #if DEBUG_PRINT
jurica238814 0:12899fa39f88 54 #include "nrf52_uart.h"
Dautor 1:326ce5e200fb 55 NRF52_UART serial = NRF52_UART(p25, p26, Baud9600); //Tx, RX BaudRate
jurica238814 0:12899fa39f88 56 char buffer[256] = {0};
jurica238814 0:12899fa39f88 57 #define SEND(...) {uint8_t len = sprintf(buffer, __VA_ARGS__); serial.send(buffer, len);}
Dautor 1:326ce5e200fb 58 #define SENDN(...) {uint8_t len = sprintf(buffer "\n\r", __VA_ARGS__); serial.send(buffer, len);}
jurica238814 0:12899fa39f88 59 #else
jurica238814 0:12899fa39f88 60 #define SEND(...);
Dautor 1:326ce5e200fb 61 #define SENDN(...);
jurica238814 0:12899fa39f88 62 #endif
jurica238814 0:12899fa39f88 63
Dautor 1:326ce5e200fb 64 static bool sleepFlag = true;
jurica238814 0:12899fa39f88 65
Dautor 3:78ceda8ef565 66 static vector3_s memsAccelerometerInit;
Dautor 3:78ceda8ef565 67 static vector3_s memsGyroscopeInit;
Dautor 3:78ceda8ef565 68 static vector3_s memsMagnetometerInit;
jurica238814 0:12899fa39f88 69
Dautor 1:326ce5e200fb 70 static BLE &ble = BLE::Instance();
Dautor 1:326ce5e200fb 71 static GapAdvertisingData adv_data = GapAdvertisingData();
jurica238814 0:12899fa39f88 72
Dautor 1:326ce5e200fb 73 struct __attribute__((packed, aligned(1))) advertising_packet{
Dautor 1:326ce5e200fb 74 uint32_t header;
Dautor 1:326ce5e200fb 75 uint8_t type;
Dautor 1:326ce5e200fb 76 union{
Dautor 1:326ce5e200fb 77 struct{
Dautor 3:78ceda8ef565 78 int16_t gyroscope[3];
Dautor 3:78ceda8ef565 79 int16_t accelerometer[3];
Dautor 3:78ceda8ef565 80 int16_t magnetometer[3];
Dautor 1:326ce5e200fb 81 };
Dautor 1:326ce5e200fb 82 struct{
Dautor 1:326ce5e200fb 83 float temperature;
Dautor 1:326ce5e200fb 84 float humidity;
Dautor 1:326ce5e200fb 85 float pressure;
Dautor 1:326ce5e200fb 86 float light;
Dautor 1:326ce5e200fb 87 };
Dautor 1:326ce5e200fb 88 };
Dautor 1:326ce5e200fb 89 };
Dautor 3:78ceda8ef565 90 static advertising_packet advertisementPacket;
jurica238814 0:12899fa39f88 91
jurica238814 0:12899fa39f88 92 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
Dautor 1:326ce5e200fb 93 // Restart Advertising on disconnection
jurica238814 0:12899fa39f88 94 BLE::Instance().gap().startAdvertising();
jurica238814 0:12899fa39f88 95 }
jurica238814 0:12899fa39f88 96
jurica238814 0:12899fa39f88 97 /**
jurica238814 0:12899fa39f88 98 * Function for waking the core up
jurica238814 0:12899fa39f88 99 */
Dautor 1:326ce5e200fb 100 void wakeMeUp(){
Dautor 1:326ce5e200fb 101 sleepFlag = false;
Dautor 1:326ce5e200fb 102 }
jurica238814 0:12899fa39f88 103
jurica238814 0:12899fa39f88 104 /**
jurica238814 0:12899fa39f88 105 * Callback triggered when the ble initialization process has finished
jurica238814 0:12899fa39f88 106 */
jurica238814 0:12899fa39f88 107 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:12899fa39f88 108 BLE& ble = params->ble;
jurica238814 0:12899fa39f88 109 ble_error_t error = params->error;
jurica238814 0:12899fa39f88 110
Dautor 3:78ceda8ef565 111 if (error != BLE_ERROR_NONE){
jurica238814 0:12899fa39f88 112 return;
jurica238814 0:12899fa39f88 113 }
jurica238814 0:12899fa39f88 114
jurica238814 0:12899fa39f88 115 /* Ensure that it is the default instance of BLE */
Dautor 3:78ceda8ef565 116 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE){
jurica238814 0:12899fa39f88 117 return;
jurica238814 0:12899fa39f88 118 }
jurica238814 0:12899fa39f88 119
jurica238814 0:12899fa39f88 120 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 0:12899fa39f88 121
jurica238814 0:12899fa39f88 122 /* setup advertising */
jurica238814 0:12899fa39f88 123 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Dautor 3:78ceda8ef565 124 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
jurica238814 0:12899fa39f88 125 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
jurica238814 0:12899fa39f88 126 ble.gap().setAdvertisingInterval(ADV_INTERVAL);
jurica238814 0:12899fa39f88 127 ble.gap().startAdvertising();
jurica238814 0:12899fa39f88 128 }
jurica238814 0:12899fa39f88 129
Dautor 1:326ce5e200fb 130 float getLight(){
Dautor 1:326ce5e200fb 131 return light.read() * VALUE_TO_PERCENTAGE;
jurica238814 0:12899fa39f88 132 }
jurica238814 0:12899fa39f88 133
jurica238814 0:12899fa39f88 134 float voltage2temp(float vOut){
jurica238814 0:12899fa39f88 135 return ((float)vOut - (float)V0)/((float)TC);
jurica238814 0:12899fa39f88 136 }
jurica238814 0:12899fa39f88 137
Dautor 1:326ce5e200fb 138 float getTemperature(){
Dautor 1:326ce5e200fb 139 return voltage2temp(temperature.read()*(float)VCC);
jurica238814 0:12899fa39f88 140 }
jurica238814 0:12899fa39f88 141
Dautor 1:326ce5e200fb 142 float getHumidity(){
Dautor 1:326ce5e200fb 143 float result;
Dautor 1:326ce5e200fb 144 si->getHumidity(&result);
Dautor 1:326ce5e200fb 145 return result;
jurica238814 0:12899fa39f88 146 }
jurica238814 0:12899fa39f88 147
Dautor 3:78ceda8ef565 148 void readGyroscope(vector3_s *gyroscopeData){
Dautor 3:78ceda8ef565 149 mems->readGyroscope((int16_t *)gyroscopeData);
Dautor 3:78ceda8ef565 150 *gyroscopeData -= memsGyroscopeInit;
jurica238814 0:12899fa39f88 151 }
jurica238814 0:12899fa39f88 152
Dautor 3:78ceda8ef565 153 void readAccelerometer(vector3_s *accelerometerData){
Dautor 3:78ceda8ef565 154 mems->readAccelerometer((int16_t *)accelerometerData);
Dautor 3:78ceda8ef565 155 *accelerometerData -= memsAccelerometerInit;
jurica238814 0:12899fa39f88 156 }
jurica238814 0:12899fa39f88 157
Dautor 3:78ceda8ef565 158 void readMagnetometer(vector3_s *magnetometerData){
Dautor 3:78ceda8ef565 159 mems->readMagnetometer((int16_t *)magnetometerData);
Dautor 3:78ceda8ef565 160 *magnetometerData -= memsMagnetometerInit;
jurica238814 0:12899fa39f88 161 }
jurica238814 0:12899fa39f88 162
Dautor 3:78ceda8ef565 163 void calibrateAccelerometer(){
Dautor 3:78ceda8ef565 164 vector3_s accelerometerData;
Dautor 1:326ce5e200fb 165 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
Dautor 3:78ceda8ef565 166 readAccelerometer(&accelerometerData);
Dautor 3:78ceda8ef565 167 memsAccelerometerInit += accelerometerData;
jurica238814 0:12899fa39f88 168 }
Dautor 3:78ceda8ef565 169 memsAccelerometerInit /= CALIBRATION_STEPS;
jurica238814 0:12899fa39f88 170 }
jurica238814 0:12899fa39f88 171
Dautor 3:78ceda8ef565 172 void calibrateGyroscope(){
Dautor 3:78ceda8ef565 173 vector3_s gyroscopeData;
Dautor 1:326ce5e200fb 174 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
Dautor 3:78ceda8ef565 175 readGyroscope(&gyroscopeData);
Dautor 3:78ceda8ef565 176 memsGyroscopeInit += gyroscopeData;
jurica238814 0:12899fa39f88 177 }
Dautor 3:78ceda8ef565 178 memsGyroscopeInit /= CALIBRATION_STEPS;
jurica238814 0:12899fa39f88 179 }
jurica238814 0:12899fa39f88 180
jurica238814 0:12899fa39f88 181 void calibrateMag(){
Dautor 3:78ceda8ef565 182 vector3_s magnetometerData;
Dautor 1:326ce5e200fb 183 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
Dautor 3:78ceda8ef565 184 readMagnetometer(&magnetometerData);
Dautor 3:78ceda8ef565 185 memsMagnetometerInit += magnetometerData;
jurica238814 0:12899fa39f88 186 }
Dautor 3:78ceda8ef565 187 memsMagnetometerInit /= CALIBRATION_STEPS;
jurica238814 0:12899fa39f88 188 }
jurica238814 0:12899fa39f88 189
jurica238814 0:12899fa39f88 190 void updateData(){
Dautor 3:78ceda8ef565 191 static uint8_t advertisementType = 0;
jurica238814 0:12899fa39f88 192
Dautor 3:78ceda8ef565 193 if(advertisementType < 1){
Dautor 3:78ceda8ef565 194 advertisementPacket.type = 0x00;
Dautor 3:78ceda8ef565 195 readGyroscope((vector3_s *)advertisementPacket.gyroscope);
Dautor 3:78ceda8ef565 196 readAccelerometer((vector3_s *)advertisementPacket.accelerometer);
Dautor 3:78ceda8ef565 197 readMagnetometer((vector3_s *)advertisementPacket.magnetometer);
jurica238814 0:12899fa39f88 198 }
jurica238814 0:12899fa39f88 199 else{
Dautor 3:78ceda8ef565 200 advertisementPacket.type = 0x01;
Dautor 3:78ceda8ef565 201 advertisementPacket.temperature = getTemperature();
Dautor 3:78ceda8ef565 202 advertisementPacket.light = getLight();
Dautor 3:78ceda8ef565 203 advertisementPacket.humidity = getHumidity();
Dautor 3:78ceda8ef565 204 advertisementPacket.pressure = mpl115a1->getPressure();
jurica238814 0:12899fa39f88 205 }
Dautor 3:78ceda8ef565 206 if(++advertisementType > 2) advertisementType = 0;
jurica238814 0:12899fa39f88 207
jurica238814 0:12899fa39f88 208 adv_data = ble.getAdvertisingData();
Dautor 3:78ceda8ef565 209 adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
jurica238814 0:12899fa39f88 210 ble.setAdvertisingData(adv_data);
jurica238814 0:12899fa39f88 211 }
jurica238814 0:12899fa39f88 212
jurica238814 0:12899fa39f88 213
Dautor 1:326ce5e200fb 214 int main(){
jurica238814 0:12899fa39f88 215 power = 1;
Dautor 1:326ce5e200fb 216 wait_ms(WAKEUP_TIME_DELAY_MS);
jurica238814 0:12899fa39f88 217 temperaturePower = 1;
jurica238814 0:12899fa39f88 218 lightPower = 1;
Dautor 1:326ce5e200fb 219 shdn = 1; // Wake up the pressure sensor
Dautor 1:326ce5e200fb 220
Dautor 3:78ceda8ef565 221 advertisementPacket.header = APPLICATION_ID;
Dautor 1:326ce5e200fb 222
Dautor 1:326ce5e200fb 223 ble.init(bleInitComplete);
jurica238814 0:12899fa39f88 224
Dautor 1:326ce5e200fb 225 I2C i2c(I2C_DATA, I2C_CLK);
Dautor 1:326ce5e200fb 226 si = new Si7006(&i2c);
Dautor 2:c0654c5fb771 227 mems = new LSM9DS1(&i2c);
Dautor 1:326ce5e200fb 228 spi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCLK);
Dautor 1:326ce5e200fb 229 mpl115a1 = new MPL115A1(*spi, cs);
jurica238814 0:12899fa39f88 230
Dautor 3:78ceda8ef565 231 mems->startAccelerometer();
Dautor 3:78ceda8ef565 232 mems->startGyroscope();
Dautor 3:78ceda8ef565 233 mems->startMagnetometer();
jurica238814 0:12899fa39f88 234
jurica238814 0:12899fa39f88 235 led = 1;
jurica238814 0:12899fa39f88 236
jurica238814 0:12899fa39f88 237 Ticker ticker;
Dautor 1:326ce5e200fb 238 ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up
jurica238814 0:12899fa39f88 239
Dautor 1:326ce5e200fb 240 while(ble.hasInitialized() == false); /* spin loop */
Dautor 1:326ce5e200fb 241 while(true){
Dautor 1:326ce5e200fb 242 if (sleepFlag && GO_TO_SLEEP){
Dautor 1:326ce5e200fb 243 ble.gap().stopAdvertising();
Dautor 1:326ce5e200fb 244 sleep();
Dautor 1:326ce5e200fb 245 ble.waitForEvent();
Dautor 1:326ce5e200fb 246 }
Dautor 1:326ce5e200fb 247 else{
Dautor 1:326ce5e200fb 248 // I'm awake
Dautor 1:326ce5e200fb 249 updateData();
Dautor 1:326ce5e200fb 250 ble.gap().startAdvertising();
Dautor 1:326ce5e200fb 251 wait_ms(WAKE_UP_TIME);
Dautor 1:326ce5e200fb 252 sleepFlag = true;
jurica238814 0:12899fa39f88 253 }
jurica238814 0:12899fa39f88 254 }
jurica238814 0:12899fa39f88 255 }