![](/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
main.cpp
- Committer:
- IonSystems
- Date:
- 2014-11-23
- Revision:
- 16:498359d078dc
- Parent:
- 15:0c5f20e15b6a
- Child:
- 17:6a0bb0ad5bb4
File content as of revision 16:498359d078dc:
bool operationMode = true; //the MBED starts in operation mode. #include "mbed.h" #include "Language.h" DigitalIn languageButton(p30); Language currentLanguage = ENGLISH; #include <sstream> #include <string> #include "lcdCommands.h" #include "mbedStorage.h" #include "rgbLED.h" #include "mbedLCD.h" #include "stateMachine.h" #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 bool colourSensor = true; bool cardReader = true; bool sorter = true; bool dispensor = true; bool testFunctions = true; bool serialComms = true; bool colourDataAcquired = false; bool chipDetected = false; int mode = 4; int maintenanceModePointer = 0; Colour lastColour = NONE; void checkSortDispenseButtons(){ if(par_port->read_bit(11)){ mode++; if(mode >= 7) mode = 0; } if(mode == 0){ //Dispense Mode printLCD("Dispense Mode"); if(par_port->read_bit(8)){ log("Red Button pressed"); stateMachine = DISPENSE_RED; processState(stateMachine); }else if(par_port->read_bit(9)){ log("Green Button pressed"); stateMachine = DISPENSE_GREEN; processState(stateMachine); }else if(par_port->read_bit(10)){ log("Blue Button pressed"); stateMachine = DISPENSE_BLUE; processState(stateMachine); } }else if(mode == 1){ //Sort Mode printLCD("Sort Mode"); if(par_port->read_bit(8)){ log("Red Button pressed"); stateMachine = SORT_RED; processState(stateMachine); }else if(par_port->read_bit(9)){ log("Green Button pressed"); stateMachine = SORT_GREEN; processState(stateMachine); }else if(par_port->read_bit(10)){ log("Blue Button pressed"); stateMachine = SORT_BLUE; processState(stateMachine); } }else if(mode == 2){ //Bin/Recycle Mode printLCD("Bin/Recycle Mode"); if(par_port->read_bit(8)){ log("Bin button pressed"); stateMachine = SORT_BIN; processState(stateMachine); }else if(par_port->read_bit(9)){ log("Recycle Button"); stateMachine = SORT_RECYCLE; processState(stateMachine); } }else if(mode == 3){ printLCD("Colour Sensor Mode"); //Colour Sensing Mode if(colourSensor){ Colour colour = readColourSensor(); /* Tries to prevent a colour mis-read. When a chip is read as BIN or 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. */ if(lastColour == NONE || lastColour == BIN){ return; } lastColour = colour; if(sorter) sort(colour); //Put (sorter && colour != BIN) in if statement. } //if }else if(mode == 4){ //Card Reader Mode if(cardReader && cardDetect){ cardAcquisition(); } }else if(mode == 5){ //Storage Mode stringstream ss; ss << "Stored Values " << "R:" << redAmount << "G:" << greenAmount << "B:" << blueAmount; string tmp = ss.str(); printLCD(tmp.c_str()); wait(0.1); if(par_port->read_bit(8)){ log("Dispense all button pressed"); stateMachine = DISPENSE_ALL; processState(stateMachine); }else if(par_port->read_bit(9)){ 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)){ log("Increment all stored values by 2."); redAmount += 2;greenAmount += 2;blueAmount += 2; writeFile(redAmount,greenAmount,blueAmount); printLCD("set values to 2"); } }else if(mode == 6){ //Maintenance Mode printLCD("Maintenace 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){ case 0://RB_LEFT: printLCD("Moving Red/Blue Slider left."); maintain(RB_LEFT); break; case 1://RB_CENTRE: printLCD("Centering Red/Blue Slider."); maintain(RB_CENTRE); break; case 2://RB_RIGHT: printLCD("Moving Red/Blue Slider right."); maintain(RB_RIGHT); break; case 3://GO_UP: printLCD("Moving Green Slider up."); maintain(GO_UP); break; case 4://GO_CENTRE: printLCD("Centering Green Slider."); maintain(GO_CENTRE); break; case 5://GO_DOWN: printLCD("Moving Green Slider down."); maintain(GO_DOWN); break; case 6://BR_LEFT: printLCD("Moving Bin Slider left."); maintain(BR_LEFT); break; case 7://BR_RIGHT: printLCD("Moving Bin Slider right."); maintain(BR_RIGHT); break; case 8://R_PUSH: printLCD("Pushing red dispensor"); maintain(R_PUSH); break; case 9://R_HOME: printLCD("Homing red dispensor"); maintain(R_HOME); break; case 10://GB_LEFT: printLCD("Pushing green dispensor"); maintain(GB_LEFT); break; case 11://GB_CENTRE: printLCD("Centre green/blue dispensor"); maintain(GB_CENTRE); break; case 12://GB_RIGHT: printLCD("Pushing blue dispensor"); maintain(GB_RIGHT ); break; } printLCD("Ready for next command"); wait(1); } } } void changeLanguage(){ currentLanguage = nextLanguage(currentLanguage); //tell PC to change language sendLanguageCharacter(); //Tell PC to change language updateLCDLanguage(); printLCD("setting language to english"); wait(0.5); } int main() { stateMachine = INITIALISING; processState(stateMachine); readChipNumbers(); printStoredChipValues(); //displayOperationMode(); while(true){ if(serialComms){ checkSerial(); }if(testFunctions) checkSortDispenseButtons(); if(languageButton) changeLanguage(); wait(0.05); } /*char c; if(c = readCharacter()){ processMessage(c); }*/ } //printStoredChipValues(); /* while(1){ if(par_port->read_bit(11)) resetForNextCustomer(); if(cardDetect & !cardDataAcquired) cardAcquisition(); setLEDs(); readButtons(); if(chipDetected & !colourDataAcquired){ Colour colour = readColourSensor(); sendColourSignal(colour); } writeFile(redQueue, greenQueue, blueQueue); readFile(); wait(2); } */ /* setLEDcolour(255,0,0); wait(0.1); setLEDcolour(0,255,0); wait(0.1); setLEDcolour(0,0,255); wait(0.1); displayOperationMode(); */