A multifunctional and modular Firmware for Multitech's mDot based on ARM mBed provides a widerange of functionality for several Sensors such as MAX44009, BME280, MPU9250, SI1143 and uBlox. It allows you to quickly build a Sensornode that measures specific data with its sensors and sends it via LoRaWAN.

Dependencies:   mDot_LoRa_Sensornode_Flowmeter_impl mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Application.cpp Source File

Application.cpp

00001 /*
00002  * Application.cpp
00003  *
00004  *  Created on: Jun 3, 2016
00005  *      Author: Adrian
00006  */
00007 
00008 #include "Application.h"
00009 
00010 Application::Application() {
00011     initInterfaces();
00012     initSensors();
00013     initMutexes();
00014     initQueueBundle();
00015     initTasks();
00016     initApplicationConfig();
00017 }
00018 
00019 Application::~Application() {
00020     delete uart;
00021     delete debugSerial;
00022     delete i2c_rt;
00023 
00024     delete gpsSensor;
00025     delete max44009;
00026     delete bme280;
00027     delete mpu9250;
00028     delete si1143;
00029 
00030     delete taskLight;
00031     delete taskTemperature;
00032     delete taskPressure;
00033     delete taskHumidity;
00034     delete taskAcceleration;
00035     delete taskGyroscope;
00036     delete taskTesla;
00037     delete taskProximity;
00038     delete taskGps;
00039 }
00040 
00041 void Application::init(APPLICATION_MODE desiredMode){
00042     config->build(desiredMode);
00043     stopAllRunningSensorTasks();
00044     configureSensors();
00045     configureLora();
00046     startRunnableSensorTasks();
00047 }
00048 
00049 void Application::stopAllRunningSensorTasks(){
00050     if(taskLight->getState() == RUNNING){
00051         taskLight->stop();
00052     }
00053     if(taskTemperature->getState() == RUNNING){
00054         taskTemperature->stop();
00055     }
00056     if(taskPressure->getState() == RUNNING){
00057         taskPressure->stop();
00058     }
00059     if(taskHumidity->getState() == RUNNING){
00060         taskHumidity->stop();
00061     }
00062     if(taskAcceleration->getState() == RUNNING){
00063         taskAcceleration->stop();
00064     }
00065     if(taskGyroscope->getState() == RUNNING){
00066         taskGyroscope->stop();
00067     }
00068     if(taskTesla->getState() == RUNNING){
00069         taskTesla->stop();
00070     }
00071     if(taskProximity->getState() == RUNNING){
00072         taskProximity->stop();
00073     }
00074     if(taskGps->getState() == RUNNING){
00075         taskGps->stop();
00076     }
00077     if(taskFlowMeter->getState() == RUNNING){
00078         taskFlowMeter->stop();
00079     }
00080     if(taskLoRaMeasurement->getState() == RUNNING){
00081         taskLoRaMeasurement->stop();
00082     }
00083     if(taskDataHandler->getState() == RUNNING){
00084         taskDataHandler->stop();
00085     }
00086 
00087     osDelay(100);
00088 }
00089 
00090 void Application::initInterfaces(){
00091     uart = new RawSerial(XBEE_DOUT,XBEE_DIN);
00092     debugSerial = new RawSerial(USBTX,USBRX);
00093     i2c_rt = new I2C_RT();
00094     pulseInput = new InterruptIn(XBEE_DIO0);
00095     dot = mDot::getInstance();
00096     lora = new LoRa(dot,debugSerial);
00097 
00098     uart->baud(BAUD_UART);
00099     uart->format(8,SerialBase::None,1);
00100     debugSerial->baud(BAUD_USB);
00101     debugSerial->format(8,SerialBase::None,1);
00102 }
00103 
00104 void Application::initSensors(){
00105     gpsSensor = new uBlox(uart);
00106     max44009 = new MAX44009(i2c_rt);
00107     bme280 = new BME280(i2c_rt);
00108     mpu9250 = new MPU9250(i2c_rt);
00109     si1143 = new SI1143(i2c_rt);
00110     flowMeter = new FlowMeter(pulseInput);
00111 }
00112 
00113 void Application::initTasks(){
00114     taskLight = new TaskLight(max44009,mutexI2C,&queueLight,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
00115     taskTemperature = new TaskTemperature(bme280,mutexI2C,&queueTemperature,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
00116     taskHumidity = new  TaskHumidity(bme280,mutexI2C,&queueHumidity,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
00117     taskPressure = new  TaskPressure(bme280,mutexI2C,&queuePressure,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
00118     taskAcceleration = new  TaskAcceleration(mpu9250,mutexI2C,&queueAcceleration,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
00119     taskGyroscope = new  TaskGyroscope(mpu9250,mutexI2C,&queueGyro,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
00120     taskTesla = new  TaskTesla(mpu9250,mutexI2C,&queueTesla,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
00121     taskProximity = new  TaskProximity(si1143,mutexI2C,&queueProximity,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
00122     taskGps = new  TaskGPS(gpsSensor,mutexUART1,&queueGps,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
00123     taskFlowMeter = new TaskFlowMeter(flowMeter,mutexFlowMeter, &queueFlowMeter, osPriorityNormal,DEFAULT_STACK_SIZE, NULL);
00124     taskLoRaMeasurement = new TaskLoRaMeasurement(lora,mutexLoRa,&queueLoRaMeasurements,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
00125     taskDataHandler = new  TaskDatahandler(lora,mutexLoRa,queueBundle,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
00126     taskDataHandler->setDebugSerial(debugSerial);
00127 }
00128 
00129 void Application::startRunnableSensorTasks(){
00130     if(config->getStateTaskLight() == RUNNING){
00131         taskLight->start();
00132     }
00133     if(config->getStateTaskTemperature() == RUNNING){
00134         taskTemperature->start();
00135     }
00136     if(config->getStateTaskPressure() == RUNNING){
00137         taskPressure->start();
00138     }
00139     if(config->getStateTaskHumidity() == RUNNING){
00140         taskHumidity->start();
00141     }
00142     if(config->getStateTaskAcceleration() == RUNNING){
00143         taskAcceleration->start();
00144     }
00145     if(config->getStateTaskGyroscope() == RUNNING){
00146         taskGyroscope->start();
00147     }
00148     if(config->getStateTaskTesla() == RUNNING){
00149         taskTesla->start();
00150     }
00151     if(config->getStateTaskProximity() == RUNNING){
00152         taskProximity->start();
00153     }
00154     if(config->getStateTaskGPS() == RUNNING){
00155         taskGps->start();
00156     }
00157     if(config->getStateTaskFlowMeter() == RUNNING){
00158         taskFlowMeter->start();
00159     }
00160     if(config->getStateTaskLoRaMeasurement() == RUNNING){
00161         taskLoRaMeasurement->start();
00162     }
00163 
00164     taskDataHandler->start();
00165 }
00166 
00167 void Application::configureSensors(){
00168     max44009->init(config->getMAX44009_MODE());
00169     bme280->init(config->getBME280_MODE());
00170     mpu9250->init(config->getMPU9250_MODE());
00171     //si1143->init(config->getSI1143_MODE());
00172     gpsSensor->init(config->getuBlox_MODE());
00173     //todo configure FlowMeter
00174 }
00175 
00176 void Application::configureLora(){
00177     lora->init(config->getLORA_MODE());
00178 }
00179 
00180 void Application::initMutexes(){
00181     this->mutexI2C = new Mutex();
00182     this->mutexUART1 = new Mutex();
00183     this->mutexLoRa = new Mutex();
00184     this->mutexFlowMeter = new Mutex();
00185 }
00186 
00187 void Application::initApplicationConfig(){
00188     config = new ApplicationConfig();
00189 }
00190 
00191 void Application::initQueueBundle(){
00192     this->queueBundle.queueAcceleration =  &queueAcceleration;
00193     this->queueBundle.queueCommands = &queueCommands;
00194     this->queueBundle.queueGps = &queueGps;
00195     this->queueBundle.queueGyro = &queueGyro;
00196     this->queueBundle.queueHumidity = &queueHumidity;
00197     this->queueBundle.queueLight = &queueLight;
00198     this->queueBundle.queueLoRaMeasurments = &queueLoRaMeasurements;
00199     this->queueBundle.queuePressure = &queuePressure;
00200     this->queueBundle.queueProximity = &queueProximity;
00201     this->queueBundle.queueTemperature = &queueTemperature;
00202     this->queueBundle.queueTesla = &queueTesla;
00203     this->queueBundle.queueFlowMeter = &queueFlowMeter;
00204 }