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:
- 10:8c0696b99692
- Parent:
- 7:6ba00694f9cd
- Child:
- 12:814a8fdbb6f7
--- a/serialCommunication.h Mon Nov 10 19:01:43 2014 +0000 +++ b/serialCommunication.h Wed Nov 12 20:14:45 2014 +0000 @@ -1,3 +1,34 @@ +#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; + } +} + +void sendCharacter(char ch){ + pc.putc(ch); +} + +void sendString(char* ch){ + pc.puts(ch); +} + + char readBit(int bitNumber){ int value = 0; switch(bitNumber){ @@ -21,21 +52,67 @@ break; } return valueToChar(value); +} +void processState(StateMachine stateMachine){ + switch(stateMachine){ + case INITIALISING: + setupLCD(); + printLCD("Welcome to the Chipin Sorter"); + wait(1); + break; + case READING_RGB_VALUES: + readChipNumbers(); + break; + case DISPENSE_RED: + dispense(RED); + break; + case DISPENSE_GREEN: + dispense(GREEN); + break; + case DISPENSE_BLUE: + dispense(BLUE); + break; + case SORT_RED: + sort(RED); + break; + case SORT_GREEN: + sort(GREEN); + break; + case SORT_BLUE: + sort(BLUE); + break; + case SORT_BIN: + sort(BIN); + break; + case SORT_RECYCLE: + sort(RECYCLE); + break; + /* case WAITING_FOR_ACK: + checkSerial(); + break; + case WAITING_FOR_CHAR: + log("About to check serial"); + printLCD("WAITING_FOR_CHAR"); + checkSerial(); + break;*/ + } +} - void processMessage(char c){ + +void processMessage(char c){ switch(c){ - case 'a' : //servo 1 clockwise - sendCharacter('a'); //bounce back for acknowledgement - printLCD("S1 clockwise"); + case 'a' : //Dispense red + dispense(RED); + sendCharacter('A'); break; - case 'b' : //servo 1 anticlockwise - sendCharacter('b'); //bounce back for acknowledgement - printLCD("S1 anticlockwise"); + case 'b' : //dispense green + dispense(GREEN); + sendCharacter('B'); break; - case 'c': - sendCharacter('c'); //bounce back for acknowledgement - printLCD("S1 stop"); + case 'c': //dispense blue + dispense(BLUE); + sendCharacter('C'); break; case 'C':// Read card command sendCharacter('R'); //Notify PC that command has been recieved @@ -66,7 +143,58 @@ //set the FPGA colour inputs to the colour code startSort = true; break; - case 't'://Test 180 servo + + case 'o': + + break; + + case 'p': + break; + + case 'q': + break; + + case 's': + break; + + case 't': + break; + + case 'u': + break; + + + + case 'v': + break; + + case 'w': + break; + case 'j': + sort(RED); + sendCharacter('J'); + break; + case 'k': + sort(GREEN); + sendCharacter('K'); + break; + case 'l': + sort(BLUE); + sendCharacter('L'); + break; + + + + + + + + + + + + + /*case 't'://Test 180 servo sendCharacter('T'); printLCD("Servo signal on"); servoTest = true; @@ -75,10 +203,11 @@ sendCharacter('W'); printLCD("Servo signal off"); servoTest = false; - break; + break;*/ + } + } - - else if (operationMode){ + /*else if (operationMode){ switch(c){ case 'n':// Start sorting sendCharacter('N'); //Confirm to PC that we are processing the instruction. @@ -97,16 +226,24 @@ break; } } +}*/ +bool checkSerial(){ + log("Checking serial"); + printLCD(""); + if(pc.readable()){ + log("serial port is readable, reading char and processing"); + char c = pc.getc(); + processMessage(c); + return true; + }else{ + log("serial port not readable, doing nothing"); + return false; + } } - -void sendCharacter(char ch){ - pc.putc(ch); +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; } -void sendString(char* ch){ - pc.puts(ch); -} -void readCharacter(){ - char c = pc.getc(); //wait for a serial character to be recieved. - processMessage(c); //Do something, based on the character recieved. -} \ No newline at end of file +