Mbed Clock application using an NTP connection to get internet time and a terminal interface to send commands

Dependencies:   4DGL-uLCD-SE EthernetInterface NTPClient mbed-rtos mbed SDFileSystem wavfile

main.cpp

Committer:
dudanian
Date:
2014-12-08
Revision:
1:c47a2f0816bb
Parent:
0:4e6ae21cbd31
Child:
2:c939d0501184

File content as of revision 1:c47a2f0816bb:

#include "mbed.h"
#include "rtos.h"
#include "EthernetInterface.h"
#include "uLCD_4DGL.h"
#include "SDFileSystem.h"
#include "Clock.h"
#include "Recorder.h"
#include <string>

#define SERVER_IP "143.215.120.157"
#define SERVER_PORT 13000
#define FILE_NAME "/sd/test-rec.wav"
#define BUF_SIZE 1024

uLCD_4DGL uLCD(p28,p27,p30);
Serial pc(USBTX, USBRX);
Serial easyVR(p13, p14);
SDFileSystem sdc(p5, p6, p7, p8, "sd");
EthernetInterface eth;
Clock clk;

DigitalOut sleepLED(LED4);
Mutex cMutex;
Mutex sMutex;
char buf[BUF_SIZE];


void execute(char *command) {
    char buffer[12];
    int hour, minute, period, zone;
    sscanf(command, "%s %d %d %d %d", buffer, &hour, &minute, &period, &zone);
    string operation(buffer);
    
    if (operation == "setTime") {
        cMutex.lock();
        clk.setTime(hour, minute, period);
        cMutex.unlock();
    } else if (operation == "setTimezone") {
        cMutex.lock();
        clk.setTimezone(zone);
        cMutex.unlock();
    } else if (operation == "sync") {
        cMutex.lock();            
        if (clk.syncTime() != 0) {
            sMutex.lock();
            pc.printf("Error syncing time\n\r");
            sMutex.unlock();
        }
        cMutex.unlock();
    } else {
        sMutex.lock();
        pc.printf("Not a valid command\n\r");
        sMutex.unlock();
    }
}


/**
 * Thread which updates the clock on the lcd while the main thread is waiting for 
 * or executing a command.
 */
void lcdUpdateThread(void const *args) {
    time_t time;
    struct tm *timeinfo;
    char buffer[20];
    
    // set initial time
    cMutex.lock();
    clk.syncTime();
    clk.setTimezone(-5);
    cMutex.unlock();
    
    // set lcd format
    sMutex.lock();
    uLCD.text_width(2);
    uLCD.text_height(2);
    uLCD.color(BLUE);
    sMutex.unlock();
    
    while (1) {
        cMutex.lock();
        time = clk.getTime();
        cMutex.unlock();
        
        timeinfo = localtime(&time);
        strftime(buffer, 20, "%I:%M:%S        %p", timeinfo);
        
        sMutex.lock();
        uLCD.locate(0,3);
        uLCD.printf("%s", buffer);
        sMutex.unlock();
        Thread::wait(0.25);
    }
}

/**
 * main function which loops getting a command, parsing that command, and executing
 * that command. It also starts a thread which updates the clock separate from the
 * command execution.
 */
int main() {
    pc.printf("Starting MbedClock\n\r");

    eth.init();
    pc.printf("Ititialized Ethernet\n\r");
    
    eth.connect();
    pc.printf("Connected using DHCP\n\r");
    wait(5);
    pc.printf("Using IP: %s\n\r", eth.getIPAddress());
    
    easyVR.putc('b');
    easyVR.getc();
    pc.printf("Initialized EasyVR\n\r");
      
    Thread updateThread(lcdUpdateThread);
    //sMutex.lock();
    pc.printf("Started LCDThread\n\n\r");
    //sMutex.unlock();
    
    while(1) {
        // set EasyVR to 3 claps
        sMutex.lock();
        easyVR.putc('s');
        easyVR.putc('A' + 4);
        sMutex.unlock();
        wait(0.2);
        
        // Clear buffer and wait for awake
        sMutex.lock();
        while (easyVR.readable())
            easyVR.getc();
        sMutex.unlock();
        sleepLED = 1;
        while(!easyVR.readable()) {
            wait(0.2);
        }
        sleepLED = 0;
        rec(FILE_NAME, 5);
        
        TCPSocketConnection server;
        if (!server.connect(SERVER_IP, SERVER_PORT)) {
            printf("Connected to %s\n\r", SERVER_IP);
            FILE *fp = fopen(FILE_NAME, "rb");
            
            printf("  Sending %s\n\r", FILE_NAME);
            int sum = 0;
            while (sum < 110296)
            {
                int i = fread(buf, 1, BUF_SIZE, fp);
                server.send(buf, i);
                sum += i;
            }
            int n = server.receive(buf, BUF_SIZE);
            buf[n] = '\0';
            printf("  Received: %s\n\n\r", buf);
            server.close();
        } else {
            printf("Unable to connect to %s\n\r", SERVER_IP);
        }  
    }
}