Program for the water play project for the course Software Testing Practical 2016 given at the VU University

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

Controller.cpp

Committer:
sbouber1
Date:
2016-06-14
Revision:
39:cb67926712d4
Parent:
38:930469a33001
Child:
55:ee80f248919d

File content as of revision 39:cb67926712d4:


#include "Controller.h"

#include <iostream>

Controller::Controller(bool threaded = false, int interval_ms = 0) {
    this->threaded = threaded;
    this->interval_ms = interval_ms;
    this->ctrl_thread = 0;
    this->prio = osPriorityNormal;
    this->num_iters = 0;
}

bool Controller::is_threaded() {
    return this->threaded;    
}

void Controller::run() {
    if(!this->threaded) {
        this->update();    
    } else if(!this->has_spawned) {
        has_spawned = true; 
        Thread t (Controller::thread_stub, this);
        this->ctrl_thread = &t;
    }
    
    this->num_iters++;
}

int Controller::get_interval_ms() {
    return this->interval_ms;    
}

void Controller::set_priority(osPriority priority) {
    this->prio = priority;
    if(this->has_spawned)
        this->ctrl_thread->set_priority(priority);
}

osPriority Controller::get_priority() {
    return this->prio;
}    

void Controller::thread_stub(void const *args) {
    Controller *controller = (Controller*)args;
    
    while(1) {
        std::cout << "[THREAD] running " << controller->get_name() << "\r\n";
        controller->run();
        Thread::wait(controller->get_interval_ms());
    }
    
}