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