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 22:32:26 2014 +0000
Revision:
22:8f11d1c178ab
Parent:
21:3d9556f5508a
Child:
23:f9e7e64784be
almost working sort, working dispense

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