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@57:8dc3192ff150, 2016-06-19 (annotated)
- Committer:
- sbouber1
- Date:
- Sun Jun 19 22:21:08 2016 +0000
- Revision:
- 57:8dc3192ff150
- Parent:
- 55:ee80f248919d
- Child:
- 58:b5f0c0f305ff
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sbouber1 | 55:ee80f248919d | 1 | #include <iostream> |
sbouber1 | 10:fd4670ec0806 | 2 | |
sbouber1 | 10:fd4670ec0806 | 3 | #include "Controller.h" |
sbouber1 | 10:fd4670ec0806 | 4 | |
sbouber1 | 10:fd4670ec0806 | 5 | Controller::Controller(bool threaded = false, int interval_ms = 0) { |
sbouber1 | 10:fd4670ec0806 | 6 | this->threaded = threaded; |
sbouber1 | 10:fd4670ec0806 | 7 | this->interval_ms = interval_ms; |
sbouber1 | 10:fd4670ec0806 | 8 | this->ctrl_thread = 0; |
sbouber1 | 10:fd4670ec0806 | 9 | this->prio = osPriorityNormal; |
sbouber1 | 38:930469a33001 | 10 | this->num_iters = 0; |
sbouber1 | 10:fd4670ec0806 | 11 | } |
sbouber1 | 10:fd4670ec0806 | 12 | |
sbouber1 | 57:8dc3192ff150 | 13 | bool Controller::isThreaded() { |
sbouber1 | 10:fd4670ec0806 | 14 | return this->threaded; |
sbouber1 | 10:fd4670ec0806 | 15 | } |
sbouber1 | 10:fd4670ec0806 | 16 | |
sbouber1 | 10:fd4670ec0806 | 17 | void Controller::run() { |
sbouber1 | 10:fd4670ec0806 | 18 | if(!this->threaded) { |
sbouber1 | 10:fd4670ec0806 | 19 | this->update(); |
sbouber1 | 10:fd4670ec0806 | 20 | } else if(!this->has_spawned) { |
sbouber1 | 10:fd4670ec0806 | 21 | has_spawned = true; |
sbouber1 | 57:8dc3192ff150 | 22 | Thread t (Controller::threadStub, this); |
sbouber1 | 10:fd4670ec0806 | 23 | this->ctrl_thread = &t; |
sbouber1 | 10:fd4670ec0806 | 24 | } |
sbouber1 | 38:930469a33001 | 25 | |
sbouber1 | 38:930469a33001 | 26 | this->num_iters++; |
sbouber1 | 10:fd4670ec0806 | 27 | } |
sbouber1 | 10:fd4670ec0806 | 28 | |
sbouber1 | 57:8dc3192ff150 | 29 | int Controller::getIntervalMs() { |
sbouber1 | 10:fd4670ec0806 | 30 | return this->interval_ms; |
sbouber1 | 10:fd4670ec0806 | 31 | } |
sbouber1 | 10:fd4670ec0806 | 32 | |
sbouber1 | 57:8dc3192ff150 | 33 | void Controller::setPriority(osPriority priority) { |
sbouber1 | 10:fd4670ec0806 | 34 | this->prio = priority; |
sbouber1 | 10:fd4670ec0806 | 35 | if(this->has_spawned) |
sbouber1 | 57:8dc3192ff150 | 36 | this->ctrl_thread->setPriority(priority); |
sbouber1 | 10:fd4670ec0806 | 37 | } |
sbouber1 | 10:fd4670ec0806 | 38 | |
sbouber1 | 57:8dc3192ff150 | 39 | osPriority Controller::getPriority() { |
sbouber1 | 10:fd4670ec0806 | 40 | return this->prio; |
sbouber1 | 10:fd4670ec0806 | 41 | } |
sbouber1 | 10:fd4670ec0806 | 42 | |
sbouber1 | 55:ee80f248919d | 43 | // Called when a thread is spawned. |
sbouber1 | 57:8dc3192ff150 | 44 | void Controller::threadStub(void const *args) { |
sbouber1 | 10:fd4670ec0806 | 45 | Controller *controller = (Controller*)args; |
sbouber1 | 10:fd4670ec0806 | 46 | |
sbouber1 | 55:ee80f248919d | 47 | // Run forever. |
sbouber1 | 10:fd4670ec0806 | 48 | while(1) { |
sbouber1 | 57:8dc3192ff150 | 49 | std::cout << "[THREAD] running " << controller->getName() << "\r\n"; |
sbouber1 | 39:cb67926712d4 | 50 | controller->run(); |
sbouber1 | 57:8dc3192ff150 | 51 | Thread::wait(controller->getIntervalMs()); |
sbouber1 | 10:fd4670ec0806 | 52 | } |
sbouber1 | 10:fd4670ec0806 | 53 | |
sbouber1 | 10:fd4670ec0806 | 54 | } |