![](/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
Diff: serialCommunication.h
- Revision:
- 23:f9e7e64784be
- Parent:
- 22:8f11d1c178ab
- Child:
- 24:8868101d01d0
diff -r 8f11d1c178ab -r f9e7e64784be serialCommunication.h --- a/serialCommunication.h Tue Nov 25 22:32:26 2014 +0000 +++ b/serialCommunication.h Sat Nov 29 16:32:57 2014 +0000 @@ -1,300 +1,223 @@ #include "cardReader.h" - - -Serial pc(USBTX, USBRX); -/* valueToChar(int value): - Expects an int value what it 0 or 1. - Returns the int value as a char. - 0 will return '0' - 1 will return '1' - Used to read the bits in the card reader. -*/ -char valueToChar(int value){ - if(value == 0){ - return '0'; - }else if(value == 1){ - return '1'; - }else{ - return NULL; - } +char bitToChar(bool bit) +{ + if(bit == 0) { + return '0'; + } else if(bit == 1) { + return '1'; + } else { + return NULL; + } } -void sendCharacter(char ch){ - pc.putc(ch); +void sendCharacter(char ch) +{ + pc.putc(ch); } -void sendCardValues(){ - pc.putc('R');//start bit - //data - pc.putc(cardValues[0]); - pc.putc(cardValues[1]); - pc.putc(cardValues[2]); - pc.putc(cardValues[3]); - pc.putc(cardValues[4]); - pc.putc(cardValues[5]); - pc.putc('C');//stop bit - } -void sendString(char* ch){ - pc.puts(ch); +void sendString(char* ch) +{ + pc.puts(ch); } - -char readBit(int bitNumber){ - int value = 0; - switch(bitNumber){ - case 1: - value = cardBit1; - break; - case 2: - value = cardBit2; - break; - case 3: - value = cardBit3; - break; - case 4: - value = cardBit4; +void processMessage(char c) +{ + switch(c) { + case 'a' : { //Dispense red + dispense(RED); + sendCharacter('A'); break; - case 5: - value = cardBit5; - break; - case 6: - value = cardBit6; - break; - } - return valueToChar(value); -} - -void processMessage(char c){ - switch(c){ - case 'a' : //Dispense red - { - dispense(RED); - sendCharacter('A'); - break; - } - { + } + { case 'b' : //dispense green dispense(GREEN); sendCharacter('B'); - break; + break; } { case 'c': //dispense blue dispense(BLUE); sendCharacter('C'); - break; + break; } { case 'C':// Read card command - sendCharacter('R'); //Notify PC that command has been recieved - printLCD("Reading Card"); - //read card here - char value = ' '; - for(int num = 1;num <= 6;num++){ - value = readBit(num); - // sendCharacter('R'); - sendCharacter(value); - printLCD("Bit read"); - //wait(0.1); - } - sendCharacter('C'); //Tells PC that the card has been read - printLCD("Card read done"); - break; + printLCD("Reading Card"); + break; } { case 'X': sendCharacter('X'); //Notify the PC that this is an MBED printLCD("Connected to PC"); - break; + break; } { case 'd': //Sort Bin sort(BIN); sendCharacter('D'); - break; + break; } { - + case 'o': //testServoredBlueLeft maintain(RB_LEFT); sendCharacter('O'); - break; + break; } { - + case 'p': maintain(RB_CENTRE); sendCharacter('P'); - break; + break; } { - + case 'q': maintain(RB_RIGHT); sendCharacter('O'); - break; + break; } { case 's': maintain(GO_UP); sendCharacter('S'); - break; + break; } { case 't': maintain(GO_CENTRE); sendCharacter('T'); - break; + break; } { case 'u': maintain(GO_DOWN); sendCharacter('U'); - break; + break; } - + { case 'v': maintain(BR_LEFT); sendCharacter('V'); - break; + break; } { case 'w': maintain(BR_RIGHT); sendCharacter('O'); - break; + break; } { case 'j': sort(RED); sendCharacter('J'); - break; + break; } { case 'k': sort(GREEN); sendCharacter('K'); - break; + break; } { case 'l': sort(BLUE); sendCharacter('L'); - break; + break; } { - + case '0': //Dispense all dispenseAll(); sendCharacter('1'); - break; + break; } { case '2': maintain(GO_CENTRE); maintain(RB_CENTRE); sendCharacter('3'); - break; + break; } { case '4': - lift(); - sendCharacter('5'); - break; + lift(); + sendCharacter('5'); + break; } { case 'e': - recycle(); - sendCharacter('E'); - break; + recycle(); + sendCharacter('E'); + break; } - - - - - - - - - - - - - /*case 't'://Test 180 servo - sendCharacter('T'); - printLCD("Servo signal on"); - servoTest = true; - break; - case 'w'://Test 180 servo - sendCharacter('W'); - printLCD("Servo signal off"); - servoTest = false; - break;*/ + } +} + +/*else if (operationMode){ + switch(c){ + case 'n':// Start sorting + sendCharacter('N'); //Confirm to PC that we are processing the instruction. + startSort = true; //Pulse the startSort control line to FPGA + wait(0.1); + startSort = false; + break; + case 'o': //Start dispensing + sendCharacter('O'); + startDispense = true; //Pulse the startDispense control line to FPGA + wait(0.1); + startDispense = false; + break; + case 'p': // + sendCharacter('P'); + break; } - } - - /*else if (operationMode){ - switch(c){ - case 'n':// Start sorting - sendCharacter('N'); //Confirm to PC that we are processing the instruction. - startSort = true; //Pulse the startSort control line to FPGA - wait(0.1); - startSort = false; - break; - case 'o': //Start dispensing - sendCharacter('O'); - startDispense = true; //Pulse the startDispense control line to FPGA - wait(0.1); - startDispense = false; - break; - case 'p': // - sendCharacter('P'); - break; - } - } + } }*/ -bool checkSerial(){ - log("Checking serial"); +bool checkSerial() +{ printLCD(""); - if(pc.readable()){ - log("serial port is readable, reading char and processing"); + if(pc.readable()) { char c = pc.getc(); processMessage(c); return true; - }else{ - log("serial port not readable, doing nothing"); + } else { return false; } } -bool readCharacter(){ +bool readCharacter() +{ char c = pc.getc(); //wait for a serial character to be recieved. processMessage(c); //Do something, based on the character recieved. return true; } + /* Set the PC's laguage to whatever our language is */ -void sendLanguageCharacter(){ - switch(currentLanguage){ +void sendLanguageCharacter() +{ + switch(currentLanguage) { case ENGLISH: - printLCD("Setting language to English"); - sendCharacter('Y'); - printLCD("Language set to English"); - break; - + printLCD("Setting language to English"); + sendCharacter('Y'); + printLCD("Language set to English"); + break; + case FRENCH: - printLCD("Setting language to French"); - sendCharacter('Z'); - printLCD("Language set to French"); - break; - + printLCD("Setting language to French"); + sendCharacter('Z'); + printLCD("Language set to French"); + break; + case GERMAN: - printLCD("Setting language to German"); - sendCharacter('!'); - printLCD("Language set to German"); - break; + printLCD("Setting language to German"); + sendCharacter('!'); + printLCD("Language set to German"); + break; } - } +}