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
Diff: main.cpp
- Revision:
- 1:c47a2f0816bb
- Parent:
- 0:4e6ae21cbd31
- Child:
- 2:c939d0501184
--- a/main.cpp Tue Dec 02 17:22:43 2014 +0000 +++ b/main.cpp Mon Dec 08 10:06:35 2014 +0000 @@ -2,116 +2,58 @@ #include "rtos.h" #include "EthernetInterface.h" #include "uLCD_4DGL.h" +#include "SDFileSystem.h" #include "Clock.h" +#include "Recorder.h" #include <string> -#include <sstream> -#include <vector> + +#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; -Clock clk; -EthernetInterface eth; +char buf[BUF_SIZE]; + -/** - * Function to get a command from the virtual serial connection. - * Will prompt the serial port for a command and read a command - * into a buffer and convert that buffer to a string object. This - * function should only be used while testing without a connection. - */ -string getCommand() { - char buffer[100]; - char next = 0; - int i = 0; - sMutex.lock(); - pc.printf("Enter a command: \n\r"); - sMutex.unlock(); - do { +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(); - if (pc.readable()) { - next = pc.getc(); - buffer[i++] = next; - } + pc.printf("Not a valid command\n\r"); sMutex.unlock(); - Thread::wait(0.1); - } while (next != '\r'); - - buffer[i] = '\0'; - sMutex.lock(); - pc.printf("Command entered: %s\n\r", buffer); - sMutex.unlock(); - return string(buffer); -} - -/** - * Function to take a string command and break it into tokens of words/numbers. - * These tokens will be added to the vector given as an argument and can then be - * parsed to execute a command. - */ -vector<string>& splitCommand(const string &command, vector<string> &tokens) { - string buf; - stringstream ss(command); - - while (ss >> buf) - tokens.push_back(buf); - - return tokens; + } } -/** - * Function to actually parse and execute a given command. If the command is not - * supported, an error function will be called to give feedback. - */ -void parseCommand(const vector<string> &tokens) { - sMutex.lock(); - pc.printf("number of tokens: %d\n\r", tokens.size()); - sMutex.unlock(); - - if (tokens.size() < 2) { - // error message - return; - } - - string operation = tokens[0]; - string destination = tokens[1]; - - if (operation == "set") { - if (destination == "time") { - int hour = atoi(tokens[2].c_str()); - int minute = atoi(tokens[3].c_str()); - int period = tokens.at(4) == "am" ? AM : PM; - cMutex.lock(); - clk.setTime(hour, minute, period); - cMutex.unlock(); - } else if (destination == "alarm") { - int hours = atoi(tokens[2].c_str()); - int minutes = atoi(tokens[3].c_str()); - } else if (destination == "timezone") { - int timezone = atoi(tokens[2].c_str()); - cMutex.lock(); - clk.setTimezone(timezone); - cMutex.unlock(); - } else { - //return error message - } - } else if (operation == "sync") { - if (destination == "time") { - - cMutex.lock(); - if (clk.syncTime() == 0) { - sMutex.lock(); - pc.printf("Set time successfully\r\n"); - } else { - sMutex.lock(); - pc.printf("Error\r\n"); - } - sMutex.unlock(); - cMutex.unlock(); - } - // return error message - } -} /** * Thread which updates the clock on the lcd while the main thread is waiting for @@ -157,20 +99,64 @@ * command execution. */ int main() { - vector<string> tokens; + pc.printf("Starting MbedClock\n\r"); eth.init(); + pc.printf("Ititialized Ethernet\n\r"); + eth.connect(); - wait(5); // I don't know why you have to wait... - - pc.printf("%s\n\r", eth.getIPAddress()); + 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); - - while (1) { - tokens.clear(); - string command = getCommand(); - splitCommand(command, tokens); - parseCommand(tokens); + //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); + } } }