Nrithya Theetharappan / Mbed OS Hexi_BLE_Time_copy

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

Fork of Hexi_BLE_Time by Adam S

Revision:
11:6776aca68628
Parent:
10:64e12acdeab5
Child:
12:b92c39b2c92a
--- a/main.cpp	Tue Jun 12 05:15:38 2018 +0000
+++ b/main.cpp	Tue Jun 12 18:35:12 2018 +0000
@@ -15,6 +15,7 @@
 void StopHaptic(void const *n);
 void resetBLE(void);
 void displayTime(void);
+void update_clock(void);
 
 Serial pc(USBTX, USBRX);
 
@@ -37,9 +38,11 @@
 Thread broadcastThread;
 Thread resetBLE_Thread;
 Thread displayTime_Thread;
+Thread update_clock_Thread;
 
 /*Create a Timer to reset BLE */ 
 Timer BLE_clock;
+Timer t;
 
  /* Text Buffer */ 
 char text[20]; 
@@ -56,6 +59,7 @@
 uint16_t z = 10000;
 
 float current_time;
+char token1[15];
 float alert_time = 0;
 double float_time = 1528686283;
 char buffer[16];
@@ -74,6 +78,7 @@
 int hour_2d = 0;
 char newtime_text[10];
 char *token[10];
+int int_time = float_time - LA_GMT;
 
 /****************************Call Back Functions*******************************/
 
@@ -123,6 +128,26 @@
     sscanf(buffer, "%lf", &float_time);
     // pc.printf("%s\r\n", Result);
     redLed = !redLed;
+    
+    int_time = float_time - LA_GMT;
+    
+    time_t timeinfo =(time_t) int_time;
+    timeinfo=(time_t)int_time;
+    sprintf(buffer,"%s",ctime(&timeinfo));
+    
+    token[0] = strtok(buffer, " ");
+    
+    // Keep printing tokens while one of the
+    // delimiters present in str[].
+    int i=0;
+    while (token[i] != NULL) {
+            // printf("[%d] %s\r\n", i, token[i]);
+            i++;
+            token[i] = strtok(NULL, " ");
+        }
+
+
+    
     new_reading = 1;
     
    
@@ -214,21 +239,23 @@
     
     float start_time = BLE_clock.read();
 
-    int int_time = float_time - LA_GMT;
+    
         
     char date_text[20];
     char time_text[10];
     
     
+    update_clock_Thread.start(update_clock);
     
     
     
     while (true) 
     {
-        counter += 1; 
+        // counter += 1; 
         status = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/   
         blueLed = status;
-        
+  
+        /*      
         if (status != BLE_status) {
             if (status == 1) {
                 pc.printf("[%f] BLE is off.\r\n",BLE_clock.read());
@@ -239,10 +266,12 @@
                 BLE_status = 0;
            }
         }
-
+        */
+        
         if (new_reading == 1) {
-            pc.printf("New time: %lf\r\n", float_time);
-            int_time = float_time - LA_GMT;
+            
+            // pc.printf("New time: %lf\r\n", float_time);
+            
             
             new_reading = 0;
             /*minute_2d = (int)time_text[3];
@@ -253,32 +282,89 @@
             
             hour_2d=token[3][0]-'0';
             hour_1d=token[3][1]-'0';
-            minute_2d=token[3][4]-'0';
-            minute_1d=token[3][5]-'0';
-            second_2d=token[3][7]-'0';
-            second_1d=token[3][8]-'0';
+            minute_2d=token[3][3]-'0';
+            minute_1d=token[3][4]-'0';
+            second_2d=token[3][6]-'0';
+            second_1d=token[3][7]-'0';
+            
+            // pc.printf("%s\r\n", token[3]);
+            // sprintf(token1,"%s",token[3]);
+            // pc.printf("%s\r\n", token1);
+            /*
+            for(int i=0;i<strlen(token1);i++)
+            {
+                pc.printf("%c\n",token1[i]);
+                }
+            */
+            // pc.printf("Conversion successful: h2 h1 m2 m1 s2 s1 = %d %d %d %d %d %d\r\n", hour_2d, hour_1d, minute_2d, minute_1d, second_2d, second_1d);
+            
         }
+    
+          sprintf(newtime_text, "%d%d:%d%d:%d%d.%d", hour_2d, hour_1d, minute_2d, minute_1d, second_2d, second_1d, second_dec);
         
+            // sprintf(date_text,"%s %s %s",token[1],token[2],token[4]);
+            // strcpy(time_text,token[3]);
+
+            oled.TextBox((uint8_t *)newtime_text,2,2, 91, 15); 
+            // Label((uint8_t *)date_text,20,20);
+            
+    
+        
+    }
+}
+
+/******************************End of Main*************************************/
+
+void StartHaptic(void)  {
+    hapticTimer.start(50);
+    haptic = 1;
+}
+
+void StopHaptic(void const *n) {
+    haptic = 0;
+    hapticTimer.stop();
+}
+
+void resetBLE(void) {
+        
+    current_time = BLE_clock.read();
+    
+    if (current_time - alert_time > 60) {
+        pc.printf("Haven't received new data in 60 seconds. Resetting BLE\r\n");
+        kw40z_device.ToggleAdvertisementMode();
+        Thread::wait(500);
+        int status = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/
+        
+        while (status == 1) {
+            StartHaptic();
+            kw40z_device.ToggleAdvertisementMode();
+            Thread::wait(5000);
+        }
+    }
+        
+    
+}
+
+void displayTime(void) {
+    
+    // Reminder to move the clock commands from main while(1) to here
+    
+}
+
+void update_clock(void) {
+    
+    t.reset();
+    t.start();
+    
+    while (1) {
+    // Updating time
         
         
-        time_t timeinfo =(time_t) int_time;
-        timeinfo=(time_t)int_time;
-        sprintf(buffer,"%s",ctime(&timeinfo));
-        
-
-        token[0] = strtok(buffer, " ");
-    
-        // Keep printing tokens while one of the
-        // delimiters present in str[].
-        int i=0;
-        while (token[i] != NULL) {
-            // printf("[%d] %s\r\n", i, token[i]);
-            i++;
-            token[i] = strtok(NULL, " ");
+        if (t.read_ms() > 100) {
+            second_dec++;
+            t.reset();
+            t.start();
         }
-
-        // Updating time
-        second_dec++;
         
         if (second_dec>9) {
             second_1d++;
@@ -330,56 +416,10 @@
             second_dec = 0;
         }
         
-        sprintf(newtime_text, "%d%d:%d%d:%d%d.%d", hour_2d, hour_1d, minute_2d, minute_1d, second_2d, second_1d, second_dec);
         
-        sprintf(date_text,"%s %s %s",token[1],token[2],token[4]);
-        strcpy(time_text,token[3]);
-
-        oled.TextBox((uint8_t *)time_text,2,2, 91, 15); 
-        // Label((uint8_t *)date_text,20,20);
-        
-        
-     
-        Thread::wait(100);
         
     }
-}
-
-/******************************End of Main*************************************/
-
-void StartHaptic(void)  {
-    hapticTimer.start(50);
-    haptic = 1;
-}
-
-void StopHaptic(void const *n) {
-    haptic = 0;
-    hapticTimer.stop();
-}
-
-void resetBLE(void) {
-        
-    current_time = BLE_clock.read();
     
-    if (current_time - alert_time > 60) {
-        pc.printf("Haven't received new data in 60 seconds. Resetting BLE\r\n");
-        kw40z_device.ToggleAdvertisementMode();
-        Thread::wait(500);
-        int status = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/
-        
-        while (status == 1) {
-            StartHaptic();
-            kw40z_device.ToggleAdvertisementMode();
-            Thread::wait(5000);
-        }
-    }
-        
     
 }
 
-void displayTime(void) {
-    
-    // Reminder to move the clock commands from main while(1) to here
-    
-}
-