diff options
-rw-r--r-- | SensorHandler/SensorHandler.cpp | 42 | ||||
-rw-r--r-- | SensorHandler/SensorHandler.h | 1 | ||||
-rw-r--r-- | main.cpp | 43 |
3 files changed, 80 insertions, 6 deletions
diff --git a/SensorHandler/SensorHandler.cpp b/SensorHandler/SensorHandler.cpp index 6b25f7e..65cbbc3 100644 --- a/SensorHandler/SensorHandler.cpp +++ b/SensorHandler/SensorHandler.cpp @@ -9,12 +9,15 @@ #include "SensorHandler.h" SensorHandler::SensorHandler() - : _getSensorThread(&SensorHandler::startSensorThread,this), + : _getSensorThread(&SensorHandler::startSensorThread, this), _mDoti2c(PC_9,PA_8), _accelerometer(_mDoti2c,MMA845x::SA0_VSS), _barometricSensor(_mDoti2c), _lightSensor(_mDoti2c) { + printf("init Sensors"); + SensorHandler::initSensors(); + printf("start Sensor thread"); _getSensorThread.signal_set(START_THREAD); return; } @@ -45,18 +48,21 @@ void SensorHandler::initSensors(){ // Clear the min-max registers in the Barometric Sensor _barometricSensor.clearMinMaxRegs(); + printf("Sensors initialized"); } void SensorHandler::startSensorThread(void const *p) { SensorHandler *instance = (SensorHandler*)p; instance->readSensors(); + printf("Sensor thread started"); } void SensorHandler::readSensors() { uint8_t result; _getSensorThread.signal_wait(START_THREAD); + printf("read Sensors"); while(1){ // Test Accelerometer XYZ data ready bit to see if acquisition complete do { @@ -65,7 +71,9 @@ void SensorHandler::readSensors() } while ((result & MMA845x::XYZDR) == 0 ); // Retrieve accelerometer data + _mutex.lock(); _accelerometerData = _accelerometer.getXYZ(); + _mutex.unlock(); // Trigger a Pressure reading _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, @@ -79,8 +87,10 @@ void SensorHandler::readSensors() } while ((result & MPL3115A2::PTDR) == 0 ); // Retrieve barometric pressure + _mutex.lock(); _pressure = _barometricSensor.getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value - + _mutex.unlock(); + // Trigger a Altitude reading _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16, MPL3115A2::AT_1); @@ -93,26 +103,46 @@ void SensorHandler::readSensors() } while ((result & MPL3115A2::PTDR) == 0 ); // Retrieve temperature + _mutex.lock(); _barometerData = _barometricSensor.getAllData(false); + _mutex.unlock(); // Retrieve light level + _mutex.lock(); _light = _lightSensor.getData(); + _mutex.unlock(); } } MMA845x_DATA SensorHandler::getAcceleration(){ - return _accelerometerData; + MMA845x_DATA data; + _mutex.lock(); + data = _accelerometerData; + _mutex.unlock(); + return data; } uint16_t SensorHandler::getLight(){ - return _light; + uint16_t light; + _mutex.lock(); + light = _light; + _mutex.unlock(); + return light; } uint32_t SensorHandler::getPressure(){ - return _pressure; + uint32_t pressure; + _mutex.lock(); + pressure = _pressure; + _mutex.unlock(); + return pressure; } MPL3115A2_DATA SensorHandler::getBarometer(){ - return _barometerData; + MPL3115A2_DATA data; + _mutex.lock(); + data = _barometerData; + _mutex.unlock(); + return data; } diff --git a/SensorHandler/SensorHandler.h b/SensorHandler/SensorHandler.h index 73f9c0f..f3ad83d 100644 --- a/SensorHandler/SensorHandler.h +++ b/SensorHandler/SensorHandler.h @@ -43,6 +43,7 @@ private: MPL3115A2_DATA _barometerData; uint16_t _light; uint32_t _pressure; + Mutex _mutex; }; #endif //SENSORHANDLER_H @@ -22,6 +22,10 @@ #include "ModeConfig.h" // misc heders #include <string> +/*------------------------------------------------------*/ +#include "SensorHandler.h" +/*------------------------------------------------------*/ + // LCD and backlight controllers SPI lcd_spi(SPI1_MOSI, SPI1_MISO, SPI1_SCK); @@ -111,6 +115,45 @@ 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; + 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); + osDelay(2000); + + light = sensors->getLight(); + 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); + + pressure = ((sensors->getBarometer())._baro) >> 12; // convert 32 bit signed to 20 bit unsigned value + num_whole = pressure >> 2; // 18 bit integer significant + num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit + sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac); + printf("%s\r\n", txtstr); + + num_whole = ((sensors->getBarometer())._temp) / 16; // 8 bit signed significant integer + num_frac = (((sensors->getBarometer())._temp) & 0x0F) * 625 / 100; // 4 bit fractional .0625 per bit + sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac); + printf("%s\r\n", txtstr); + } + +/*------------------------------------------------------*/ + + while (true) { // reset session between modes dot->resetNetworkSession(); |