Jasmine Karlsson
/
train_rail
New project
main.cpp@22:c024b20a0e2d, 2018-06-13 (annotated)
- Committer:
- jasminealice
- Date:
- Wed Jun 13 09:06:55 2018 +0000
- Revision:
- 22:c024b20a0e2d
- Parent:
- 21:31647d80614f
- Child:
- 23:bb57966cb776
Try new mcp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jasminealice | 0:57cbecaac770 | 1 | #include "mbed.h" |
Kwame |
2:9bb410612929 | 2 | #include "TextLCD.h" |
jasminealice | 18:f5824ba95892 | 3 | #include "MCP23017.h" |
jasminealice | 18:f5824ba95892 | 4 | #include "Train.h" |
jasminealice | 20:32ba0a5f2d02 | 5 | #include "Switch.h" |
jasminealice | 20:32ba0a5f2d02 | 6 | #include "Track.h" |
jasminealice | 21:31647d80614f | 7 | #include "Detector.h" |
jasminealice | 22:c024b20a0e2d | 8 | #include "MCP23017.h" |
KwamsC | 10:cda57976225f | 9 | |
jasminealice | 16:55c3c5727f14 | 10 | //Board 1 |
jasminealice | 16:55c3c5727f14 | 11 | /*---------------------------------------------------------------------------- |
jasminealice | 16:55c3c5727f14 | 12 | Pin definitions |
jasminealice | 16:55c3c5727f14 | 13 | *----------------------------------------------------------------------------*/ |
jasminealice | 17:b7b5f40cebff | 14 | DigitalOut Track(p20); // train track |
jasminealice | 17:b7b5f40cebff | 15 | |
jasminealice | 17:b7b5f40cebff | 16 | DigitalOut myled1(LED1); |
KwamsC | 14:a5fd98a957e6 | 17 | DigitalOut myled2(LED2); |
KwamsC | 14:a5fd98a957e6 | 18 | DigitalOut myled3(LED3); |
KwamsC | 14:a5fd98a957e6 | 19 | DigitalOut myled4(LED4); |
jasminealice | 17:b7b5f40cebff | 20 | DigitalOut externalLed1(p15); |
jasminealice | 17:b7b5f40cebff | 21 | DigitalOut externalLed2(p16); |
jasminealice | 17:b7b5f40cebff | 22 | DigitalOut externalLed3(p17); |
jasminealice | 17:b7b5f40cebff | 23 | DigitalOut externalLed4(p18); |
KwamsC | 14:a5fd98a957e6 | 24 | |
KwamsC | 14:a5fd98a957e6 | 25 | TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd |
KwamsC | 14:a5fd98a957e6 | 26 | |
jasminealice | 16:55c3c5727f14 | 27 | //AnalogIn Ain(p20); // pot. met. |
KwamsC | 14:a5fd98a957e6 | 28 | /* Train detectors d2, d21, d22 (trainstation) */ |
jasminealice | 16:55c3c5727f14 | 29 | // detect_21(p17); |
jasminealice | 16:55c3c5727f14 | 30 | //DigitalIn detect_22(p16); |
jasminealice | 16:55c3c5727f14 | 31 | //DigitalIn detect_2(p15); |
KwamsC | 10:cda57976225f | 32 | |
jasminealice | 16:55c3c5727f14 | 33 | DigitalIn sw1(p29); |
jasminealice | 16:55c3c5727f14 | 34 | DigitalIn sw2(p30); |
jasminealice | 16:55c3c5727f14 | 35 | DigitalIn sw3(p11); |
jasminealice | 16:55c3c5727f14 | 36 | DigitalIn sw4(p12); |
jasminealice | 21:31647d80614f | 37 | |
jasminealice | 16:55c3c5727f14 | 38 | //InterruptIn sw1(p5); |
jasminealice | 22:c024b20a0e2d | 39 | InterruptIn int0(p13); |
jasminealice | 22:c024b20a0e2d | 40 | InterruptIn int1(p14); |
jasminealice | 22:c024b20a0e2d | 41 | I2C i2c(p28, p27); |
jasminealice | 18:f5824ba95892 | 42 | MCP23017 *mcp; |
jasminealice | 17:b7b5f40cebff | 43 | |
jasminealice | 17:b7b5f40cebff | 44 | /*---------------------------------------------------------------------------- |
jasminealice | 17:b7b5f40cebff | 45 | Addresses |
jasminealice | 17:b7b5f40cebff | 46 | *----------------------------------------------------------------------------*/ |
jasminealice | 21:31647d80614f | 47 | const unsigned int DCCaddress_silver = 0x01; |
jasminealice | 21:31647d80614f | 48 | const unsigned int DCCaddress_red = 0x03; |
jasminealice | 17:b7b5f40cebff | 49 | const unsigned int DCCaddress_switch = 0x06; |
KwamsC | 14:a5fd98a957e6 | 50 | |
KwamsC | 14:a5fd98a957e6 | 51 | /*---------------------------------------------------------------------------- |
KwamsC | 14:a5fd98a957e6 | 52 | Train movement |
KwamsC | 14:a5fd98a957e6 | 53 | *----------------------------------------------------------------------------*/ |
KwamsC | 14:a5fd98a957e6 | 54 | //move backwards/reverse |
jasminealice | 21:31647d80614f | 55 | const unsigned int DCCinst_reverse = 0x48; //reverse speed |
KwamsC | 14:a5fd98a957e6 | 56 | |
jasminealice | 15:8d96f7a06103 | 57 | //speed dial forward |
jasminealice | 21:31647d80614f | 58 | const unsigned int DCCinst_step2 = 0x72; //step 2 |
jasminealice | 18:f5824ba95892 | 59 | //const unsigned int DCCinst_step4 = 0x73; //step 4 |
jasminealice | 18:f5824ba95892 | 60 | //const unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed |
jasminealice | 16:55c3c5727f14 | 61 | const unsigned int DCCinst_step13 = 0x78; //step 13 1/2 speed |
jasminealice | 18:f5824ba95892 | 62 | //const unsigned int DCCinst_step20 = 0x75; //step 20 3/4 speed |
jasminealice | 18:f5824ba95892 | 63 | //const unsigned int DCCinst_step28 = 0x7F; //step 28 Full speed |
jasminealice | 17:b7b5f40cebff | 64 | const unsigned int DCCinst_switch1 = 0x81; //Activate switch1 |
jasminealice | 17:b7b5f40cebff | 65 | const unsigned int DCCinst_switch2 = 0x82; //Activate switch2 |
jasminealice | 17:b7b5f40cebff | 66 | const unsigned int DCCinst_switch3 = 0x84; //Activate switch3 |
jasminealice | 17:b7b5f40cebff | 67 | const unsigned int DCCinst_switch4 = 0x88; //Activate switch4 |
jasminealice | 17:b7b5f40cebff | 68 | const unsigned int DCCinst_deactive_switch = 0x80; //Deactivate switches |
KwamsC | 14:a5fd98a957e6 | 69 | //stop |
jasminealice | 16:55c3c5727f14 | 70 | const unsigned int DCCinst_stop = 0x40; //forward and stop 01100000 |
KwamsC | 10:cda57976225f | 71 | |
jasminealice | 18:f5824ba95892 | 72 | //detectors addresses |
jasminealice | 18:f5824ba95892 | 73 | const unsigned int detector_0 = 0xFFFE; |
jasminealice | 18:f5824ba95892 | 74 | const unsigned int detector_0_1 = 0xFFFC; |
jasminealice | 18:f5824ba95892 | 75 | const unsigned int detector_1 = 0xFFFD; |
jasminealice | 18:f5824ba95892 | 76 | |
jasminealice | 17:b7b5f40cebff | 77 | /*---------------------------------------------------------------------------- |
jasminealice | 17:b7b5f40cebff | 78 | Function definitions |
jasminealice | 17:b7b5f40cebff | 79 | *----------------------------------------------------------------------------*/ |
jasminealice | 18:f5824ba95892 | 80 | bool readDetector(DigitalIn detector); |
jasminealice | 17:b7b5f40cebff | 81 | bool readSwitch(DigitalIn theSwitch); |
jasminealice | 22:c024b20a0e2d | 82 | //void initialize_mcp(); |
jasminealice | 22:c024b20a0e2d | 83 | void on_int0_change(); |
jasminealice | 22:c024b20a0e2d | 84 | void on_int1_change(); |
jasminealice | 22:c024b20a0e2d | 85 | void init_mcp(); |
jasminealice | 22:c024b20a0e2d | 86 | void init(); |
jasminealice | 22:c024b20a0e2d | 87 | |
jasminealice | 16:55c3c5727f14 | 88 | |
KwamsC | 10:cda57976225f | 89 | |
jasminealice | 21:31647d80614f | 90 | //Trains |
jasminealice | 21:31647d80614f | 91 | Train redTrain(DCCaddress_red, DCCinst_step13, 2); |
jasminealice | 21:31647d80614f | 92 | Train silverTrain(DCCaddress_silver,DCCinst_step13, 12); |
jasminealice | 21:31647d80614f | 93 | //Switches |
jasminealice | 21:31647d80614f | 94 | Switch switch1(DCCaddress_switch,DCCinst_switch1); |
jasminealice | 21:31647d80614f | 95 | //Switch switch2(DCCaddress_switch,DCCinst_switch2); |
jasminealice | 21:31647d80614f | 96 | Switch switch3(DCCaddress_switch,DCCinst_switch3); |
jasminealice | 21:31647d80614f | 97 | Switch switch4(DCCaddress_switch,DCCinst_switch4); |
jasminealice | 21:31647d80614f | 98 | //Detectors |
jasminealice | 21:31647d80614f | 99 | Detector dect; |
jasminealice | 21:31647d80614f | 100 | |
jasminealice | 16:55c3c5727f14 | 101 | /*---------------------------------------------------------------------------- |
jasminealice | 16:55c3c5727f14 | 102 | Main |
jasminealice | 16:55c3c5727f14 | 103 | *----------------------------------------------------------------------------*/ |
jasminealice | 16:55c3c5727f14 | 104 | int main() { |
jasminealice | 16:55c3c5727f14 | 105 | /*sw1.rise(&testInterupt2); |
jasminealice | 16:55c3c5727f14 | 106 | sw1.fall(&testInterupt); |
jasminealice | 16:55c3c5727f14 | 107 | inter0.rise(&riseFunction); |
jasminealice | 16:55c3c5727f14 | 108 | inter1.rise(&riseFunction);*/ |
jasminealice | 22:c024b20a0e2d | 109 | lcd.printf("Start journey"); |
jasminealice | 22:c024b20a0e2d | 110 | init_mcp(); |
jasminealice | 22:c024b20a0e2d | 111 | init(); |
jasminealice | 22:c024b20a0e2d | 112 | /*int res[2] = {2,4}; |
jasminealice | 21:31647d80614f | 113 | dect.reserveDetector(res); //Run trains counterclockwise and reserve the |
jasminealice | 21:31647d80614f | 114 | int res2[2] = {12,13}; |
jasminealice | 21:31647d80614f | 115 | dect.reserveDetector(res2);//detectors in front |
jasminealice | 22:c024b20a0e2d | 116 | */ |
jasminealice | 21:31647d80614f | 117 | while(1){ |
jasminealice | 22:c024b20a0e2d | 118 | |
jasminealice | 21:31647d80614f | 119 | if(readSwitch(sw1)){ //Control switches |
jasminealice | 22:c024b20a0e2d | 120 | /*switch1.switchOn(); |
jasminealice | 20:32ba0a5f2d02 | 121 | lcd.printf("Switch 1 on"); |
jasminealice | 20:32ba0a5f2d02 | 122 | //switch2.switchOn(); // Need to turn it off immediately!!! |
jasminealice | 20:32ba0a5f2d02 | 123 | switch3.switchOn(); |
jasminealice | 22:c024b20a0e2d | 124 | switch4.switchOn();*/ |
jasminealice | 22:c024b20a0e2d | 125 | |
jasminealice | 22:c024b20a0e2d | 126 | // Clear current interrupts |
jasminealice | 22:c024b20a0e2d | 127 | mcp->_read(GPIOA); |
jasminealice | 22:c024b20a0e2d | 128 | mcp->_read(GPIOB); |
jasminealice | 22:c024b20a0e2d | 129 | // Register callbacks |
jasminealice | 22:c024b20a0e2d | 130 | int0.fall(&on_int0_change); |
jasminealice | 22:c024b20a0e2d | 131 | int1.fall(&on_int1_change); |
jasminealice | 20:32ba0a5f2d02 | 132 | |
jasminealice | 20:32ba0a5f2d02 | 133 | } |
jasminealice | 22:c024b20a0e2d | 134 | /*else{ |
jasminealice | 20:32ba0a5f2d02 | 135 | //DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10); |
jasminealice | 20:32ba0a5f2d02 | 136 | switch1.switchOff(); |
jasminealice | 20:32ba0a5f2d02 | 137 | //switch2.switchOff(); |
jasminealice | 20:32ba0a5f2d02 | 138 | switch3.switchOff(); |
jasminealice | 20:32ba0a5f2d02 | 139 | switch4.switchOff(); |
jasminealice | 20:32ba0a5f2d02 | 140 | lcd.printf("Switch 1 off"); |
jasminealice | 20:32ba0a5f2d02 | 141 | |
jasminealice | 20:32ba0a5f2d02 | 142 | } |
jasminealice | 20:32ba0a5f2d02 | 143 | |
jasminealice | 21:31647d80614f | 144 | if(readSwitch(sw2)){ //run train |
jasminealice | 21:31647d80614f | 145 | redTrain.goForward(); |
jasminealice | 21:31647d80614f | 146 | silverTrain.goForward(); |
jasminealice | 20:32ba0a5f2d02 | 147 | } |
jasminealice | 20:32ba0a5f2d02 | 148 | else |
jasminealice | 20:32ba0a5f2d02 | 149 | { |
jasminealice | 21:31647d80614f | 150 | redTrain.Stop(); |
jasminealice | 21:31647d80614f | 151 | silverTrain.Stop(); |
jasminealice | 20:32ba0a5f2d02 | 152 | } |
jasminealice | 21:31647d80614f | 153 | if(!readSwitch(sw3)){ //Change speed |
jasminealice | 21:31647d80614f | 154 | if(silverTrain.getSpeed() == DCCinst_step2) |
jasminealice | 21:31647d80614f | 155 | silverTrain.changeSpeed(DCCinst_step13); |
jasminealice | 21:31647d80614f | 156 | else |
jasminealice | 21:31647d80614f | 157 | silverTrain.changeSpeed(DCCinst_step2); |
jasminealice | 21:31647d80614f | 158 | |
jasminealice | 22:c024b20a0e2d | 159 | } |
jasminealice | 22:c024b20a0e2d | 160 | } */ |
jasminealice | 20:32ba0a5f2d02 | 161 | |
jasminealice | 20:32ba0a5f2d02 | 162 | /*lcd.printf("Start the journey"); |
jasminealice | 18:f5824ba95892 | 163 | time_t tstart, tend; |
jasminealice | 18:f5824ba95892 | 164 | initialize_mcp(); |
jasminealice | 18:f5824ba95892 | 165 | int data = mcp->readRegister(0x12); |
jasminealice | 18:f5824ba95892 | 166 | lcd.printf("%d", data); |
jasminealice | 18:f5824ba95892 | 167 | wait(2); |
jasminealice | 21:31647d80614f | 168 | */ |
jasminealice | 22:c024b20a0e2d | 169 | } |
jasminealice | 16:55c3c5727f14 | 170 | } |
KwamsC | 10:cda57976225f | 171 | |
KwamsC | 14:a5fd98a957e6 | 172 | /*---------------------------------------------------------------------------- |
KwamsC | 14:a5fd98a957e6 | 173 | Functions |
KwamsC | 14:a5fd98a957e6 | 174 | *----------------------------------------------------------------------------*/ |
jasminealice | 4:4b62680f91b5 | 175 | |
jasminealice | 21:31647d80614f | 176 | |
jasminealice | 21:31647d80614f | 177 | bool testInterupt(){ |
jasminealice | 22:c024b20a0e2d | 178 | int pos = 0;// mcp->readRegister(0x12); |
jasminealice | 15:8d96f7a06103 | 179 | lcd.cls(); |
jasminealice | 21:31647d80614f | 180 | //Check which train got the interupt |
jasminealice | 21:31647d80614f | 181 | Train *temptrain; |
jasminealice | 21:31647d80614f | 182 | if(redTrain.checkInterupt(pos)){ |
jasminealice | 21:31647d80614f | 183 | redTrain.setPosition(pos); |
jasminealice | 21:31647d80614f | 184 | temptrain = &redTrain; |
jasminealice | 21:31647d80614f | 185 | } |
jasminealice | 21:31647d80614f | 186 | else if(silverTrain.checkInterupt(pos)){ |
jasminealice | 21:31647d80614f | 187 | silverTrain.setPosition(pos); |
jasminealice | 21:31647d80614f | 188 | temptrain = &silverTrain; |
jasminealice | 21:31647d80614f | 189 | } |
jasminealice | 21:31647d80614f | 190 | else{ |
jasminealice | 21:31647d80614f | 191 | //WE HAVE A PROBLEM |
jasminealice | 21:31647d80614f | 192 | lcd.printf("WRONG DETECTOR DETECTED"); |
jasminealice | 21:31647d80614f | 193 | return false; |
jasminealice | 21:31647d80614f | 194 | } |
jasminealice | 21:31647d80614f | 195 | |
jasminealice | 21:31647d80614f | 196 | switch(pos){ |
jasminealice | 21:31647d80614f | 197 | case 0: |
jasminealice | 21:31647d80614f | 198 | case 1: |
jasminealice | 22:c024b20a0e2d | 199 | //Slow down |
jasminealice | 21:31647d80614f | 200 | case 2: |
jasminealice | 21:31647d80614f | 201 | //IF Clock 2->1 |
jasminealice | 21:31647d80614f | 202 | //Disable Switch2 |
jasminealice | 21:31647d80614f | 203 | //Free 3,4,9 |
jasminealice | 21:31647d80614f | 204 | //Move forward |
jasminealice | 21:31647d80614f | 205 | //IF CC |
jasminealice | 21:31647d80614f | 206 | //Check if 4 or 3 is reserved |
jasminealice | 21:31647d80614f | 207 | //IF NOT randomize which way to go |
jasminealice | 21:31647d80614f | 208 | //0.3 chance to enable the switch2, reserve 3,9 and go |
jasminealice | 21:31647d80614f | 209 | //0.7 chance to disable switch2, reserve 4 and go |
jasminealice | 21:31647d80614f | 210 | //IF 4 is reserved |
jasminealice | 21:31647d80614f | 211 | //Enable switch2, reserve 3,9 and go |
jasminealice | 21:31647d80614f | 212 | //IF 3 reserved |
jasminealice | 21:31647d80614f | 213 | //Disable switch2, reserve 4 and go |
jasminealice | 21:31647d80614f | 214 | case 3: |
jasminealice | 21:31647d80614f | 215 | //Clock: Goes from 3 to 2 |
jasminealice | 21:31647d80614f | 216 | //Enable switch2 |
jasminealice | 21:31647d80614f | 217 | //Reserve 0,1,2, 12, 13 |
jasminealice | 21:31647d80614f | 218 | //Go forward |
jasminealice | 21:31647d80614f | 219 | //Other Clock: Goes from 3->9 |
jasminealice | 21:31647d80614f | 220 | //disable switch2 |
jasminealice | 21:31647d80614f | 221 | //free 0,1,2,12,13 |
jasminealice | 21:31647d80614f | 222 | //Go forward |
jasminealice | 21:31647d80614f | 223 | case 4: |
jasminealice | 21:31647d80614f | 224 | //IF CC : goes towards 6 |
jasminealice | 21:31647d80614f | 225 | //free nr 0,1,2,12, 13 |
jasminealice | 21:31647d80614f | 226 | //Turn of switch3 |
jasminealice | 21:31647d80614f | 227 | //Check if 6 is reserved |
jasminealice | 21:31647d80614f | 228 | //IF yes STOP |
jasminealice | 21:31647d80614f | 229 | //else reserve 6,7,8 and go forward |
jasminealice | 21:31647d80614f | 230 | //IF Clock goes towards 2 |
jasminealice | 21:31647d80614f | 231 | //free nr 5,6, 7, 8 |
jasminealice | 21:31647d80614f | 232 | //Turn of switch2 |
jasminealice | 21:31647d80614f | 233 | //Check if 2 is reserved |
jasminealice | 21:31647d80614f | 234 | //IF yes STOP |
jasminealice | 21:31647d80614f | 235 | //else reserve 12,13,0,1,2 go forward |
jasminealice | 21:31647d80614f | 236 | |
jasminealice | 21:31647d80614f | 237 | case 5: |
jasminealice | 21:31647d80614f | 238 | //Clock: Goes from 5->6 |
jasminealice | 21:31647d80614f | 239 | //Enable switch3 |
jasminealice | 21:31647d80614f | 240 | //Reserve 6,7,8 |
jasminealice | 21:31647d80614f | 241 | //Go forward |
jasminealice | 21:31647d80614f | 242 | //Other Clock: Goes from 5->11 |
jasminealice | 21:31647d80614f | 243 | //disable switch3 |
jasminealice | 21:31647d80614f | 244 | //free 6,7,8 |
jasminealice | 21:31647d80614f | 245 | //Go forward |
jasminealice | 21:31647d80614f | 246 | case 6: |
jasminealice | 21:31647d80614f | 247 | //IF CC |
jasminealice | 21:31647d80614f | 248 | //Disable switch3 |
jasminealice | 21:31647d80614f | 249 | //Free 4,5,11 |
jasminealice | 21:31647d80614f | 250 | //Move forward |
jasminealice | 21:31647d80614f | 251 | //IF Clock |
jasminealice | 21:31647d80614f | 252 | //Check if 4 or 5 is reserved |
jasminealice | 21:31647d80614f | 253 | //IF NOT randomize which way to go |
jasminealice | 21:31647d80614f | 254 | //0.3 chance to enable the switch3, reserve 5,11 and go |
jasminealice | 21:31647d80614f | 255 | //0.7 chance to disable switch3, reserve 4 and go |
jasminealice | 21:31647d80614f | 256 | //IF 4 is reserved |
jasminealice | 21:31647d80614f | 257 | //Enable switch3, reserve 5,11 and go |
jasminealice | 21:31647d80614f | 258 | //IF 5 reserved |
jasminealice | 21:31647d80614f | 259 | //Disable switch3, reserve 4 and go |
jasminealice | 21:31647d80614f | 260 | |
jasminealice | 21:31647d80614f | 261 | |
jasminealice | 21:31647d80614f | 262 | case 7: |
jasminealice | 21:31647d80614f | 263 | case 8: |
jasminealice | 21:31647d80614f | 264 | //IF Clock 8->7 |
jasminealice | 21:31647d80614f | 265 | //Disable switch4 |
jasminealice | 21:31647d80614f | 266 | //Free 3,9,10 |
jasminealice | 21:31647d80614f | 267 | //Move forward |
jasminealice | 21:31647d80614f | 268 | //IF CC |
jasminealice | 21:31647d80614f | 269 | //Check if 9 or 10 is reserved |
jasminealice | 21:31647d80614f | 270 | //IF NOT randomize which way to go |
jasminealice | 21:31647d80614f | 271 | //0.3 chance to enable the switch4, reserve 3,9 and go |
jasminealice | 21:31647d80614f | 272 | //0.7 chance to disable switch4, reserve 10 and go |
jasminealice | 21:31647d80614f | 273 | //IF 10 is reserved |
jasminealice | 21:31647d80614f | 274 | //Enable switch4, reserve 3,9 and go |
jasminealice | 21:31647d80614f | 275 | //IF 9 reserved |
jasminealice | 21:31647d80614f | 276 | //Disable switch4, reserve 10 and go |
jasminealice | 21:31647d80614f | 277 | case 9: |
jasminealice | 21:31647d80614f | 278 | //Clock: Goes from 9 to 8 |
jasminealice | 21:31647d80614f | 279 | //Enable switch4 |
jasminealice | 21:31647d80614f | 280 | //Reserve 6,7,8 |
jasminealice | 21:31647d80614f | 281 | //Go forward |
jasminealice | 21:31647d80614f | 282 | //Other Clock: Goes from 9->3 |
jasminealice | 21:31647d80614f | 283 | //disable switch4 |
jasminealice | 21:31647d80614f | 284 | //free 6,7,8 |
jasminealice | 21:31647d80614f | 285 | //Go forward |
jasminealice | 21:31647d80614f | 286 | case 10: |
jasminealice | 21:31647d80614f | 287 | //IF CC |
jasminealice | 21:31647d80614f | 288 | //free nr 6,7,8,9 |
jasminealice | 21:31647d80614f | 289 | //Turn off switch1 |
jasminealice | 21:31647d80614f | 290 | //Check if 12 is reserved |
jasminealice | 21:31647d80614f | 291 | //IF yes STOP |
jasminealice | 21:31647d80614f | 292 | //else reserve 12,13,0,1,2 and go forward |
jasminealice | 21:31647d80614f | 293 | //IF Clock |
jasminealice | 21:31647d80614f | 294 | //Free nr 0,1,2,11, 12, 13 |
jasminealice | 21:31647d80614f | 295 | //Turn off switch4 |
jasminealice | 21:31647d80614f | 296 | //Check if 8 is reserved |
jasminealice | 21:31647d80614f | 297 | //IF yes STOP |
jasminealice | 21:31647d80614f | 298 | //else reserve 6,7,8 go forward |
jasminealice | 21:31647d80614f | 299 | case 11: |
jasminealice | 21:31647d80614f | 300 | //Clock: Goes from 11 to 12 |
jasminealice | 21:31647d80614f | 301 | //Enable switch1 |
jasminealice | 21:31647d80614f | 302 | //Reserve 0,1,2, 12, 13 |
jasminealice | 21:31647d80614f | 303 | //Go forward |
jasminealice | 21:31647d80614f | 304 | //Other Clock: Goes from 11->5 |
jasminealice | 21:31647d80614f | 305 | //disable switch1 |
jasminealice | 21:31647d80614f | 306 | //free 0,1,2,12,13 |
jasminealice | 21:31647d80614f | 307 | //Go forward |
jasminealice | 21:31647d80614f | 308 | case 12: |
jasminealice | 21:31647d80614f | 309 | //IF CC: 12->13 |
jasminealice | 21:31647d80614f | 310 | //Disable switch1 |
jasminealice | 21:31647d80614f | 311 | //Free 5,10,11 |
jasminealice | 21:31647d80614f | 312 | //Move forward |
jasminealice | 21:31647d80614f | 313 | //IF Clock |
jasminealice | 21:31647d80614f | 314 | //Check if 11 or 10 is reserved |
jasminealice | 21:31647d80614f | 315 | //IF NOT randomize which way to go |
jasminealice | 21:31647d80614f | 316 | //0.3 chance to enable switch1, reserve 5,11 and go |
jasminealice | 21:31647d80614f | 317 | //0.7 chance to disable switch1, reserve 10 and go |
jasminealice | 21:31647d80614f | 318 | //IF 10 is reserved |
jasminealice | 21:31647d80614f | 319 | //Enable switch1, reserve 5,11 and go |
jasminealice | 21:31647d80614f | 320 | //IF 11 reserved |
jasminealice | 21:31647d80614f | 321 | //Disable switch1,reserve 10 and go |
jasminealice | 21:31647d80614f | 322 | case 13: |
jasminealice | 21:31647d80614f | 323 | |
jasminealice | 21:31647d80614f | 324 | lcd.printf("In case "); |
jasminealice | 21:31647d80614f | 325 | lcd.printf("%d", pos); |
jasminealice | 21:31647d80614f | 326 | break; |
jasminealice | 21:31647d80614f | 327 | default: |
jasminealice | 21:31647d80614f | 328 | lcd.printf("Not in a valid case"); |
jasminealice | 21:31647d80614f | 329 | break; |
jasminealice | 21:31647d80614f | 330 | } |
jasminealice | 21:31647d80614f | 331 | |
jasminealice | 21:31647d80614f | 332 | return true; |
jasminealice | 15:8d96f7a06103 | 333 | } |
jasminealice | 15:8d96f7a06103 | 334 | |
jasminealice | 21:31647d80614f | 335 | /* |
jasminealice | 15:8d96f7a06103 | 336 | void testInterupt2(){ |
jasminealice | 15:8d96f7a06103 | 337 | lcd.cls(); |
jasminealice | 15:8d96f7a06103 | 338 | myled = 0; |
jasminealice | 15:8d96f7a06103 | 339 | lcd.printf("In interupt2 function"); |
jasminealice | 15:8d96f7a06103 | 340 | lcd.printf("%d", sw1.read()); |
jasminealice | 15:8d96f7a06103 | 341 | myled4 = 1; |
jasminealice | 15:8d96f7a06103 | 342 | } |
jasminealice | 15:8d96f7a06103 | 343 | |
jasminealice | 15:8d96f7a06103 | 344 | void riseFunction(){ |
jasminealice | 15:8d96f7a06103 | 345 | lcd.printf("In rise function"); |
jasminealice | 15:8d96f7a06103 | 346 | lcd.printf("%d", inter1.read()); |
jasminealice | 15:8d96f7a06103 | 347 | } |
jasminealice | 15:8d96f7a06103 | 348 | |
jasminealice | 15:8d96f7a06103 | 349 | void fallFunction(){ |
jasminealice | 15:8d96f7a06103 | 350 | lcd.printf("In fall function"); |
jasminealice | 15:8d96f7a06103 | 351 | lcd.printf("%d", inter1.read()); |
jasminealice | 16:55c3c5727f14 | 352 | }*/ |
jasminealice | 16:55c3c5727f14 | 353 | |
jasminealice | 17:b7b5f40cebff | 354 | bool readSwitch(DigitalIn theSwitch){ |
jasminealice | 17:b7b5f40cebff | 355 | int val = theSwitch.read(); |
jasminealice | 22:c024b20a0e2d | 356 | //lcd.printf("%d", val); |
jasminealice | 16:55c3c5727f14 | 357 | if(val == 1) |
jasminealice | 16:55c3c5727f14 | 358 | return true; |
jasminealice | 16:55c3c5727f14 | 359 | else |
jasminealice | 16:55c3c5727f14 | 360 | return false; |
tanjinamicky | 13:78a2481fd65b | 361 | } |
tanjinamicky | 13:78a2481fd65b | 362 | |
jasminealice | 18:f5824ba95892 | 363 | bool readDetector(DigitalIn detector){ |
jasminealice | 18:f5824ba95892 | 364 | /*int val = detect.read(); |
jasminealice | 18:f5824ba95892 | 365 | if(val == 1) |
jasminealice | 15:8d96f7a06103 | 366 | { |
jasminealice | 15:8d96f7a06103 | 367 | lcd.cls(); |
jasminealice | 18:f5824ba95892 | 368 | lcd.printf("Detect: "); |
jasminealice | 18:f5824ba95892 | 369 | lcd.printf("%d", val); |
jasminealice | 15:8d96f7a06103 | 370 | return true; |
jasminealice | 16:55c3c5727f14 | 371 | }*/ |
jasminealice | 17:b7b5f40cebff | 372 | return false; |
jasminealice | 15:8d96f7a06103 | 373 | } |
tanjinamicky | 13:78a2481fd65b | 374 | |
jasminealice | 22:c024b20a0e2d | 375 | void init_mcp() { |
jasminealice | 22:c024b20a0e2d | 376 | // Initialisation of MCP registers,documentation on registers is availableatNiels/Jacco/Natalia |
jasminealice | 22:c024b20a0e2d | 377 | mcp = new MCP23017(i2c, 0x40); |
jasminealice | 22:c024b20a0e2d | 378 | mcp->_write(IODIRA, (unsigned char )0xff); |
jasminealice | 22:c024b20a0e2d | 379 | mcp->_write(IODIRB, (unsigned char )0xff); |
jasminealice | 22:c024b20a0e2d | 380 | mcp->_write(IPOLA, (unsigned char )0x00); |
jasminealice | 22:c024b20a0e2d | 381 | mcp->_write(IPOLB, (unsigned char )0x00); |
jasminealice | 22:c024b20a0e2d | 382 | mcp->_write(DEFVALA, (unsigned char )0xff); |
jasminealice | 22:c024b20a0e2d | 383 | mcp->_write(DEFVALB, (unsigned char )0xff); |
jasminealice | 22:c024b20a0e2d | 384 | mcp->_write(INTCONA, (unsigned char )0xff); |
jasminealice | 22:c024b20a0e2d | 385 | mcp->_write(INTCONB, (unsigned char )0xff); |
jasminealice | 22:c024b20a0e2d | 386 | mcp->_write(IOCONA, (unsigned char )0x2); |
jasminealice | 22:c024b20a0e2d | 387 | mcp->_write(IOCONB, (unsigned char )0x2); |
jasminealice | 22:c024b20a0e2d | 388 | mcp->_write(GPPUA, (unsigned char )0xff); |
jasminealice | 22:c024b20a0e2d | 389 | mcp->_write(GPPUB, (unsigned char )0xff); |
jasminealice | 18:f5824ba95892 | 390 | } |
jasminealice | 22:c024b20a0e2d | 391 | |
jasminealice | 22:c024b20a0e2d | 392 | void init() { |
jasminealice | 22:c024b20a0e2d | 393 | // Clear current interrupts |
jasminealice | 22:c024b20a0e2d | 394 | mcp->_read(GPIOA); |
jasminealice | 22:c024b20a0e2d | 395 | mcp->_read(GPIOB); |
jasminealice | 22:c024b20a0e2d | 396 | // Register callbacks |
jasminealice | 22:c024b20a0e2d | 397 | int0.fall(&on_int0_change); |
jasminealice | 22:c024b20a0e2d | 398 | int1.fall(&on_int1_change); |
jasminealice | 22:c024b20a0e2d | 399 | // Enable interrupts on MCP |
jasminealice | 22:c024b20a0e2d | 400 | mcp->_write(GPINTENA, (unsigned char )0xff); |
jasminealice | 22:c024b20a0e2d | 401 | mcp->_write(GPINTENB, (unsigned char )0xff);// Ready to go! |
jasminealice | 22:c024b20a0e2d | 402 | } |
jasminealice | 22:c024b20a0e2d | 403 | |
jasminealice | 22:c024b20a0e2d | 404 | void on_int0_change() { |
jasminealice | 22:c024b20a0e2d | 405 | lcd.printf("In int0)"); |
jasminealice | 22:c024b20a0e2d | 406 | // In your code, you might want to move this logic out of the interrupt routine |
jasminealice | 22:c024b20a0e2d | 407 | // (for instance, by setting a flag and checking this flagin your main-loop) |
jasminealice | 22:c024b20a0e2d | 408 | wait_us(200000); |
jasminealice | 22:c024b20a0e2d | 409 | int sensor_data = mcp->_read(INTCAPA); |
jasminealice | 22:c024b20a0e2d | 410 | // The only thing we do with the interrupt signal is printing it |
jasminealice | 22:c024b20a0e2d | 411 | lcd.cls(); |
jasminealice | 22:c024b20a0e2d | 412 | lcd.printf("GOT INT0 (0x%x)\n", sensor_data); |
jasminealice | 22:c024b20a0e2d | 413 | } |
jasminealice | 22:c024b20a0e2d | 414 | |
jasminealice | 22:c024b20a0e2d | 415 | void on_int1_change() { |
jasminealice | 22:c024b20a0e2d | 416 | lcd.printf("In int1)"); |
jasminealice | 22:c024b20a0e2d | 417 | // In your code, you might want to move this logic out of the interrupt routine |
jasminealice | 22:c024b20a0e2d | 418 | // (for instance, by setting a flag and checking this flagin your main-loop) |
jasminealice | 22:c024b20a0e2d | 419 | wait_us(200000); |
jasminealice | 22:c024b20a0e2d | 420 | int sensor_data = mcp->_read(INTCAPB); |
jasminealice | 22:c024b20a0e2d | 421 | // The only thing we do with the interrupt signal is printing it |
jasminealice | 22:c024b20a0e2d | 422 | lcd.cls(); |
jasminealice | 22:c024b20a0e2d | 423 | lcd.printf("GOT INT0 (1) (0x%x)\n", sensor_data); |
jasminealice | 22:c024b20a0e2d | 424 | } |