ControllerBox directs electromechanical equipment in restaurants to display information.

Dependencies:   EthernetInterface HTTPClient_KVplus MbedJSONValue SDFileSystem TextLCD mbed-rtos mbed picojson

Revision:
1:d54aed10ddf3
Parent:
0:2279181caaa1
Child:
2:ad4509a9d051
--- a/main.cpp	Sat Oct 25 00:11:12 2014 +0000
+++ b/main.cpp	Sat Nov 15 13:04:09 2014 +0000
@@ -2,9 +2,13 @@
 //the library "TextLCD.h" was slightly altered to work with the GDM2004D LCD
 #include "TextLCD/TextLCD.h"
 #include "Controller.h"
-#include "MODSERIAL.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);
 
@@ -18,27 +22,37 @@
 
 //Comms
 Serial pc(USBTX, USBRX); //tx, rx
-MODSERIAL rfd(p9, p10); //tx, rx
+Serial rfd(p9, p10); //tx, rx
+EthernetInterface eth;
 
-//
-string displayLines[26] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
+//Storage
+//SD card
+//CS P14
+//MOSI P11
+//CLK P13
+//MISO P12
+//SDFileSystem sdfs(p11,p12,p13,p14, "sdCard");
 
-//Main functionality in here
+
+//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();
-    //a "\n" in a text string causes a line feed
-    lcd.printf("HELLO WORLDZ\n");
-    //if the end of a line is reached, the text is written to the next line automatically
-    //lcd.printf("Testbed for mbed\nLCD example software with altered library");
 
     pc.printf("Entering loop\r\n");
 
@@ -46,55 +60,192 @@
     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.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));
     
-    if(!rfd.writeable())
-    {
-        c._lcd->locate(0,1);
-        c._lcd->clearLine();
-        c._lcd->printf("rfd not writeable");
-    } else {
-        c._lcd->locate(0,1);
-        c._lcd->clearLine();
-        c._lcd->printf("rfd writeable");
-        if(!rfd.txBufferSane())
-        {
-            c._lcd->locate(0,3);
-            c._lcd->printf("buffer not sane");   
-        }
-    }
+    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) {
-//        rfd.printf("aaiPoesjes"); //TEST broadcast text
-        received = rfd.txGetLastChar();
-        c.displWriteLine(3, 'a');
-        pc.printf("%c", received);
-        received = 'b';
-        if(!Up)
-        {
-            c.setStatus(c.BUSY);
-            wait(1);
-            rfd.printf("%s","161;");
-            wait_ms(100);
-            rfd.printf("%s","308;");
-        } else {
-            c.setStatus(c.READY);
+
+//        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(1000);
+
+
+        wait_ms(200);
         //__WFI();
     }
-}
\ No newline at end of file
+}
+
+
+/*
+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();
+    */
\ No newline at end of file