Saltware / Mbed 2 deprecated Water Play

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

main.cpp

Committer:
sbouber1
Date:
2016-06-15
Revision:
46:7e4c1f2ab76c
Parent:
44:7c932cc5991b
Child:
55:ee80f248919d

File content as of revision 46:7e4c1f2ab76c:

#include <vector>
#include <iostream>

#include "SalinityController.h"
#include "TemperatureController.h"
#include "LCDController.h"
#include "ProximityController.h"
#include "AlarmController.h"
#include "MockSensorController.h"
#include "PIDController.h"

#include "mbed.h"
#include "rtos.h"
#include "testing.h"

#define MAIN_THREAD_DELAY_MS 1000

int main();
int test_main();
int real_main();




MOCK(temp_mock, 20.0+0.01*i)
MOCK(salt_mock, 3.0+0.001*i)
MOCK(prox_mock, 10.0f)


int main() {
    return MAIN();    
}

extern int test_main();

int real_main() 
{
    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);
    //MockSensorController temperature(false,0,temp_mock);
    controllers.push_back((void*)&temperature);
    
    ProximityController proximity(false,0);
    //MockSensorController proximity(false,0,prox_mock);
    controllers.push_back((void*)&proximity);
    
    SalinityController salt(false,0);
    //MockSensorController salt(false,0,salt_mock);
    controllers.push_back((void *)&salt);
    
    PIDController pidc(false,0,&temperature,&salt,&proximity);
    controllers.push_back((void *)&pidc);
    
    AlarmController alarm(false,0,&temperature,&salt,&proximity);
   // controllers.push_back((void *)&alarm);
    
    
    // -----------------------------------------------------------------------------
    
    
    // Only the main thread calls the LCDController, it doesn't matter if there is a small delay
    LCDController lcd;
    lcd.splash();
    
    float s = 0.0;
    float t = 0.0;
    float d = 0.0;
    
    int i = 0;
    
    /* UNCOMMENT TO TEST MOTORS
        while(1)
            pidc.doTestingStuff(10);
    */
    
    
    
    while(1) {
        
        Thread::wait(MAIN_THREAD_DELAY_MS);
        
        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 exit
        // TODO maybe signal threads to terminate
        if(alarm.is_error()) {
            lcd.error(alarm.get_error_message());    
            break;
        }        

        t = temperature.getValue();
        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\r\n", s);
        printf("Grabbing distance value from main thread: %f\r\n", d);
        
        
        // Show either temperature and salinity or the water level given by the proximity controller
        if(i++ % 2)
            lcd.updateScreen(t, s, &pidc);
        else
            lcd.updateScreen(d);
        
    }
    
    return 1;
}