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: Hexi_KW40Z Hexi_OLED_SSD1351
Fork of Hexi_BLE_Time by
main.cpp
00001 #include "mbed.h" 00002 #include "Hexi_KW40Z.h" 00003 #include "Hexi_OLED_SSD1351.h" 00004 #include "OLED_types.h" 00005 #include "OpenSans_Font.h" 00006 #include "string.h" 00007 #include "time.h" 00008 #include <sstream> 00009 00010 00011 #define LED_ON 0 00012 #define LED_OFF 1 00013 00014 void StartHaptic(void); 00015 void StopHaptic(void const *n); 00016 void resetBLE(void); 00017 void displayTime(void); 00018 void update_clock(void); 00019 00020 Serial pc(USBTX, USBRX); 00021 00022 DigitalOut redLed(LED1,1); 00023 DigitalOut greenLed(LED2,1); 00024 DigitalOut blueLed(LED3,1); 00025 DigitalOut haptic(PTB9); 00026 00027 /* Define timer for haptic feedback */ 00028 RtosTimer hapticTimer(StopHaptic, osTimerOnce); 00029 00030 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ 00031 KW40Z kw40z_device(PTE24, PTE25); 00032 00033 /* Instantiate the SSD1351 OLED Driver */ 00034 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */ 00035 00036 /*Create a Thread to handle sending BLE Sensor Data */ 00037 //Thread txThread; 00038 //Thread broadcastThread; 00039 //Thread resetBLE_Thread; 00040 Thread displayTime_Thread; 00041 Thread update_clock_Thread; 00042 00043 /*Create a Timer to reset BLE */ 00044 Timer BLE_clock; 00045 Timer t; 00046 00047 /* Text Buffer */ 00048 char text[20]; 00049 char time_text[20]; 00050 char date_text[25]; 00051 00052 uint8_t battery = 100; 00053 uint8_t light = 0; 00054 uint16_t humidity = 4500; 00055 uint16_t temperature = 2000; 00056 uint16_t pressure = 9000; 00057 uint16_t x = 0; 00058 uint16_t y = 5000; 00059 uint16_t z = 10000; 00060 00061 float current_time; 00062 char token1[15]; 00063 float alert_time = 0; 00064 double float_time = 1528686283; 00065 char buffer[16]; 00066 int new_reading = 0; 00067 int BLE_status = 0; 00068 char time_buffer[30]; 00069 char *smallbuff; 00070 std::time_t timeinfo; 00071 int LA_GMT = 25200; 00072 int second_dec = 0; 00073 int second_1d = 0; 00074 int second_2d = 0; 00075 int minute_1d = 0; 00076 int minute_2d = 0; 00077 int hour_1d = 0; 00078 int hour_2d = 0; 00079 char newtime_text[10]; 00080 char *token[10]; 00081 int int_time = float_time - LA_GMT; 00082 00083 /****************************Call Back Functions*******************************/ 00084 00085 00086 void ButtonRight(void) 00087 { 00088 StartHaptic(); 00089 kw40z_device.ToggleAdvertisementMode(); 00090 } 00091 00092 void ButtonLeft(void) 00093 { 00094 StartHaptic(); 00095 kw40z_device.ToggleAdvertisementMode(); 00096 00097 } 00098 00099 00100 void PassKey(void) 00101 { 00102 StartHaptic(); 00103 strcpy((char *) text,"PAIR CODE"); 00104 oled.TextBox((uint8_t *)text,0,25,95,18); 00105 00106 /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */ 00107 sprintf(text,"%d", kw40z_device.GetPassKey()); 00108 oled.TextBox((uint8_t *)text,0,40,95,18); 00109 } 00110 00111 // Key modification: use the alert functionality enabled by the host-ble interface 00112 // to define our own command. 00113 void AlertReceived(uint8_t *data, uint8_t length) 00114 { 00115 redLed = !redLed; 00116 alert_time = BLE_clock.read(); 00117 00118 /* 00119 data[17] = 0; 00120 data[18] = 0; 00121 data[19] = 0; 00122 data[20] = 0; 00123 */ 00124 // pc.printf("%s\r\n", data); 00125 // pc.printf("%i\r\n", data); 00126 00127 sprintf(buffer, "%s", data); 00128 sscanf(buffer, "%lf", &float_time); 00129 // pc.printf("%s\r\n", Result); 00130 redLed = !redLed; 00131 00132 int_time = float_time - LA_GMT; 00133 00134 time_t timeinfo =(time_t) int_time; 00135 timeinfo=(time_t)int_time; 00136 sprintf(buffer,"%s",ctime(&timeinfo)); 00137 00138 token[0] = strtok(buffer, " "); 00139 00140 // Keep printing tokens while one of the 00141 // delimiters present in str[]. 00142 int i=0; 00143 while (token[i] != NULL) { 00144 // printf("[%d] %s\r\n", i, token[i]); 00145 i++; 00146 token[i] = strtok(NULL, " "); 00147 } 00148 00149 00150 00151 new_reading = 1; 00152 00153 00154 00155 /* 00156 if (data) { 00157 blueLed = LED_OFF; 00158 greenLed = LED_ON; 00159 wait_ms(1000); 00160 greenLed = LED_OFF; 00161 blueLed = LED_ON; 00162 } 00163 */ 00164 00165 /* 00166 // data (our command) must 20 bytes long. 00167 // CMD for turning on: 'ledonledonledonledon' 00168 if (data[4] == 'n') { 00169 greenLed = LED_ON; 00170 redLed = LED_ON; 00171 blueLed = LED_ON; 00172 pc.printf("on\n\r", data); 00173 00174 // CMD for turning off: 'ledoffledoffledoffled' 00175 } else if (data[4] == 'f') { 00176 greenLed = LED_OFF; 00177 redLed = LED_OFF; 00178 blueLed = LED_OFF; 00179 pc.printf("off\n\r", data); 00180 } 00181 */ 00182 00183 } 00184 /***********************End of Call Back Functions*****************************/ 00185 00186 /********************************Main******************************************/ 00187 00188 int main() 00189 { 00190 /* Register callbacks to application functions */ 00191 kw40z_device.attach_buttonLeft(&ButtonLeft); 00192 kw40z_device.attach_buttonRight(&ButtonRight); 00193 kw40z_device.attach_passkey(&PassKey); 00194 kw40z_device.attach_alert(&AlertReceived); 00195 00196 00197 //pc.printf("hello\n\r"); 00198 00199 /* Turn on the backlight of the OLED Display */ 00200 oled.DimScreenON(); 00201 00202 /* Fills the screen with solid black */ 00203 oled.FillScreen(COLOR_BLACK); 00204 00205 /* Get OLED Class Default Text Properties */ 00206 oled_text_properties_t textProperties = {0}; 00207 oled.GetTextProperties(&textProperties); 00208 00209 /* Change font color to Blue */ 00210 textProperties.fontColor = COLOR_BLUE; 00211 oled.SetTextProperties(&textProperties); 00212 00213 /* Display Bluetooth Label at x=17,y=65 */ 00214 strcpy((char *) text,"BLUETOOTH"); 00215 oled.Label((uint8_t *)text,17,65); 00216 00217 /* Change font color to white */ 00218 textProperties.fontColor = COLOR_WHITE; 00219 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00220 oled.SetTextProperties(&textProperties); 00221 00222 /* Display Label at x=22,y=80 */ 00223 strcpy((char *) text,"Tap Below"); 00224 oled.Label((uint8_t *)text,22,80); 00225 00226 // txThread.start(txTask); /*Start transmitting Sensor Tag Data */ 00227 00228 //resetBLE_Thread.start(resetBLE); 00229 00230 // displayTime_Thread.start(displayTime); 00231 00232 00233 00234 int counter = 0; 00235 int status = 0; 00236 00237 BLE_clock.reset(); 00238 BLE_clock.start(); 00239 00240 float start_time = BLE_clock.read(); 00241 00242 00243 00244 char date_text[20]; 00245 char time_text[10]; 00246 00247 00248 update_clock_Thread.start(update_clock); 00249 00250 00251 00252 while (true) 00253 { 00254 // counter += 1; 00255 status = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ 00256 blueLed = status; 00257 00258 /* 00259 if (status != BLE_status) { 00260 if (status == 1) { 00261 pc.printf("[%f] BLE is off.\r\n",BLE_clock.read()); 00262 BLE_status = 1; 00263 } 00264 else { 00265 pc.printf("[%f] BLE is on.\r\n",BLE_clock.read(), status); 00266 BLE_status = 0; 00267 } 00268 } 00269 */ 00270 00271 if (new_reading == 1) { 00272 00273 // pc.printf("New time: %lf\r\n", float_time); 00274 00275 00276 new_reading = 0; 00277 /*minute_2d = (int)time_text[3]; 00278 minute_1d = (int)time_text[4]; 00279 second_2d = (int)time_text[6]; 00280 second_1d = (int)time_text[7]; 00281 second_dec = 0;*/ 00282 00283 hour_2d=token[3][0]-'0'; 00284 hour_1d=token[3][1]-'0'; 00285 minute_2d=token[3][3]-'0'; 00286 minute_1d=token[3][4]-'0'; 00287 second_2d=token[3][6]-'0'; 00288 second_1d=token[3][7]-'0'; 00289 sprintf(newtime_text, "%d%d:%d%d:%d%d.%d", hour_2d, hour_1d, minute_2d, minute_1d, second_2d, second_1d, second_dec); 00290 // pc.printf("%s\r\n", token[3]); 00291 // sprintf(token1,"%s",token[3]); 00292 // pc.printf("%s\r\n", token1); 00293 /* 00294 for(int i=0;i<strlen(token1);i++) 00295 { 00296 pc.printf("%c\n",token1[i]); 00297 } 00298 */ 00299 // 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); 00300 00301 } 00302 00303 //sprintf(newtime_text, "%d%d:%d%d:%d%d.%d", hour_2d, hour_1d, minute_2d, minute_1d, second_2d, second_1d, second_dec); 00304 00305 // sprintf(date_text,"%s %s %s",token[1],token[2],token[4]); 00306 // strcpy(time_text,token[3]); 00307 00308 oled.TextBox((uint8_t *)newtime_text,2,2, 91, 15); 00309 // Label((uint8_t *)date_text,20,20); 00310 00311 00312 00313 } 00314 } 00315 00316 /******************************End of Main*************************************/ 00317 00318 void StartHaptic(void) { 00319 hapticTimer.start(50); 00320 haptic = 1; 00321 } 00322 00323 void StopHaptic(void const *n) { 00324 haptic = 0; 00325 hapticTimer.stop(); 00326 } 00327 00328 void resetBLE(void) { 00329 00330 current_time = BLE_clock.read(); 00331 00332 if (current_time - alert_time > 60) { 00333 pc.printf("Haven't received new data in 60 seconds. Resetting BLE\r\n"); 00334 kw40z_device.ToggleAdvertisementMode(); 00335 Thread::wait(500); 00336 int status = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ 00337 00338 while (status == 1) { 00339 StartHaptic(); 00340 kw40z_device.ToggleAdvertisementMode(); 00341 Thread::wait(5000); 00342 } 00343 } 00344 00345 00346 } 00347 00348 void displayTime(void) { 00349 00350 // Reminder to move the clock commands from main while(1) to here 00351 00352 } 00353 00354 void update_clock(void) { 00355 00356 t.reset(); 00357 t.start(); 00358 00359 while (1) { 00360 // Updating time 00361 00362 00363 if (t.read_ms() > 100) { 00364 second_dec++; 00365 t.reset(); 00366 t.start(); 00367 } 00368 00369 if (second_dec>9) { 00370 second_1d++; 00371 second_dec = 0; 00372 } 00373 00374 if (second_1d>9) { 00375 second_2d++; 00376 second_1d = 0; 00377 second_dec = 0; 00378 } 00379 00380 if (second_2d*10 + second_1d > 59) { 00381 minute_1d++; 00382 second_2d = 0; 00383 second_1d = 0; 00384 second_dec = 0; 00385 } 00386 00387 if (minute_1d>9) { 00388 minute_2d++; 00389 minute_1d = 0; 00390 second_2d = 0; 00391 second_1d = 0; 00392 second_dec = 0; 00393 } 00394 00395 if (minute_2d*10 + minute_1d > 59) { 00396 hour_1d++; 00397 minute_2d = 0; 00398 minute_1d = 0; 00399 second_2d = 0; 00400 second_1d = 0; 00401 second_dec = 0; 00402 } 00403 00404 if (hour_1d > 9) { 00405 hour_2d++; 00406 hour_1d = 0; 00407 } 00408 00409 if (hour_2d == 2 && hour_1d > 3) { 00410 hour_2d = 0; 00411 hour_1d = 0; 00412 minute_2d = 0; 00413 minute_1d = 0; 00414 second_2d = 0; 00415 second_1d = 0; 00416 second_dec = 0; 00417 } 00418 00419 00420 00421 } 00422 00423 00424 } 00425
Generated on Thu Aug 11 2022 23:19:32 by
1.7.2
