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
Diff: main.cpp
- Revision:
- 10:fd4670ec0806
- Parent:
- 9:b3674516729d
- Child:
- 11:1a0a8fd74bc0
--- a/main.cpp Fri Jun 10 12:56:53 2016 +0000 +++ b/main.cpp Fri Jun 10 22:21:04 2016 +0000 @@ -1,88 +1,83 @@ +#include <vector> +#include <iostream> + #include "SalinityController.h" #include "TemperatureController.h" #include "MotorController.h" #include "LCDController.h" #include "ProximityController.h" +#include "AlarmController.h" #include "mbed.h" #include "rtos.h" -typedef struct Controllers { - TemperatureController *temperature; - SalinityController *salinity; - ProximityController *proximity; - LCDController *lcd; - -} Controllers; - - - -/* - Continuously update controller every 100 ms -*/ -void controller_thread(void const *args) { - Controller *controller = (Controller*)args; - - while(1) { - controller->update(); - Thread::wait(100); - } - -} - int main() { - int count; + std::vector<void *> controllers; - TemperatureController temperature; - Thread t1(controller_thread, (Controller*)&temperature); + // ----------------------------------------------------------------------------- + /* 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); - //SalinityController salt; - //Thread t2(controller_thread, (Controller*)&salt); + ProximityController proximity(false,0); + controllers.push_back((void*)&proximity); + SalinityController salt(false,0); + controllers.push_back((void *)&salt); + + AlarmController alarm(false,0,&temperature,&salt,&proximity); + controllers.push_back((void *)&alarm); + // ----------------------------------------------------------------------------- MotorController motor; - // Keep LCD controller outside thread + + // Only the main thread calls the LCDController, it doesn't matter if there is a small delay LCDController lcd; - -// for(int i = 0; i < 20; i++) -// { -// motor.test(); -// motor.test2(); -// } - //lcd.splash(); - //while(1) { - // printf("%d> ", count); - - // salt.displayPPT(); - - //temperature.displayTemperature(); - - // temperature.controlHeater(); - - // lcd.updateScreen(temperature.getTemperature(),salt.getVoltage()); - // count++; - - // wait(0.5); - //} + lcd.splash(); float s = 0.0; float t = 0.0; + float d = 0.0; + + int i = 0; while(1) { - lcd.splash(); + Thread::wait(1000); + + vector<void *>::iterator v = controllers.begin(); + while(v != controllers.end()) { + Controller *c = ((Controller *)*v); + if(!c->is_threaded()) + cout << "Running " << c->get_name() << " from main loop" << "\r\n"; + c->run(); + v++; + } + + // if alarm then update lcd and exit + if(alarm.is_error()) { + lcd.error(alarm.get_error_string()); + break; + } t = temperature.getValue(); - // s=... + d = proximity.getValue(); + s = salt.getValue(); printf("Grabbing temperature value from main thread: %f\r\n", t); - //printf("Grabbing salinity value from main thread: %f\n", salt.getValue()); + printf("Grabbing salinity value from main thread: %f\r\n", s); + printf("Grabbing distance value from main thread: %f\r\n", d); + - lcd.updateScreen(t, s); + if(i++ % 2) + lcd.updateScreen(t, s); + else + lcd.updateScreen(d); - motor.test(); } } \ No newline at end of file