Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE RPCInterface
main.cpp
- Committer:
- robo1340
- Date:
- 2019-11-12
- Revision:
- 13:f1649dc31b04
- Parent:
- 12:f1856a0b8ced
- Child:
- 14:5b3f49d7bf19
File content as of revision 13:f1649dc31b04:
#include "mbed.h" #include "rtos.h" #include "mbed_rpc.h" #include "uLCD_4DGL.h" /* Example RPC commands that have currently been implemented /writeLCD/run Hello_world /setTime/run 1256729737 */ uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; Serial bluetooth(p13,p14); Serial pc(USBTX, USBRX); Mutex stdio_mutex; //mutex used when accessing stdio functions Mutex lcd_mutex; //mutex used when accessing the bluetooth serial object Thread bluetooth_thread; //thread responsible for receiving rpc commands over bluetooth Thread time_thread; //thread responsible for updating the lcd with the current time //rpc function prototypes void writeLCD(Arguments *in, Reply *out); void setTime (Arguments *in, Reply *out); RPCFunction rpcWriteLCD(&writeLCD, "writeLCD"); RPCFunction rpcSetTime(&setTime, "setTime"); void time_thread_func() { while (true) { time_t seconds = time(NULL); stdio_mutex.lock(); lcd_mutex.lock(); uLCD.locate(0,0); uLCD.printf("%s", ctime(&seconds)); //Time as a basic string stdio_mutex.unlock(); lcd_mutex.unlock(); //printf("Time as seconds since January 1, 1970 = %d\n", seconds); //char buffer[32]; //strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); //printf("Time as a custom formatted string = %s", buffer); Thread::wait(1000); //only update every second } } void bluetooth_thread_func() { //The mbed RPC classes are now wrapped to create an RPC enabled version - see RpcClasses.h so don't add to base class // receive commands, and send back the responses char buf[256], outbuf[256]; uint16_t buf_pos = 0; while(true) { //Thread::wait(20); if (pc.readable() == true) { //comment out when using bluetooth to receive rpc commands //if (bluetooth.readable() == true) { stdio_mutex.lock(); buf[buf_pos] = pc.getc(); //comment out when using bluetooth to receive rpc commands //buf[buf_pos] = bluetooth.getc(); stdio_mutex.unlock(); if (buf[buf_pos] == '\n') { //the end of the RPC command has been received buf[buf_pos] = '\0'; buf_pos = 0; RPC::call(buf, outbuf); //make an RPC call stdio_mutex.lock(); pc.printf("%s\n", outbuf); //send the response stdio_mutex.unlock(); } else { buf_pos++; } } else { Thread::yield(); } } } int main() { bluetooth_thread.start(bluetooth_thread_func); time_thread.start(time_thread_func); } // Make sure the method takes in Arguments and Reply objects. void setTime (Arguments *in, Reply *out) { static const char * unix_time_str; uint32_t unix_time; //set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 unix_time_str = in->getArg<const char*>(); //get a pointer to the location where the argument string is stored unix_time = atoll(unix_time_str); set_time(unix_time); // Set RTC time to Wed, 28 Oct 2009 11:35:37 } // Make sure the method takes in Arguments and Reply objects. void writeLCD (Arguments *in, Reply *out) { static const char * msg_str; msg_str = in->getArg<const char*>(); //get a pointer to the location where the argument string is stored stdio_mutex.lock(); lcd_mutex.lock(); uLCD.locate(0,2); uLCD.printf("%s\r\n",msg_str); stdio_mutex.unlock(); lcd_mutex.unlock(); }