Degree Computation
Dependencies: aconno_SEGGER_RTT LSM9DS1 Si7006A20 adc52832_common aconnoMPL115A1 aconno_bsp
Diff: aconnoHelpers/aconnoHelpers.cpp
- Revision:
- 3:b62a73dc76b7
- Parent:
- 0:442d98af8cc7
- Child:
- 4:cb3513aa9814
--- a/aconnoHelpers/aconnoHelpers.cpp Fri Dec 14 13:01:55 2018 +0100 +++ b/aconnoHelpers/aconnoHelpers.cpp Fri Dec 14 13:33:03 2018 +0100 @@ -21,8 +21,6 @@ extern DigitalOut temperaturePower; extern DigitalOut shdn; extern DigitalOut power; -Si7006 *si; -extern LSM9DS1 *mems; vector3_s memsAccelerometerInit; vector3_s memsGyroscopeInit; vector3_s memsMagnetometerInit; @@ -30,6 +28,12 @@ extern advertising_packet advertisementPacket; DigitalOut cs(p7); +I2C i2c(I2C_DATA, I2C_CLK); +Si7006 si(&i2c); +SPI spi(SPI_MOSI, SPI_MISO, SPI_SCLK); +MPL115A1 mpl115a1(spi, cs); +LSM9DS1 mems(&i2c); + float getLight(){ return ((float)analogIn.getData()[1])/ADC_RESOLUTION * VALUE_TO_PERCENTAGE; } @@ -70,22 +74,22 @@ float getHumidity(){ float result; - si->getHumidity(&result); + si.getHumidity(&result); return result; } void readGyroscope(vector3_s *gyroscopeData){ - mems->readGyroscope((int16_t *)gyroscopeData); + mems.readGyroscope((int16_t *)gyroscopeData); *gyroscopeData -= memsGyroscopeInit; } void readAccelerometer(vector3_s *accelerometerData){ - mems->readAccelerometer((int16_t *)accelerometerData); + mems.readAccelerometer((int16_t *)accelerometerData); *accelerometerData -= memsAccelerometerInit; } void readMagnetometer(vector3_s *magnetometerData){ - mems->readMagnetometer((int16_t *)magnetometerData); + mems.readMagnetometer((int16_t *)magnetometerData); *magnetometerData -= memsMagnetometerInit; } @@ -116,63 +120,54 @@ 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(); + if(advertisementType < 1) + { + power = 1; + mems.startAccelerometer(); + mems.startGyroscope(); + mems.startMagnetometer(); + wait_ms(WAKEUP_TIME_DELAY_MS); + advertisementPacket.type = 0x00; + readGyroscope((vector3_s *)advertisementPacket.gyroscope); + readAccelerometer((vector3_s *)temp_acc); + readMagnetometer((vector3_s *)advertisementPacket.magnetometer); + advertisementPacket.acc_lsb_value = (0xF9E);//(0x3D80); + advertisementPacket.accelerometer[0] = temp_acc[1]; + advertisementPacket.accelerometer[1] = temp_acc[0]; + advertisementPacket.accelerometer[2] = temp_acc[2]; + // ^--- That's in ug cuz MSB is 1 + } + else + { + 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(); + wait_ms(WAKEUP_TIME_DELAY_MS); - advertisementPacket.type = 0x01; - advertisementPacket.temperature = getTemperature(); - advertisementPacket.light = getLight(); - advertisementPacket.humidity = getHumidity(); - advertisementPacket.pressure = mpl115a1.getPressure(); + advertisementPacket.type = 0x01; + advertisementPacket.temperature = getTemperature(); + advertisementPacket.light = getLight(); + advertisementPacket.humidity = getHumidity(); + advertisementPacket.pressure = mpl115a1.getPressure(); + advertisementPacket.battery = getBattery(); + } - //disableI2C(); - //I2C dummy(I2C_DATA, I2C_CLK); - SPI dummySpi(SPI_MOSI, SPI_MISO, SPI_SCLK); power = 0; shdn = 0; @@ -180,16 +175,9 @@ 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)); + adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, + (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); ble.gap().setAdvertisingPayload(adv_data); - - //printf("Data updated.\r\n"); } \ No newline at end of file