software opt

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

Fork of Hexi_BLE_Time by Adam S

Committer:
astamb
Date:
Sat Jun 09 16:56:45 2018 +0000
Revision:
5:0076f669169f
Parent:
4:20d4eebfa986
Child:
6:ae8a23b0735b
hw3

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;
xihan94 4:20d4eebfa986 85 pc.printf("%s\n\r", data);
xihan94 4:20d4eebfa986 86
xihan94 4:20d4eebfa986 87 // data (our command) must 20 bytes long.
xihan94 4:20d4eebfa986 88 // CMD for turning on: 'ledonledonledonledon'
xihan94 4:20d4eebfa986 89 if (data[4] == 'n') {
xihan94 4:20d4eebfa986 90 greenLed = LED_ON;
xihan94 4:20d4eebfa986 91 redLed = LED_ON;
xihan94 4:20d4eebfa986 92 blueLed = LED_ON;
xihan94 4:20d4eebfa986 93 pc.printf("on\n\r", data);
xihan94 4:20d4eebfa986 94
xihan94 4:20d4eebfa986 95 // CMD for turning off: 'ledoffledoffledoffled'
xihan94 4:20d4eebfa986 96 } else if (data[4] == 'f') {
xihan94 4:20d4eebfa986 97 greenLed = LED_OFF;
xihan94 4:20d4eebfa986 98 redLed = LED_OFF;
xihan94 4:20d4eebfa986 99 blueLed = LED_OFF;
xihan94 4:20d4eebfa986 100 pc.printf("off\n\r", data);
xihan94 4:20d4eebfa986 101 }
astamb 5:0076f669169f 102
xihan94 4:20d4eebfa986 103 }
khuang 1:a0d9eeedb771 104 /***********************End of Call Back Functions*****************************/
khuang 1:a0d9eeedb771 105
khuang 1:a0d9eeedb771 106 /********************************Main******************************************/
khuang 1:a0d9eeedb771 107
khuang 0:c80666325948 108 int main()
khuang 0:c80666325948 109 {
cotigac 3:c2ab3a0de448 110 /* Register callbacks to application functions */
cotigac 3:c2ab3a0de448 111 kw40z_device.attach_buttonLeft(&ButtonLeft);
cotigac 3:c2ab3a0de448 112 kw40z_device.attach_buttonRight(&ButtonRight);
cotigac 3:c2ab3a0de448 113 kw40z_device.attach_passkey(&PassKey);
xihan94 4:20d4eebfa986 114 kw40z_device.attach_alert(&AlertReceived);
astamb 5:0076f669169f 115
astamb 5:0076f669169f 116
xihan94 4:20d4eebfa986 117 pc.printf("hello\n\r");
xihan94 4:20d4eebfa986 118
khuang 0:c80666325948 119 /* Turn on the backlight of the OLED Display */
khuang 0:c80666325948 120 oled.DimScreenON();
khuang 0:c80666325948 121
khuang 0:c80666325948 122 /* Fills the screen with solid black */
khuang 0:c80666325948 123 oled.FillScreen(COLOR_BLACK);
cotigac 3:c2ab3a0de448 124
cotigac 3:c2ab3a0de448 125 /* Get OLED Class Default Text Properties */
cotigac 3:c2ab3a0de448 126 oled_text_properties_t textProperties = {0};
cotigac 3:c2ab3a0de448 127 oled.GetTextProperties(&textProperties);
khuang 0:c80666325948 128
khuang 0:c80666325948 129 /* Change font color to Blue */
khuang 0:c80666325948 130 textProperties.fontColor = COLOR_BLUE;
khuang 0:c80666325948 131 oled.SetTextProperties(&textProperties);
khuang 0:c80666325948 132
khuang 0:c80666325948 133 /* Display Bluetooth Label at x=17,y=65 */
khuang 0:c80666325948 134 strcpy((char *) text,"BLUETOOTH");
khuang 0:c80666325948 135 oled.Label((uint8_t *)text,17,65);
khuang 0:c80666325948 136
khuang 0:c80666325948 137 /* Change font color to white */
khuang 0:c80666325948 138 textProperties.fontColor = COLOR_WHITE;
khuang 0:c80666325948 139 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
khuang 0:c80666325948 140 oled.SetTextProperties(&textProperties);
khuang 0:c80666325948 141
khuang 1:a0d9eeedb771 142 /* Display Label at x=22,y=80 */
khuang 0:c80666325948 143 strcpy((char *) text,"Tap Below");
khuang 0:c80666325948 144 oled.Label((uint8_t *)text,22,80);
astamb 5:0076f669169f 145
astamb 5:0076f669169f 146 // txThread.start(txTask); /*Start transmitting Sensor Tag Data */
astamb 5:0076f669169f 147
astamb 5:0076f669169f 148 int counter = 0;
astamb 5:0076f669169f 149 int status = 0;
khuang 0:c80666325948 150
khuang 1:a0d9eeedb771 151 while (true)
khuang 1:a0d9eeedb771 152 {
astamb 5:0076f669169f 153 counter += 1;
astamb 5:0076f669169f 154 status = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/
astamb 5:0076f669169f 155 blueLed = status;
astamb 5:0076f669169f 156 pc.printf("[%i] blueLed = %i\r\n",counter, status);
astamb 5:0076f669169f 157
astamb 5:0076f669169f 158 time_t seconds = time(&seconds);
astamb 5:0076f669169f 159 const tm *t = localtime(&seconds); // Convert the unix time to actual time
astamb 5:0076f669169f 160 /*
astamb 5:0076f669169f 161 int h = (t->tm_hour); // The hours
astamb 5:0076f669169f 162 int year = (t->tm_year);
astamb 5:0076f669169f 163
astamb 5:0076f669169f 164 // Format the time
astamb 5:0076f669169f 165 sprintf(time_text,"%d:%d:%d",h, t->tm_min, t->tm_sec);
astamb 5:0076f669169f 166 sprintf(date_text,"%d-%d-%d",year, (t->tm_mon), (t->tm_mday));
astamb 5:0076f669169f 167 */
astamb 5:0076f669169f 168 char buf[80];
astamb 5:0076f669169f 169 struct tm ts;
astamb 5:0076f669169f 170 // Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
astamb 5:0076f669169f 171 ts = *localtime(&seconds);
astamb 5:0076f669169f 172 strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
astamb 5:0076f669169f 173 printf("%s\r\n", buf);
astamb 5:0076f669169f 174
astamb 5:0076f669169f 175 pc.printf(time_text);
astamb 5:0076f669169f 176 pc.printf("\r\n");
astamb 5:0076f669169f 177 pc.printf(date_text);
astamb 5:0076f669169f 178 pc.printf("\r\n");
astamb 5:0076f669169f 179
astamb 5:0076f669169f 180 // Display the time on screen
astamb 5:0076f669169f 181 // oled_status_t SSD1351::TextBox(const uint8_t* text, int8_t xCrd, int8_t yCrd,uint8_t width,uint8_t height)
astamb 5:0076f669169f 182 oled.TextBox((uint8_t *)time_text,2,2, 91, 15);
astamb 5:0076f669169f 183 oled.TextBox((uint8_t *)date_text,2,16, 91, 15);
astamb 5:0076f669169f 184 redLed = !redLed;
astamb 5:0076f669169f 185
astamb 5:0076f669169f 186 Thread::wait(500);
astamb 5:0076f669169f 187
astamb 5:0076f669169f 188
astamb 5:0076f669169f 189
khuang 1:a0d9eeedb771 190 }
khuang 1:a0d9eeedb771 191 }
khuang 1:a0d9eeedb771 192
khuang 1:a0d9eeedb771 193 /******************************End of Main*************************************/
khuang 1:a0d9eeedb771 194
khuang 1:a0d9eeedb771 195
khuang 1:a0d9eeedb771 196 /* txTask() transmits the sensor data */
khuang 1:a0d9eeedb771 197 void txTask(void){
khuang 1:a0d9eeedb771 198
khuang 1:a0d9eeedb771 199 while (true)
khuang 1:a0d9eeedb771 200 {
cotigac 3:c2ab3a0de448 201 UpdateSensorData();
khuang 0:c80666325948 202
khuang 1:a0d9eeedb771 203 /*Notify Hexiwear App that it is running Sensor Tag mode*/
khuang 0:c80666325948 204 kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
khuang 0:c80666325948 205
khuang 1:a0d9eeedb771 206 /*The following is sending dummy data over BLE. Replace with real data*/
khuang 1:a0d9eeedb771 207
khuang 0:c80666325948 208 /*Send Battery Level for 20% */
cotigac 3:c2ab3a0de448 209 kw40z_device.SendBatteryLevel(battery);
khuang 0:c80666325948 210
khuang 0:c80666325948 211 /*Send Ambient Light Level at 50% */
cotigac 3:c2ab3a0de448 212 kw40z_device.SendAmbientLight(light);
khuang 0:c80666325948 213
khuang 0:c80666325948 214 /*Send Humidity at 90% */
cotigac 3:c2ab3a0de448 215 kw40z_device.SendHumidity(humidity);
khuang 0:c80666325948 216
khuang 0:c80666325948 217 /*Send Temperature at 25 degrees Celsius */
cotigac 3:c2ab3a0de448 218 kw40z_device.SendTemperature(temperature);
khuang 0:c80666325948 219
khuang 0:c80666325948 220 /*Send Pressure at 100kPA */
cotigac 3:c2ab3a0de448 221 kw40z_device.SendPressure(pressure);
khuang 0:c80666325948 222
khuang 1:a0d9eeedb771 223 /*Send Mag,Accel,Gyro Data. */
cotigac 3:c2ab3a0de448 224 kw40z_device.SendGyro(x,y,z);
cotigac 3:c2ab3a0de448 225 kw40z_device.SendAccel(z,x,y);
cotigac 3:c2ab3a0de448 226 kw40z_device.SendMag(y,z,x);
cotigac 3:c2ab3a0de448 227
khuang 1:a0d9eeedb771 228 Thread::wait(1000);
khuang 0:c80666325948 229 }
khuang 0:c80666325948 230 }
khuang 0:c80666325948 231
astamb 5:0076f669169f 232
cotigac 3:c2ab3a0de448 233 void UpdateSensorData(void)
cotigac 3:c2ab3a0de448 234 {
cotigac 3:c2ab3a0de448 235 battery -= 5;
cotigac 3:c2ab3a0de448 236 if(battery < 5) battery = 100;
cotigac 3:c2ab3a0de448 237
cotigac 3:c2ab3a0de448 238 light += 20;
cotigac 3:c2ab3a0de448 239 if(light > 100) light = 0;
cotigac 3:c2ab3a0de448 240
cotigac 3:c2ab3a0de448 241 humidity += 500;
cotigac 3:c2ab3a0de448 242 if(humidity > 8000) humidity = 2000;
cotigac 3:c2ab3a0de448 243
cotigac 3:c2ab3a0de448 244 temperature -= 200;
cotigac 3:c2ab3a0de448 245 if(temperature < 200) temperature = 4200;
cotigac 3:c2ab3a0de448 246
cotigac 3:c2ab3a0de448 247 pressure += 300;
cotigac 3:c2ab3a0de448 248 if(pressure > 10300) pressure = 7500;
cotigac 3:c2ab3a0de448 249
cotigac 3:c2ab3a0de448 250 x += 1400;
cotigac 3:c2ab3a0de448 251 y -= 2300;
cotigac 3:c2ab3a0de448 252 z += 1700;
cotigac 3:c2ab3a0de448 253 }
cotigac 3:c2ab3a0de448 254
khuang 1:a0d9eeedb771 255 void StartHaptic(void) {
khuang 0:c80666325948 256 hapticTimer.start(50);
khuang 0:c80666325948 257 haptic = 1;
khuang 0:c80666325948 258 }
khuang 0:c80666325948 259
khuang 0:c80666325948 260 void StopHaptic(void const *n) {
khuang 0:c80666325948 261 haptic = 0;
khuang 0:c80666325948 262 hapticTimer.stop();
khuang 0:c80666325948 263 }
khuang 1:a0d9eeedb771 264