diff options
author | Leon Lindenfelser <llindenfelser@multitech.com> | 2015-12-08 16:17:13 -0600 |
---|---|---|
committer | Leon Lindenfelser <llindenfelser@multitech.com> | 2015-12-08 16:17:13 -0600 |
commit | cbcf16a4dafeb8c1e2120c3cc08e2c2ef72e164f (patch) | |
tree | 2d505e2f3088f568de394498507870e5340e6f34 | |
parent | 9a9407a11dad3013a99cefbbc00ecf1db561eb3f (diff) | |
download | mtdot-box-evb-factory-firmware-cbcf16a4dafeb8c1e2120c3cc08e2c2ef72e164f.tar.gz mtdot-box-evb-factory-firmware-cbcf16a4dafeb8c1e2120c3cc08e2c2ef72e164f.tar.bz2 mtdot-box-evb-factory-firmware-cbcf16a4dafeb8c1e2120c3cc08e2c2ef72e164f.zip |
Added more getters to the sensor handler.
Added test code in main to print out data from getters.
-rw-r--r-- | Mode/Mode.cpp | 9 | ||||
-rw-r--r-- | Mode/Mode.h | 9 | ||||
-rw-r--r-- | SensorHandler/SensorHandler.cpp | 59 | ||||
-rw-r--r-- | SensorHandler/SensorHandler.h | 12 | ||||
-rw-r--r-- | main.cpp | 70 |
5 files changed, 145 insertions, 14 deletions
diff --git a/Mode/Mode.cpp b/Mode/Mode.cpp index 059ddb3..8268a4a 100644 --- a/Mode/Mode.cpp +++ b/Mode/Mode.cpp @@ -156,8 +156,11 @@ void Mode::updateData(DataItem& data, DataType type, bool status) { void Mode::updateSensors(SensorItem& data) { data.accel_data = _mdot_sensors.getAcceleration(); data.baro_data = _mdot_sensors.getBarometer(); - data.lux_data = _mdot_sensors.getLight(); + data.lux_data_raw = _mdot_sensors.getLightRaw(); + data.pressure_raw = _mdot_sensors.getPressureRaw(); + data.light = _mdot_sensors.getLight(); data.pressure = _mdot_sensors.getPressure(); + data.altitude = _mdot_sensors.getAltitude(); } uint32_t Mode::getIndex(DataType type) { @@ -297,12 +300,12 @@ std::vector<uint8_t> Mode::formatSensorData(SensorItem& data) { convertS.f_s = data.accel_data._z * 4; // shift data 2 bits while retaining sign send_data.push_back(convertS.t_u[1]); // get 8 MSB of 14 bit value send_data.push_back(0x08); // key for Current Pressure Value - convertL.f_u = data.pressure; // pressure data is 20 bits unsigned + convertL.f_u = data.pressure_raw; // pressure data is 20 bits unsigned send_data.push_back(convertL.t_u[2]); send_data.push_back(convertL.t_u[1]); send_data.push_back(convertL.t_u[0]); send_data.push_back(0x05); // key for Current Ambient Light Value - convertS.f_u = data.lux_data; // data is 16 bits unsigned + convertS.f_u = data.lux_data_raw; // data is 16 bits unsigned send_data.push_back(convertS.t_u[1]); send_data.push_back(convertS.t_u[0]); send_data.push_back(0x0B); // key for Current Temperature Value diff --git a/Mode/Mode.h b/Mode/Mode.h index d97dfb1..ff9ba6c 100644 --- a/Mode/Mode.h +++ b/Mode/Mode.h @@ -35,9 +35,12 @@ class Mode { typedef struct { MMA845x_DATA accel_data; MPL3115A2_DATA baro_data; - uint16_t lux_data; - uint32_t pressure; - } SensorItem; + uint16_t lux_data_raw; + uint32_t pressure_raw; + float light; + float pressure; + float altitude; + } SensorItem; Mode(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora); ~Mode(); diff --git a/SensorHandler/SensorHandler.cpp b/SensorHandler/SensorHandler.cpp index 33515f7..89f7d2d 100644 --- a/SensorHandler/SensorHandler.cpp +++ b/SensorHandler/SensorHandler.cpp @@ -86,7 +86,7 @@ void SensorHandler::readSensors() _pressure = _barometricSensor.getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value _mutex.unlock(); - // Trigger a Altitude reading + // Trigger an Altitude reading _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16, MPL3115A2::AT_1); _barometricSensor.triggerOneShot(); @@ -97,7 +97,7 @@ void SensorHandler::readSensors() result = _barometricSensor.getStatus(); } while ((result & MPL3115A2::PTDR) == 0 ); - // Retrieve temperature + // Retrieve temperature and altitude. _mutex.lock(); _barometerData = _barometricSensor.getAllData(false); _mutex.unlock(); @@ -117,7 +117,19 @@ MMA845x_DATA SensorHandler::getAcceleration(){ return data; } -uint16_t SensorHandler::getLight(){ +float SensorHandler::getLight(){ + float light; + uint16_t whole; + _mutex.lock(); + whole = _light; + _mutex.unlock(); + light = whole * 24 % 100; + light /= 100; + light = light + (whole * 24 / 100); // 16000 lux full scale .24 lux per bit + return light; +} + +uint16_t SensorHandler::getLightRaw(){ uint16_t light; _mutex.lock(); light = _light; @@ -125,7 +137,18 @@ uint16_t SensorHandler::getLight(){ return light; } -uint32_t SensorHandler::getPressure(){ +float SensorHandler::getPressure(){ + float pressure; + uint32_t whole; + _mutex.lock(); + whole = _pressure; + _mutex.unlock(); + pressure = (whole & 3) * .25; + pressure = pressure + (whole >> 2); + return pressure; +} + +uint32_t SensorHandler::getPressureRaw(){ uint32_t pressure; _mutex.lock(); pressure = _pressure; @@ -133,6 +156,34 @@ uint32_t SensorHandler::getPressure(){ return pressure; } +float SensorHandler::getTemp(Scale scale){ + float temperature; + uint16_t whole; + _mutex.lock(); + whole = _barometerData._temp; + _mutex.unlock(); + temperature = whole & 0x0f; + temperature *= .0625; + temperature += (whole >> 4); + if(scale == FAHRENHEIT){ + temperature = temperature * 1.8 + 32; + } + return temperature; +} + +float SensorHandler::getAltitude(){ + float altitude; + uint32_t whole; + _mutex.lock(); + whole = _barometerData._baro; + _mutex.unlock(); + whole /= 4096; + altitude = (whole & 0x0f) * .0625; + whole /= 16; + altitude += whole; + return altitude; +} + MPL3115A2_DATA SensorHandler::getBarometer(){ MPL3115A2_DATA data; _mutex.lock(); diff --git a/SensorHandler/SensorHandler.h b/SensorHandler/SensorHandler.h index f3ad83d..01ccf77 100644 --- a/SensorHandler/SensorHandler.h +++ b/SensorHandler/SensorHandler.h @@ -22,14 +22,18 @@ class SensorHandler { public: + enum Scale{CELSIUS, FAHRENHEIT}; SensorHandler(void); ~SensorHandler(void); MMA845x_DATA getAcceleration(void); - uint16_t getLight(void); - uint32_t getPressure(void); + float getLight(void); + uint16_t getLightRaw(void); + float getPressure(void); + uint32_t getPressureRaw(void); + float getTemp(Scale); + float getAltitude(void); MPL3115A2_DATA getBarometer(void); - - + private: Thread _getSensorThread; static void startSensorThread (void const *p); @@ -23,6 +23,11 @@ // misc heders #include <string> +/*------------------------------------------------------*/ +#include "SensorHandler.h" +/*------------------------------------------------------*/ + + // LCD and backlight controllers SPI lcd_spi(SPI1_MOSI, SPI1_MISO, SPI1_SCK); I2C backlight_i2c(I2C_SDA, I2C_SCL); @@ -111,6 +116,71 @@ void mainMenu() { items.push_back(menu_strings[single]); items.push_back(menu_strings[sweep]); + /*------------------------------------------------------*/ + SensorHandler *sensors = new SensorHandler(); + MMA845x_DATA accel; + uint16_t light; + int32_t num_whole; + int16_t num_frac; + uint32_t pressure; + float temp; + char txtstr[17]; + while(1){ + accel = sensors->getAcceleration(); + sprintf(txtstr, "Accel: x = %d", accel._x); + printf("%s\r\n", txtstr); + sprintf(txtstr, "y = %d", accel._y); + printf("%s\r\n", txtstr); + sprintf(txtstr, "z = %d", accel._z ); + printf("%s\r\n", txtstr); + + light = sensors->getLightRaw(); + num_whole = light * 24 / 100; // 16000 lux full scale .24 lux per bit + num_frac = light * 24 % 100; + sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac ); + printf("%s\r\n", txtstr); + + temp = sensors->getLight(); + printf("getLight call = %4.3f\r\n", temp); + + printf("----------------------Pressure-----------------------\r\n"); + pressure = sensors->getPressureRaw(); + num_whole = pressure >> 2; // 18 bit integer significant + num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit + sprintf(txtstr,"Raw...Press=%ld.%02d Pa", num_whole, num_frac); + printf("%s\r\n", txtstr); + + temp = sensors->getPressure(); + printf("getPressure call = %3.2f\r\n", temp); + printf("-----------------------------------------------------\r\n"); + + printf("----------------------Altitude-----------------------\r\n"); + num_whole = sensors->getBarometer()._baro; // 8 bit signed significant integer + num_whole /= 4096; + num_frac = (num_whole & 0x0F) * 625 / 10; // 4 bit fractional .0625 per bit + num_whole /= 16; + sprintf(txtstr,"Alti=%ld.%02d m", num_whole, num_frac); + printf("%s\r\n", txtstr); + + temp = sensors->getAltitude(); + printf("getAltitude call = %3.3f\r\n", temp); + printf("-----------------------------------------------------\r\n"); + + printf("---------------------Temperature---------------------\r\n"); + num_whole = ((sensors->getBarometer())._temp) / 16; // 8 bit signed significant integer + num_frac = (((sensors->getBarometer())._temp) & 0x0F) * 625 / 10; // 4 bit fractional .0625 per bit + sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac); + printf("%s\r\n", txtstr); + + temp = sensors->getTemp(SensorHandler::CELSIUS); + printf("getTemp call = %3.3f\r\n", temp); + printf("-----------------------------------------------------\r\n\r\n"); + + osDelay(5000); + } + + /*------------------------------------------------------*/ + while (true) { // reset session between modes dot->resetNetworkSession(); |