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

Revision:
6:e64796f1f384
Parent:
5:644bca33c1ca
Child:
7:6ba00694f9cd
--- a/main.cpp	Thu Nov 06 18:31:50 2014 +0000
+++ b/main.cpp	Fri Nov 07 20:16:23 2014 +0000
@@ -1,371 +1,35 @@
 #include "mbed.h"
-#include "MCP23017.h"
-#include "WattBob_TextLCD.h"
-
-/*
-#include "TCS3472_I2C.h"
-#include <string>
-#include "Colour.h"
-
-//Setup test LEDs
-DigitalOut testLED1(LED1);
-DigitalOut testLED2(LED2);
-DigitalOut testLED3(LED3);
-DigitalOut testLED4(LED4);
-
-//Setup Buttons
-DigitalIn testButton1(P1_8);
-
-//Boolean values to easily enable and disable certain features for individual testing
-bool colourSensor =  true;
-bool cardReader =    true;
-bool sorter =        true;
-bool dispensor =     true;
-*/
-/*
-There are two high level states representing the two main
-functionalities of the MBED: Maintenance mode and Operation mode.
-
-When operationMode is true, the MBED is in operation mode.
-When operationMode is false, the MBED is in maintenance mode.
-*/
-
-//bool operationMode = true; //the MBED starts in operation mode.
-
-#define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT)
-#define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT)
-
-//Creating all the global variables.
-MCP23017 *par_port;
-WattBob_TextLCD *lcd;
-Serial pc(USBTX, USBRX);
-//Stuff that saves stuff to the MBED.
-//LocalFileSystem local("local");
-
-
-/*
-void writeFile(int r, int g, int b){
-    FILE* File1 = fopen("/local/datafile.txt","w"); // open file
-    fputc(r, File1); // put char (data value) into file
-    fputc(g, File1); // put char (data value) into file
-    fputc(b, File1); // put char (data value) into file
-    fclose(File1); // close file 
-    }
-    
-int readFile(){
-    FILE* File2 = fopen ("/local/datafile.txt","r"); // open file for reading
-    int read_var = fgetc(File2); // read first data value
-    fclose(File2); // close file
-    return read_var;   
-    }
-int redQueue = readFile();
-int greenQueue = readFile();
-int blueQueue = readFile();
-
-//Setup colour sensor variables
-int rgb_readings[4];
-double rMax = 9244;
-double gMax = 3194;
-double bMax = 3590;
-TCS3472_I2C rgb_sensor(p28,p27); //p28 =sda, p27=scl
-//thresholds are in the RGB format.
-int redLT [3] = {79,23,41};
-int redUT [3]= {92,28,49};
-int greenLT [3] = {46,81,61};
-int greenUT [3]= {60,107,81};
-int blueLT [3]= {25,47,47};
-int blueUT [3]= {28,52,52};
-
+#include <sstream>
+#include "lcdCommands.h"
+#include "mbedStorage.h"
+#include "rgbLED.h"
+#include "mbedLCD.h"
 
 
-//setup Card Reader pins
-DigitalIn cardBit1(p21);
-DigitalIn cardBit2(p22);
-DigitalIn cardBit3(p23);
-DigitalIn cardBit4(p24);
-DigitalIn cardBit5(p25);
-DigitalIn cardBit6(p26);
-DigitalIn cardDetect(p29);
-bool cardDataAcquired = false;
-bool colourDataAcquired = false;
-bool chipDetected = false;
-
-//Setup pins to FPGA
-DigitalOut startSort(p5);       //A positive edge tells the FPGA to start sorting.
-  The command codes for the three sort select bits(ColourBit1 - ColourBit2)
-    000     Sort red chip
-    001     Sort green chip
-    010     Sort blue chip
-    011     Bin the chip
-    100     Recycle the chip
-    101     Nothing
-    110     Nothing
-    111     Nothing
-*/
-/*
-DigitalOut colourBit1(p6);      //The 3 bits below are select bits for the sorter.
-DigitalOut colourBit2(p7);
-DigitalOut colourBit3(p8);
-DigitalOut startDispense(p9);   //A positive edge tells the FPGA to start dispensing.
-
-    00      Dispense red chip
-    01      Dispense green chip
-    10      Dispense blue chip
-    11      Nothing
-*/
-/*
-DigitalOut DispenseBit1(p10);  //The 2 bits below are select bits for the dispenser.
-DigitalOut DispenseBit2(p11);
-
-DigitalOut servoTest(p21); //for basic servo test fro deadline day 31/10/14
-
-//Global card bits
-int cardValue1 = 0;
-int cardValue2 = 0;
-int cardValue3 = 0;
-int cardValue4 = 0;
-int cardValue5 = 0;
-int cardValue6 = 0;
-
-//Functions go here
-void sendCharacter(char ch){
-        pc.putc(ch);
-        }
-        
-        
-void setLEDs(){
-    testLED1 = cardDetect;
-    testLED4 = true;
-}        
-
-  A simple print function for use on the MBED LCD Screen.
- *  Will format a char array into two lines and display the char array on the LCD.
- */   
- /*   
-void printLCD(const char * text){
-        std::string txt_str = std::string(text); //Convert ther character array to a std::string, 
-                                                 //to use in the string manipulation functions.
-        lcd->reset();
-        if(txt_str.length() > 32){
-            int length = txt_str.length();
-            txt_str = txt_str.erase(32,length-32);
-            lcd->locate(0,0);   //Going to new line
-            text = txt_str.c_str(); 
-        }
-            if(txt_str.length() > 16){
-            string line1 = txt_str.substr(0,16);
-            string line2 = txt_str.substr(16,16);
-            lcd->locate(0,0);   //Going to new line
-            lcd->printf(line1.c_str());
-            lcd->locate(1,0);   //Going to new line
-            lcd->printf(line2.c_str());
-            lcd->locate(0,0);   //Going to new line
-            }
-      
-}
+Serial pc(USBTX, USBRX);
 
 
-void readButtons(){
-    chipDetected = par_port->read_bit(8); 
-    testLED1 = par_port->read_bit(8); 
-    
-    }
- Reads all 6 bits from the card IO pins and prints them to the MBED LCD.
- * Stores the values read to the global variables cardValue1 to cardValue6.
-*/
-/*
-void cardAcquisition(){
-        lcd->reset();
-        lcd->locate(0,0);   //Going to new line
-        if(cardBit1) lcd->printf("p1=1,");
-        else lcd->printf("p1=0,");
-        cardValue1 = cardBit1;
-        
-        if(cardBit2) lcd->printf("p2=1,");
-        else lcd->printf("p2=0,");
-        cardValue2 = cardBit2;
-        
-        if(cardBit3) lcd->printf("p3=1");
-        else lcd->printf("p3=0");
-        cardValue3 = cardBit3;
-        
-        lcd->locate(1,0);   //Going to new line
-        
-        if(cardBit4) lcd->printf("p4=1");
-        else lcd->printf("p4=0,");
-        cardValue4 = cardBit4;
-        
-        
-        if(cardBit5) lcd->printf("p5=1,");
-        else lcd->printf("p5=0,");
-        cardValue5 = cardBit5;
-        
-        if(cardBit6) lcd->printf("p6=1");
-        else lcd->printf("p6=0");
-        cardValue6 = cardBit6;
-        
-        cardDataAcquired = true;
-        
-    }
- 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;    
-            }
-       }
-  readBit(int bitNumber):
-    Takes a bit number between 1 and 6 inclusive.
-    Returns the value of the cardBit associated with the bit number as a char.
-*/
-/*
-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;
-            break;
-            case 5:
-                value = cardBit5;
-            break;
-            case 6:
-                value = cardBit6;
-            break;
-            }
-            return valueToChar(value);
-    }
-  processMessage(char c):
-    Runs when the MBED recieves a character from the PC.
-    Carries out tasks based on the char value.
-    An acknowledge signal is always send back to the PC so that it knows the MBED 
-    has recieved the command.
-    Has two case statements: one for maintenance mode and one for operation mode.
-*/
-/*   
-void processMessage(char c){
-     switch(c){
-            case 'a' : //servo 1 clockwise
-                sendCharacter('a'); //bounce back for acknowledgement
-                printLCD("S1 clockwise");
-            break;
-            case 'b' :  //servo 1 anticlockwise
-                sendCharacter('b');  //bounce back for acknowledgement
-                printLCD("S1 anticlockwise");
-            break;
-            case 'c':
-                sendCharacter('c'); //bounce back for acknowledgement
-                printLCD("S1 stop");
-            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;
-            case 'X':
-                sendCharacter('X'); //Notify the PC that this is an MBED
-                printLCD("Connected to PC");
-            break;
-            case 'd':   //Dispense a chip
-                sendCharacter('D');
-                printLCD("Dispensing chip");
-              
-                //read from color sensor
-                //convert readings to colour
-                //convert colour to 3-bit colour code
-                //set the FPGA colour inputs to the colour code
-                startSort = true; 
-            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;
-                }
-           }
-}
-  
+//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 cardDataAcquired   = false;
+bool colourDataAcquired = false;
+bool chipDetected       = false;
+bool operationMode      = true; //the MBED starts in operation mode.
 
 
-
-
-void resetForNextCustomer(){
-        colourDataAcquired = false;
-        cardDataAcquired = false;
-        printLCD("Ready for next customer");
-        }
-     */   
-int main() {
-    //Setting up all the global variables
-    par_port = new MCP23017(p9, p10, 0x40);
-    par_port->config(0x0F00, 0x0F00, 0x0F00); // configure MCP23017 chip on WattBob
-    BACK_LIGHT_ON(par_port);
-    lcd = new WattBob_TextLCD(par_port);
-    //rgb_sensor.enablePowerAndRGBC();
-    //rgb_sensor.setIntegrationTime(100);
-    //Writing initial text on LCD
-    lcd->printf("Welcome to the");
-    lcd->locate(1,0);   //Going to new line
-    lcd->printf("Chipin Sorter");
-  
-
-  
-    //printLCD("Welcome to the  Chipin Sorter");
-    wait(1);
-    lcd->reset();       //Clear LCD
-/*
+      
+int main() { 
+writeFile(2,6,22);
+readChipNumbers();
+setupLCD();
+printLCD("Welcome to the  Chipin Sorter");
+wait(1);
+printStoredChipValues();
+/*     
     while(1){
        if(par_port->read_bit(11)) resetForNextCustomer();
        // char c = pc.getc(); //wait for a serial character to be recieved.
@@ -384,7 +48,15 @@
         wait(2);
         
         }
-      
-    */    
+    */
+    
+    setLEDcolour(255,0,0);
+    wait(0.1);
+    setLEDcolour(0,255,0);
+     wait(0.1);
+    setLEDcolour(0,0,255);
+    wait(0.1);
+    
+    displayOperationMode();    
 
-    }
+    }
\ No newline at end of file