Gerrod Ubben / Mbed 2 deprecated ECE4180_Final_Project

Dependencies:   mbed mbed-rtos 4DGL-uLCD-SE RPCInterface

Revision:
15:5120c88a7a87
Parent:
14:5b3f49d7bf19
Child:
16:d4b686118853
--- a/main.cpp	Wed Dec 04 01:55:03 2019 +0000
+++ b/main.cpp	Wed Dec 04 19:09:11 2019 +0000
@@ -5,10 +5,11 @@
 #include "uLCD_4DGL.h"
 #include <time.h>
 #include <math.h>
+#include "chime.h"
 
 /* Example RPC commands that have currently been implemented
 
-/writeLCD/run Hello_world
+/notify/run This_is_a_notification_message_that_should_be_displayed_on_the_lcd
 /setTime/run <unix time> <UTC offset (-5 for Atlanta)>
 /setTime/run 1256729737 -5
 
@@ -32,12 +33,28 @@
 Thread time_thread; //thread responsible for updating the lcd with the current time
 
 //rpc function prototypes
-void displayNotification(Arguments *in, Reply *out);
-void setTime (Arguments *in, Reply *out);
-RPCFunction rpcWriteLCD(&displayNotification, "notify");
-RPCFunction rpcSetTime(&setTime, "setTime");
+void display_notification_rpc_func(Arguments *in, Reply *out);
+void set_time_rpc_func (Arguments *in, Reply *out);
+RPCFunction rpcWriteLCD(&display_notification_rpc_func, "notify");
+RPCFunction rpcset_time_rpc_func(&set_time_rpc_func, "setTime");
+
+#define sample_freq 16000.0
+AnalogOut speaker(p18);
+Ticker notification_chime_ticker;
+
+//interrupt routine to play next audio sample from array in flash
+void audio_sample () {
+    static int i=0;
+    speaker.write_u16(((uint16_t)data[i]<<7));
+    i++;
+    if (i>= sizeof(data)) {
+        i = 0;
+        notification_chime_ticker.detach();
+    }
+}
 
 //interrupt routine for when the input button is pressed
+//when the view button is pressed, dismiss the currently shown notification and display the current time
 void view_button_pressed(void){
     display_notification = false;
     display_time = true;
@@ -68,7 +85,7 @@
 #define RAD_PER_SEC (2*M_PI)/60
 #define RAD_PER_MIN (2*M_PI)/60
 #define RAD_PER_HOUR (2*M_PI)/12
-//create the second, minute, and hour hands for an analog clock on the lcd display
+//function to handle the display of the time on the lcd screen
 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) {
     static double angle;
     static int prev_sec;
@@ -100,8 +117,6 @@
     stdio_mutex.lock();
     
     //print the current date in a month/day/year format
-    //uLCD.locate(4,10);
-    //uLCD.printf("        ");
     uLCD.locate(4,10);
     uLCD.printf("%2d/%2d/%4d",month, day, year);
     uLCD.locate(8,11);
@@ -115,13 +130,13 @@
     prev_sec = sec;
     prev_minute = minute;
     prev_hour = hour;
-  
 }
 
+//function to update the time displayed on the lcd approximately every second
 void time_thread_func() {
     struct tm * t; //time struct defined in time.h
-    static time_t unix_time;
-    static bool prev_display_time;
+    static time_t unix_time; //the time in unix time
+    static bool prev_display_time; //indicates whether time was being displayed the last time the thread ran
     
     while (true) {
         if (display_time == true) {
@@ -136,6 +151,7 @@
             setup_analog_clock(WHITE);
             int hour = (t->tm_hour + utc_offset);
             if (hour < 0){ hour += 24;}
+            else if (hour >= 24) {hour -= 24;}
             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);
         }
         prev_display_time = display_time;
@@ -145,16 +161,12 @@
     
 }
 
-
+//function to continuously take in characters over bluetooth serial and parse them as RPC commands
 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) {
@@ -167,7 +179,7 @@
             stdio_mutex.unlock();
             
             if (buf[buf_pos] == '\n') { //the end of the RPC command has been received
-                buf[buf_pos] = '\0';
+                buf[buf_pos] = '\0'; //replace the newline character with a null character
                 buf_pos = 0;
                 RPC::call(buf, outbuf);  //make an RPC call  
                 
@@ -186,26 +198,14 @@
     }
 }
 
-int main() {
-    
-    uLCD.baudrate(3000000); //jack up baud rate to max for fast display
-    view_button.mode(PullUp);
-    view_button.fall(&view_button_pressed);
-    
-    
-    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)   {
+//RPC function to receive the current time
+//the first argument is unix time and the second argument is the offset from UTC time
+void set_time_rpc_func (Arguments *in, Reply *out)   {
     static const char * unix_time_str;
     uint32_t unix_time;
     int offset;
-    //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
-    offset = in->getArg<int>();
+    offset = in->getArg<int>(); //get the second argument which indicates the offeset (in hours) from UTC time
     unix_time = atoll(unix_time_str);
     utc_offset = offset;
     
@@ -214,8 +214,10 @@
 }
 
 
-// Make sure the method takes in Arguments and Reply objects.
-void displayNotification (Arguments *in, Reply *out)   {
+//RPC function to receive notification strings
+//note notification strings should not contain the character ' ', in lieu
+//they should contain '_' to indicate spaces
+void display_notification_rpc_func (Arguments *in, Reply *out)   {
     static char display_str[18];
     static const char * msg_str;
     int i,j;
@@ -249,4 +251,20 @@
     stdio_mutex.unlock();
     lcd_mutex.unlock();
     
+    notification_chime_ticker.attach(&audio_sample, 1.0/sample_freq);
+    
 }
+
+int main() {
+    
+    uLCD.baudrate(3000000); //increase the lcd baud rate
+    
+    //configure the input button and attach an interrupt routine to it
+    view_button.mode(PullUp);
+    view_button.fall(&view_button_pressed);
+    
+    
+    bluetooth_thread.start(bluetooth_thread_func); //start the thread that takes in characters to construct RPC commands
+    time_thread.start(time_thread_func); //start the thread that updates the displayed time
+
+}