aconno acnsensa project for iOS devices with iBeacon packets support.
Dependencies: LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common
Diff: main.cpp
- Branch:
- noSensors
- Revision:
- 20:fc639ef579b6
- Parent:
- 18:442bc914996b
- Child:
- 22:3710de547ff1
--- a/main.cpp Thu Aug 02 10:28:57 2018 +0200 +++ b/main.cpp Thu Aug 02 10:38:30 2018 +0200 @@ -49,18 +49,8 @@ #define INVERT_AXES (0) uint8_t gConnected = 0; - -static NRF52_SAADC analogIn; -static NRF52_DigitalOut lightPower(p28); -static NRF52_DigitalOut temperaturePower(p31); -static NRF52_DigitalOut shdn(p6); -static NRF52_DigitalOut led(p23); -static NRF52_DigitalOut power(p2); -static NRF52_DigitalOut cs(p7); -static Si7006 *si; -static LSM9DS1 *mems; -static SPI *spi; -static MPL115A1 *mpl115a1; +DigitalOut aliveLed(p23); +DigitalOut connectedLed(p24); static EventQueue eventQueue(32 * EVENTS_EVENT_SIZE); uint8_t myMacAddress[6] = {}; @@ -126,20 +116,14 @@ { // Restart Advertising on disconnection gConnected = 0; + connectedLed = 1; BLE::Instance().gap().startAdvertising(); } -/** - * Function for waking the core up - */ -void wakeMeUp() -{ - sleepFlag = false; -} - void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params) { printf("Connection callback.\n"); + connectedLed = 0; gConnected = 1; } @@ -184,101 +168,6 @@ printf("Init done.\n"); } -float getLight() -{ - return ((float)analogIn.getData()[1])/ADC_RESOLUTION * VALUE_TO_PERCENTAGE; -} - -float voltage2temp(float vOut) -{ - return ((float)vOut - (float)V0)/((float)TC); -} - -float getTemperature() -{ - return voltage2temp(((float)analogIn.getData()[2])/ADC_RESOLUTION * (float)VCC); -} - -uint8_t getBattery() -{ - uint16_t batteryVoltage = analogIn.getData()[0]; - const uint16_t zero_percent_limit = 739; - const uint16_t onehundred_percent_limit = 810; - const uint16_t percentage_increments = 5; - uint8_t percentage; - - if (batteryVoltage < zero_percent_limit) - { - percentage = 0; - } - else if(batteryVoltage > onehundred_percent_limit) - { - percentage = 100; - } - else - { - batteryVoltage -= zero_percent_limit; - percentage = (batteryVoltage*100)/(onehundred_percent_limit - zero_percent_limit); - percentage = percentage/percentage_increments*percentage_increments; - } - - return percentage; -} - -float getHumidity() -{ - float result; - si->getHumidity(&result); - return result; -} - -void readGyroscope(vector3_s *gyroscopeData) -{ - mems->readGyroscope((int16_t *)gyroscopeData); - *gyroscopeData -= memsGyroscopeInit; -} - -void readAccelerometer(vector3_s *accelerometerData) -{ - mems->readAccelerometer((int16_t *)accelerometerData); - *accelerometerData -= memsAccelerometerInit; -} - -void readMagnetometer(vector3_s *magnetometerData){ - mems->readMagnetometer((int16_t *)magnetometerData); - *magnetometerData -= memsMagnetometerInit; -} - -void calibrateAccelerometer(){ - vector3_s accelerometerData; - for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter) - { - readAccelerometer(&accelerometerData); - memsAccelerometerInit += accelerometerData; - } - memsAccelerometerInit /= CALIBRATION_STEPS; -} - -void calibrateGyroscope(){ - vector3_s gyroscopeData; - for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter) - { - readGyroscope(&gyroscopeData); - memsGyroscopeInit += gyroscopeData; - } - memsGyroscopeInit /= CALIBRATION_STEPS; -} - -void calibrateMag(){ - vector3_s magnetometerData; - for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter) - { - readMagnetometer(&magnetometerData); - memsMagnetometerInit += magnetometerData; - } - memsMagnetometerInit /= CALIBRATION_STEPS; -} - void updateData(){ static uint8_t advertisementType = 0; int16_t temp_acc[3]; @@ -299,9 +188,6 @@ adv_data = ble.getAdvertisingData(); advertisementPacket.type = 0x00; - readGyroscope((vector3_s *)advertisementPacket.gyroscope); - readAccelerometer((vector3_s *)temp_acc); - readMagnetometer((vector3_s *)advertisementPacket.magnetometer); advertisementPacket.acc_lsb_value = (0xF9E); // ^--- That's in ug cuz MSB is 1 #if INVERT_AXES @@ -317,14 +203,9 @@ else if (advertisementType == 1 && !gConnected) { printf("Sensor format 2.\n"); - analogIn.updateData(); adv_data = ble.getAdvertisingData(); advertisementPacket.type = 0x01; - advertisementPacket.temperature = getTemperature(); - advertisementPacket.light = getLight(); - advertisementPacket.humidity = getHumidity(); - advertisementPacket.pressure = mpl115a1->getPressure(); - advertisementPacket.battery = getBattery(); + advertisementPacket.temperature = 0.23; adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); @@ -345,21 +226,18 @@ if(++advertisementType > 2) advertisementType = 0; } +void blinky() +{ + aliveLed = !aliveLed; +} + int main() { printf("Main started.\n"); Thread bleT; - power = 1; - wait_ms(WAKEUP_TIME_DELAY_MS); - temperaturePower = 1; - lightPower = 1; - shdn = 1; // Wake up the pressure sensor - analogIn.addChannel(9); // Set VDD as source to SAADC - analogIn.addChannel(6); // Light - analogIn.addChannel(7); // Temp - analogIn.calibrate(); + connectedLed = 1; advertisementPacket.header = APPLICATION_ID; @@ -378,21 +256,7 @@ } ble.onEventsToProcess(scheduleBleEventsProcessing); - I2C i2c(I2C_DATA, I2C_CLK); - si = new Si7006(&i2c); - mems = new LSM9DS1(&i2c); - spi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCLK); - mpl115a1 = new MPL115A1(*spi, cs); - - mems->startAccelerometer(); - mems->startGyroscope(); - mems->startMagnetometer(); - - led = 1; - - Ticker ticker; - ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up - + eventQueue.call_every(500, blinky); eventQueue.call_every(500, updateData); // This call stops main thread