Nrithya Theetharappan / Mbed OS Hexi_BLE_Time_copy

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

Fork of Hexi_BLE_Time by Adam S

Committer:
nrithya
Date:
Mon Jun 11 02:02:58 2018 +0000
Revision:
8:25985d2711e0
Parent:
7:88af507a85bd
display to screen

Who changed what in which revision?

UserRevisionLine numberNew contents of line
khuang 0:c80666325948 1 #include "mbed.h"
khuang 0:c80666325948 2 #include "Hexi_KW40Z.h"
khuang 0:c80666325948 3 #include "Hexi_OLED_SSD1351.h"
khuang 0:c80666325948 4 #include "OLED_types.h"
khuang 0:c80666325948 5 #include "OpenSans_Font.h"
khuang 0:c80666325948 6 #include "string.h"
astamb 5:0076f669169f 7 #include "time.h"
khuang 0:c80666325948 8
khuang 0:c80666325948 9 #define LED_ON 0
khuang 0:c80666325948 10 #define LED_OFF 1
cotigac 3:c2ab3a0de448 11
cotigac 3:c2ab3a0de448 12 void UpdateSensorData(void);
khuang 0:c80666325948 13 void StartHaptic(void);
khuang 0:c80666325948 14 void StopHaptic(void const *n);
khuang 1:a0d9eeedb771 15 void txTask(void);
khuang 0:c80666325948 16
xihan94 4:20d4eebfa986 17 Serial pc(USBTX, USBRX);
xihan94 4:20d4eebfa986 18
khuang 0:c80666325948 19 DigitalOut redLed(LED1,1);
khuang 0:c80666325948 20 DigitalOut greenLed(LED2,1);
khuang 0:c80666325948 21 DigitalOut blueLed(LED3,1);
khuang 0:c80666325948 22 DigitalOut haptic(PTB9);
khuang 0:c80666325948 23
khuang 0:c80666325948 24 /* Define timer for haptic feedback */
khuang 0:c80666325948 25 RtosTimer hapticTimer(StopHaptic, osTimerOnce);
khuang 0:c80666325948 26
khuang 0:c80666325948 27 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */
khuang 0:c80666325948 28 KW40Z kw40z_device(PTE24, PTE25);
khuang 0:c80666325948 29
khuang 0:c80666325948 30 /* Instantiate the SSD1351 OLED Driver */
khuang 0:c80666325948 31 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */
khuang 0:c80666325948 32
khuang 1:a0d9eeedb771 33 /*Create a Thread to handle sending BLE Sensor Data */
khuang 1:a0d9eeedb771 34 Thread txThread;
astamb 5:0076f669169f 35 Thread broadcastThread;
khuang 0:c80666325948 36
khuang 1:a0d9eeedb771 37 /* Text Buffer */
khuang 1:a0d9eeedb771 38 char text[20];
astamb 5:0076f669169f 39 char time_text[20];
astamb 5:0076f669169f 40 char date_text[25];
khuang 1:a0d9eeedb771 41
cotigac 3:c2ab3a0de448 42 uint8_t battery = 100;
cotigac 3:c2ab3a0de448 43 uint8_t light = 0;
cotigac 3:c2ab3a0de448 44 uint16_t humidity = 4500;
cotigac 3:c2ab3a0de448 45 uint16_t temperature = 2000;
cotigac 3:c2ab3a0de448 46 uint16_t pressure = 9000;
cotigac 3:c2ab3a0de448 47 uint16_t x = 0;
cotigac 3:c2ab3a0de448 48 uint16_t y = 5000;
cotigac 3:c2ab3a0de448 49 uint16_t z = 10000;
cotigac 3:c2ab3a0de448 50
khuang 1:a0d9eeedb771 51 /****************************Call Back Functions*******************************/
astamb 5:0076f669169f 52
astamb 5:0076f669169f 53
khuang 0:c80666325948 54 void ButtonRight(void)
khuang 0:c80666325948 55 {
khuang 0:c80666325948 56 StartHaptic();
khuang 0:c80666325948 57 kw40z_device.ToggleAdvertisementMode();
khuang 0:c80666325948 58 }
khuang 0:c80666325948 59
khuang 0:c80666325948 60 void ButtonLeft(void)
khuang 0:c80666325948 61 {
khuang 0:c80666325948 62 StartHaptic();
khuang 0:c80666325948 63 kw40z_device.ToggleAdvertisementMode();
astamb 5:0076f669169f 64
khuang 0:c80666325948 65 }
khuang 0:c80666325948 66
astamb 5:0076f669169f 67
khuang 0:c80666325948 68 void PassKey(void)
khuang 0:c80666325948 69 {
khuang 0:c80666325948 70 StartHaptic();
khuang 0:c80666325948 71 strcpy((char *) text,"PAIR CODE");
khuang 1:a0d9eeedb771 72 oled.TextBox((uint8_t *)text,0,25,95,18);
khuang 0:c80666325948 73
khuang 0:c80666325948 74 /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
khuang 0:c80666325948 75 sprintf(text,"%d", kw40z_device.GetPassKey());
khuang 0:c80666325948 76 oled.TextBox((uint8_t *)text,0,40,95,18);
khuang 0:c80666325948 77 }
khuang 1:a0d9eeedb771 78
xihan94 4:20d4eebfa986 79 // Key modification: use the alert functionality enabled by the host-ble interface
xihan94 4:20d4eebfa986 80 // to define our own command.
xihan94 4:20d4eebfa986 81 void AlertReceived(uint8_t *data, uint8_t length)
xihan94 4:20d4eebfa986 82 {
xihan94 4:20d4eebfa986 83 StartHaptic();
xihan94 4:20d4eebfa986 84 data[19] = 0;
nrithya 7:88af507a85bd 85 char buf[20];
nrithya 7:88af507a85bd 86 char buffer[30];
nrithya 7:88af507a85bd 87 char *smallbuff;
xihan94 4:20d4eebfa986 88 pc.printf("%s\n\r", data);
nrithya 7:88af507a85bd 89 for(int i=0;i<20;i++)
nrithya 7:88af507a85bd 90 {
nrithya 7:88af507a85bd 91 printf("%c",data[i]);
nrithya 7:88af507a85bd 92 }
nrithya 7:88af507a85bd 93
astamb 6:ae8a23b0735b 94 if (data) {
nrithya 7:88af507a85bd 95 //time_t seconds = (uint8_t)data;
nrithya 7:88af507a85bd 96 std::time_t timeinfo;
nrithya 7:88af507a85bd 97 //time(&rawtime);
nrithya 7:88af507a85bd 98
nrithya 7:88af507a85bd 99 //timeinfo = data;
nrithya 7:88af507a85bd 100 sprintf(buf,"%s",data);
nrithya 7:88af507a85bd 101 //printf("%s\n",buf);
nrithya 7:88af507a85bd 102 int timedata= atoi(buf);
nrithya 7:88af507a85bd 103 //printf("%d\n",timedata);
nrithya 7:88af507a85bd 104 timeinfo=(time_t)timedata;
nrithya 7:88af507a85bd 105 //printf("%ld\n",long(timeinfo));
nrithya 7:88af507a85bd 106 //printf("Time as a string = %s", ctime(&timeinfo));
nrithya 7:88af507a85bd 107 sprintf(buffer,"%s",ctime(&timeinfo));
nrithya 8:25985d2711e0 108 sprintf(text,"%s"," ");
nrithya 8:25985d2711e0 109 //char str[]="sun jun 10 12:34:33 2018";
nrithya 8:25985d2711e0 110 //printf("%s\n", str);
nrithya 8:25985d2711e0 111 oled.TextBox((uint8_t *)text,1,40,55,15);
nrithya 8:25985d2711e0 112 oled.FillScreen(COLOR_BLACK);
nrithya 8:25985d2711e0 113
nrithya 8:25985d2711e0 114 char *token[10];
nrithya 8:25985d2711e0 115 token[0] = strtok(buffer, " ");
nrithya 8:25985d2711e0 116
nrithya 8:25985d2711e0 117 // Keep printing tokens while one of the
nrithya 8:25985d2711e0 118 // delimiters present in str[].
nrithya 8:25985d2711e0 119 int i=0;
nrithya 8:25985d2711e0 120 while (token[i] != NULL)
nrithya 8:25985d2711e0 121 {
nrithya 8:25985d2711e0 122 printf("%s\n", token[i]);
nrithya 8:25985d2711e0 123 i++;
nrithya 8:25985d2711e0 124 token[i] = strtok(NULL, " ");
nrithya 8:25985d2711e0 125 }
nrithya 8:25985d2711e0 126 char str1[60];
nrithya 8:25985d2711e0 127 char str2[10];
nrithya 8:25985d2711e0 128 //char str[80];
nrithya 8:25985d2711e0 129 /*strcpy (str1,token[0]);
nrithya 8:25985d2711e0 130 strcat (str1,token[1]);
nrithya 8:25985d2711e0 131 strcat (str1,token[2]);
nrithya 8:25985d2711e0 132 strcat (str1,token[4]);*/
nrithya 8:25985d2711e0 133 sprintf(str1,"%s %s %s %s",token[0],token[1],token[2],token[4]);
nrithya 8:25985d2711e0 134 strcpy(str2,token[3]);
nrithya 8:25985d2711e0 135 printf("%s\n %s\n",str1,str2);
nrithya 8:25985d2711e0 136 oled.Label((uint8_t *)str1,1,40);
nrithya 8:25985d2711e0 137 oled.Label((uint8_t *)str2,1,60);
nrithya 8:25985d2711e0 138 /*for(int i=0;i<10;i++){
nrithya 8:25985d2711e0 139 //oled.Label((uint8_t *)str[i],1,40+i);
nrithya 8:25985d2711e0 140 sprintf(str1,"%s",str[i]);
nrithya 8:25985d2711e0 141 printf("%s\n",str1);
nrithya 8:25985d2711e0 142 }
nrithya 8:25985d2711e0 143 printf("%s\n",str1);*/
nrithya 8:25985d2711e0 144 //printf("%s\n", str);
nrithya 8:25985d2711e0 145
nrithya 8:25985d2711e0 146
nrithya 8:25985d2711e0 147
nrithya 7:88af507a85bd 148 printf("%s",buffer);
nrithya 7:88af507a85bd 149 for(int i=0;i<10;i++)
nrithya 7:88af507a85bd 150 {
nrithya 7:88af507a85bd 151 sprintf(smallbuff,"%s",buffer[i]);
nrithya 7:88af507a85bd 152 }
nrithya 7:88af507a85bd 153 printf("%s\n",smallbuff);
nrithya 7:88af507a85bd 154 strcpy((char *) text,"sun jun 10 88");
nrithya 7:88af507a85bd 155 oled.Label((uint8_t *)text,17,65);
astamb 6:ae8a23b0735b 156 blueLed = LED_OFF;
astamb 6:ae8a23b0735b 157 greenLed = LED_ON;
astamb 6:ae8a23b0735b 158 wait_ms(1000);
astamb 6:ae8a23b0735b 159 greenLed = LED_OFF;
astamb 6:ae8a23b0735b 160 blueLed = LED_ON;
astamb 6:ae8a23b0735b 161 }
astamb 6:ae8a23b0735b 162
astamb 6:ae8a23b0735b 163 /*
xihan94 4:20d4eebfa986 164 // data (our command) must 20 bytes long.
xihan94 4:20d4eebfa986 165 // CMD for turning on: 'ledonledonledonledon'
xihan94 4:20d4eebfa986 166 if (data[4] == 'n') {
xihan94 4:20d4eebfa986 167 greenLed = LED_ON;
xihan94 4:20d4eebfa986 168 redLed = LED_ON;
xihan94 4:20d4eebfa986 169 blueLed = LED_ON;
xihan94 4:20d4eebfa986 170 pc.printf("on\n\r", data);
xihan94 4:20d4eebfa986 171
xihan94 4:20d4eebfa986 172 // CMD for turning off: 'ledoffledoffledoffled'
xihan94 4:20d4eebfa986 173 } else if (data[4] == 'f') {
xihan94 4:20d4eebfa986 174 greenLed = LED_OFF;
xihan94 4:20d4eebfa986 175 redLed = LED_OFF;
xihan94 4:20d4eebfa986 176 blueLed = LED_OFF;
xihan94 4:20d4eebfa986 177 pc.printf("off\n\r", data);
xihan94 4:20d4eebfa986 178 }
astamb 6:ae8a23b0735b 179 */
astamb 5:0076f669169f 180
xihan94 4:20d4eebfa986 181 }
khuang 1:a0d9eeedb771 182 /***********************End of Call Back Functions*****************************/
khuang 1:a0d9eeedb771 183
khuang 1:a0d9eeedb771 184 /********************************Main******************************************/
khuang 1:a0d9eeedb771 185
khuang 0:c80666325948 186 int main()
khuang 0:c80666325948 187 {
cotigac 3:c2ab3a0de448 188 /* Register callbacks to application functions */
cotigac 3:c2ab3a0de448 189 kw40z_device.attach_buttonLeft(&ButtonLeft);
cotigac 3:c2ab3a0de448 190 kw40z_device.attach_buttonRight(&ButtonRight);
cotigac 3:c2ab3a0de448 191 kw40z_device.attach_passkey(&PassKey);
xihan94 4:20d4eebfa986 192 kw40z_device.attach_alert(&AlertReceived);
astamb 5:0076f669169f 193
astamb 5:0076f669169f 194
xihan94 4:20d4eebfa986 195 pc.printf("hello\n\r");
xihan94 4:20d4eebfa986 196
khuang 0:c80666325948 197 /* Turn on the backlight of the OLED Display */
khuang 0:c80666325948 198 oled.DimScreenON();
khuang 0:c80666325948 199
khuang 0:c80666325948 200 /* Fills the screen with solid black */
khuang 0:c80666325948 201 oled.FillScreen(COLOR_BLACK);
cotigac 3:c2ab3a0de448 202
cotigac 3:c2ab3a0de448 203 /* Get OLED Class Default Text Properties */
cotigac 3:c2ab3a0de448 204 oled_text_properties_t textProperties = {0};
cotigac 3:c2ab3a0de448 205 oled.GetTextProperties(&textProperties);
khuang 0:c80666325948 206
khuang 0:c80666325948 207 /* Change font color to Blue */
khuang 0:c80666325948 208 textProperties.fontColor = COLOR_BLUE;
khuang 0:c80666325948 209 oled.SetTextProperties(&textProperties);
khuang 0:c80666325948 210
khuang 0:c80666325948 211 /* Display Bluetooth Label at x=17,y=65 */
nrithya 8:25985d2711e0 212 //strcpy((char *) text,"BLUETOOTH");
nrithya 7:88af507a85bd 213 //oled.Label((uint8_t *)text,17,65);
khuang 0:c80666325948 214
khuang 0:c80666325948 215 /* Change font color to white */
nrithya 7:88af507a85bd 216 /*textProperties.fontColor = COLOR_WHITE;
khuang 0:c80666325948 217 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
nrithya 7:88af507a85bd 218 oled.SetTextProperties(&textProperties);*/
khuang 0:c80666325948 219
khuang 1:a0d9eeedb771 220 /* Display Label at x=22,y=80 */
nrithya 8:25985d2711e0 221 //strcpy((char *) text,"Tap Below");
nrithya 7:88af507a85bd 222 //oled.Label((uint8_t *)text,22,80);
astamb 5:0076f669169f 223
astamb 5:0076f669169f 224 // txThread.start(txTask); /*Start transmitting Sensor Tag Data */
astamb 5:0076f669169f 225
astamb 5:0076f669169f 226 int counter = 0;
astamb 5:0076f669169f 227 int status = 0;
khuang 0:c80666325948 228
khuang 1:a0d9eeedb771 229 while (true)
khuang 1:a0d9eeedb771 230 {
astamb 5:0076f669169f 231 counter += 1;
astamb 5:0076f669169f 232 status = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/
astamb 5:0076f669169f 233 blueLed = status;
nrithya 7:88af507a85bd 234 //pc.printf("[%i] blueLed = %i\r\n",counter, status);
astamb 5:0076f669169f 235
nrithya 7:88af507a85bd 236 /*time_t rawtime;
astamb 6:ae8a23b0735b 237 struct tm * timeinfo;
astamb 6:ae8a23b0735b 238 time(&rawtime);
astamb 6:ae8a23b0735b 239 timeinfo = localtime (&rawtime);
astamb 6:ae8a23b0735b 240 const tm *t = localtime(&rawtime); // Convert the unix time to actual time
astamb 6:ae8a23b0735b 241
astamb 5:0076f669169f 242 int h = (t->tm_hour); // The hours
astamb 5:0076f669169f 243 int year = (t->tm_year);
astamb 5:0076f669169f 244
astamb 5:0076f669169f 245 // Format the time
astamb 5:0076f669169f 246 sprintf(time_text,"%d:%d:%d",h, t->tm_min, t->tm_sec);
astamb 5:0076f669169f 247 sprintf(date_text,"%d-%d-%d",year, (t->tm_mon), (t->tm_mday));
astamb 6:ae8a23b0735b 248
astamb 5:0076f669169f 249 char buf[80];
astamb 6:ae8a23b0735b 250
astamb 6:ae8a23b0735b 251 // Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
astamb 6:ae8a23b0735b 252 // ts = *localtime(&rawtime);
astamb 6:ae8a23b0735b 253 // pc.printf ("Current local time and date: %s", asctime(timeinfo));
astamb 6:ae8a23b0735b 254 // strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
astamb 6:ae8a23b0735b 255 // pc.printf("%s\r\n", buf);
astamb 5:0076f669169f 256
astamb 6:ae8a23b0735b 257 /*
astamb 5:0076f669169f 258 pc.printf(time_text);
astamb 5:0076f669169f 259 pc.printf("\r\n");
astamb 5:0076f669169f 260 pc.printf(date_text);
astamb 5:0076f669169f 261 pc.printf("\r\n");
astamb 6:ae8a23b0735b 262 */
astamb 5:0076f669169f 263
astamb 5:0076f669169f 264 // Display the time on screen
nrithya 7:88af507a85bd 265 // oled_status_t SSD1351::TextBox(const uint8_t* text, int8_t xCrd, int8_t yCrd,uint8_t width,uint8_t height)*/
nrithya 8:25985d2711e0 266 //oled.TextBox((uint8_t *)time_text,2,2, 91, 15);
nrithya 8:25985d2711e0 267 //oled.TextBox((uint8_t *)date_text,2,16, 91, 15);
astamb 5:0076f669169f 268 redLed = !redLed;
astamb 5:0076f669169f 269
astamb 5:0076f669169f 270 Thread::wait(500);
astamb 5:0076f669169f 271
astamb 5:0076f669169f 272
astamb 5:0076f669169f 273
khuang 1:a0d9eeedb771 274 }
khuang 1:a0d9eeedb771 275 }
khuang 1:a0d9eeedb771 276
khuang 1:a0d9eeedb771 277 /******************************End of Main*************************************/
khuang 1:a0d9eeedb771 278
khuang 1:a0d9eeedb771 279
khuang 1:a0d9eeedb771 280 /* txTask() transmits the sensor data */
khuang 1:a0d9eeedb771 281 void txTask(void){
khuang 1:a0d9eeedb771 282
khuang 1:a0d9eeedb771 283 while (true)
khuang 1:a0d9eeedb771 284 {
cotigac 3:c2ab3a0de448 285 UpdateSensorData();
khuang 0:c80666325948 286
khuang 1:a0d9eeedb771 287 /*Notify Hexiwear App that it is running Sensor Tag mode*/
khuang 0:c80666325948 288 kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
khuang 0:c80666325948 289
khuang 1:a0d9eeedb771 290 /*The following is sending dummy data over BLE. Replace with real data*/
khuang 1:a0d9eeedb771 291
khuang 0:c80666325948 292 /*Send Battery Level for 20% */
cotigac 3:c2ab3a0de448 293 kw40z_device.SendBatteryLevel(battery);
khuang 0:c80666325948 294
khuang 0:c80666325948 295 /*Send Ambient Light Level at 50% */
cotigac 3:c2ab3a0de448 296 kw40z_device.SendAmbientLight(light);
khuang 0:c80666325948 297
khuang 0:c80666325948 298 /*Send Humidity at 90% */
cotigac 3:c2ab3a0de448 299 kw40z_device.SendHumidity(humidity);
khuang 0:c80666325948 300
khuang 0:c80666325948 301 /*Send Temperature at 25 degrees Celsius */
cotigac 3:c2ab3a0de448 302 kw40z_device.SendTemperature(temperature);
khuang 0:c80666325948 303
khuang 0:c80666325948 304 /*Send Pressure at 100kPA */
cotigac 3:c2ab3a0de448 305 kw40z_device.SendPressure(pressure);
khuang 0:c80666325948 306
khuang 1:a0d9eeedb771 307 /*Send Mag,Accel,Gyro Data. */
cotigac 3:c2ab3a0de448 308 kw40z_device.SendGyro(x,y,z);
cotigac 3:c2ab3a0de448 309 kw40z_device.SendAccel(z,x,y);
cotigac 3:c2ab3a0de448 310 kw40z_device.SendMag(y,z,x);
cotigac 3:c2ab3a0de448 311
khuang 1:a0d9eeedb771 312 Thread::wait(1000);
khuang 0:c80666325948 313 }
khuang 0:c80666325948 314 }
khuang 0:c80666325948 315
astamb 5:0076f669169f 316
cotigac 3:c2ab3a0de448 317 void UpdateSensorData(void)
cotigac 3:c2ab3a0de448 318 {
cotigac 3:c2ab3a0de448 319 battery -= 5;
cotigac 3:c2ab3a0de448 320 if(battery < 5) battery = 100;
cotigac 3:c2ab3a0de448 321
cotigac 3:c2ab3a0de448 322 light += 20;
cotigac 3:c2ab3a0de448 323 if(light > 100) light = 0;
cotigac 3:c2ab3a0de448 324
cotigac 3:c2ab3a0de448 325 humidity += 500;
cotigac 3:c2ab3a0de448 326 if(humidity > 8000) humidity = 2000;
cotigac 3:c2ab3a0de448 327
cotigac 3:c2ab3a0de448 328 temperature -= 200;
cotigac 3:c2ab3a0de448 329 if(temperature < 200) temperature = 4200;
cotigac 3:c2ab3a0de448 330
cotigac 3:c2ab3a0de448 331 pressure += 300;
cotigac 3:c2ab3a0de448 332 if(pressure > 10300) pressure = 7500;
cotigac 3:c2ab3a0de448 333
cotigac 3:c2ab3a0de448 334 x += 1400;
cotigac 3:c2ab3a0de448 335 y -= 2300;
cotigac 3:c2ab3a0de448 336 z += 1700;
cotigac 3:c2ab3a0de448 337 }
cotigac 3:c2ab3a0de448 338
khuang 1:a0d9eeedb771 339 void StartHaptic(void) {
khuang 0:c80666325948 340 hapticTimer.start(50);
khuang 0:c80666325948 341 haptic = 1;
khuang 0:c80666325948 342 }
khuang 0:c80666325948 343
khuang 0:c80666325948 344 void StopHaptic(void const *n) {
khuang 0:c80666325948 345 haptic = 0;
khuang 0:c80666325948 346 hapticTimer.stop();
khuang 0:c80666325948 347 }
khuang 1:a0d9eeedb771 348