Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed DRV88255 TextLCD Ping mbed-rtos
main.cpp
- Committer:
- sbouber1
- Date:
- 2016-06-19
- Revision:
- 59:614f713fb48b
- Parent:
- 57:8dc3192ff150
- Child:
- 61:6b5c2ddcea0c
File content as of revision 59:614f713fb48b:
#include <vector> #include <iostream> #include "SalinityController.h" #include "TemperatureController.h" #include "LCDController.h" #include "ProximityController.h" #include "SensorAlarmController.h" #include "PIDController.h" #include "settings.h" #include "mbed.h" extern int testMain(); int realMain(); int main() { // Either test_main() or real_main() depending on TEST_MODE define in settings.h return MAIN(); } int realMain() { // Collection of all controllers updated in the main loop, updated in the order they were added std::vector<void *> controllers; // ----------------------------------------------------------------------------- /* THE FOLLOWING CONTROLLERS CAN BE THREADED BY SETTING THE FIRST ARG TO TRUE */ /* THE SECOND ARG SPECIFIES THE DELAY AFTER EACH UPDATE, ONLY WHEN THREADED */ // ----------------------------------------------------------------------------- TemperatureController temperature(false, 0); controllers.push_back((void*)&temperature); ProximityController proximity(false, 0); controllers.push_back((void*)&proximity); SalinityController salt(false, 0); controllers.push_back((void *)&salt); // Add alarms to monitor sensor values SensorAlarmController temp_alarm = SensorAlarmController(false, 0, &temperature, TEMP_MIN_CRIT, TEMP_MIN_UNDESIRED, TEMP_MAX_CRIT, TEMP_MAX_UNDESIRED); controllers.push_back((void *)&temp_alarm); SensorAlarmController salt_alarm = SensorAlarmController(false, 0, &salt, SALT_MIN_CRIT, SALT_MIN_UNDESIRED, SALT_MAX_CRIT, SALT_MAX_UNDESIRED); controllers.push_back((void *)&salt_alarm); SensorAlarmController prox_alarm = SensorAlarmController(false, 0, &proximity, VOLUME_MIN_CRIT, VOLUME_MIN_UNDESIRED, VOLUME_MAX_CRIT, VOLUME_MAX_UNDESIRED); controllers.push_back((void *)&prox_alarm); // PIDController last, as alarms should update first PIDController pidc(false, 0, &temperature,&salt,&proximity); controllers.push_back((void *)&pidc); // ----------------------------------------------------------------------------- // Show the splash screen indicating the system is starting up LCDController::splash(); int i = 0; // Loop forever, only breaks when an alarm triggers while(1) { // Wait for a specified amount of time after each iteration Thread::wait(MAIN_THREAD_DELAY_MS); // Iterate over all available Controllers vector<void *>::iterator v = controllers.begin(); while(v != controllers.end()) { // Get the next controller Controller *c = ((Controller *)*v); if(!c->is_threaded()) cout << "Running " << c->get_name() << " from main loop" << "\r\n"; // The controller only updates here if it's not threaded c->run(); // Advance to the next controller v++; } // If the alarm controller detected a dangerous situation then update lcd and possibly exit // TODO move LCD updating to alarm controller if(temp_alarm.isError() || salt_alarm.isError() || prox_alarm.isError()) { if(temp_alarm.isError()) LCDController::error(temp_alarm.getErrorMessage()); else if(salt_alarm.isError()) LCDController::error(salt_alarm.getErrorMessage()); else LCDController::error(prox_alarm.getErrorMessage()); #ifdef HALT_ON_ALARM break; #endif } //Show temperature, salinity and volume of the water tank on the PC if connected through serial cout << "Temperature value: " << temperature.getValue() << "\r\n"; cout << "Salinity value: " << salt.getValue() << "\r\n"; cout << "Volume value: " << proximity.getValue() << "\r\n"; // Show either temperature and salinity or the water level given by the proximity controller if(i++ % 2) LCDController::updateScreen(temperature.getValue(), salt.getValue(), &pidc); else LCDController::updateScreen(proximity.getValue()); } return 1; }