![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
The MBED firmware used on the Chipin sorter, developed over 12 weeks for a 3rd year university systems project. Chipin is a token sorter, it sorts tokens by colours and dispenses them to order through an online booking system and card reader. This program interfaces with an FPGA, PC and LCD screen to control the sorter. The sorter has an operation mode where it can process orders when a card is entered into the machine. There is also a maintenance mode where the device responds to maintenance instructions such as 'dispense all'. More information at http://www.ionsystems.uk/
Dependencies: MCP23017 TCS3472_I2C WattBob_TextLCD mbed-rtos mbed
main.cpp@13:0661d658d9d1, 2014-11-20 (annotated)
- Committer:
- IonSystems
- Date:
- Thu Nov 20 11:15:47 2014 +0000
- Revision:
- 13:0661d658d9d1
- Parent:
- 12:814a8fdbb6f7
- Child:
- 14:31ba3e56c788
Maintenance mode added, still need to do the printouts to the lcd.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IonSystems | 12:814a8fdbb6f7 | 1 | bool operationMode = true; //the MBED starts in operation mode. |
IonSystems | 0:8d54ffcf256e | 2 | #include "mbed.h" |
IonSystems | 6:e64796f1f384 | 3 | #include <sstream> |
IonSystems | 12:814a8fdbb6f7 | 4 | #include <string> |
IonSystems | 6:e64796f1f384 | 5 | #include "lcdCommands.h" |
IonSystems | 6:e64796f1f384 | 6 | #include "mbedStorage.h" |
IonSystems | 6:e64796f1f384 | 7 | #include "rgbLED.h" |
IonSystems | 6:e64796f1f384 | 8 | #include "mbedLCD.h" |
IonSystems | 7:6ba00694f9cd | 9 | #include "stateMachine.h" |
IonSystems | 8:b7771391adc9 | 10 | #include "FPGAcomms.h" |
IonSystems | 10:8c0696b99692 | 11 | #ifndef serialComm |
IonSystems | 10:8c0696b99692 | 12 | #define serialComm |
IonSystems | 10:8c0696b99692 | 13 | #include "serialCommunication.h" |
IonSystems | 10:8c0696b99692 | 14 | #endif |
IonSystems | 10:8c0696b99692 | 15 | #include "testFunctions.h" |
IonSystems | 5:644bca33c1ca | 16 | |
IonSystems | 0:8d54ffcf256e | 17 | |
IonSystems | 11:06f6e82b40a8 | 18 | |
IonSystems | 11:06f6e82b40a8 | 19 | |
IonSystems | 7:6ba00694f9cd | 20 | StateMachine stateMachine; |
IonSystems | 7:6ba00694f9cd | 21 | |
IonSystems | 6:e64796f1f384 | 22 | //Boolean values to easily enable and disable certain features for individual testing |
IonSystems | 6:e64796f1f384 | 23 | bool colourSensor = true; |
IonSystems | 6:e64796f1f384 | 24 | bool cardReader = true; |
IonSystems | 6:e64796f1f384 | 25 | bool sorter = true; |
IonSystems | 6:e64796f1f384 | 26 | bool dispensor = true; |
IonSystems | 10:8c0696b99692 | 27 | bool testFunctions = true; |
IonSystems | 10:8c0696b99692 | 28 | bool serialComms = true; |
IonSystems | 6:e64796f1f384 | 29 | |
IonSystems | 6:e64796f1f384 | 30 | bool colourDataAcquired = false; |
IonSystems | 6:e64796f1f384 | 31 | bool chipDetected = false; |
IonSystems | 12:814a8fdbb6f7 | 32 | |
IonSystems | 3:97668a4cd69d | 33 | |
IonSystems | 12:814a8fdbb6f7 | 34 | int mode = 0; |
IonSystems | 13:0661d658d9d1 | 35 | int maintenanceModePointer = 0; |
IonSystems | 10:8c0696b99692 | 36 | |
IonSystems | 10:8c0696b99692 | 37 | |
IonSystems | 10:8c0696b99692 | 38 | void checkSortDispenseButtons(){ |
IonSystems | 11:06f6e82b40a8 | 39 | |
IonSystems | 11:06f6e82b40a8 | 40 | if(par_port->read_bit(11)){ |
IonSystems | 12:814a8fdbb6f7 | 41 | mode++; |
IonSystems | 13:0661d658d9d1 | 42 | if(mode >= 7) mode = 0; |
IonSystems | 11:06f6e82b40a8 | 43 | } |
IonSystems | 11:06f6e82b40a8 | 44 | |
IonSystems | 11:06f6e82b40a8 | 45 | |
IonSystems | 13:0661d658d9d1 | 46 | if(mode == 0){ //Dispense Mode |
IonSystems | 11:06f6e82b40a8 | 47 | printLCD("Dispense Mode"); |
IonSystems | 10:8c0696b99692 | 48 | if(par_port->read_bit(8)){ |
IonSystems | 10:8c0696b99692 | 49 | log("Red Button pressed"); |
IonSystems | 10:8c0696b99692 | 50 | stateMachine = DISPENSE_RED; |
IonSystems | 10:8c0696b99692 | 51 | processState(stateMachine); |
IonSystems | 10:8c0696b99692 | 52 | }else if(par_port->read_bit(9)){ |
IonSystems | 10:8c0696b99692 | 53 | log("Green Button pressed"); |
IonSystems | 10:8c0696b99692 | 54 | stateMachine = DISPENSE_GREEN; |
IonSystems | 10:8c0696b99692 | 55 | processState(stateMachine); |
IonSystems | 10:8c0696b99692 | 56 | }else if(par_port->read_bit(10)){ |
IonSystems | 10:8c0696b99692 | 57 | log("Blue Button pressed"); |
IonSystems | 10:8c0696b99692 | 58 | stateMachine = DISPENSE_BLUE; |
IonSystems | 10:8c0696b99692 | 59 | processState(stateMachine); |
IonSystems | 10:8c0696b99692 | 60 | } |
IonSystems | 13:0661d658d9d1 | 61 | }else if(mode == 1){ //Sort Mode |
IonSystems | 11:06f6e82b40a8 | 62 | printLCD("Sort Mode"); |
IonSystems | 10:8c0696b99692 | 63 | if(par_port->read_bit(8)){ |
IonSystems | 10:8c0696b99692 | 64 | log("Red Button pressed"); |
IonSystems | 10:8c0696b99692 | 65 | stateMachine = SORT_RED; |
IonSystems | 10:8c0696b99692 | 66 | processState(stateMachine); |
IonSystems | 10:8c0696b99692 | 67 | }else if(par_port->read_bit(9)){ |
IonSystems | 13:0661d658d9d1 | 68 | log("Green Button pressed"); |
IonSystems | 10:8c0696b99692 | 69 | stateMachine = SORT_GREEN; |
IonSystems | 10:8c0696b99692 | 70 | processState(stateMachine); |
IonSystems | 10:8c0696b99692 | 71 | }else if(par_port->read_bit(10)){ |
IonSystems | 10:8c0696b99692 | 72 | log("Blue Button pressed"); |
IonSystems | 10:8c0696b99692 | 73 | stateMachine = SORT_BLUE; |
IonSystems | 10:8c0696b99692 | 74 | processState(stateMachine); |
IonSystems | 10:8c0696b99692 | 75 | } |
IonSystems | 11:06f6e82b40a8 | 76 | |
IonSystems | 13:0661d658d9d1 | 77 | }else if(mode == 2){ //Bin/Recycle Mode |
IonSystems | 11:06f6e82b40a8 | 78 | printLCD("Bin/Recycle Mode"); |
IonSystems | 11:06f6e82b40a8 | 79 | if(par_port->read_bit(8)){ |
IonSystems | 11:06f6e82b40a8 | 80 | log("Bin button pressed"); |
IonSystems | 11:06f6e82b40a8 | 81 | stateMachine = SORT_BIN; |
IonSystems | 11:06f6e82b40a8 | 82 | processState(stateMachine); |
IonSystems | 11:06f6e82b40a8 | 83 | }else if(par_port->read_bit(9)){ |
IonSystems | 11:06f6e82b40a8 | 84 | log("Recycle Button"); |
IonSystems | 11:06f6e82b40a8 | 85 | stateMachine = SORT_RECYCLE; |
IonSystems | 11:06f6e82b40a8 | 86 | processState(stateMachine); |
IonSystems | 11:06f6e82b40a8 | 87 | } |
IonSystems | 13:0661d658d9d1 | 88 | }else if(mode == 3){ //Colour Sensing Mode |
IonSystems | 11:06f6e82b40a8 | 89 | if(colourSensor){ |
IonSystems | 11:06f6e82b40a8 | 90 | Colour colour = readColourSensor(); |
IonSystems | 13:0661d658d9d1 | 91 | if(sorter) sort(colour); //Put (sorter && colour != BIN) in if statement. |
IonSystems | 11:06f6e82b40a8 | 92 | } |
IonSystems | 13:0661d658d9d1 | 93 | }else if(mode == 4){ //Card Reader Mode |
IonSystems | 12:814a8fdbb6f7 | 94 | if(cardReader && cardDetect){ |
IonSystems | 12:814a8fdbb6f7 | 95 | cardAcquisition(); |
IonSystems | 12:814a8fdbb6f7 | 96 | |
IonSystems | 12:814a8fdbb6f7 | 97 | } |
IonSystems | 13:0661d658d9d1 | 98 | }else if(mode == 5){ //Storage Mode |
IonSystems | 12:814a8fdbb6f7 | 99 | stringstream ss; |
IonSystems | 12:814a8fdbb6f7 | 100 | ss << "Stored Values " << "R:" << redAmount << "G:" << greenAmount << "B:" << blueAmount; |
IonSystems | 12:814a8fdbb6f7 | 101 | string tmp = ss.str(); |
IonSystems | 12:814a8fdbb6f7 | 102 | printLCD(tmp.c_str()); |
IonSystems | 12:814a8fdbb6f7 | 103 | wait(0.1); |
IonSystems | 12:814a8fdbb6f7 | 104 | if(par_port->read_bit(8)){ |
IonSystems | 12:814a8fdbb6f7 | 105 | log("Dispense all button pressed"); |
IonSystems | 12:814a8fdbb6f7 | 106 | stateMachine = DISPENSE_ALL; |
IonSystems | 12:814a8fdbb6f7 | 107 | processState(stateMachine); |
IonSystems | 12:814a8fdbb6f7 | 108 | }else if(par_port->read_bit(9)){ |
IonSystems | 12:814a8fdbb6f7 | 109 | log("Clear stored numbers button pressed"); |
IonSystems | 12:814a8fdbb6f7 | 110 | writeFile(0,0,0); |
IonSystems | 12:814a8fdbb6f7 | 111 | redAmount = 0;greenAmount = 0;blueAmount = 0; |
IonSystems | 12:814a8fdbb6f7 | 112 | printLCD("Cleared values"); |
IonSystems | 12:814a8fdbb6f7 | 113 | |
IonSystems | 12:814a8fdbb6f7 | 114 | }else if(par_port->read_bit(10)){ |
IonSystems | 12:814a8fdbb6f7 | 115 | log("set all stored values to 10"); |
IonSystems | 13:0661d658d9d1 | 116 | writeFile(2,2,2); |
IonSystems | 13:0661d658d9d1 | 117 | redAmount = 2;greenAmount = 2;blueAmount = 2; |
IonSystems | 13:0661d658d9d1 | 118 | printLCD("set values to 2"); |
IonSystems | 12:814a8fdbb6f7 | 119 | |
IonSystems | 12:814a8fdbb6f7 | 120 | } |
IonSystems | 13:0661d658d9d1 | 121 | }else if(mode == 6){ //Maintenance Mode |
IonSystems | 13:0661d658d9d1 | 122 | if(par_port->read_bit(8)){ //Next maintenance function |
IonSystems | 13:0661d658d9d1 | 123 | maintenanceModePointer++; |
IonSystems | 13:0661d658d9d1 | 124 | if(maintenanceModePointer > 12) maintenanceModePointer = 0; |
IonSystems | 13:0661d658d9d1 | 125 | }else if(par_port->read_bit(9)){ //Previous maintenance function |
IonSystems | 13:0661d658d9d1 | 126 | maintenanceModePointer--; |
IonSystems | 13:0661d658d9d1 | 127 | if(maintenanceModePointer < 0) maintenanceModePointer = 12; |
IonSystems | 13:0661d658d9d1 | 128 | }else if(par_port->read_bit(10)){ //Process Instruction |
IonSystems | 13:0661d658d9d1 | 129 | switch(maintenanceModePointer){ |
IonSystems | 13:0661d658d9d1 | 130 | case 0://RB_LEFT: |
IonSystems | 13:0661d658d9d1 | 131 | maintain(RB_LEFT); |
IonSystems | 13:0661d658d9d1 | 132 | break; |
IonSystems | 13:0661d658d9d1 | 133 | |
IonSystems | 13:0661d658d9d1 | 134 | case 1://RB_CENTRE: |
IonSystems | 13:0661d658d9d1 | 135 | maintain(RB_CENTRE); |
IonSystems | 13:0661d658d9d1 | 136 | break; |
IonSystems | 13:0661d658d9d1 | 137 | |
IonSystems | 13:0661d658d9d1 | 138 | case 2://RB_RIGHT: |
IonSystems | 13:0661d658d9d1 | 139 | maintain(RB_RIGHT); |
IonSystems | 13:0661d658d9d1 | 140 | break; |
IonSystems | 13:0661d658d9d1 | 141 | |
IonSystems | 13:0661d658d9d1 | 142 | case 3://GO_UP: |
IonSystems | 13:0661d658d9d1 | 143 | maintain(GO_UP); |
IonSystems | 13:0661d658d9d1 | 144 | break; |
IonSystems | 13:0661d658d9d1 | 145 | |
IonSystems | 13:0661d658d9d1 | 146 | case 4://GO_CENTRE: |
IonSystems | 13:0661d658d9d1 | 147 | maintain(GO_CENTRE); |
IonSystems | 13:0661d658d9d1 | 148 | break; |
IonSystems | 13:0661d658d9d1 | 149 | |
IonSystems | 13:0661d658d9d1 | 150 | case 5://GO_DOWN: |
IonSystems | 13:0661d658d9d1 | 151 | maintain(GO_DOWN); |
IonSystems | 13:0661d658d9d1 | 152 | break; |
IonSystems | 13:0661d658d9d1 | 153 | |
IonSystems | 13:0661d658d9d1 | 154 | case 6://BR_LEFT: |
IonSystems | 13:0661d658d9d1 | 155 | maintain(BR_LEFT); |
IonSystems | 13:0661d658d9d1 | 156 | break; |
IonSystems | 13:0661d658d9d1 | 157 | |
IonSystems | 13:0661d658d9d1 | 158 | case 7://BR_RIGHT: |
IonSystems | 13:0661d658d9d1 | 159 | maintain(BR_RIGHT); |
IonSystems | 13:0661d658d9d1 | 160 | break; |
IonSystems | 13:0661d658d9d1 | 161 | |
IonSystems | 13:0661d658d9d1 | 162 | case 8://R_PUSH: |
IonSystems | 13:0661d658d9d1 | 163 | maintain(R_PUSH); |
IonSystems | 13:0661d658d9d1 | 164 | break; |
IonSystems | 13:0661d658d9d1 | 165 | |
IonSystems | 13:0661d658d9d1 | 166 | case 9://R_HOME: |
IonSystems | 13:0661d658d9d1 | 167 | maintain(R_HOME); |
IonSystems | 13:0661d658d9d1 | 168 | break; |
IonSystems | 13:0661d658d9d1 | 169 | |
IonSystems | 13:0661d658d9d1 | 170 | case 10://GB_LEFT: |
IonSystems | 13:0661d658d9d1 | 171 | maintain(GB_LEFT); |
IonSystems | 13:0661d658d9d1 | 172 | break; |
IonSystems | 13:0661d658d9d1 | 173 | |
IonSystems | 13:0661d658d9d1 | 174 | case 11://GB_CENTRE: |
IonSystems | 13:0661d658d9d1 | 175 | maintain(GB_CENTRE); |
IonSystems | 13:0661d658d9d1 | 176 | break; |
IonSystems | 13:0661d658d9d1 | 177 | |
IonSystems | 13:0661d658d9d1 | 178 | case 12://GB_RIGHT: |
IonSystems | 13:0661d658d9d1 | 179 | maintain(GB_RIGHT); |
IonSystems | 13:0661d658d9d1 | 180 | break; |
IonSystems | 13:0661d658d9d1 | 181 | |
IonSystems | 13:0661d658d9d1 | 182 | |
IonSystems | 13:0661d658d9d1 | 183 | |
IonSystems | 13:0661d658d9d1 | 184 | } |
IonSystems | 13:0661d658d9d1 | 185 | } |
IonSystems | 12:814a8fdbb6f7 | 186 | } |
IonSystems | 9:8d78eb55ad5e | 187 | } |
IonSystems | 12:814a8fdbb6f7 | 188 | |
IonSystems | 12:814a8fdbb6f7 | 189 | |
IonSystems | 6:e64796f1f384 | 190 | |
IonSystems | 6:e64796f1f384 | 191 | int main() { |
IonSystems | 12:814a8fdbb6f7 | 192 | |
IonSystems | 7:6ba00694f9cd | 193 | stateMachine = INITIALISING; |
IonSystems | 8:b7771391adc9 | 194 | processState(stateMachine); |
IonSystems | 12:814a8fdbb6f7 | 195 | readChipNumbers(); |
IonSystems | 12:814a8fdbb6f7 | 196 | printStoredChipValues(); |
IonSystems | 12:814a8fdbb6f7 | 197 | displayOperationMode(); |
IonSystems | 8:b7771391adc9 | 198 | while(true){ |
IonSystems | 10:8c0696b99692 | 199 | if(serialComms){ |
IonSystems | 10:8c0696b99692 | 200 | checkSerial(); |
IonSystems | 12:814a8fdbb6f7 | 201 | }if(testFunctions) checkSortDispenseButtons(); |
IonSystems | 12:814a8fdbb6f7 | 202 | |
IonSystems | 11:06f6e82b40a8 | 203 | wait(0.05); |
IonSystems | 12:814a8fdbb6f7 | 204 | |
IonSystems | 8:b7771391adc9 | 205 | } |
IonSystems | 10:8c0696b99692 | 206 | /*char c; |
IonSystems | 10:8c0696b99692 | 207 | if(c = readCharacter()){ |
IonSystems | 10:8c0696b99692 | 208 | processMessage(c); |
IonSystems | 10:8c0696b99692 | 209 | }*/ |
IonSystems | 11:06f6e82b40a8 | 210 | |
IonSystems | 11:06f6e82b40a8 | 211 | |
IonSystems | 10:8c0696b99692 | 212 | } |
IonSystems | 9:8d78eb55ad5e | 213 | |
IonSystems | 9:8d78eb55ad5e | 214 | |
IonSystems | 9:8d78eb55ad5e | 215 | |
IonSystems | 8:b7771391adc9 | 216 | |
IonSystems | 8:b7771391adc9 | 217 | |
IonSystems | 8:b7771391adc9 | 218 | |
IonSystems | 8:b7771391adc9 | 219 | |
IonSystems | 8:b7771391adc9 | 220 | |
IonSystems | 8:b7771391adc9 | 221 | |
IonSystems | 8:b7771391adc9 | 222 | //printStoredChipValues(); |
IonSystems | 6:e64796f1f384 | 223 | /* |
IonSystems | 0:8d54ffcf256e | 224 | while(1){ |
IonSystems | 3:97668a4cd69d | 225 | if(par_port->read_bit(11)) resetForNextCustomer(); |
IonSystems | 7:6ba00694f9cd | 226 | |
IonSystems | 3:97668a4cd69d | 227 | if(cardDetect & !cardDataAcquired) cardAcquisition(); |
IonSystems | 3:97668a4cd69d | 228 | setLEDs(); |
IonSystems | 3:97668a4cd69d | 229 | readButtons(); |
IonSystems | 3:97668a4cd69d | 230 | |
IonSystems | 4:f3be545b3826 | 231 | if(chipDetected & !colourDataAcquired){ |
IonSystems | 4:f3be545b3826 | 232 | Colour colour = readColourSensor(); |
IonSystems | 4:f3be545b3826 | 233 | sendColourSignal(colour); |
IonSystems | 4:f3be545b3826 | 234 | } |
IonSystems | 4:f3be545b3826 | 235 | |
IonSystems | 5:644bca33c1ca | 236 | writeFile(redQueue, greenQueue, blueQueue); |
IonSystems | 4:f3be545b3826 | 237 | readFile(); |
IonSystems | 4:f3be545b3826 | 238 | wait(2); |
IonSystems | 4:f3be545b3826 | 239 | |
IonSystems | 0:8d54ffcf256e | 240 | } |
IonSystems | 6:e64796f1f384 | 241 | */ |
IonSystems | 6:e64796f1f384 | 242 | |
IonSystems | 8:b7771391adc9 | 243 | /* setLEDcolour(255,0,0); |
IonSystems | 6:e64796f1f384 | 244 | wait(0.1); |
IonSystems | 6:e64796f1f384 | 245 | setLEDcolour(0,255,0); |
IonSystems | 6:e64796f1f384 | 246 | wait(0.1); |
IonSystems | 6:e64796f1f384 | 247 | setLEDcolour(0,0,255); |
IonSystems | 6:e64796f1f384 | 248 | wait(0.1); |
IonSystems | 6:e64796f1f384 | 249 | |
IonSystems | 8:b7771391adc9 | 250 | displayOperationMode(); */ |
IonSystems | 3:97668a4cd69d | 251 | |
IonSystems | 10:8c0696b99692 | 252 | |
IonSystems | 10:8c0696b99692 | 253 |