Degree Computation
Dependencies: aconno_SEGGER_RTT LSM9DS1 Si7006A20 adc52832_common aconnoMPL115A1 aconno_bsp
aconnoHelpers/aconnoHelpers.cpp
- Committer:
- jurica238814
- Date:
- 2018-12-12
- Revision:
- 0:442d98af8cc7
- Child:
- 3:b62a73dc76b7
File content as of revision 0:442d98af8cc7:
/** * Made by Jurica @ aconno * jurica@aconno.de * More info @ aconno.de */ #include "mbed.h" #include "ble/BLE.h" #include "acd52832_bsp.h" #include "GapAdvertisingData.h" #include "Si7006A20.h" #include "LSM9DS1.h" #include "math.h" #include "adc52832_common/utilities.h" #include "MPL115A1.h" #include "acd_nrf52_saadc.h" #include "aconnoConfig.h" NRF52_SAADC analogIn; extern DigitalOut lightPower; extern DigitalOut temperaturePower; extern DigitalOut shdn; extern DigitalOut power; Si7006 *si; extern LSM9DS1 *mems; vector3_s memsAccelerometerInit; vector3_s memsGyroscopeInit; vector3_s memsMagnetometerInit; extern GapAdvertisingData adv_data; extern advertising_packet advertisementPacket; DigitalOut cs(p7); 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 disableI2C() { NRF_TWI0->ENABLE = 0; NRF_TWI0->PSELSCL = 0xFFFFFFFF; NRF_TWI1->ENABLE = 0; NRF_TWI1->PSELSCL = 0xFFFFFFFF; NRF_TWIM0->ENABLE = 0; NRF_TWIM0->PSEL.SCL = 0x80000000; NRF_TWIM0->PSEL.SDA = 0x80000000; NRF_TWIM1->ENABLE = 0; NRF_TWIM0->PSEL.SCL = 0x80000000; NRF_TWIM0->PSEL.SDA = 0x80000000; //foo = new DigitalOut(I2C_DATA); //bar = new DigitalOut(I2C_CLK); //foo = 1; //bar = 1; } void updateData() { static uint8_t advertisementType = 0; int16_t temp_acc[3]; BLE &ble = BLE::Instance(); I2C i2c(I2C_DATA, I2C_CLK); si = new Si7006(&i2c); SPI spi(SPI_MOSI, SPI_MISO, SPI_SCLK); MPL115A1 mpl115a1(spi, cs); float result; NRF_SAADC->ENABLE = 1; power = 1; temperaturePower = 1; lightPower = 1; shdn = 1; analogIn.addChannel(9); // Set VDD as source to SAADC analogIn.addChannel(6); // Light analogIn.addChannel(7); // Temp analogIn.calibrate(); wait_ms(WAKEUP_TIME_DELAY_MS); analogIn.updateData(); advertisementPacket.type = 0x01; advertisementPacket.temperature = getTemperature(); advertisementPacket.light = getLight(); advertisementPacket.humidity = getHumidity(); advertisementPacket.pressure = mpl115a1.getPressure(); //disableI2C(); //I2C dummy(I2C_DATA, I2C_CLK); SPI dummySpi(SPI_MOSI, SPI_MISO, SPI_SCLK); power = 0; shdn = 0; temperaturePower = 0; lightPower = 0; NRF_SAADC->ENABLE = 0; //DigitalOut foo(I2C_DATA); //DigitalOut bar(I2C_CLK); //foo = 1; //bar = 1; if(++advertisementType > 2) advertisementType = 0; adv_data = ble.gap().getAdvertisingPayload(); adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); ble.gap().setAdvertisingPayload(adv_data); //printf("Data updated.\r\n"); }