Emanuel Kuflik / Mbed OS HW05

Dependencies:   TextLCD

Committer:
MannyK
Date:
Wed Oct 09 16:29:06 2019 +0000
Revision:
0:f17da79e74c9
For Evans

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MannyK 0:f17da79e74c9 1 #include "Car.h"
MannyK 0:f17da79e74c9 2 #include <stdlib.h>
MannyK 0:f17da79e74c9 3
MannyK 0:f17da79e74c9 4 #define TICK 1000
MannyK 0:f17da79e74c9 5
MannyK 0:f17da79e74c9 6 Car::Car(int id, Road* road, int flag) {
MannyK 0:f17da79e74c9 7 this->id = id;
MannyK 0:f17da79e74c9 8 this->road = road;
MannyK 0:f17da79e74c9 9 this->flag = flag;
MannyK 0:f17da79e74c9 10
MannyK 0:f17da79e74c9 11 cycle = 0;
MannyK 0:f17da79e74c9 12
MannyK 0:f17da79e74c9 13 this->thread = NULL;
MannyK 0:f17da79e74c9 14 }
MannyK 0:f17da79e74c9 15
MannyK 0:f17da79e74c9 16 void Car::update() {
MannyK 0:f17da79e74c9 17 while (true) {
MannyK 0:f17da79e74c9 18 ThisThread::sleep_for(TICK);
MannyK 0:f17da79e74c9 19 road->go_flags.wait_all(flag);
MannyK 0:f17da79e74c9 20 cycle++;
MannyK 0:f17da79e74c9 21
MannyK 0:f17da79e74c9 22 position = position + speed;
MannyK 0:f17da79e74c9 23
MannyK 0:f17da79e74c9 24 if (cycle % 5 == 0) {
MannyK 0:f17da79e74c9 25 speed = rand() % 11 + 5;
MannyK 0:f17da79e74c9 26 }
MannyK 0:f17da79e74c9 27
MannyK 0:f17da79e74c9 28 road->done_flags.set(flag);
MannyK 0:f17da79e74c9 29 }
MannyK 0:f17da79e74c9 30 }
MannyK 0:f17da79e74c9 31
MannyK 0:f17da79e74c9 32 void Car::reset(int position, int speed) {
MannyK 0:f17da79e74c9 33 road->done_flags.clear(flag);
MannyK 0:f17da79e74c9 34
MannyK 0:f17da79e74c9 35 if (thread != NULL) {
MannyK 0:f17da79e74c9 36 thread->terminate();
MannyK 0:f17da79e74c9 37 }
MannyK 0:f17da79e74c9 38
MannyK 0:f17da79e74c9 39 thread = new Thread();
MannyK 0:f17da79e74c9 40 thread->start( callback(this, &Car::update) );
MannyK 0:f17da79e74c9 41
MannyK 0:f17da79e74c9 42 cycle = 0;
MannyK 0:f17da79e74c9 43 this->position = position;
MannyK 0:f17da79e74c9 44 this->speed = speed;
MannyK 0:f17da79e74c9 45 }
MannyK 0:f17da79e74c9 46
MannyK 0:f17da79e74c9 47 void Car::stop() {
MannyK 0:f17da79e74c9 48 if (thread != NULL) {
MannyK 0:f17da79e74c9 49 thread->terminate();
MannyK 0:f17da79e74c9 50 }
MannyK 0:f17da79e74c9 51 }