![](/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: main.cpp
- Revision:
- 18:12e2c82a5a6c
- Parent:
- 17:6a0bb0ad5bb4
- Child:
- 19:78d4b78fa736
diff -r 6a0bb0ad5bb4 -r 12e2c82a5a6c main.cpp --- a/main.cpp Mon Nov 24 14:00:46 2014 +0000 +++ b/main.cpp Mon Nov 24 16:09:52 2014 +0000 @@ -5,8 +5,6 @@ Language currentLanguage = ENGLISH; #include <sstream> #include <string> - - #include "mbedStorage.h" #include "rgbLED.h" #include "mbedLCD.h" @@ -14,14 +12,10 @@ #include "FPGAcomms.h" #ifndef serialComm #define serialComm - #include "serialCommunication.h" #endif #include "testFunctions.h" - - - StateMachine stateMachine; //Boolean values to easily enable and disable certain features for individual testing @@ -32,18 +26,22 @@ bool testFunctions = true; bool serialComms = true; -bool colourDataAcquired = false; -bool chipDetected = false; +int mode = 3; +int maintenanceModeSelect = 0; - -int mode = 3; -int maintenanceModePointer = 0; Colour lastColour = NONE; Colour colourStore = NONE; +bool buttonPressed(int buttonNumber){ + if(buttonNumber > -1 && buttonNumber < 4){ + return par_port->read_bit(buttonNumber + 8); + } + return false; +} + void checkSortDispenseButtons(){ - if(par_port->read_bit(11)){ + if(buttonPressed(3)){ mode++; if(mode >= 7) mode = 0; } @@ -51,45 +49,37 @@ if(mode == 0){ //Dispense Mode printLCD("Dispense Mode"); - if(par_port->read_bit(8)){ + if(buttonPressed(0)){ log("Red Button pressed"); - stateMachine = DISPENSE_RED; - processState(stateMachine); - }else if(par_port->read_bit(9)){ + dispense(RED); + }else if(buttonPressed(1)){ log("Green Button pressed"); - stateMachine = DISPENSE_GREEN; - processState(stateMachine); - }else if(par_port->read_bit(10)){ + dispense(GREEN); + }else if(buttonPressed(2)){ log("Blue Button pressed"); - stateMachine = DISPENSE_BLUE; - processState(stateMachine); + dispense(BLUE); } }else if(mode == 1){ //Sort Mode printLCD("Sort Mode"); - if(par_port->read_bit(8)){ + if(buttonPressed(0)){ log("Red Button pressed"); - stateMachine = SORT_RED; - processState(stateMachine); - }else if(par_port->read_bit(9)){ + sort(RED); + }else if(buttonPressed(1)){ log("Green Button pressed"); - stateMachine = SORT_GREEN; - processState(stateMachine); - }else if(par_port->read_bit(10)){ + sort(GREEN); + }else if(buttonPressed(2)){ log("Blue Button pressed"); - stateMachine = SORT_BLUE; - processState(stateMachine); + sort(BLUE); } }else if(mode == 2){ //Bin/Recycle Mode printLCD("Bin/Recycle Mode"); - if(par_port->read_bit(8)){ + if(buttonPressed(0)){ log("Bin button pressed"); - stateMachine = SORT_BIN; - processState(stateMachine); - }else if(par_port->read_bit(9)){ + sort(BIN); + }else if(buttonPressed(1)){ log("Recycle Button"); - stateMachine = SORT_RECYCLE; - processState(stateMachine); + sort(RECYCLE); } }else if(mode == 3){ printLCD("Colour Sensor Mode"); //Colour Sensing Mode @@ -99,7 +89,7 @@ Colour colour = readColourSensor(); lastColour = colourStore; colourStore = colour; - /* Tries to prevent a colour mis-read. When a chip is read as BIN or NONE, + /* Tries to prevent a colour mis-read. When a chip is read as NONE, the chip may be falling down the tube as the reading was taken. The chip may also be out of position on the slider. Ignoring the next reading is trying to prevent a mis-read being valid. @@ -124,17 +114,16 @@ string tmp = ss.str(); printLCD(tmp.c_str()); wait(0.1); - if(par_port->read_bit(8)){ + if(buttonPressed(0)){ log("Dispense all button pressed"); - stateMachine = DISPENSE_ALL; - processState(stateMachine); - }else if(par_port->read_bit(9)){ + dispenseAll(); + }else if(buttonPressed(1)){ log("Clear stored numbers button pressed"); writeFile(0,0,0); redAmount = 0;greenAmount = 0;blueAmount = 0; printLCD("Cleared values"); - }else if(par_port->read_bit(10)){ + }else if(buttonPressed(2)){ log("Increment all stored values by 2."); redAmount += 2;greenAmount += 2;blueAmount += 2; writeFile(redAmount,greenAmount,blueAmount); @@ -143,17 +132,20 @@ } }else if(mode == 6){ - printLCD("Main. Mode: " + maintenanceModeSelect); + stringstream ss; + ss << maintenanceModeSelect; + string mainModeSelect = ss.str(); + printLCD("Main. Mode: " + mainModeSelect); wait(0.1); //Maintenance Mode //printLCD("Maintenance Mode"); - if(par_port->read_bit(8)){ //Next maintenance function - maintenanceModePointer++; - if(maintenanceModePointer > 12) maintenanceModePointer = 0; - }else if(par_port->read_bit(9)){ //Previous maintenance function - maintenanceModePointer--; - if(maintenanceModePointer < 0) maintenanceModePointer = 12; - }else if(par_port->read_bit(10)){ //Process Instruction - switch(maintenanceModePointer){ + if(buttonPressed(0)){ //Next maintenance function + maintenanceModeSelect++; + if(maintenanceModeSelect > 12) maintenanceModeSelect = 0; + }else if(buttonPressed(1)){ //Previous maintenance function + maintenanceModeSelect--; + if(maintenanceModeSelect < 0) maintenanceModeSelect = 12; + }else if(buttonPressed(2)){ //Process Instruction + switch(maintenanceModeSelect){ case 0://RB_LEFT: printLCD("Moving Red/Blue Slider left."); maintain(RB_LEFT); @@ -230,15 +222,16 @@ void changeLanguage(){ currentLanguage = nextLanguage(currentLanguage); //Change the language on teh MBED. sendLanguageCharacter(); //Tell PC to change language - ////Change Language //Tell Arduino to change language. + //Change Language //Tell Arduino to change language. printLCD("setting language to english"); wait(1); - } +} int main() { - stateMachine = INITIALISING; - processState(stateMachine); + setupLCD(); + printLCD("Welcome to the Chipin Sorter"); + wait(1); readChipNumbers(); printStoredChipValues(); //displayOperationMode(); @@ -246,20 +239,9 @@ if(serialComms){ checkSerial(); }if(testFunctions) checkSortDispenseButtons(); - //if(languageButton) changeLanguage(); - - - - - wait(0.05); - - } - /*char c; - if(c = readCharacter()){ - processMessage(c); - }*/ - - + if(languageButton) changeLanguage(); + wait(0.05); //Slowi8ng things down a little bit. + } }