Degree Computation

Dependencies:   aconno_SEGGER_RTT LSM9DS1 Si7006A20 adc52832_common aconnoMPL115A1 aconno_bsp

Committer:
jurica238814
Date:
Wed Dec 12 19:28:16 2018 +0100
Revision:
0:442d98af8cc7
Child:
3:b62a73dc76b7
Init commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 0:442d98af8cc7 1 /**
jurica238814 0:442d98af8cc7 2 * Made by Jurica @ aconno
jurica238814 0:442d98af8cc7 3 * jurica@aconno.de
jurica238814 0:442d98af8cc7 4 * More info @ aconno.de
jurica238814 0:442d98af8cc7 5 */
jurica238814 0:442d98af8cc7 6
jurica238814 0:442d98af8cc7 7 #include "mbed.h"
jurica238814 0:442d98af8cc7 8 #include "ble/BLE.h"
jurica238814 0:442d98af8cc7 9 #include "acd52832_bsp.h"
jurica238814 0:442d98af8cc7 10 #include "GapAdvertisingData.h"
jurica238814 0:442d98af8cc7 11 #include "Si7006A20.h"
jurica238814 0:442d98af8cc7 12 #include "LSM9DS1.h"
jurica238814 0:442d98af8cc7 13 #include "math.h"
jurica238814 0:442d98af8cc7 14 #include "adc52832_common/utilities.h"
jurica238814 0:442d98af8cc7 15 #include "MPL115A1.h"
jurica238814 0:442d98af8cc7 16 #include "acd_nrf52_saadc.h"
jurica238814 0:442d98af8cc7 17 #include "aconnoConfig.h"
jurica238814 0:442d98af8cc7 18
jurica238814 0:442d98af8cc7 19 NRF52_SAADC analogIn;
jurica238814 0:442d98af8cc7 20 extern DigitalOut lightPower;
jurica238814 0:442d98af8cc7 21 extern DigitalOut temperaturePower;
jurica238814 0:442d98af8cc7 22 extern DigitalOut shdn;
jurica238814 0:442d98af8cc7 23 extern DigitalOut power;
jurica238814 0:442d98af8cc7 24 Si7006 *si;
jurica238814 0:442d98af8cc7 25 extern LSM9DS1 *mems;
jurica238814 0:442d98af8cc7 26 vector3_s memsAccelerometerInit;
jurica238814 0:442d98af8cc7 27 vector3_s memsGyroscopeInit;
jurica238814 0:442d98af8cc7 28 vector3_s memsMagnetometerInit;
jurica238814 0:442d98af8cc7 29 extern GapAdvertisingData adv_data;
jurica238814 0:442d98af8cc7 30 extern advertising_packet advertisementPacket;
jurica238814 0:442d98af8cc7 31 DigitalOut cs(p7);
jurica238814 0:442d98af8cc7 32
jurica238814 0:442d98af8cc7 33 float getLight(){
jurica238814 0:442d98af8cc7 34 return ((float)analogIn.getData()[1])/ADC_RESOLUTION * VALUE_TO_PERCENTAGE;
jurica238814 0:442d98af8cc7 35 }
jurica238814 0:442d98af8cc7 36
jurica238814 0:442d98af8cc7 37 float voltage2temp(float vOut){
jurica238814 0:442d98af8cc7 38 return ((float)vOut - (float)V0)/((float)TC);
jurica238814 0:442d98af8cc7 39 }
jurica238814 0:442d98af8cc7 40
jurica238814 0:442d98af8cc7 41 float getTemperature(){
jurica238814 0:442d98af8cc7 42 return voltage2temp(((float)analogIn.getData()[2])/ADC_RESOLUTION * (float)VCC);
jurica238814 0:442d98af8cc7 43 }
jurica238814 0:442d98af8cc7 44
jurica238814 0:442d98af8cc7 45 uint8_t getBattery(){
jurica238814 0:442d98af8cc7 46 uint16_t batteryVoltage = analogIn.getData()[0];
jurica238814 0:442d98af8cc7 47
jurica238814 0:442d98af8cc7 48 const uint16_t zero_percent_limit = 739;
jurica238814 0:442d98af8cc7 49 const uint16_t onehundred_percent_limit = 810;
jurica238814 0:442d98af8cc7 50 const uint16_t percentage_increments = 5;
jurica238814 0:442d98af8cc7 51 uint8_t percentage;
jurica238814 0:442d98af8cc7 52
jurica238814 0:442d98af8cc7 53 if (batteryVoltage < zero_percent_limit)
jurica238814 0:442d98af8cc7 54 {
jurica238814 0:442d98af8cc7 55 percentage = 0;
jurica238814 0:442d98af8cc7 56 }
jurica238814 0:442d98af8cc7 57 else if(batteryVoltage > onehundred_percent_limit)
jurica238814 0:442d98af8cc7 58 {
jurica238814 0:442d98af8cc7 59 percentage = 100;
jurica238814 0:442d98af8cc7 60 }
jurica238814 0:442d98af8cc7 61 else
jurica238814 0:442d98af8cc7 62 {
jurica238814 0:442d98af8cc7 63 batteryVoltage -= zero_percent_limit;
jurica238814 0:442d98af8cc7 64 percentage = (batteryVoltage*100)/(onehundred_percent_limit - zero_percent_limit);
jurica238814 0:442d98af8cc7 65 percentage = percentage/percentage_increments*percentage_increments;
jurica238814 0:442d98af8cc7 66 }
jurica238814 0:442d98af8cc7 67
jurica238814 0:442d98af8cc7 68 return percentage;
jurica238814 0:442d98af8cc7 69 }
jurica238814 0:442d98af8cc7 70
jurica238814 0:442d98af8cc7 71 float getHumidity(){
jurica238814 0:442d98af8cc7 72 float result;
jurica238814 0:442d98af8cc7 73 si->getHumidity(&result);
jurica238814 0:442d98af8cc7 74 return result;
jurica238814 0:442d98af8cc7 75 }
jurica238814 0:442d98af8cc7 76
jurica238814 0:442d98af8cc7 77 void readGyroscope(vector3_s *gyroscopeData){
jurica238814 0:442d98af8cc7 78 mems->readGyroscope((int16_t *)gyroscopeData);
jurica238814 0:442d98af8cc7 79 *gyroscopeData -= memsGyroscopeInit;
jurica238814 0:442d98af8cc7 80 }
jurica238814 0:442d98af8cc7 81
jurica238814 0:442d98af8cc7 82 void readAccelerometer(vector3_s *accelerometerData){
jurica238814 0:442d98af8cc7 83 mems->readAccelerometer((int16_t *)accelerometerData);
jurica238814 0:442d98af8cc7 84 *accelerometerData -= memsAccelerometerInit;
jurica238814 0:442d98af8cc7 85 }
jurica238814 0:442d98af8cc7 86
jurica238814 0:442d98af8cc7 87 void readMagnetometer(vector3_s *magnetometerData){
jurica238814 0:442d98af8cc7 88 mems->readMagnetometer((int16_t *)magnetometerData);
jurica238814 0:442d98af8cc7 89 *magnetometerData -= memsMagnetometerInit;
jurica238814 0:442d98af8cc7 90 }
jurica238814 0:442d98af8cc7 91
jurica238814 0:442d98af8cc7 92 void calibrateAccelerometer(){
jurica238814 0:442d98af8cc7 93 vector3_s accelerometerData;
jurica238814 0:442d98af8cc7 94 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
jurica238814 0:442d98af8cc7 95 readAccelerometer(&accelerometerData);
jurica238814 0:442d98af8cc7 96 memsAccelerometerInit += accelerometerData;
jurica238814 0:442d98af8cc7 97 }
jurica238814 0:442d98af8cc7 98 memsAccelerometerInit /= CALIBRATION_STEPS;
jurica238814 0:442d98af8cc7 99 }
jurica238814 0:442d98af8cc7 100
jurica238814 0:442d98af8cc7 101 void calibrateGyroscope(){
jurica238814 0:442d98af8cc7 102 vector3_s gyroscopeData;
jurica238814 0:442d98af8cc7 103 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
jurica238814 0:442d98af8cc7 104 readGyroscope(&gyroscopeData);
jurica238814 0:442d98af8cc7 105 memsGyroscopeInit += gyroscopeData;
jurica238814 0:442d98af8cc7 106 }
jurica238814 0:442d98af8cc7 107 memsGyroscopeInit /= CALIBRATION_STEPS;
jurica238814 0:442d98af8cc7 108 }
jurica238814 0:442d98af8cc7 109
jurica238814 0:442d98af8cc7 110 void calibrateMag(){
jurica238814 0:442d98af8cc7 111 vector3_s magnetometerData;
jurica238814 0:442d98af8cc7 112 for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
jurica238814 0:442d98af8cc7 113 readMagnetometer(&magnetometerData);
jurica238814 0:442d98af8cc7 114 memsMagnetometerInit += magnetometerData;
jurica238814 0:442d98af8cc7 115 }
jurica238814 0:442d98af8cc7 116 memsMagnetometerInit /= CALIBRATION_STEPS;
jurica238814 0:442d98af8cc7 117 }
jurica238814 0:442d98af8cc7 118
jurica238814 0:442d98af8cc7 119 void disableI2C()
jurica238814 0:442d98af8cc7 120 {
jurica238814 0:442d98af8cc7 121
jurica238814 0:442d98af8cc7 122 NRF_TWI0->ENABLE = 0;
jurica238814 0:442d98af8cc7 123 NRF_TWI0->PSELSCL = 0xFFFFFFFF;
jurica238814 0:442d98af8cc7 124 NRF_TWI1->ENABLE = 0;
jurica238814 0:442d98af8cc7 125 NRF_TWI1->PSELSCL = 0xFFFFFFFF;
jurica238814 0:442d98af8cc7 126 NRF_TWIM0->ENABLE = 0;
jurica238814 0:442d98af8cc7 127 NRF_TWIM0->PSEL.SCL = 0x80000000;
jurica238814 0:442d98af8cc7 128 NRF_TWIM0->PSEL.SDA = 0x80000000;
jurica238814 0:442d98af8cc7 129 NRF_TWIM1->ENABLE = 0;
jurica238814 0:442d98af8cc7 130 NRF_TWIM0->PSEL.SCL = 0x80000000;
jurica238814 0:442d98af8cc7 131 NRF_TWIM0->PSEL.SDA = 0x80000000;
jurica238814 0:442d98af8cc7 132
jurica238814 0:442d98af8cc7 133 //foo = new DigitalOut(I2C_DATA);
jurica238814 0:442d98af8cc7 134 //bar = new DigitalOut(I2C_CLK);
jurica238814 0:442d98af8cc7 135
jurica238814 0:442d98af8cc7 136 //foo = 1;
jurica238814 0:442d98af8cc7 137 //bar = 1;
jurica238814 0:442d98af8cc7 138
jurica238814 0:442d98af8cc7 139 }
jurica238814 0:442d98af8cc7 140
jurica238814 0:442d98af8cc7 141 void updateData()
jurica238814 0:442d98af8cc7 142 {
jurica238814 0:442d98af8cc7 143 static uint8_t advertisementType = 0;
jurica238814 0:442d98af8cc7 144 int16_t temp_acc[3];
jurica238814 0:442d98af8cc7 145 BLE &ble = BLE::Instance();
jurica238814 0:442d98af8cc7 146
jurica238814 0:442d98af8cc7 147 I2C i2c(I2C_DATA, I2C_CLK);
jurica238814 0:442d98af8cc7 148 si = new Si7006(&i2c);
jurica238814 0:442d98af8cc7 149 SPI spi(SPI_MOSI, SPI_MISO, SPI_SCLK);
jurica238814 0:442d98af8cc7 150 MPL115A1 mpl115a1(spi, cs);
jurica238814 0:442d98af8cc7 151
jurica238814 0:442d98af8cc7 152 float result;
jurica238814 0:442d98af8cc7 153
jurica238814 0:442d98af8cc7 154 NRF_SAADC->ENABLE = 1;
jurica238814 0:442d98af8cc7 155 power = 1;
jurica238814 0:442d98af8cc7 156 temperaturePower = 1;
jurica238814 0:442d98af8cc7 157 lightPower = 1;
jurica238814 0:442d98af8cc7 158 shdn = 1;
jurica238814 0:442d98af8cc7 159 analogIn.addChannel(9); // Set VDD as source to SAADC
jurica238814 0:442d98af8cc7 160 analogIn.addChannel(6); // Light
jurica238814 0:442d98af8cc7 161 analogIn.addChannel(7); // Temp
jurica238814 0:442d98af8cc7 162 analogIn.calibrate();
jurica238814 0:442d98af8cc7 163 wait_ms(WAKEUP_TIME_DELAY_MS);
jurica238814 0:442d98af8cc7 164
jurica238814 0:442d98af8cc7 165 analogIn.updateData();
jurica238814 0:442d98af8cc7 166
jurica238814 0:442d98af8cc7 167 advertisementPacket.type = 0x01;
jurica238814 0:442d98af8cc7 168 advertisementPacket.temperature = getTemperature();
jurica238814 0:442d98af8cc7 169 advertisementPacket.light = getLight();
jurica238814 0:442d98af8cc7 170 advertisementPacket.humidity = getHumidity();
jurica238814 0:442d98af8cc7 171 advertisementPacket.pressure = mpl115a1.getPressure();
jurica238814 0:442d98af8cc7 172
jurica238814 0:442d98af8cc7 173 //disableI2C();
jurica238814 0:442d98af8cc7 174 //I2C dummy(I2C_DATA, I2C_CLK);
jurica238814 0:442d98af8cc7 175
jurica238814 0:442d98af8cc7 176 SPI dummySpi(SPI_MOSI, SPI_MISO, SPI_SCLK);
jurica238814 0:442d98af8cc7 177 power = 0;
jurica238814 0:442d98af8cc7 178 shdn = 0;
jurica238814 0:442d98af8cc7 179 temperaturePower = 0;
jurica238814 0:442d98af8cc7 180 lightPower = 0;
jurica238814 0:442d98af8cc7 181 NRF_SAADC->ENABLE = 0;
jurica238814 0:442d98af8cc7 182
jurica238814 0:442d98af8cc7 183
jurica238814 0:442d98af8cc7 184 //DigitalOut foo(I2C_DATA);
jurica238814 0:442d98af8cc7 185 //DigitalOut bar(I2C_CLK);
jurica238814 0:442d98af8cc7 186 //foo = 1;
jurica238814 0:442d98af8cc7 187 //bar = 1;
jurica238814 0:442d98af8cc7 188
jurica238814 0:442d98af8cc7 189 if(++advertisementType > 2) advertisementType = 0;
jurica238814 0:442d98af8cc7 190 adv_data = ble.gap().getAdvertisingPayload();
jurica238814 0:442d98af8cc7 191 adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
jurica238814 0:442d98af8cc7 192 ble.gap().setAdvertisingPayload(adv_data);
jurica238814 0:442d98af8cc7 193
jurica238814 0:442d98af8cc7 194 //printf("Data updated.\r\n");
jurica238814 0:442d98af8cc7 195 }