ControllerBox directs electromechanical equipment in restaurants to display information.

Dependencies:   TextLCD MbedJSONValue libMotiv picojson mbed-rtos mbed

Fork of Mbed_MotiVControllerBox by Tim Wöstemeier

main.cpp

Committer:
TimWoo
Date:
2014-11-15
Revision:
1:d54aed10ddf3
Parent:
0:2279181caaa1
Child:
2:ad4509a9d051

File content as of revision 1:d54aed10ddf3:

#include "mbed.h"
//the library "TextLCD.h" was slightly altered to work with the GDM2004D LCD
#include "TextLCD/TextLCD.h"
#include "Controller.h"
#include "EthernetInterface.h"
#include "SDFileSystem.h"
#include <string>

/*****************
    Global vars
******************/
//the object "lcd" is initialized to act as a TextLCD with 20x4 characters
TextLCD lcd(p26, p25, p24, p23, p22, p20, p19, TextLCD::LCD20x4);


//inputs
DigitalIn Up(p8);
DigitalIn Down(p14);

//outputs
DigitalOut led(p6);

//Comms
Serial pc(USBTX, USBRX); //tx, rx
Serial rfd(p9, p10); //tx, rx
EthernetInterface eth;

//Storage
//SD card
//CS P14
//MOSI P11
//CLK P13
//MISO P12
//SDFileSystem sdfs(p11,p12,p13,p14, "sdCard");


//Controller mainly directs the program. It directs data and also controls the lcd output.
Controller c(&lcd);

/**********************
    Declare functions
***********************/

void setup(); //Called once to setup
void runProgram(); //Program with loop

/****************
      MAIN
*****************/
int main()
{
    setup();

    //the LCD is cleared using function .cls()
    lcd.cls();

    pc.printf("Entering loop\r\n");

    //the endless loop keeps mbed in low power mode
    runProgram();
}

/*************************
  Implement  Function
*************************/

void setup()
{
    pc.printf("Setup Controller Box\r\n");
    c.setStatus(Controller::INIT);
    rfd.baud(9600);

    //Initialize Ethernet
    eth.init();
    eth.connect();
}

void runProgram()
{
    c.lcd->cls();
    c.lcd->locate(0,0);
    c.displayStatus();
//    c.printDesc(1);
    string ip = eth.getIPAddress();
    c.setStatus(Controller::READY);

    UDPSocket sock;
    sock.init();
    
    Endpoint nist;
    nist.set_address("utcnist.colorado.edu", 37);
    
    char out_buffer[] = "plop"; // Does not matter
    sock.sendTo(nist, out_buffer, sizeof(out_buffer));
    
    char in_buffer[4];
    int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer));
    
    unsigned int timeRes = ntohl( *((unsigned int*)in_buffer));
    pc.printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes);
    
    sock.close();


    pc.printf("%s", ip);

//    c.writeLine(3, ("IP: " + ipa));
    c.lcd->locate(0,3);
    c.lcd->clearLine();
    c.lcd->locate(0,3);
    c.lcd->printf("IP: %s", ip);

    //Reset flower
    rfd.putc(2);
    rfd.putc(0);
    rfd.putc(0);
    rfd.putc(';');
    wait_ms(2000);

    char received = 'a';

    string str = "";
    int counter = 0;
    int counterLast = -1;
    bool pushedUp = false;
    bool pushedDown = false;

    while(1) {

//        received = rfd.txGetLastChar();
//        c.displWriteLine(3, 'a');
//        pc.printf("%c", received);
//        received = 'b';
        if(!Up && !pushedUp) {
            pushedUp = true;
            //Last resort
//            c.setStatus(c.BUSY);
            if(counter >= 6)
                counter = 0;
            else
                counter++;
        } else if(!Down && !pushedDown) {
            pushedDown = true;
            //Last resort
            //  c.setStatus(c.READY);
            if(counter < 0)
                counter = 5;
            else
                counter--;
        } else if(Up)
            pushedUp = false;
            else if(Down)
            pushedDown = false;

        if(counter != counterLast) {
            switch(counter) {
                case 0: //Tafel vrij
                    rfd.putc(2);
                    rfd.putc(50);
                    rfd.putc(1);
                    rfd.putc(';');
                    str = "Tafel vrij";
                    break;

                case 1: //Tafel gereserveerd
                    rfd.putc(2);
                    rfd.putc(52);
                    rfd.putc(1);
                    rfd.putc(';');
                    str = "Tafel gereserveerd";
                    break;

                case 2: //Tafel in gebruik
                    rfd.putc(2);
                    rfd.putc(29);
                    rfd.putc(1);
                    rfd.putc(';');
                    wait_ms(500);
                    rfd.putc(2);
                    rfd.putc(51);
                    rfd.putc(1);
                    rfd.putc(';');
                    str = "Tafel in gebruik";
                    break;

                case 3: //Eten besteld 20
                    rfd.putc(2);
                    rfd.putc(54);
                    rfd.putc(50);
                    rfd.putc(';');
                    str = "Eten best. 50";
                    break;

                case 4: //Eten besteld 100
                    rfd.putc(2);
                    rfd.putc(54);
                    rfd.putc(100);
                    rfd.putc(';');
                    str = "Eten best. 100";
                    break;

                case 5: //Demo 1
                    rfd.putc(2);
                    rfd.putc(101);
                    rfd.putc(1);
                    rfd.putc(';');
                    str = "Demo 1";
                    break;

                default: //Default: lamp modus
                    rfd.putc(2);
                    rfd.putc(100);
                    rfd.putc(1);
                    rfd.putc(';');
                    break;
            }
            counterLast = counter;
        c.lcdWriteLine(2, str);
        }


        wait_ms(200);
        //__WFI();
    }
}


/*
EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    
    UDPSocket sock;
    sock.init();
    
    Endpoint nist;
    nist.set_address("utcnist.colorado.edu", 37);
    
    char out_buffer[] = "plop"; // Does not matter
    sock.sendTo(nist, out_buffer, sizeof(out_buffer));
    
    char in_buffer[4];
    int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer));
    
    unsigned int timeRes = ntohl( *((unsigned int*)in_buffer));
    printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes);
    
    sock.close();
    
    eth.disconnect();
    */