![](/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: FPGAcomms.h
- Revision:
- 12:814a8fdbb6f7
- Parent:
- 11:06f6e82b40a8
- Child:
- 13:0661d658d9d1
diff -r 06f6e82b40a8 -r 814a8fdbb6f7 FPGAcomms.h --- a/FPGAcomms.h Thu Nov 13 11:59:39 2014 +0000 +++ b/FPGAcomms.h Mon Nov 17 21:31:42 2014 +0000 @@ -30,39 +30,105 @@ DigitalOut dispenseBit1(p19); //The 2 bits below are select bits for the dispenser.p10 DigitalOut dispenseBit2(p20); //p11 -DigitalIn dispenseComplete(p12); //FPGA sets to 1 once complete until another signal is recieved. +DigitalIn dispenseComplete(p11); //FPGA sets to 1 once complete until another signal is recieved. DigitalOut select(p15); //0 for control, 1 for maintenance. + +void setMaintenanceSelect(bool bit6, bool bit5, bool bit4, bool bit3, bool bit2, bool bit1){ + startSort = bit1; + colourBit1 = bit2; + colourBit2 = bit3; + colourBit3 = bit4; + startDispense = bit5; + dispenseBit1 = bit6; + } + +void setMaintenanceStart(bool value){ + dispenseBit2 = value; + } +void waitForMaintenanceComplete(){ + while(!sortComplete){ + wait(0.1); + } +} +void maintain(StateMachine maintainState){ + select = 1; //setting FPGA to maintenance mode. + setMaintenanceSelect(true,true,true,true,true,true); + switch(maintainState){ + case RB_LEFT: + setMaintenanceSelect(false,false,false,true,false,true); + break; + case RB_CENTRE: + setMaintenanceSelect(false,false,false,true,true,false); + break; + case RB_RIGHT: + setMaintenanceSelect(false,false,false,true,true,true); + break; + case GO_UP: + setMaintenanceSelect(false,false,true,false,false,false); + break; + case GO_CENTRE: + setMaintenanceSelect(false,false,true,false,false,true); + break; + case GO_DOWN: + setMaintenanceSelect(false,false,true,false,true,false); + break; + case BR_LEFT: + setMaintenanceSelect(false,false,true,false,true,true); + break; + case BR_RIGHT: + setMaintenanceSelect(false,false,true,true,false,false); + break; + case R_PUSH: + setMaintenanceSelect(false,false,true,true,false,true); + break; + } + setMaintenanceStart(true);//Bit 7 + waitForMaintenanceComplete(); + setMaintenanceStart(false); + } + + void dispense(Colour colour){ - + bool validDispense = false; select = 0; //Setting to operation mode just in case it has not been set. dispenseBit1 = true; dispenseBit2 = true; switch(colour){ case RED: - log("RED"); - log("Setting dispense bits to 00"); - dispenseBit1 = false; - dispenseBit2 = false; - + if(redAmount > 0 || operationMode == false){ + log("RED"); + log("Setting dispense bits to 00"); + dispenseBit1 = false; + dispenseBit2 = false; + redAmount--; + validDispense = true; + } break; case GREEN: - log("GREEN"); - log("Setting dispense bits to 10"); - dispenseBit1 = true; - dispenseBit2 = false; - + if(greenAmount > 0 || operationMode == false){ + log("GREEN"); + log("Setting dispense bits to 10"); + dispenseBit1 = true; + dispenseBit2 = false; + greenAmount--; + validDispense = true; + } break; case BLUE: + if(blueAmount > 0 || operationMode == false){ log("BLUE"); log("Setting dispense bits to 01"); dispenseBit1 = false; dispenseBit2 = true; - + blueAmount--; + validDispense = true; + } break; + writeFile(redAmount,greenAmount,blueAmount); } - + if(validDispense || operationMode == false){ log("Setting start dispense to true"); startDispense = true; //set the startDispense line high. @@ -74,7 +140,7 @@ printLCD("Waiting for dispense complete"); } - wait(0.5); + wait(0.6); log("Complete"); log("Setting start dispense to false"); startDispense = false; @@ -86,6 +152,7 @@ printLCD("DISPENSE COMPLETE"); + } } void sort(Colour colour){ @@ -100,7 +167,7 @@ colourBit1 = false; colourBit2 = false; colourBit3 = false; - + redAmount++; break; case GREEN: log("Sort GREEN"); @@ -108,7 +175,7 @@ colourBit1 = true; colourBit2 = false; colourBit3 = false; - + greenAmount++; break; case BLUE: log("Sort BLUE"); @@ -116,7 +183,7 @@ colourBit1 = false; colourBit2 = true; colourBit3 = false; - + blueAmount++; break; case BIN: log("Sort BIN"); @@ -148,7 +215,7 @@ log("Complete"); - wait(0.5); + wait(0.6); log("Setting start sort to false"); startSort = false; @@ -157,5 +224,27 @@ colourBit2 = true; //reset the dispense select to do nothing colourBit3 = true; printLCD("SORT COMPLETE"); + writeFile(redAmount,greenAmount,blueAmount); -} \ No newline at end of file +} + +void dispenseAll(){ + int red = redAmount; + int green = greenAmount; + int blue = blueAmount; + for(int r = 0;r < red;r++){ + dispense(RED); + wait(0.5); + }redAmount = 0; + + for(int g = 0;g < green;g++){ + dispense(GREEN); + wait(0.5); + }greenAmount = 0; + + for(int b = 0;b < blue;b++){ + dispense(BLUE); + wait(0.5); + }blueAmount = 0; + writeFile(redAmount,greenAmount,blueAmount); + } \ No newline at end of file