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@13:f1649dc31b04, 2019-11-12 (annotated)
- Committer:
- robo1340
- Date:
- Tue Nov 12 19:27:35 2019 +0000
- Revision:
- 13:f1649dc31b04
- Parent:
- 12:f1856a0b8ced
- Child:
- 14:5b3f49d7bf19
added comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
mbed_official | 11:0309bef74ba8 | 2 | #include "rtos.h" |
robo1340 | 12:f1856a0b8ced | 3 | |
robo1340 | 12:f1856a0b8ced | 4 | #include "mbed_rpc.h" |
robo1340 | 12:f1856a0b8ced | 5 | #include "uLCD_4DGL.h" |
robo1340 | 12:f1856a0b8ced | 6 | |
robo1340 | 13:f1649dc31b04 | 7 | /* Example RPC commands that have currently been implemented |
robo1340 | 13:f1649dc31b04 | 8 | |
robo1340 | 13:f1649dc31b04 | 9 | /writeLCD/run Hello_world |
robo1340 | 13:f1649dc31b04 | 10 | /setTime/run 1256729737 |
robo1340 | 13:f1649dc31b04 | 11 | |
robo1340 | 13:f1649dc31b04 | 12 | */ |
robo1340 | 13:f1649dc31b04 | 13 | |
robo1340 | 12:f1856a0b8ced | 14 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
robo1340 | 12:f1856a0b8ced | 15 | Serial bluetooth(p13,p14); |
robo1340 | 12:f1856a0b8ced | 16 | Serial pc(USBTX, USBRX); |
robo1340 | 12:f1856a0b8ced | 17 | |
robo1340 | 13:f1649dc31b04 | 18 | Mutex stdio_mutex; //mutex used when accessing stdio functions |
robo1340 | 13:f1649dc31b04 | 19 | Mutex lcd_mutex; //mutex used when accessing the bluetooth serial object |
robo1340 | 12:f1856a0b8ced | 20 | |
robo1340 | 13:f1649dc31b04 | 21 | Thread bluetooth_thread; //thread responsible for receiving rpc commands over bluetooth |
robo1340 | 13:f1649dc31b04 | 22 | Thread time_thread; //thread responsible for updating the lcd with the current time |
robo1340 | 12:f1856a0b8ced | 23 | |
robo1340 | 13:f1649dc31b04 | 24 | //rpc function prototypes |
robo1340 | 12:f1856a0b8ced | 25 | void writeLCD(Arguments *in, Reply *out); |
robo1340 | 12:f1856a0b8ced | 26 | void setTime (Arguments *in, Reply *out); |
robo1340 | 12:f1856a0b8ced | 27 | RPCFunction rpcWriteLCD(&writeLCD, "writeLCD"); |
robo1340 | 12:f1856a0b8ced | 28 | RPCFunction rpcSetTime(&setTime, "setTime"); |
robo1340 | 12:f1856a0b8ced | 29 | |
robo1340 | 12:f1856a0b8ced | 30 | |
robo1340 | 12:f1856a0b8ced | 31 | void time_thread_func() { |
emilmont | 1:491820ee784d | 32 | |
robo1340 | 12:f1856a0b8ced | 33 | while (true) { |
robo1340 | 12:f1856a0b8ced | 34 | time_t seconds = time(NULL); |
robo1340 | 12:f1856a0b8ced | 35 | |
robo1340 | 12:f1856a0b8ced | 36 | stdio_mutex.lock(); |
robo1340 | 12:f1856a0b8ced | 37 | lcd_mutex.lock(); |
robo1340 | 12:f1856a0b8ced | 38 | |
robo1340 | 12:f1856a0b8ced | 39 | uLCD.locate(0,0); |
robo1340 | 12:f1856a0b8ced | 40 | uLCD.printf("%s", ctime(&seconds)); //Time as a basic string |
robo1340 | 12:f1856a0b8ced | 41 | |
robo1340 | 12:f1856a0b8ced | 42 | stdio_mutex.unlock(); |
robo1340 | 12:f1856a0b8ced | 43 | lcd_mutex.unlock(); |
robo1340 | 12:f1856a0b8ced | 44 | |
robo1340 | 12:f1856a0b8ced | 45 | //printf("Time as seconds since January 1, 1970 = %d\n", seconds); |
emilmont | 1:491820ee784d | 46 | |
robo1340 | 12:f1856a0b8ced | 47 | //char buffer[32]; |
robo1340 | 12:f1856a0b8ced | 48 | //strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); |
robo1340 | 12:f1856a0b8ced | 49 | //printf("Time as a custom formatted string = %s", buffer); |
robo1340 | 12:f1856a0b8ced | 50 | |
robo1340 | 12:f1856a0b8ced | 51 | Thread::wait(1000); //only update every second |
robo1340 | 12:f1856a0b8ced | 52 | } |
robo1340 | 12:f1856a0b8ced | 53 | |
robo1340 | 12:f1856a0b8ced | 54 | } |
robo1340 | 12:f1856a0b8ced | 55 | |
robo1340 | 12:f1856a0b8ced | 56 | |
robo1340 | 12:f1856a0b8ced | 57 | void bluetooth_thread_func() { |
robo1340 | 12:f1856a0b8ced | 58 | |
robo1340 | 12:f1856a0b8ced | 59 | //The mbed RPC classes are now wrapped to create an RPC enabled version - see RpcClasses.h so don't add to base class |
robo1340 | 12:f1856a0b8ced | 60 | // receive commands, and send back the responses |
robo1340 | 12:f1856a0b8ced | 61 | char buf[256], outbuf[256]; |
robo1340 | 12:f1856a0b8ced | 62 | uint16_t buf_pos = 0; |
robo1340 | 12:f1856a0b8ced | 63 | |
robo1340 | 12:f1856a0b8ced | 64 | while(true) { |
robo1340 | 12:f1856a0b8ced | 65 | //Thread::wait(20); |
robo1340 | 12:f1856a0b8ced | 66 | |
robo1340 | 13:f1649dc31b04 | 67 | if (pc.readable() == true) { //comment out when using bluetooth to receive rpc commands |
robo1340 | 12:f1856a0b8ced | 68 | //if (bluetooth.readable() == true) { |
robo1340 | 12:f1856a0b8ced | 69 | |
robo1340 | 12:f1856a0b8ced | 70 | stdio_mutex.lock(); |
robo1340 | 12:f1856a0b8ced | 71 | |
robo1340 | 13:f1649dc31b04 | 72 | buf[buf_pos] = pc.getc(); //comment out when using bluetooth to receive rpc commands |
robo1340 | 12:f1856a0b8ced | 73 | //buf[buf_pos] = bluetooth.getc(); |
robo1340 | 12:f1856a0b8ced | 74 | |
robo1340 | 12:f1856a0b8ced | 75 | stdio_mutex.unlock(); |
robo1340 | 12:f1856a0b8ced | 76 | |
robo1340 | 12:f1856a0b8ced | 77 | if (buf[buf_pos] == '\n') { //the end of the RPC command has been received |
robo1340 | 12:f1856a0b8ced | 78 | buf[buf_pos] = '\0'; |
robo1340 | 12:f1856a0b8ced | 79 | buf_pos = 0; |
robo1340 | 12:f1856a0b8ced | 80 | RPC::call(buf, outbuf); //make an RPC call |
robo1340 | 12:f1856a0b8ced | 81 | |
robo1340 | 12:f1856a0b8ced | 82 | stdio_mutex.lock(); |
robo1340 | 12:f1856a0b8ced | 83 | pc.printf("%s\n", outbuf); //send the response |
robo1340 | 12:f1856a0b8ced | 84 | stdio_mutex.unlock(); |
robo1340 | 12:f1856a0b8ced | 85 | } |
robo1340 | 12:f1856a0b8ced | 86 | else { |
robo1340 | 12:f1856a0b8ced | 87 | buf_pos++; |
robo1340 | 12:f1856a0b8ced | 88 | } |
robo1340 | 12:f1856a0b8ced | 89 | |
robo1340 | 12:f1856a0b8ced | 90 | } else { |
robo1340 | 12:f1856a0b8ced | 91 | Thread::yield(); |
robo1340 | 12:f1856a0b8ced | 92 | } |
robo1340 | 12:f1856a0b8ced | 93 | |
emilmont | 1:491820ee784d | 94 | } |
emilmont | 1:491820ee784d | 95 | } |
robo1340 | 12:f1856a0b8ced | 96 | |
emilmont | 1:491820ee784d | 97 | int main() { |
robo1340 | 12:f1856a0b8ced | 98 | |
robo1340 | 12:f1856a0b8ced | 99 | bluetooth_thread.start(bluetooth_thread_func); |
robo1340 | 12:f1856a0b8ced | 100 | time_thread.start(time_thread_func); |
robo1340 | 12:f1856a0b8ced | 101 | |
robo1340 | 12:f1856a0b8ced | 102 | } |
robo1340 | 12:f1856a0b8ced | 103 | |
robo1340 | 12:f1856a0b8ced | 104 | |
robo1340 | 12:f1856a0b8ced | 105 | // Make sure the method takes in Arguments and Reply objects. |
robo1340 | 12:f1856a0b8ced | 106 | void setTime (Arguments *in, Reply *out) { |
robo1340 | 12:f1856a0b8ced | 107 | static const char * unix_time_str; |
robo1340 | 12:f1856a0b8ced | 108 | uint32_t unix_time; |
robo1340 | 12:f1856a0b8ced | 109 | |
robo1340 | 12:f1856a0b8ced | 110 | //set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
robo1340 | 12:f1856a0b8ced | 111 | unix_time_str = in->getArg<const char*>(); //get a pointer to the location where the argument string is stored |
robo1340 | 12:f1856a0b8ced | 112 | unix_time = atoll(unix_time_str); |
robo1340 | 12:f1856a0b8ced | 113 | |
robo1340 | 12:f1856a0b8ced | 114 | set_time(unix_time); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
emilmont | 1:491820ee784d | 115 | |
emilmont | 1:491820ee784d | 116 | } |
robo1340 | 12:f1856a0b8ced | 117 | |
robo1340 | 12:f1856a0b8ced | 118 | |
robo1340 | 12:f1856a0b8ced | 119 | // Make sure the method takes in Arguments and Reply objects. |
robo1340 | 12:f1856a0b8ced | 120 | void writeLCD (Arguments *in, Reply *out) { |
robo1340 | 12:f1856a0b8ced | 121 | static const char * msg_str; |
robo1340 | 12:f1856a0b8ced | 122 | |
robo1340 | 12:f1856a0b8ced | 123 | msg_str = in->getArg<const char*>(); //get a pointer to the location where the argument string is stored |
robo1340 | 12:f1856a0b8ced | 124 | |
robo1340 | 12:f1856a0b8ced | 125 | stdio_mutex.lock(); |
robo1340 | 12:f1856a0b8ced | 126 | lcd_mutex.lock(); |
robo1340 | 12:f1856a0b8ced | 127 | |
robo1340 | 12:f1856a0b8ced | 128 | uLCD.locate(0,2); |
robo1340 | 12:f1856a0b8ced | 129 | uLCD.printf("%s\r\n",msg_str); |
robo1340 | 12:f1856a0b8ced | 130 | |
robo1340 | 12:f1856a0b8ced | 131 | stdio_mutex.unlock(); |
robo1340 | 12:f1856a0b8ced | 132 | lcd_mutex.unlock(); |
robo1340 | 12:f1856a0b8ced | 133 | |
robo1340 | 12:f1856a0b8ced | 134 | } |