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

Committer:
IonSystems
Date:
Tue Nov 25 12:59:11 2014 +0000
Revision:
20:6de191ac7ff3
Parent:
19:78d4b78fa736
Child:
21:3d9556f5508a
Added wait functions for complete signals from FPGA.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IonSystems 10:8c0696b99692 1 #include "cardReader.h"
IonSystems 10:8c0696b99692 2
IonSystems 10:8c0696b99692 3
IonSystems 10:8c0696b99692 4
IonSystems 10:8c0696b99692 5 Serial pc(USBTX, USBRX);
IonSystems 10:8c0696b99692 6 /* valueToChar(int value):
IonSystems 10:8c0696b99692 7 Expects an int value what it 0 or 1.
IonSystems 10:8c0696b99692 8 Returns the int value as a char.
IonSystems 10:8c0696b99692 9 0 will return '0'
IonSystems 10:8c0696b99692 10 1 will return '1'
IonSystems 10:8c0696b99692 11 Used to read the bits in the card reader.
IonSystems 10:8c0696b99692 12 */
IonSystems 10:8c0696b99692 13 char valueToChar(int value){
IonSystems 10:8c0696b99692 14 if(value == 0){
IonSystems 10:8c0696b99692 15 return '0';
IonSystems 10:8c0696b99692 16 }else if(value == 1){
IonSystems 10:8c0696b99692 17 return '1';
IonSystems 10:8c0696b99692 18 }else{
IonSystems 10:8c0696b99692 19 return NULL;
IonSystems 10:8c0696b99692 20 }
IonSystems 10:8c0696b99692 21 }
IonSystems 10:8c0696b99692 22
IonSystems 10:8c0696b99692 23 void sendCharacter(char ch){
IonSystems 10:8c0696b99692 24 pc.putc(ch);
IonSystems 10:8c0696b99692 25 }
IonSystems 10:8c0696b99692 26
IonSystems 10:8c0696b99692 27 void sendString(char* ch){
IonSystems 10:8c0696b99692 28 pc.puts(ch);
IonSystems 10:8c0696b99692 29 }
IonSystems 10:8c0696b99692 30
IonSystems 10:8c0696b99692 31
IonSystems 6:e64796f1f384 32 char readBit(int bitNumber){
IonSystems 6:e64796f1f384 33 int value = 0;
IonSystems 6:e64796f1f384 34 switch(bitNumber){
IonSystems 6:e64796f1f384 35 case 1:
IonSystems 6:e64796f1f384 36 value = cardBit1;
IonSystems 6:e64796f1f384 37 break;
IonSystems 6:e64796f1f384 38 case 2:
IonSystems 6:e64796f1f384 39 value = cardBit2;
IonSystems 6:e64796f1f384 40 break;
IonSystems 6:e64796f1f384 41 case 3:
IonSystems 6:e64796f1f384 42 value = cardBit3;
IonSystems 6:e64796f1f384 43 break;
IonSystems 6:e64796f1f384 44 case 4:
IonSystems 6:e64796f1f384 45 value = cardBit4;
IonSystems 6:e64796f1f384 46 break;
IonSystems 6:e64796f1f384 47 case 5:
IonSystems 6:e64796f1f384 48 value = cardBit5;
IonSystems 6:e64796f1f384 49 break;
IonSystems 6:e64796f1f384 50 case 6:
IonSystems 6:e64796f1f384 51 value = cardBit6;
IonSystems 6:e64796f1f384 52 break;
IonSystems 6:e64796f1f384 53 }
IonSystems 6:e64796f1f384 54 return valueToChar(value);
IonSystems 10:8c0696b99692 55 }
IonSystems 6:e64796f1f384 56
IonSystems 10:8c0696b99692 57 void processMessage(char c){
IonSystems 6:e64796f1f384 58 switch(c){
IonSystems 10:8c0696b99692 59 case 'a' : //Dispense red
IonSystems 20:6de191ac7ff3 60 {
IonSystems 10:8c0696b99692 61 dispense(RED);
IonSystems 10:8c0696b99692 62 sendCharacter('A');
IonSystems 6:e64796f1f384 63 break;
IonSystems 20:6de191ac7ff3 64 }
IonSystems 20:6de191ac7ff3 65 {
IonSystems 10:8c0696b99692 66 case 'b' : //dispense green
IonSystems 10:8c0696b99692 67 dispense(GREEN);
IonSystems 10:8c0696b99692 68 sendCharacter('B');
IonSystems 6:e64796f1f384 69 break;
IonSystems 20:6de191ac7ff3 70 }
IonSystems 20:6de191ac7ff3 71 {
IonSystems 10:8c0696b99692 72 case 'c': //dispense blue
IonSystems 10:8c0696b99692 73 dispense(BLUE);
IonSystems 10:8c0696b99692 74 sendCharacter('C');
IonSystems 6:e64796f1f384 75 break;
IonSystems 20:6de191ac7ff3 76 }
IonSystems 20:6de191ac7ff3 77 {
IonSystems 6:e64796f1f384 78 case 'C':// Read card command
IonSystems 6:e64796f1f384 79 sendCharacter('R'); //Notify PC that command has been recieved
IonSystems 6:e64796f1f384 80 printLCD("Reading Card");
IonSystems 6:e64796f1f384 81 //read card here
IonSystems 6:e64796f1f384 82 char value = ' ';
IonSystems 6:e64796f1f384 83 for(int num = 1;num <= 6;num++){
IonSystems 6:e64796f1f384 84 value = readBit(num);
IonSystems 6:e64796f1f384 85 // sendCharacter('R');
IonSystems 6:e64796f1f384 86 sendCharacter(value);
IonSystems 6:e64796f1f384 87 printLCD("Bit read");
IonSystems 6:e64796f1f384 88 //wait(0.1);
IonSystems 6:e64796f1f384 89 }
IonSystems 6:e64796f1f384 90 sendCharacter('C'); //Tells PC that the card has been read
IonSystems 6:e64796f1f384 91 printLCD("Card read done");
IonSystems 6:e64796f1f384 92 break;
IonSystems 20:6de191ac7ff3 93 }
IonSystems 20:6de191ac7ff3 94 {
IonSystems 6:e64796f1f384 95 case 'X':
IonSystems 6:e64796f1f384 96 sendCharacter('X'); //Notify the PC that this is an MBED
IonSystems 6:e64796f1f384 97 printLCD("Connected to PC");
IonSystems 6:e64796f1f384 98 break;
IonSystems 20:6de191ac7ff3 99 }
IonSystems 20:6de191ac7ff3 100 {
IonSystems 6:e64796f1f384 101 case 'd': //Dispense a chip
IonSystems 6:e64796f1f384 102 sendCharacter('D');
IonSystems 6:e64796f1f384 103 printLCD("Dispensing chip");
IonSystems 6:e64796f1f384 104
IonSystems 6:e64796f1f384 105 //read from color sensor
IonSystems 6:e64796f1f384 106 //convert readings to colour
IonSystems 6:e64796f1f384 107 //convert colour to 3-bit colour code
IonSystems 6:e64796f1f384 108 //set the FPGA colour inputs to the colour code
IonSystems 6:e64796f1f384 109 startSort = true;
IonSystems 6:e64796f1f384 110 break;
IonSystems 20:6de191ac7ff3 111 }
IonSystems 20:6de191ac7ff3 112 {
IonSystems 10:8c0696b99692 113
IonSystems 12:814a8fdbb6f7 114 case 'o': //testServoredBlueLeft
IonSystems 12:814a8fdbb6f7 115 maintain(RB_LEFT);
IonSystems 12:814a8fdbb6f7 116 sendCharacter('O');
IonSystems 10:8c0696b99692 117 break;
IonSystems 20:6de191ac7ff3 118 }
IonSystems 20:6de191ac7ff3 119 {
IonSystems 10:8c0696b99692 120
IonSystems 10:8c0696b99692 121 case 'p':
IonSystems 12:814a8fdbb6f7 122 maintain(RB_CENTRE);
IonSystems 12:814a8fdbb6f7 123 sendCharacter('P');
IonSystems 10:8c0696b99692 124 break;
IonSystems 20:6de191ac7ff3 125 }
IonSystems 20:6de191ac7ff3 126 {
IonSystems 10:8c0696b99692 127
IonSystems 10:8c0696b99692 128 case 'q':
IonSystems 12:814a8fdbb6f7 129 maintain(RB_RIGHT);
IonSystems 12:814a8fdbb6f7 130 sendCharacter('O');
IonSystems 10:8c0696b99692 131 break;
IonSystems 20:6de191ac7ff3 132 }
IonSystems 20:6de191ac7ff3 133 {
IonSystems 10:8c0696b99692 134 case 's':
IonSystems 12:814a8fdbb6f7 135 maintain(GO_UP);
IonSystems 12:814a8fdbb6f7 136 sendCharacter('S');
IonSystems 10:8c0696b99692 137 break;
IonSystems 20:6de191ac7ff3 138 }
IonSystems 20:6de191ac7ff3 139 {
IonSystems 10:8c0696b99692 140 case 't':
IonSystems 12:814a8fdbb6f7 141 maintain(GO_CENTRE);
IonSystems 12:814a8fdbb6f7 142 sendCharacter('T');
IonSystems 10:8c0696b99692 143 break;
IonSystems 20:6de191ac7ff3 144 }
IonSystems 20:6de191ac7ff3 145 {
IonSystems 10:8c0696b99692 146 case 'u':
IonSystems 12:814a8fdbb6f7 147 maintain(GO_DOWN);
IonSystems 12:814a8fdbb6f7 148 sendCharacter('U');
IonSystems 10:8c0696b99692 149 break;
IonSystems 20:6de191ac7ff3 150 }
IonSystems 10:8c0696b99692 151
IonSystems 20:6de191ac7ff3 152 {
IonSystems 10:8c0696b99692 153 case 'v':
IonSystems 12:814a8fdbb6f7 154 maintain(BR_LEFT);
IonSystems 12:814a8fdbb6f7 155 sendCharacter('V');
IonSystems 10:8c0696b99692 156 break;
IonSystems 20:6de191ac7ff3 157 }
IonSystems 20:6de191ac7ff3 158 {
IonSystems 10:8c0696b99692 159 case 'w':
IonSystems 12:814a8fdbb6f7 160 maintain(BR_LEFT);
IonSystems 12:814a8fdbb6f7 161 sendCharacter('O');
IonSystems 10:8c0696b99692 162 break;
IonSystems 20:6de191ac7ff3 163 }
IonSystems 20:6de191ac7ff3 164 {
IonSystems 10:8c0696b99692 165 case 'j':
IonSystems 10:8c0696b99692 166 sort(RED);
IonSystems 10:8c0696b99692 167 sendCharacter('J');
IonSystems 10:8c0696b99692 168 break;
IonSystems 20:6de191ac7ff3 169 }
IonSystems 20:6de191ac7ff3 170 {
IonSystems 10:8c0696b99692 171 case 'k':
IonSystems 10:8c0696b99692 172 sort(GREEN);
IonSystems 10:8c0696b99692 173 sendCharacter('K');
IonSystems 10:8c0696b99692 174 break;
IonSystems 20:6de191ac7ff3 175 }
IonSystems 20:6de191ac7ff3 176 {
IonSystems 10:8c0696b99692 177 case 'l':
IonSystems 10:8c0696b99692 178 sort(BLUE);
IonSystems 10:8c0696b99692 179 sendCharacter('L');
IonSystems 10:8c0696b99692 180 break;
IonSystems 20:6de191ac7ff3 181 }
IonSystems 20:6de191ac7ff3 182 {
IonSystems 10:8c0696b99692 183
IonSystems 18:12e2c82a5a6c 184 case '0': //Dispense all
IonSystems 12:814a8fdbb6f7 185 dispenseAll();
IonSystems 18:12e2c82a5a6c 186 sendCharacter('1');
IonSystems 12:814a8fdbb6f7 187 break;
IonSystems 20:6de191ac7ff3 188 }
IonSystems 20:6de191ac7ff3 189 {
IonSystems 19:78d4b78fa736 190 case '2':
IonSystems 19:78d4b78fa736 191 maintain(GO_CENTRE);
IonSystems 19:78d4b78fa736 192 maintain(RB_CENTRE);
IonSystems 19:78d4b78fa736 193 sendCharacter('3');
IonSystems 19:78d4b78fa736 194 break;
IonSystems 20:6de191ac7ff3 195 }
IonSystems 20:6de191ac7ff3 196 {
IonSystems 20:6de191ac7ff3 197 case '4':
IonSystems 20:6de191ac7ff3 198 lift();
IonSystems 20:6de191ac7ff3 199 sendCharacter('5');
IonSystems 20:6de191ac7ff3 200 break;
IonSystems 20:6de191ac7ff3 201 }
IonSystems 20:6de191ac7ff3 202
IonSystems 10:8c0696b99692 203
IonSystems 10:8c0696b99692 204
IonSystems 10:8c0696b99692 205
IonSystems 10:8c0696b99692 206
IonSystems 10:8c0696b99692 207
IonSystems 10:8c0696b99692 208
IonSystems 10:8c0696b99692 209
IonSystems 10:8c0696b99692 210
IonSystems 10:8c0696b99692 211
IonSystems 10:8c0696b99692 212
IonSystems 10:8c0696b99692 213
IonSystems 10:8c0696b99692 214 /*case 't'://Test 180 servo
IonSystems 6:e64796f1f384 215 sendCharacter('T');
IonSystems 6:e64796f1f384 216 printLCD("Servo signal on");
IonSystems 6:e64796f1f384 217 servoTest = true;
IonSystems 6:e64796f1f384 218 break;
IonSystems 6:e64796f1f384 219 case 'w'://Test 180 servo
IonSystems 6:e64796f1f384 220 sendCharacter('W');
IonSystems 6:e64796f1f384 221 printLCD("Servo signal off");
IonSystems 6:e64796f1f384 222 servoTest = false;
IonSystems 10:8c0696b99692 223 break;*/
IonSystems 10:8c0696b99692 224 }
IonSystems 10:8c0696b99692 225 }
IonSystems 6:e64796f1f384 226
IonSystems 10:8c0696b99692 227 /*else if (operationMode){
IonSystems 6:e64796f1f384 228 switch(c){
IonSystems 6:e64796f1f384 229 case 'n':// Start sorting
IonSystems 6:e64796f1f384 230 sendCharacter('N'); //Confirm to PC that we are processing the instruction.
IonSystems 6:e64796f1f384 231 startSort = true; //Pulse the startSort control line to FPGA
IonSystems 6:e64796f1f384 232 wait(0.1);
IonSystems 6:e64796f1f384 233 startSort = false;
IonSystems 6:e64796f1f384 234 break;
IonSystems 6:e64796f1f384 235 case 'o': //Start dispensing
IonSystems 6:e64796f1f384 236 sendCharacter('O');
IonSystems 6:e64796f1f384 237 startDispense = true; //Pulse the startDispense control line to FPGA
IonSystems 6:e64796f1f384 238 wait(0.1);
IonSystems 6:e64796f1f384 239 startDispense = false;
IonSystems 6:e64796f1f384 240 break;
IonSystems 6:e64796f1f384 241 case 'p': //
IonSystems 6:e64796f1f384 242 sendCharacter('P');
IonSystems 6:e64796f1f384 243 break;
IonSystems 6:e64796f1f384 244 }
IonSystems 6:e64796f1f384 245 }
IonSystems 10:8c0696b99692 246 }*/
IonSystems 10:8c0696b99692 247 bool checkSerial(){
IonSystems 10:8c0696b99692 248 log("Checking serial");
IonSystems 10:8c0696b99692 249 printLCD("");
IonSystems 10:8c0696b99692 250 if(pc.readable()){
IonSystems 10:8c0696b99692 251 log("serial port is readable, reading char and processing");
IonSystems 10:8c0696b99692 252 char c = pc.getc();
IonSystems 10:8c0696b99692 253 processMessage(c);
IonSystems 10:8c0696b99692 254 return true;
IonSystems 10:8c0696b99692 255 }else{
IonSystems 10:8c0696b99692 256 log("serial port not readable, doing nothing");
IonSystems 10:8c0696b99692 257 return false;
IonSystems 10:8c0696b99692 258 }
IonSystems 6:e64796f1f384 259 }
IonSystems 10:8c0696b99692 260 bool readCharacter(){
IonSystems 10:8c0696b99692 261 char c = pc.getc(); //wait for a serial character to be recieved.
IonSystems 10:8c0696b99692 262 processMessage(c); //Do something, based on the character recieved.
IonSystems 10:8c0696b99692 263 return true;
IonSystems 6:e64796f1f384 264 }
IonSystems 15:0c5f20e15b6a 265 /* Set the PC's laguage to whatever our language is
IonSystems 15:0c5f20e15b6a 266 */
IonSystems 15:0c5f20e15b6a 267 void sendLanguageCharacter(){
IonSystems 15:0c5f20e15b6a 268 switch(currentLanguage){
IonSystems 15:0c5f20e15b6a 269 case ENGLISH:
IonSystems 15:0c5f20e15b6a 270 printLCD("Setting language to English");
IonSystems 15:0c5f20e15b6a 271 sendCharacter('Y');
IonSystems 15:0c5f20e15b6a 272 printLCD("Language set to English");
IonSystems 15:0c5f20e15b6a 273 break;
IonSystems 15:0c5f20e15b6a 274
IonSystems 15:0c5f20e15b6a 275 case FRENCH:
IonSystems 15:0c5f20e15b6a 276 printLCD("Setting language to French");
IonSystems 15:0c5f20e15b6a 277 sendCharacter('Z');
IonSystems 15:0c5f20e15b6a 278 printLCD("Language set to French");
IonSystems 15:0c5f20e15b6a 279 break;
IonSystems 15:0c5f20e15b6a 280
IonSystems 15:0c5f20e15b6a 281 case GERMAN:
IonSystems 15:0c5f20e15b6a 282 printLCD("Setting language to German");
IonSystems 15:0c5f20e15b6a 283 sendCharacter('!');
IonSystems 15:0c5f20e15b6a 284 printLCD("Language set to German");
IonSystems 15:0c5f20e15b6a 285 break;
IonSystems 15:0c5f20e15b6a 286 }
IonSystems 15:0c5f20e15b6a 287 }
IonSystems 6:e64796f1f384 288
IonSystems 10:8c0696b99692 289