aconno acnsensa project for iOS devices with iBeacon packets support.

Dependencies:   LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common

Committer:
Dautor
Date:
Tue Mar 20 08:40:21 2018 +0000
Revision:
6:51745805d8b0
Parent:
4:634796e5b8c3
Child:
7:7fa0da697c8d
Fixed data read

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