Jasmine Karlsson / Mbed 2 deprecated train_rail

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Train.cpp Source File

Train.cpp

00001 #include "Train.h"
00002 
00003 Train::Train(const unsigned int newaddress, const unsigned int newinst, int pos, const unsigned int speed, const unsigned int speed2): normSpeed(speed), slowSpeed(speed2)
00004 {
00005     //ctor
00006     address = newaddress;
00007     inst = newinst;
00008     position = pos;
00009     dirClockwise = false;
00010     nrPacket = 20;
00011     isStopped = false;
00012 
00013 }
00014 
00015 Train::~Train()
00016 {
00017     //dtor
00018 }
00019 
00020 unsigned int Train::normalSpeed(){
00021     return normSpeed;    
00022 }
00023 
00024 unsigned int Train::slowlySpeed(){
00025     return slowSpeed;    
00026 }
00027 
00028 int Train::getPosition(){
00029 
00030     return position;
00031 }
00032 
00033 void Train::sendCommand(){
00034     //lcd.cls();
00035     //lcd.printf("Send command");
00036     //lcd.printf("%d", inst);
00037     DCC_send_command(address, inst, nrPacket);
00038         
00039 }
00040 
00041 void Train::changeSpeed(unsigned int speed){
00042     isStopped = false;
00043     inst = speed;    
00044 }
00045 
00046 void Train::Stop(){
00047         inst = 0x40;
00048         isStopped = true;
00049         DCC_send_command(address, inst, nrPacket);
00050 }
00051 
00052 unsigned int Train::getSpeed(){
00053     return inst;
00054 }
00055 
00056 void Train::setPosition(int pos){
00057     position = pos;    
00058 }
00059 
00060 bool Train::isClockwise(){
00061     return dirClockwise;    
00062 }
00063 
00064 void Train::changeDirection(){
00065     dirClockwise = !dirClockwise;    
00066 }
00067 
00068 bool Train::checkStop(){
00069     return isStopped;
00070 }
00071     
00072 bool Train::checkInterupt(int pos){
00073     switch(pos){
00074     case 0:
00075         if(position == 13 || position == 0 || position == 12)
00076             return true;
00077         break;
00078     case 1: 
00079         if(position == 0 || position == 1 || position == 13)
00080             return true;
00081         break;
00082     case 2: 
00083         if(position == 15 || position == 2)
00084             return true;
00085         break;
00086     case 3: 
00087         if(position == 14 || position == 3 || position == 2)
00088             return true;
00089         break;
00090     case 4: 
00091         if(position == 14 || position == 4 || position == 2)
00092             return true;
00093         break;
00094     case 5: 
00095         if(position == 11 || position == 6 || position == 5)
00096             return true;
00097         break;
00098     case 6: 
00099         if(position == 7 || position == 4 || position == 5 || position == 6 || position == 8)
00100             return true;
00101         break;
00102     case 7: 
00103         if(position == 8 || position == 6 || position == 7 || position == 9)
00104             return true;
00105         break;
00106     case 8: 
00107         if(position == 7 || position == 9 || position == 8 || position == 6)
00108             return true;
00109         break;
00110     case 9: 
00111         if(position == 3 || position == 9 || position == 14)
00112             return true;
00113         break;
00114     case 10: 
00115         if(position == 8 || position == 10 || position == 7)
00116             return true;
00117         break;
00118     case 11: 
00119         if(position == 5 || position == 11)
00120             return true;
00121         break;
00122     case 12: 
00123         if(position == 11 || position == 10 || position == 12)
00124             return true;
00125         break;
00126     case 13: 
00127         if(position == 12 || position == 13)
00128             return true;
00129         break;
00130     case 14:
00131         if(position == 2 || position == 14 || position == 15)
00132             return true;
00133         break;
00134     case 15:
00135         if(position == 1 || position == 15)
00136             return true;
00137         break;
00138     default: 
00139         lcd.cls();
00140         lcd.printf("Train not right one");
00141         break;
00142     }
00143     return false;
00144 }