Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 bool crossed = false; 00018 while (true) { 00019 ThisThread::sleep_for(TICK); 00020 road->go_flags.wait_all(flag); 00021 cycle++; 00022 00023 position = position + speed; 00024 00025 if (cycle % 5 == 0) { 00026 speed = rand() % 11 + 5; 00027 } 00028 00029 if (position < 54) { 00030 speed = std::min(speed, 54 - position); 00031 } else if (!crossed && position == 54) { 00032 speed = 0; 00033 crossed = true; 00034 } else if (position < 56) { 00035 speed = 1; 00036 } 00037 00038 road->done_flags.set(flag); 00039 } 00040 } 00041 00042 void Car::reset(int position, int speed) { 00043 road->done_flags.clear(flag); 00044 00045 if (thread != NULL) { 00046 thread->terminate(); 00047 } 00048 00049 thread = new Thread(); 00050 thread->start( callback(this, &Car::update) ); 00051 00052 cycle = 0; 00053 this->position = position; 00054 this->speed = speed; 00055 } 00056 00057 void Car::stop() { 00058 if (thread != NULL) { 00059 thread->terminate(); 00060 } 00061 }
Generated on Fri Jul 22 2022 13:26:47 by
1.7.2