aconno acnsensa project for iOS devices with iBeacon packets support.

Dependencies:   LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common

Committer:
Dautor
Date:
Wed Dec 13 09:37:21 2017 +0000
Revision:
1:326ce5e200fb
Parent:
0:12899fa39f88
Child:
2:c0654c5fb771
Code refactoring.

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 1:326ce5e200fb 66 static vector3_s memsAccInit;
Dautor 1:326ce5e200fb 67 static vector3_s memsGyrInit;
Dautor 1:326ce5e200fb 68 static vector3_s memsMagInit;
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 1:326ce5e200fb 78 int16_t gyr[3];
Dautor 1:326ce5e200fb 79 int16_t acc[3];
Dautor 1:326ce5e200fb 80 int16_t mag[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 1:326ce5e200fb 90 static advertising_packet advPacket;
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 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 0:12899fa39f88 105 /* Avoid compiler warnings */
jurica238814 0:12899fa39f88 106 (void) ble;
jurica238814 0:12899fa39f88 107 (void) error;
jurica238814 0:12899fa39f88 108 }
jurica238814 0:12899fa39f88 109
jurica238814 0:12899fa39f88 110 /**
jurica238814 0:12899fa39f88 111 * Callback triggered when the ble initialization process has finished
jurica238814 0:12899fa39f88 112 */
jurica238814 0:12899fa39f88 113 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:12899fa39f88 114 BLE& ble = params->ble;
jurica238814 0:12899fa39f88 115 ble_error_t error = params->error;
jurica238814 0:12899fa39f88 116
jurica238814 0:12899fa39f88 117 if (error != BLE_ERROR_NONE) {
jurica238814 0:12899fa39f88 118 onBleInitError(ble, error);
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 */
jurica238814 0:12899fa39f88 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 1:326ce5e200fb 131 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advPacket, sizeof(advPacket));
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 1:326ce5e200fb 138 return light.read() * 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 1:326ce5e200fb 146 return voltage2temp(temperature.read()*(float)VCC);
jurica238814 0:12899fa39f88 147 }
jurica238814 0:12899fa39f88 148
Dautor 1:326ce5e200fb 149 float getHumidity(){
Dautor 1:326ce5e200fb 150 float result;
Dautor 1:326ce5e200fb 151 si->getHumidity(&result);
Dautor 1:326ce5e200fb 152 return result;
jurica238814 0:12899fa39f88 153 }
jurica238814 0:12899fa39f88 154
Dautor 1:326ce5e200fb 155 void readGyr(vector3_s *gyro_data){
Dautor 1:326ce5e200fb 156 mems->readGyro((int16_t *)gyro_data);
Dautor 1:326ce5e200fb 157 *gyro_data -= memsGyrInit;
jurica238814 0:12899fa39f88 158 }
jurica238814 0:12899fa39f88 159
Dautor 1:326ce5e200fb 160 void readAcc(vector3_s *acc_data){
Dautor 1:326ce5e200fb 161 mems->readAcc((int16_t *)acc_data);
Dautor 1:326ce5e200fb 162 *acc_data -= memsAccInit;
jurica238814 0:12899fa39f88 163 }
jurica238814 0:12899fa39f88 164
Dautor 1:326ce5e200fb 165 void readMag(vector3_s *magneto_data){
Dautor 1:326ce5e200fb 166 mems->readMag((int16_t *)magneto_data);
Dautor 1:326ce5e200fb 167 *magneto_data -= memsMagInit;
jurica238814 0:12899fa39f88 168 }
jurica238814 0:12899fa39f88 169
jurica238814 0:12899fa39f88 170 void calibrateAcc(){
Dautor 1:326ce5e200fb 171 vector3_s acc_data;
Dautor 1:326ce5e200fb 172 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
Dautor 1:326ce5e200fb 173 readAcc(&acc_data);
Dautor 1:326ce5e200fb 174 memsAccInit += acc_data;
jurica238814 0:12899fa39f88 175 }
Dautor 1:326ce5e200fb 176 memsAccInit /= CALIBRATION_STEPS;
jurica238814 0:12899fa39f88 177 }
jurica238814 0:12899fa39f88 178
Dautor 1:326ce5e200fb 179 void calibrateGyr(){
Dautor 1:326ce5e200fb 180 vector3_s gyro_data;
Dautor 1:326ce5e200fb 181 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
Dautor 1:326ce5e200fb 182 readGyr(&gyro_data);
Dautor 1:326ce5e200fb 183 memsGyrInit += gyro_data;
jurica238814 0:12899fa39f88 184 }
Dautor 1:326ce5e200fb 185 memsGyrInit /= CALIBRATION_STEPS;
jurica238814 0:12899fa39f88 186 }
jurica238814 0:12899fa39f88 187
jurica238814 0:12899fa39f88 188 void calibrateMag(){
Dautor 1:326ce5e200fb 189 vector3_s mag_data;
Dautor 1:326ce5e200fb 190 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
Dautor 1:326ce5e200fb 191 readMag(&mag_data);
Dautor 1:326ce5e200fb 192 memsMagInit += mag_data;
jurica238814 0:12899fa39f88 193 }
Dautor 1:326ce5e200fb 194 memsMagInit /= CALIBRATION_STEPS;
jurica238814 0:12899fa39f88 195 }
jurica238814 0:12899fa39f88 196
jurica238814 0:12899fa39f88 197 void updateData(){
jurica238814 0:12899fa39f88 198 static uint8_t adv_type = 0;
jurica238814 0:12899fa39f88 199
Dautor 1:326ce5e200fb 200 if(adv_type < 1){
Dautor 1:326ce5e200fb 201 advPacket.type = 0x00;
Dautor 1:326ce5e200fb 202 readGyr((vector3_s *)advPacket.gyr);
Dautor 1:326ce5e200fb 203 readAcc((vector3_s *)advPacket.acc);
Dautor 1:326ce5e200fb 204 readMag((vector3_s *)advPacket.mag);
jurica238814 0:12899fa39f88 205 }
jurica238814 0:12899fa39f88 206 else{
Dautor 1:326ce5e200fb 207 advPacket.type = 0x01;
Dautor 1:326ce5e200fb 208 advPacket.temperature = getTemperature();
Dautor 1:326ce5e200fb 209 advPacket.light = getLight();
Dautor 1:326ce5e200fb 210 advPacket.humidity = getHumidity();
Dautor 1:326ce5e200fb 211 advPacket.pressure = mpl115a1->getPressure();
jurica238814 0:12899fa39f88 212 }
Dautor 1:326ce5e200fb 213 if(++adv_type > 2) adv_type = 0;
jurica238814 0:12899fa39f88 214
jurica238814 0:12899fa39f88 215 adv_data = ble.getAdvertisingData();
Dautor 1:326ce5e200fb 216 adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advPacket, sizeof(advPacket));
jurica238814 0:12899fa39f88 217 ble.setAdvertisingData(adv_data);
jurica238814 0:12899fa39f88 218 }
jurica238814 0:12899fa39f88 219
jurica238814 0:12899fa39f88 220
Dautor 1:326ce5e200fb 221 int main(){
jurica238814 0:12899fa39f88 222 power = 1;
Dautor 1:326ce5e200fb 223 wait_ms(WAKEUP_TIME_DELAY_MS);
jurica238814 0:12899fa39f88 224 temperaturePower = 1;
jurica238814 0:12899fa39f88 225 lightPower = 1;
Dautor 1:326ce5e200fb 226 shdn = 1; // Wake up the pressure sensor
Dautor 1:326ce5e200fb 227
Dautor 1:326ce5e200fb 228 advPacket.header = APPLICATION_ID;
Dautor 1:326ce5e200fb 229
Dautor 1:326ce5e200fb 230 ble.init(bleInitComplete);
jurica238814 0:12899fa39f88 231
Dautor 1:326ce5e200fb 232 I2C i2c(I2C_DATA, I2C_CLK);
Dautor 1:326ce5e200fb 233 si = new Si7006(&i2c);
Dautor 1:326ce5e200fb 234 mems = new LSM9DS1(i2c);
Dautor 1:326ce5e200fb 235 spi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCLK);
Dautor 1:326ce5e200fb 236 mpl115a1 = new MPL115A1(*spi, cs);
jurica238814 0:12899fa39f88 237
jurica238814 0:12899fa39f88 238 mems->startAcc();
jurica238814 0:12899fa39f88 239 mems->startGyro();
jurica238814 0:12899fa39f88 240 mems->startMag();
jurica238814 0:12899fa39f88 241
jurica238814 0:12899fa39f88 242 led = 1;
jurica238814 0:12899fa39f88 243
jurica238814 0:12899fa39f88 244 Ticker ticker;
Dautor 1:326ce5e200fb 245 ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up
jurica238814 0:12899fa39f88 246
Dautor 1:326ce5e200fb 247 while(ble.hasInitialized() == false); /* spin loop */
Dautor 1:326ce5e200fb 248 while(true){
Dautor 1:326ce5e200fb 249 if (sleepFlag && GO_TO_SLEEP){
Dautor 1:326ce5e200fb 250 ble.gap().stopAdvertising();
Dautor 1:326ce5e200fb 251 sleep();
Dautor 1:326ce5e200fb 252 ble.waitForEvent();
Dautor 1:326ce5e200fb 253 }
Dautor 1:326ce5e200fb 254 else{
Dautor 1:326ce5e200fb 255 // I'm awake
Dautor 1:326ce5e200fb 256 updateData();
Dautor 1:326ce5e200fb 257 ble.gap().startAdvertising();
Dautor 1:326ce5e200fb 258 wait_ms(WAKE_UP_TIME);
Dautor 1:326ce5e200fb 259 sleepFlag = true;
jurica238814 0:12899fa39f88 260 }
jurica238814 0:12899fa39f88 261 }
jurica238814 0:12899fa39f88 262 }