![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
POTATO
Dependencies: Hexi_KW40Z Hexi_OLED_SSD1351
main.cpp@9:fe52fbcc6187, 2019-05-06 (annotated)
- Committer:
- natasha41575
- Date:
- Mon May 06 05:29:48 2019 +0000
- Revision:
- 9:fe52fbcc6187
- Parent:
- 8:de0336411ac1
POTATO; ;
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" |
khuang | 0:c80666325948 | 7 | |
khuang | 0:c80666325948 | 8 | #define LED_ON 0 |
khuang | 0:c80666325948 | 9 | #define LED_OFF 1 |
cotigac | 3:c2ab3a0de448 | 10 | |
cotigac | 3:c2ab3a0de448 | 11 | void UpdateSensorData(void); |
khuang | 0:c80666325948 | 12 | void StartHaptic(void); |
khuang | 0:c80666325948 | 13 | void StopHaptic(void const *n); |
khuang | 1:a0d9eeedb771 | 14 | void txTask(void); |
khuang | 0:c80666325948 | 15 | |
xihan94 | 5:8b1ff723682d | 16 | static Serial pc(USBTX, USBRX); |
xihan94 | 4:20d4eebfa986 | 17 | |
khuang | 0:c80666325948 | 18 | DigitalOut redLed(LED1,1); |
khuang | 0:c80666325948 | 19 | DigitalOut greenLed(LED2,1); |
khuang | 0:c80666325948 | 20 | DigitalOut blueLed(LED3,1); |
khuang | 0:c80666325948 | 21 | DigitalOut haptic(PTB9); |
khuang | 0:c80666325948 | 22 | |
khuang | 0:c80666325948 | 23 | /* Define timer for haptic feedback */ |
khuang | 0:c80666325948 | 24 | RtosTimer hapticTimer(StopHaptic, osTimerOnce); |
khuang | 0:c80666325948 | 25 | |
khuang | 0:c80666325948 | 26 | /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ |
khuang | 0:c80666325948 | 27 | KW40Z kw40z_device(PTE24, PTE25); |
khuang | 0:c80666325948 | 28 | |
khuang | 0:c80666325948 | 29 | /* Instantiate the SSD1351 OLED Driver */ |
khuang | 0:c80666325948 | 30 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */ |
khuang | 0:c80666325948 | 31 | |
khuang | 1:a0d9eeedb771 | 32 | /*Create a Thread to handle sending BLE Sensor Data */ |
khuang | 1:a0d9eeedb771 | 33 | Thread txThread; |
khuang | 0:c80666325948 | 34 | |
khuang | 1:a0d9eeedb771 | 35 | /* Text Buffer */ |
khuang | 1:a0d9eeedb771 | 36 | char text[20]; |
khuang | 1:a0d9eeedb771 | 37 | |
cotigac | 3:c2ab3a0de448 | 38 | uint8_t battery = 100; |
cotigac | 3:c2ab3a0de448 | 39 | uint8_t light = 0; |
cotigac | 3:c2ab3a0de448 | 40 | uint16_t humidity = 4500; |
cotigac | 3:c2ab3a0de448 | 41 | uint16_t temperature = 2000; |
cotigac | 3:c2ab3a0de448 | 42 | uint16_t pressure = 9000; |
cotigac | 3:c2ab3a0de448 | 43 | uint16_t x = 0; |
cotigac | 3:c2ab3a0de448 | 44 | uint16_t y = 5000; |
cotigac | 3:c2ab3a0de448 | 45 | uint16_t z = 10000; |
natasha41575 | 8:de0336411ac1 | 46 | char seconds = 0; |
natasha41575 | 8:de0336411ac1 | 47 | DigitalOut output(PTA10); |
cotigac | 3:c2ab3a0de448 | 48 | |
khuang | 1:a0d9eeedb771 | 49 | /****************************Call Back Functions*******************************/ |
khuang | 0:c80666325948 | 50 | void ButtonRight(void) |
khuang | 0:c80666325948 | 51 | { |
natasha41575 | 7:3afe21b24e58 | 52 | pc.printf("Right\r\n"); |
khuang | 0:c80666325948 | 53 | StartHaptic(); |
natasha41575 | 8:de0336411ac1 | 54 | |
natasha41575 | 8:de0336411ac1 | 55 | uint8_t buf[6] = "Right"; |
natasha41575 | 8:de0336411ac1 | 56 | kw40z_device.SendMessage(buf, 5); |
natasha41575 | 8:de0336411ac1 | 57 | |
khuang | 0:c80666325948 | 58 | } |
khuang | 0:c80666325948 | 59 | |
khuang | 0:c80666325948 | 60 | void ButtonLeft(void) |
khuang | 0:c80666325948 | 61 | { |
natasha41575 | 7:3afe21b24e58 | 62 | pc.printf("Left\r\n"); |
khuang | 0:c80666325948 | 63 | StartHaptic(); |
natasha41575 | 9:fe52fbcc6187 | 64 | |
natasha41575 | 8:de0336411ac1 | 65 | uint8_t buf[6] = "Left"; |
natasha41575 | 8:de0336411ac1 | 66 | kw40z_device.SendMessage(buf, 5); |
natasha41575 | 8:de0336411ac1 | 67 | |
khuang | 0:c80666325948 | 68 | } |
khuang | 0:c80666325948 | 69 | |
khuang | 0:c80666325948 | 70 | void PassKey(void) |
khuang | 0:c80666325948 | 71 | { |
natasha41575 | 7:3afe21b24e58 | 72 | pc.printf("passkey\n"); |
natasha41575 | 8:de0336411ac1 | 73 | //StartHaptic(); |
khuang | 0:c80666325948 | 74 | strcpy((char *) text,"PAIR CODE"); |
khuang | 1:a0d9eeedb771 | 75 | oled.TextBox((uint8_t *)text,0,25,95,18); |
khuang | 0:c80666325948 | 76 | |
khuang | 0:c80666325948 | 77 | /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */ |
khuang | 0:c80666325948 | 78 | sprintf(text,"%d", kw40z_device.GetPassKey()); |
khuang | 0:c80666325948 | 79 | oled.TextBox((uint8_t *)text,0,40,95,18); |
khuang | 0:c80666325948 | 80 | } |
khuang | 1:a0d9eeedb771 | 81 | |
xihan94 | 4:20d4eebfa986 | 82 | // Key modification: use the alert functionality enabled by the host-ble interface |
xihan94 | 4:20d4eebfa986 | 83 | // to define our own command. |
natasha41575 | 7:3afe21b24e58 | 84 | void alertReceived(uint8_t *data, uint8_t length) |
xihan94 | 4:20d4eebfa986 | 85 | { |
xihan94 | 4:20d4eebfa986 | 86 | StartHaptic(); |
xihan94 | 5:8b1ff723682d | 87 | data[length] = 0; |
xihan94 | 5:8b1ff723682d | 88 | printf("%s\n\r", data); |
natasha41575 | 8:de0336411ac1 | 89 | oled.TextBox((uint8_t *)data,0,40,95,18); |
natasha41575 | 8:de0336411ac1 | 90 | seconds = data[7]; |
natasha41575 | 8:de0336411ac1 | 91 | if ((seconds - '0')%2 == 0) { |
natasha41575 | 8:de0336411ac1 | 92 | output.write(0); |
natasha41575 | 8:de0336411ac1 | 93 | } else { |
natasha41575 | 8:de0336411ac1 | 94 | output.write(1); |
natasha41575 | 8:de0336411ac1 | 95 | } |
xihan94 | 4:20d4eebfa986 | 96 | |
natasha41575 | 7:3afe21b24e58 | 97 | // data (our command) must 20 bytes long. |
natasha41575 | 7:3afe21b24e58 | 98 | // CMD for turning on: 'ledonledonledonledon' |
natasha41575 | 8:de0336411ac1 | 99 | // if (data[4] == 'n') { |
natasha41575 | 8:de0336411ac1 | 100 | // greenLed = LED_ON; |
natasha41575 | 8:de0336411ac1 | 101 | // redLed = LED_ON; |
natasha41575 | 8:de0336411ac1 | 102 | // blueLed = LED_ON; |
natasha41575 | 8:de0336411ac1 | 103 | // pc.printf("on\n\r", data); |
natasha41575 | 8:de0336411ac1 | 104 | // |
natasha41575 | 8:de0336411ac1 | 105 | // //CMD for turning off: 'ledoffledoffledoffled' |
natasha41575 | 8:de0336411ac1 | 106 | // } else if (data[4] == 'f') { |
natasha41575 | 8:de0336411ac1 | 107 | // greenLed = LED_OFF; |
natasha41575 | 8:de0336411ac1 | 108 | // redLed = LED_OFF; |
natasha41575 | 8:de0336411ac1 | 109 | // blueLed = LED_OFF; |
natasha41575 | 8:de0336411ac1 | 110 | // pc.printf("off\n\r", data); |
natasha41575 | 8:de0336411ac1 | 111 | // } |
xihan94 | 4:20d4eebfa986 | 112 | } |
khuang | 1:a0d9eeedb771 | 113 | /***********************End of Call Back Functions*****************************/ |
khuang | 1:a0d9eeedb771 | 114 | |
khuang | 1:a0d9eeedb771 | 115 | /********************************Main******************************************/ |
khuang | 1:a0d9eeedb771 | 116 | |
khuang | 0:c80666325948 | 117 | int main() |
khuang | 0:c80666325948 | 118 | { |
natasha41575 | 8:de0336411ac1 | 119 | pc.baud(9600); |
natasha41575 | 8:de0336411ac1 | 120 | pc.printf("hello\n\r"); |
natasha41575 | 8:de0336411ac1 | 121 | |
cotigac | 3:c2ab3a0de448 | 122 | /* Register callbacks to application functions */ |
cotigac | 3:c2ab3a0de448 | 123 | kw40z_device.attach_buttonLeft(&ButtonLeft); |
cotigac | 3:c2ab3a0de448 | 124 | kw40z_device.attach_buttonRight(&ButtonRight); |
cotigac | 3:c2ab3a0de448 | 125 | kw40z_device.attach_passkey(&PassKey); |
natasha41575 | 7:3afe21b24e58 | 126 | kw40z_device.attach_alert(&alertReceived); |
natasha41575 | 8:de0336411ac1 | 127 | |
natasha41575 | 9:fe52fbcc6187 | 128 | while (kw40z_device.GetAdvertisementMode() != 0) { |
natasha41575 | 9:fe52fbcc6187 | 129 | kw40z_device.ToggleAdvertisementMode(); |
natasha41575 | 9:fe52fbcc6187 | 130 | } |
natasha41575 | 9:fe52fbcc6187 | 131 | |
natasha41575 | 9:fe52fbcc6187 | 132 | |
natasha41575 | 8:de0336411ac1 | 133 | memset(text, 0, sizeof(char)*20); |
khuang | 0:c80666325948 | 134 | |
xihan94 | 4:20d4eebfa986 | 135 | |
khuang | 0:c80666325948 | 136 | /* Turn on the backlight of the OLED Display */ |
khuang | 0:c80666325948 | 137 | oled.DimScreenON(); |
khuang | 0:c80666325948 | 138 | |
khuang | 0:c80666325948 | 139 | /* Fills the screen with solid black */ |
khuang | 0:c80666325948 | 140 | oled.FillScreen(COLOR_BLACK); |
cotigac | 3:c2ab3a0de448 | 141 | |
cotigac | 3:c2ab3a0de448 | 142 | /* Get OLED Class Default Text Properties */ |
cotigac | 3:c2ab3a0de448 | 143 | oled_text_properties_t textProperties = {0}; |
cotigac | 3:c2ab3a0de448 | 144 | oled.GetTextProperties(&textProperties); |
khuang | 0:c80666325948 | 145 | |
khuang | 0:c80666325948 | 146 | /* Change font color to Blue */ |
khuang | 0:c80666325948 | 147 | textProperties.fontColor = COLOR_BLUE; |
khuang | 0:c80666325948 | 148 | oled.SetTextProperties(&textProperties); |
khuang | 0:c80666325948 | 149 | |
khuang | 0:c80666325948 | 150 | /* Display Bluetooth Label at x=17,y=65 */ |
khuang | 0:c80666325948 | 151 | strcpy((char *) text,"BLUETOOTH"); |
khuang | 0:c80666325948 | 152 | oled.Label((uint8_t *)text,17,65); |
khuang | 0:c80666325948 | 153 | |
khuang | 0:c80666325948 | 154 | /* Change font color to white */ |
khuang | 0:c80666325948 | 155 | textProperties.fontColor = COLOR_WHITE; |
khuang | 0:c80666325948 | 156 | textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; |
khuang | 0:c80666325948 | 157 | oled.SetTextProperties(&textProperties); |
khuang | 0:c80666325948 | 158 | |
khuang | 1:a0d9eeedb771 | 159 | /* Display Label at x=22,y=80 */ |
khuang | 0:c80666325948 | 160 | strcpy((char *) text,"Tap Below"); |
khuang | 0:c80666325948 | 161 | oled.Label((uint8_t *)text,22,80); |
khuang | 0:c80666325948 | 162 | |
khuang | 1:a0d9eeedb771 | 163 | uint8_t prevLinkState = 0; |
cotigac | 3:c2ab3a0de448 | 164 | uint8_t currLinkState = 0; |
cotigac | 3:c2ab3a0de448 | 165 | |
khuang | 1:a0d9eeedb771 | 166 | txThread.start(txTask); /*Start transmitting Sensor Tag Data */ |
khuang | 0:c80666325948 | 167 | |
natasha41575 | 7:3afe21b24e58 | 168 | int i = 0; |
khuang | 1:a0d9eeedb771 | 169 | while (true) |
khuang | 1:a0d9eeedb771 | 170 | { |
natasha41575 | 8:de0336411ac1 | 171 | if (i % 20 == 0) |
natasha41575 | 8:de0336411ac1 | 172 | // strcpy((char *) text,"potato"); |
natasha41575 | 8:de0336411ac1 | 173 | // text[0] = 'a' + (i%26); |
natasha41575 | 8:de0336411ac1 | 174 | // oled.Label((uint8_t *)text,17,65); |
natasha41575 | 7:3afe21b24e58 | 175 | pc.printf("still here, %d\n\r", i); |
natasha41575 | 8:de0336411ac1 | 176 | // } |
natasha41575 | 8:de0336411ac1 | 177 | i = (i + 1); |
xihan94 | 4:20d4eebfa986 | 178 | // blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
khuang | 1:a0d9eeedb771 | 179 | Thread::wait(50); |
khuang | 1:a0d9eeedb771 | 180 | } |
khuang | 1:a0d9eeedb771 | 181 | } |
khuang | 1:a0d9eeedb771 | 182 | |
khuang | 1:a0d9eeedb771 | 183 | /******************************End of Main*************************************/ |
khuang | 1:a0d9eeedb771 | 184 | |
khuang | 1:a0d9eeedb771 | 185 | |
khuang | 1:a0d9eeedb771 | 186 | /* txTask() transmits the sensor data */ |
natasha41575 | 8:de0336411ac1 | 187 | void txTask(void) { |
khuang | 1:a0d9eeedb771 | 188 | |
khuang | 1:a0d9eeedb771 | 189 | while (true) |
khuang | 1:a0d9eeedb771 | 190 | { |
cotigac | 3:c2ab3a0de448 | 191 | UpdateSensorData(); |
khuang | 0:c80666325948 | 192 | |
khuang | 1:a0d9eeedb771 | 193 | /*Notify Hexiwear App that it is running Sensor Tag mode*/ |
khuang | 0:c80666325948 | 194 | kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); |
khuang | 0:c80666325948 | 195 | |
khuang | 1:a0d9eeedb771 | 196 | /*The following is sending dummy data over BLE. Replace with real data*/ |
khuang | 1:a0d9eeedb771 | 197 | |
khuang | 0:c80666325948 | 198 | /*Send Battery Level for 20% */ |
cotigac | 3:c2ab3a0de448 | 199 | kw40z_device.SendBatteryLevel(battery); |
khuang | 0:c80666325948 | 200 | |
khuang | 0:c80666325948 | 201 | /*Send Ambient Light Level at 50% */ |
cotigac | 3:c2ab3a0de448 | 202 | kw40z_device.SendAmbientLight(light); |
khuang | 0:c80666325948 | 203 | |
khuang | 0:c80666325948 | 204 | /*Send Humidity at 90% */ |
cotigac | 3:c2ab3a0de448 | 205 | kw40z_device.SendHumidity(humidity); |
khuang | 0:c80666325948 | 206 | |
khuang | 0:c80666325948 | 207 | /*Send Temperature at 25 degrees Celsius */ |
cotigac | 3:c2ab3a0de448 | 208 | kw40z_device.SendTemperature(temperature); |
khuang | 0:c80666325948 | 209 | |
khuang | 0:c80666325948 | 210 | /*Send Pressure at 100kPA */ |
cotigac | 3:c2ab3a0de448 | 211 | kw40z_device.SendPressure(pressure); |
khuang | 0:c80666325948 | 212 | |
khuang | 1:a0d9eeedb771 | 213 | /*Send Mag,Accel,Gyro Data. */ |
cotigac | 3:c2ab3a0de448 | 214 | kw40z_device.SendGyro(x,y,z); |
cotigac | 3:c2ab3a0de448 | 215 | kw40z_device.SendAccel(z,x,y); |
cotigac | 3:c2ab3a0de448 | 216 | kw40z_device.SendMag(y,z,x); |
xihan94 | 5:8b1ff723682d | 217 | |
natasha41575 | 8:de0336411ac1 | 218 | |
xihan94 | 5:8b1ff723682d | 219 | uint8_t buf[6] = "Hello"; |
xihan94 | 5:8b1ff723682d | 220 | kw40z_device.SendMessage(buf, 5); |
cotigac | 3:c2ab3a0de448 | 221 | |
khuang | 1:a0d9eeedb771 | 222 | Thread::wait(1000); |
khuang | 0:c80666325948 | 223 | } |
khuang | 0:c80666325948 | 224 | } |
khuang | 0:c80666325948 | 225 | |
cotigac | 3:c2ab3a0de448 | 226 | void UpdateSensorData(void) |
cotigac | 3:c2ab3a0de448 | 227 | { |
cotigac | 3:c2ab3a0de448 | 228 | battery -= 5; |
cotigac | 3:c2ab3a0de448 | 229 | if(battery < 5) battery = 100; |
cotigac | 3:c2ab3a0de448 | 230 | |
cotigac | 3:c2ab3a0de448 | 231 | light += 20; |
cotigac | 3:c2ab3a0de448 | 232 | if(light > 100) light = 0; |
cotigac | 3:c2ab3a0de448 | 233 | |
cotigac | 3:c2ab3a0de448 | 234 | humidity += 500; |
cotigac | 3:c2ab3a0de448 | 235 | if(humidity > 8000) humidity = 2000; |
cotigac | 3:c2ab3a0de448 | 236 | |
cotigac | 3:c2ab3a0de448 | 237 | temperature -= 200; |
cotigac | 3:c2ab3a0de448 | 238 | if(temperature < 200) temperature = 4200; |
cotigac | 3:c2ab3a0de448 | 239 | |
cotigac | 3:c2ab3a0de448 | 240 | pressure += 300; |
cotigac | 3:c2ab3a0de448 | 241 | if(pressure > 10300) pressure = 7500; |
cotigac | 3:c2ab3a0de448 | 242 | |
cotigac | 3:c2ab3a0de448 | 243 | x += 1400; |
cotigac | 3:c2ab3a0de448 | 244 | y -= 2300; |
cotigac | 3:c2ab3a0de448 | 245 | z += 1700; |
cotigac | 3:c2ab3a0de448 | 246 | } |
cotigac | 3:c2ab3a0de448 | 247 | |
khuang | 1:a0d9eeedb771 | 248 | void StartHaptic(void) { |
khuang | 0:c80666325948 | 249 | hapticTimer.start(50); |
khuang | 0:c80666325948 | 250 | haptic = 1; |
khuang | 0:c80666325948 | 251 | } |
khuang | 0:c80666325948 | 252 | |
khuang | 0:c80666325948 | 253 | void StopHaptic(void const *n) { |
khuang | 0:c80666325948 | 254 | haptic = 0; |
khuang | 0:c80666325948 | 255 | hapticTimer.stop(); |
khuang | 0:c80666325948 | 256 | } |
khuang | 1:a0d9eeedb771 | 257 |