Saltware / Mbed 2 deprecated Water Play

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

Committer:
sbouber1
Date:
Fri Jun 17 19:10:15 2016 +0000
Revision:
55:ee80f248919d
Parent:
46:7e4c1f2ab76c
Child:
57:8dc3192ff150
comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sbouber1 10:fd4670ec0806 1 #include <vector>
sbouber1 10:fd4670ec0806 2 #include <iostream>
sbouber1 10:fd4670ec0806 3
6366295 0:dab140a197e0 4 #include "SalinityController.h"
6366295 0:dab140a197e0 5 #include "TemperatureController.h"
joran 6:067e999b9c6e 6 #include "LCDController.h"
sbouber1 9:b3674516729d 7 #include "ProximityController.h"
sbouber1 10:fd4670ec0806 8 #include "AlarmController.h"
sbouber1 28:f4a4ee58d57e 9 #include "PIDController.h"
sbouber1 55:ee80f248919d 10 #include "settings.h"
6366295 0:dab140a197e0 11
6366295 0:dab140a197e0 12 #include "mbed.h"
sbouber1 9:b3674516729d 13
sbouber1 55:ee80f248919d 14 extern int test_main();
sbouber1 13:1323e2c0b697 15 int real_main();
sbouber1 13:1323e2c0b697 16
sbouber1 13:1323e2c0b697 17 int main() {
sbouber1 55:ee80f248919d 18 // Either test_main() or real_main() depending on TEST_MODE define in settings.h
sbouber1 13:1323e2c0b697 19 return MAIN();
sbouber1 13:1323e2c0b697 20 }
sbouber1 13:1323e2c0b697 21
sbouber1 13:1323e2c0b697 22
sbouber1 55:ee80f248919d 23 int real_main() {
sbouber1 55:ee80f248919d 24
sbouber1 10:fd4670ec0806 25 std::vector<void *> controllers;
6366295 0:dab140a197e0 26
sbouber1 10:fd4670ec0806 27 // -----------------------------------------------------------------------------
sbouber1 10:fd4670ec0806 28 /* THE FOLLOWING CONTROLLERS CAN BE THREADED BY SETTING THE FIRST ARG TO TRUE */
sbouber1 10:fd4670ec0806 29 /* THE SECOND ARG SPECIFIES THE DELAY AFTER EACH UPDATE, ONLY WHEN THREADED */
sbouber1 10:fd4670ec0806 30 // -----------------------------------------------------------------------------
sbouber1 12:c51f3aba84fe 31
joran 44:7c932cc5991b 32 TemperatureController temperature(false,0);
sbouber1 10:fd4670ec0806 33 controllers.push_back((void*)&temperature);
sbouber1 9:b3674516729d 34
joran 44:7c932cc5991b 35 ProximityController proximity(false,0);
sbouber1 10:fd4670ec0806 36 controllers.push_back((void*)&proximity);
sbouber1 9:b3674516729d 37
joran 44:7c932cc5991b 38 SalinityController salt(false,0);
sbouber1 10:fd4670ec0806 39 controllers.push_back((void *)&salt);
sbouber1 10:fd4670ec0806 40
joran 40:1668630544c7 41 PIDController pidc(false,0,&temperature,&salt,&proximity);
joran 40:1668630544c7 42 controllers.push_back((void *)&pidc);
joran 40:1668630544c7 43
joran 43:0abb54448b75 44 AlarmController alarm(false,0,&temperature,&salt,&proximity);
sbouber1 55:ee80f248919d 45 controllers.push_back((void *)&alarm);
sbouber1 55:ee80f248919d 46
sbouber1 10:fd4670ec0806 47 // -----------------------------------------------------------------------------
sbouber1 9:b3674516729d 48
sbouber1 9:b3674516729d 49
sbouber1 55:ee80f248919d 50 // Show the splash screen indicating the system is starting up
sbouber1 55:ee80f248919d 51 LCDController::splash();
sbouber1 10:fd4670ec0806 52
sbouber1 55:ee80f248919d 53 int i = 0;
sbouber1 9:b3674516729d 54
sbouber1 55:ee80f248919d 55 // Loop forever, only breaks when an alarm triggers
sbouber1 9:b3674516729d 56 while(1) {
sbouber1 10:fd4670ec0806 57
sbouber1 55:ee80f248919d 58 // Wait for a specified amount of time after each iteration
sbouber1 11:1a0a8fd74bc0 59 Thread::wait(MAIN_THREAD_DELAY_MS);
sbouber1 10:fd4670ec0806 60
sbouber1 55:ee80f248919d 61 // Iterate over all available Controllers
sbouber1 10:fd4670ec0806 62 vector<void *>::iterator v = controllers.begin();
sbouber1 10:fd4670ec0806 63 while(v != controllers.end()) {
sbouber1 11:1a0a8fd74bc0 64
sbouber1 11:1a0a8fd74bc0 65 // Get the next controller
sbouber1 10:fd4670ec0806 66 Controller *c = ((Controller *)*v);
sbouber1 11:1a0a8fd74bc0 67
sbouber1 10:fd4670ec0806 68 if(!c->is_threaded())
sbouber1 10:fd4670ec0806 69 cout << "Running " << c->get_name() << " from main loop" << "\r\n";
sbouber1 11:1a0a8fd74bc0 70
sbouber1 11:1a0a8fd74bc0 71 // The controller only updates here if it's not threaded
sbouber1 10:fd4670ec0806 72 c->run();
sbouber1 11:1a0a8fd74bc0 73
sbouber1 11:1a0a8fd74bc0 74 // Advance to the next controller
sbouber1 10:fd4670ec0806 75 v++;
sbouber1 10:fd4670ec0806 76 }
sbouber1 10:fd4670ec0806 77
sbouber1 55:ee80f248919d 78 // If the alarm controller detected a dangerous situation then update lcd and possibly exit
sbouber1 10:fd4670ec0806 79 if(alarm.is_error()) {
sbouber1 55:ee80f248919d 80 LCDController::error(alarm.get_error_message());
sbouber1 55:ee80f248919d 81 #ifdef HALT_ON_ALARM
sbouber1 10:fd4670ec0806 82 break;
sbouber1 55:ee80f248919d 83 #endif
sbouber1 55:ee80f248919d 84 }
sbouber1 9:b3674516729d 85
sbouber1 9:b3674516729d 86
sbouber1 55:ee80f248919d 87 //Show temperature, salinity and volume of the water tank on the PC if connected through serial
sbouber1 55:ee80f248919d 88 cout << "Temperature value: " << temperature.getValue() << "\r\n";
sbouber1 55:ee80f248919d 89 cout << "Salinity value: " << salt.getValue() << "\r\n";
sbouber1 55:ee80f248919d 90 cout << "Volume value: " << proximity.getValue() << "\r\n";
sbouber1 55:ee80f248919d 91
sbouber1 11:1a0a8fd74bc0 92 // Show either temperature and salinity or the water level given by the proximity controller
sbouber1 10:fd4670ec0806 93 if(i++ % 2)
sbouber1 55:ee80f248919d 94 LCDController::updateScreen(temperature.getValue(), salt.getValue(), &pidc);
sbouber1 10:fd4670ec0806 95 else
sbouber1 55:ee80f248919d 96 LCDController::updateScreen(proximity.getValue());
sbouber1 9:b3674516729d 97
6366295 0:dab140a197e0 98 }
sbouber1 35:c9261391a995 99
sbouber1 35:c9261391a995 100 return 1;
6366295 0:dab140a197e0 101 }