aconno acnsensa project for iOS devices with iBeacon packets support.
Dependencies: LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common
Diff: main.cpp
- Revision:
- 4:634796e5b8c3
- Parent:
- 3:78ceda8ef565
- Child:
- 6:51745805d8b0
--- a/main.cpp Fri Mar 02 12:37:27 2018 +0000 +++ b/main.cpp Fri Mar 02 14:32:08 2018 +0000 @@ -16,6 +16,7 @@ #include "nrf52_digital.h" #include "adc52832_common/utilities.h" #include "MPL115A1.h" +#include "acd_nrf52_saadc.h" #define V0 0.47 /* In volts */ #define TC 0.01 /* In volts */ @@ -24,6 +25,9 @@ #define WAKEUP_TIME_DELAY_MS (150) #define APPLICATION_ID (0xCF170059) +#define ADC_REFERENCE (3.6f) /* adc reference voltage */ +#define ADC_RESOLUTION (1024) /* 10-bit adc */ + #define I2C_DATA (p19) #define I2C_CLK (p20) #define SPI_MISO (p5) @@ -37,8 +41,7 @@ #define GO_TO_SLEEP (0) /* Sleep flag: 0 -> Device will not go to sleep, 1 -> Will go to sleep mode */ #define CALIBRATION_STEPS 20 -static AnalogIn temperature(ADC_TEMP); -static AnalogIn light(ADC_LIGHT); +static NRF52_SAADC analogIn; static NRF52_DigitalOut lightPower(p28); static NRF52_DigitalOut temperaturePower(p31); static NRF52_DigitalOut shdn(p6); @@ -84,6 +87,7 @@ float humidity; float pressure; float light; + uint8_t battery; }; }; }; @@ -128,7 +132,7 @@ } float getLight(){ - return light.read() * VALUE_TO_PERCENTAGE; + return ((float)analogIn.getData()[1])/ADC_RESOLUTION * VALUE_TO_PERCENTAGE; } float voltage2temp(float vOut){ @@ -136,7 +140,18 @@ } float getTemperature(){ - return voltage2temp(temperature.read()*(float)VCC); + return voltage2temp(((float)analogIn.getData()[2])/ADC_RESOLUTION * (float)VCC); +} + +uint8_t getBattery(){ + uint16_t batteryVoltage = analogIn.getData()[2]; + if(batteryVoltage >= 810) return 100; + if(batteryVoltage >= 796) return 85; + if(batteryVoltage >= 782) return 75; + if(batteryVoltage >= 768) return 50; + if(batteryVoltage >= 754) return 25; + if(batteryVoltage >= 740) return 10; + return 0; } float getHumidity(){ @@ -202,6 +217,7 @@ advertisementPacket.light = getLight(); advertisementPacket.humidity = getHumidity(); advertisementPacket.pressure = mpl115a1->getPressure(); + advertisementPacket.battery = getBattery(); } if(++advertisementType > 2) advertisementType = 0; @@ -217,7 +233,11 @@ 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(); + advertisementPacket.header = APPLICATION_ID; ble.init(bleInitComplete); @@ -237,7 +257,10 @@ Ticker ticker; ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up - while(ble.hasInitialized() == false); /* spin loop */ + while(ble.hasInitialized() == false){ + /* spin loop */ + } + while(true){ if (sleepFlag && GO_TO_SLEEP){ ble.gap().stopAdvertising();