Emanuel Kuflik / Mbed OS HW05

Dependencies:   TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Car.cpp Source File

Car.cpp

00001 #include "Car.h"
00002 #include <stdlib.h>
00003 
00004 #define TICK 1000
00005 
00006 Car::Car(int id, Road* road, int flag) {
00007     this->id = id;   
00008     this->road = road;
00009     this->flag = flag;
00010     
00011     cycle = 0;
00012     
00013     this->thread = NULL;
00014 }
00015 
00016 void Car::update() {
00017     while (true) {
00018         ThisThread::sleep_for(TICK);
00019         road->go_flags.wait_all(flag);
00020         cycle++;
00021         
00022         position = position + speed;
00023         
00024         if (cycle % 5 == 0) {
00025             speed = rand() % 11 + 5;
00026         }
00027  
00028         road->done_flags.set(flag);   
00029     }
00030 }
00031 
00032 void Car::reset(int position, int speed) {
00033     road->done_flags.clear(flag);
00034     
00035     if (thread != NULL) {
00036         thread->terminate();   
00037     }
00038     
00039     thread = new Thread();
00040     thread->start( callback(this, &Car::update) );
00041     
00042     cycle = 0;
00043     this->position = position;
00044     this->speed = speed;
00045 }
00046 
00047 void Car::stop() {
00048     if (thread != NULL) {
00049         thread->terminate();   
00050     } 
00051 }