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@14:5b3f49d7bf19, 2019-12-04 (annotated)
- Committer:
- robo1340
- Date:
- Wed Dec 04 01:55:03 2019 +0000
- Revision:
- 14:5b3f49d7bf19
- Parent:
- 13:f1649dc31b04
- Child:
- 15:5120c88a7a87
added analog watch hands to the clock. Added button to dismiss notifications;
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 | 14:5b3f49d7bf19 | 6 | #include <time.h> |
robo1340 | 14:5b3f49d7bf19 | 7 | #include <math.h> |
robo1340 | 12:f1856a0b8ced | 8 | |
robo1340 | 13:f1649dc31b04 | 9 | /* Example RPC commands that have currently been implemented |
robo1340 | 13:f1649dc31b04 | 10 | |
robo1340 | 13:f1649dc31b04 | 11 | /writeLCD/run Hello_world |
robo1340 | 14:5b3f49d7bf19 | 12 | /setTime/run <unix time> <UTC offset (-5 for Atlanta)> |
robo1340 | 14:5b3f49d7bf19 | 13 | /setTime/run 1256729737 -5 |
robo1340 | 13:f1649dc31b04 | 14 | |
robo1340 | 13:f1649dc31b04 | 15 | */ |
robo1340 | 13:f1649dc31b04 | 16 | |
robo1340 | 14:5b3f49d7bf19 | 17 | volatile bool display_notification = false; |
robo1340 | 14:5b3f49d7bf19 | 18 | volatile bool display_time = true; |
robo1340 | 14:5b3f49d7bf19 | 19 | |
robo1340 | 14:5b3f49d7bf19 | 20 | volatile int utc_offset; //keeps track of the current timezone of the watch |
robo1340 | 14:5b3f49d7bf19 | 21 | |
robo1340 | 12:f1856a0b8ced | 22 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
robo1340 | 14:5b3f49d7bf19 | 23 | //Serial bluetooth(p13,p14); |
robo1340 | 12:f1856a0b8ced | 24 | Serial pc(USBTX, USBRX); |
robo1340 | 12:f1856a0b8ced | 25 | |
robo1340 | 14:5b3f49d7bf19 | 26 | InterruptIn view_button(p12); |
robo1340 | 14:5b3f49d7bf19 | 27 | |
robo1340 | 13:f1649dc31b04 | 28 | Mutex stdio_mutex; //mutex used when accessing stdio functions |
robo1340 | 14:5b3f49d7bf19 | 29 | Mutex lcd_mutex; //mutex used when accessing the lcd object |
robo1340 | 12:f1856a0b8ced | 30 | |
robo1340 | 13:f1649dc31b04 | 31 | Thread bluetooth_thread; //thread responsible for receiving rpc commands over bluetooth |
robo1340 | 13:f1649dc31b04 | 32 | Thread time_thread; //thread responsible for updating the lcd with the current time |
robo1340 | 12:f1856a0b8ced | 33 | |
robo1340 | 13:f1649dc31b04 | 34 | //rpc function prototypes |
robo1340 | 14:5b3f49d7bf19 | 35 | void displayNotification(Arguments *in, Reply *out); |
robo1340 | 12:f1856a0b8ced | 36 | void setTime (Arguments *in, Reply *out); |
robo1340 | 14:5b3f49d7bf19 | 37 | RPCFunction rpcWriteLCD(&displayNotification, "notify"); |
robo1340 | 12:f1856a0b8ced | 38 | RPCFunction rpcSetTime(&setTime, "setTime"); |
robo1340 | 12:f1856a0b8ced | 39 | |
robo1340 | 14:5b3f49d7bf19 | 40 | //interrupt routine for when the input button is pressed |
robo1340 | 14:5b3f49d7bf19 | 41 | void view_button_pressed(void){ |
robo1340 | 14:5b3f49d7bf19 | 42 | display_notification = false; |
robo1340 | 14:5b3f49d7bf19 | 43 | display_time = true; |
robo1340 | 14:5b3f49d7bf19 | 44 | } |
robo1340 | 14:5b3f49d7bf19 | 45 | |
robo1340 | 14:5b3f49d7bf19 | 46 | //flip the y coordinate around so that standard cartesian coordinates can be used |
robo1340 | 14:5b3f49d7bf19 | 47 | int flipy(int y_coord){ |
robo1340 | 14:5b3f49d7bf19 | 48 | return (128-y_coord); |
robo1340 | 14:5b3f49d7bf19 | 49 | } |
robo1340 | 14:5b3f49d7bf19 | 50 | |
robo1340 | 14:5b3f49d7bf19 | 51 | #define C_X 64 |
robo1340 | 14:5b3f49d7bf19 | 52 | #define C_Y 64 |
robo1340 | 14:5b3f49d7bf19 | 53 | #define M_PI 3.141592 |
robo1340 | 14:5b3f49d7bf19 | 54 | //create the tick marks for an analog clock on the lcd display |
robo1340 | 14:5b3f49d7bf19 | 55 | void setup_analog_clock(uint32_t color){ |
robo1340 | 14:5b3f49d7bf19 | 56 | |
robo1340 | 14:5b3f49d7bf19 | 57 | lcd_mutex.lock(); |
robo1340 | 14:5b3f49d7bf19 | 58 | uLCD.filled_circle(64, 64, 5, color); //centercircle |
robo1340 | 14:5b3f49d7bf19 | 59 | double angle; |
robo1340 | 14:5b3f49d7bf19 | 60 | //start from 3 oclock and draw all the clock tick marks counter-clockwise |
robo1340 | 14:5b3f49d7bf19 | 61 | for(angle = 0; angle < (2*M_PI)-(M_PI/12); angle += M_PI/6){ |
robo1340 | 14:5b3f49d7bf19 | 62 | uLCD.line(54*cos(angle)+C_X,flipy(54*sin(angle)+C_Y), 64*cos(angle)+C_X,flipy(64*sin(angle)+C_Y), color); //3 oclock tick mark |
robo1340 | 14:5b3f49d7bf19 | 63 | } |
robo1340 | 14:5b3f49d7bf19 | 64 | lcd_mutex.unlock(); |
robo1340 | 14:5b3f49d7bf19 | 65 | |
robo1340 | 14:5b3f49d7bf19 | 66 | } |
robo1340 | 14:5b3f49d7bf19 | 67 | |
robo1340 | 14:5b3f49d7bf19 | 68 | #define RAD_PER_SEC (2*M_PI)/60 |
robo1340 | 14:5b3f49d7bf19 | 69 | #define RAD_PER_MIN (2*M_PI)/60 |
robo1340 | 14:5b3f49d7bf19 | 70 | #define RAD_PER_HOUR (2*M_PI)/12 |
robo1340 | 14:5b3f49d7bf19 | 71 | //create the second, minute, and hour hands for an analog clock on the lcd display |
robo1340 | 14:5b3f49d7bf19 | 72 | void show_time_analog(int sec, int minute, int hour, int day, int month, int year, uint32_t sec_color, uint32_t min_color, uint32_t hour_color, uint32_t back_color) { |
robo1340 | 14:5b3f49d7bf19 | 73 | static double angle; |
robo1340 | 14:5b3f49d7bf19 | 74 | static int prev_sec; |
robo1340 | 14:5b3f49d7bf19 | 75 | static int prev_minute; |
robo1340 | 14:5b3f49d7bf19 | 76 | static int prev_hour; |
robo1340 | 14:5b3f49d7bf19 | 77 | |
robo1340 | 14:5b3f49d7bf19 | 78 | lcd_mutex.lock(); |
robo1340 | 14:5b3f49d7bf19 | 79 | |
robo1340 | 14:5b3f49d7bf19 | 80 | //tear down the previous hands that were drawn |
robo1340 | 14:5b3f49d7bf19 | 81 | angle = -(RAD_PER_SEC*prev_sec) + M_PI/2; |
robo1340 | 14:5b3f49d7bf19 | 82 | uLCD.line(C_X,C_Y,64*cos(angle)+C_X, flipy(64*sin(angle)+C_Y),back_color); |
robo1340 | 14:5b3f49d7bf19 | 83 | |
robo1340 | 14:5b3f49d7bf19 | 84 | angle = -(RAD_PER_MIN*prev_minute) + M_PI/2; |
robo1340 | 14:5b3f49d7bf19 | 85 | uLCD.line(C_X,C_Y,52*cos(angle)+C_X, flipy(52*sin(angle)+C_Y),back_color); |
robo1340 | 14:5b3f49d7bf19 | 86 | |
robo1340 | 14:5b3f49d7bf19 | 87 | angle = -(RAD_PER_HOUR*prev_hour) + M_PI/2; |
robo1340 | 14:5b3f49d7bf19 | 88 | uLCD.line(C_X,C_Y,40*cos(angle)+C_X, flipy(40*sin(angle)+C_Y),back_color); |
robo1340 | 14:5b3f49d7bf19 | 89 | |
robo1340 | 14:5b3f49d7bf19 | 90 | //draw the new hands |
robo1340 | 14:5b3f49d7bf19 | 91 | angle = -(RAD_PER_SEC*sec) + M_PI/2; |
robo1340 | 14:5b3f49d7bf19 | 92 | uLCD.line(C_X,C_Y,64*cos(angle)+C_X, flipy(64*sin(angle)+C_Y),sec_color); |
robo1340 | 14:5b3f49d7bf19 | 93 | |
robo1340 | 14:5b3f49d7bf19 | 94 | angle = -(RAD_PER_MIN*minute) + M_PI/2; |
robo1340 | 14:5b3f49d7bf19 | 95 | uLCD.line(C_X,C_Y,52*cos(angle)+C_X, flipy(52*sin(angle)+C_Y),min_color); |
robo1340 | 14:5b3f49d7bf19 | 96 | |
robo1340 | 14:5b3f49d7bf19 | 97 | angle = -(RAD_PER_HOUR*hour) + M_PI/2; |
robo1340 | 14:5b3f49d7bf19 | 98 | uLCD.line(C_X,C_Y,40*cos(angle)+C_X, flipy(40*sin(angle)+C_Y),hour_color); |
robo1340 | 14:5b3f49d7bf19 | 99 | |
robo1340 | 14:5b3f49d7bf19 | 100 | stdio_mutex.lock(); |
robo1340 | 14:5b3f49d7bf19 | 101 | |
robo1340 | 14:5b3f49d7bf19 | 102 | //print the current date in a month/day/year format |
robo1340 | 14:5b3f49d7bf19 | 103 | //uLCD.locate(4,10); |
robo1340 | 14:5b3f49d7bf19 | 104 | //uLCD.printf(" "); |
robo1340 | 14:5b3f49d7bf19 | 105 | uLCD.locate(4,10); |
robo1340 | 14:5b3f49d7bf19 | 106 | uLCD.printf("%2d/%2d/%4d",month, day, year); |
robo1340 | 14:5b3f49d7bf19 | 107 | uLCD.locate(8,11); |
robo1340 | 14:5b3f49d7bf19 | 108 | if (hour < 12) {uLCD.printf("AM");} |
robo1340 | 14:5b3f49d7bf19 | 109 | else {uLCD.printf("PM");} |
robo1340 | 14:5b3f49d7bf19 | 110 | |
robo1340 | 14:5b3f49d7bf19 | 111 | stdio_mutex.unlock(); |
robo1340 | 14:5b3f49d7bf19 | 112 | lcd_mutex.unlock(); |
robo1340 | 14:5b3f49d7bf19 | 113 | |
robo1340 | 14:5b3f49d7bf19 | 114 | //store the location of the current hands |
robo1340 | 14:5b3f49d7bf19 | 115 | prev_sec = sec; |
robo1340 | 14:5b3f49d7bf19 | 116 | prev_minute = minute; |
robo1340 | 14:5b3f49d7bf19 | 117 | prev_hour = hour; |
robo1340 | 14:5b3f49d7bf19 | 118 | |
robo1340 | 14:5b3f49d7bf19 | 119 | } |
robo1340 | 12:f1856a0b8ced | 120 | |
robo1340 | 12:f1856a0b8ced | 121 | void time_thread_func() { |
robo1340 | 14:5b3f49d7bf19 | 122 | struct tm * t; //time struct defined in time.h |
robo1340 | 14:5b3f49d7bf19 | 123 | static time_t unix_time; |
robo1340 | 14:5b3f49d7bf19 | 124 | static bool prev_display_time; |
robo1340 | 14:5b3f49d7bf19 | 125 | |
robo1340 | 12:f1856a0b8ced | 126 | while (true) { |
robo1340 | 14:5b3f49d7bf19 | 127 | if (display_time == true) { |
robo1340 | 14:5b3f49d7bf19 | 128 | if (prev_display_time == false){ //clear whatever was previously on the screen |
robo1340 | 14:5b3f49d7bf19 | 129 | lcd_mutex.lock(); |
robo1340 | 14:5b3f49d7bf19 | 130 | uLCD.cls(); |
robo1340 | 14:5b3f49d7bf19 | 131 | lcd_mutex.unlock(); |
robo1340 | 14:5b3f49d7bf19 | 132 | } |
robo1340 | 14:5b3f49d7bf19 | 133 | unix_time = time(NULL); |
robo1340 | 14:5b3f49d7bf19 | 134 | t = localtime(&unix_time); |
robo1340 | 14:5b3f49d7bf19 | 135 | |
robo1340 | 14:5b3f49d7bf19 | 136 | setup_analog_clock(WHITE); |
robo1340 | 14:5b3f49d7bf19 | 137 | int hour = (t->tm_hour + utc_offset); |
robo1340 | 14:5b3f49d7bf19 | 138 | if (hour < 0){ hour += 24;} |
robo1340 | 14:5b3f49d7bf19 | 139 | show_time_analog(t->tm_sec,t->tm_min,hour,t->tm_mday,t->tm_mon+1,t->tm_year+1900,RED+BLUE,WHITE,BLUE,BLACK); |
robo1340 | 14:5b3f49d7bf19 | 140 | } |
robo1340 | 14:5b3f49d7bf19 | 141 | prev_display_time = display_time; |
robo1340 | 12:f1856a0b8ced | 142 | |
robo1340 | 12:f1856a0b8ced | 143 | Thread::wait(1000); //only update every second |
robo1340 | 12:f1856a0b8ced | 144 | } |
robo1340 | 12:f1856a0b8ced | 145 | |
robo1340 | 12:f1856a0b8ced | 146 | } |
robo1340 | 12:f1856a0b8ced | 147 | |
robo1340 | 12:f1856a0b8ced | 148 | |
robo1340 | 12:f1856a0b8ced | 149 | void bluetooth_thread_func() { |
robo1340 | 12:f1856a0b8ced | 150 | |
robo1340 | 12:f1856a0b8ced | 151 | //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 | 152 | // receive commands, and send back the responses |
robo1340 | 12:f1856a0b8ced | 153 | char buf[256], outbuf[256]; |
robo1340 | 12:f1856a0b8ced | 154 | uint16_t buf_pos = 0; |
robo1340 | 12:f1856a0b8ced | 155 | |
robo1340 | 12:f1856a0b8ced | 156 | while(true) { |
robo1340 | 12:f1856a0b8ced | 157 | //Thread::wait(20); |
robo1340 | 12:f1856a0b8ced | 158 | |
robo1340 | 13:f1649dc31b04 | 159 | if (pc.readable() == true) { //comment out when using bluetooth to receive rpc commands |
robo1340 | 12:f1856a0b8ced | 160 | //if (bluetooth.readable() == true) { |
robo1340 | 12:f1856a0b8ced | 161 | |
robo1340 | 12:f1856a0b8ced | 162 | stdio_mutex.lock(); |
robo1340 | 12:f1856a0b8ced | 163 | |
robo1340 | 13:f1649dc31b04 | 164 | buf[buf_pos] = pc.getc(); //comment out when using bluetooth to receive rpc commands |
robo1340 | 12:f1856a0b8ced | 165 | //buf[buf_pos] = bluetooth.getc(); |
robo1340 | 12:f1856a0b8ced | 166 | |
robo1340 | 12:f1856a0b8ced | 167 | stdio_mutex.unlock(); |
robo1340 | 12:f1856a0b8ced | 168 | |
robo1340 | 12:f1856a0b8ced | 169 | if (buf[buf_pos] == '\n') { //the end of the RPC command has been received |
robo1340 | 12:f1856a0b8ced | 170 | buf[buf_pos] = '\0'; |
robo1340 | 12:f1856a0b8ced | 171 | buf_pos = 0; |
robo1340 | 12:f1856a0b8ced | 172 | RPC::call(buf, outbuf); //make an RPC call |
robo1340 | 12:f1856a0b8ced | 173 | |
robo1340 | 12:f1856a0b8ced | 174 | stdio_mutex.lock(); |
robo1340 | 12:f1856a0b8ced | 175 | pc.printf("%s\n", outbuf); //send the response |
robo1340 | 12:f1856a0b8ced | 176 | stdio_mutex.unlock(); |
robo1340 | 12:f1856a0b8ced | 177 | } |
robo1340 | 12:f1856a0b8ced | 178 | else { |
robo1340 | 12:f1856a0b8ced | 179 | buf_pos++; |
robo1340 | 12:f1856a0b8ced | 180 | } |
robo1340 | 12:f1856a0b8ced | 181 | |
robo1340 | 12:f1856a0b8ced | 182 | } else { |
robo1340 | 12:f1856a0b8ced | 183 | Thread::yield(); |
robo1340 | 12:f1856a0b8ced | 184 | } |
robo1340 | 12:f1856a0b8ced | 185 | |
emilmont | 1:491820ee784d | 186 | } |
emilmont | 1:491820ee784d | 187 | } |
robo1340 | 12:f1856a0b8ced | 188 | |
emilmont | 1:491820ee784d | 189 | int main() { |
robo1340 | 12:f1856a0b8ced | 190 | |
robo1340 | 14:5b3f49d7bf19 | 191 | uLCD.baudrate(3000000); //jack up baud rate to max for fast display |
robo1340 | 14:5b3f49d7bf19 | 192 | view_button.mode(PullUp); |
robo1340 | 14:5b3f49d7bf19 | 193 | view_button.fall(&view_button_pressed); |
robo1340 | 14:5b3f49d7bf19 | 194 | |
robo1340 | 14:5b3f49d7bf19 | 195 | |
robo1340 | 12:f1856a0b8ced | 196 | bluetooth_thread.start(bluetooth_thread_func); |
robo1340 | 12:f1856a0b8ced | 197 | time_thread.start(time_thread_func); |
robo1340 | 12:f1856a0b8ced | 198 | |
robo1340 | 12:f1856a0b8ced | 199 | } |
robo1340 | 12:f1856a0b8ced | 200 | |
robo1340 | 12:f1856a0b8ced | 201 | // Make sure the method takes in Arguments and Reply objects. |
robo1340 | 12:f1856a0b8ced | 202 | void setTime (Arguments *in, Reply *out) { |
robo1340 | 12:f1856a0b8ced | 203 | static const char * unix_time_str; |
robo1340 | 12:f1856a0b8ced | 204 | uint32_t unix_time; |
robo1340 | 14:5b3f49d7bf19 | 205 | int offset; |
robo1340 | 12:f1856a0b8ced | 206 | //set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
robo1340 | 12:f1856a0b8ced | 207 | unix_time_str = in->getArg<const char*>(); //get a pointer to the location where the argument string is stored |
robo1340 | 14:5b3f49d7bf19 | 208 | offset = in->getArg<int>(); |
robo1340 | 12:f1856a0b8ced | 209 | unix_time = atoll(unix_time_str); |
robo1340 | 14:5b3f49d7bf19 | 210 | utc_offset = offset; |
robo1340 | 12:f1856a0b8ced | 211 | |
robo1340 | 12:f1856a0b8ced | 212 | set_time(unix_time); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
emilmont | 1:491820ee784d | 213 | |
emilmont | 1:491820ee784d | 214 | } |
robo1340 | 12:f1856a0b8ced | 215 | |
robo1340 | 12:f1856a0b8ced | 216 | |
robo1340 | 12:f1856a0b8ced | 217 | // Make sure the method takes in Arguments and Reply objects. |
robo1340 | 14:5b3f49d7bf19 | 218 | void displayNotification (Arguments *in, Reply *out) { |
robo1340 | 14:5b3f49d7bf19 | 219 | static char display_str[18]; |
robo1340 | 12:f1856a0b8ced | 220 | static const char * msg_str; |
robo1340 | 14:5b3f49d7bf19 | 221 | int i,j; |
robo1340 | 14:5b3f49d7bf19 | 222 | bool break_out = false; |
robo1340 | 14:5b3f49d7bf19 | 223 | |
robo1340 | 14:5b3f49d7bf19 | 224 | display_notification = true; |
robo1340 | 14:5b3f49d7bf19 | 225 | display_time = false; |
robo1340 | 12:f1856a0b8ced | 226 | |
robo1340 | 12:f1856a0b8ced | 227 | msg_str = in->getArg<const char*>(); //get a pointer to the location where the argument string is stored |
robo1340 | 12:f1856a0b8ced | 228 | |
robo1340 | 12:f1856a0b8ced | 229 | stdio_mutex.lock(); |
robo1340 | 12:f1856a0b8ced | 230 | lcd_mutex.lock(); |
robo1340 | 12:f1856a0b8ced | 231 | |
robo1340 | 14:5b3f49d7bf19 | 232 | uLCD.cls(); |
robo1340 | 14:5b3f49d7bf19 | 233 | uLCD.locate(0,0); |
robo1340 | 14:5b3f49d7bf19 | 234 | i = 0; |
robo1340 | 14:5b3f49d7bf19 | 235 | while(true){ |
robo1340 | 14:5b3f49d7bf19 | 236 | for(j=0; j<18; j++){ |
robo1340 | 14:5b3f49d7bf19 | 237 | if (msg_str[i+j] == '_'){ |
robo1340 | 14:5b3f49d7bf19 | 238 | display_str[j] = ' '; |
robo1340 | 14:5b3f49d7bf19 | 239 | } else { |
robo1340 | 14:5b3f49d7bf19 | 240 | display_str[j] = msg_str[i+j]; |
robo1340 | 14:5b3f49d7bf19 | 241 | } |
robo1340 | 14:5b3f49d7bf19 | 242 | if (msg_str[i+j] == '\0') {break_out = true; break;} |
robo1340 | 14:5b3f49d7bf19 | 243 | } |
robo1340 | 14:5b3f49d7bf19 | 244 | i+= 18; |
robo1340 | 14:5b3f49d7bf19 | 245 | uLCD.printf("%s\r\n",display_str); |
robo1340 | 14:5b3f49d7bf19 | 246 | if (break_out){break;} |
robo1340 | 14:5b3f49d7bf19 | 247 | } |
robo1340 | 12:f1856a0b8ced | 248 | |
robo1340 | 12:f1856a0b8ced | 249 | stdio_mutex.unlock(); |
robo1340 | 12:f1856a0b8ced | 250 | lcd_mutex.unlock(); |
robo1340 | 12:f1856a0b8ced | 251 | |
robo1340 | 12:f1856a0b8ced | 252 | } |