![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Code to be run for the project Group Fitness
Dependencies: FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351 MAX30101
Fork of final_project_ee119 by
main.cpp@8:f5a21cfd3833, 2018-06-17 (annotated)
- Committer:
- angelasnail
- Date:
- Sun Jun 17 20:54:03 2018 +0000
- Revision:
- 8:f5a21cfd3833
- Parent:
- 7:e5fcb30509ea
- Child:
- 9:c3f399b835ce
EE119_finalproject
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xihan94 | 0:33686dd26bf9 | 1 | #include "mbed.h" |
xihan94 | 0:33686dd26bf9 | 2 | #include "mbed_events.h" |
catchvibes95 | 4:eb89733b8642 | 3 | #include "Hexi_KW40Z.h" |
catchvibes95 | 4:eb89733b8642 | 4 | #include "Hexi_OLED_SSD1351.h" |
catchvibes95 | 4:eb89733b8642 | 5 | #include "OLED_types.h" |
catchvibes95 | 4:eb89733b8642 | 6 | #include "OpenSans_Font.h" |
catchvibes95 | 4:eb89733b8642 | 7 | #include "string.h" |
catchvibes95 | 4:eb89733b8642 | 8 | #include "FXOS8700.h" |
catchvibes95 | 4:eb89733b8642 | 9 | |
angelasnail | 6:328ef7a29083 | 10 | void StartHaptic(void); |
angelasnail | 6:328ef7a29083 | 11 | void StopHaptic(void const *n); |
angelasnail | 6:328ef7a29083 | 12 | float Filter(int s); |
angelasnail | 6:328ef7a29083 | 13 | void AlertReceived(uint8_t *data, uint8_t length); |
trhackett | 7:e5fcb30509ea | 14 | void clearScreen(); |
angelasnail | 6:328ef7a29083 | 15 | |
trhackett | 7:e5fcb30509ea | 16 | void displayString(); |
catchvibes95 | 4:eb89733b8642 | 17 | void UpdateSensorData(void); |
trhackett | 7:e5fcb30509ea | 18 | void BTTask(void); |
trhackett | 7:e5fcb30509ea | 19 | void dataTask(void); |
trhackett | 7:e5fcb30509ea | 20 | |
trhackett | 7:e5fcb30509ea | 21 | void ButtonLeft(); |
trhackett | 7:e5fcb30509ea | 22 | void ButtonRight(); |
trhackett | 7:e5fcb30509ea | 23 | void ButtonDown(); |
trhackett | 7:e5fcb30509ea | 24 | void ButtonUp(); |
trhackett | 7:e5fcb30509ea | 25 | void ButtonLeft(void); |
catchvibes95 | 4:eb89733b8642 | 26 | |
catchvibes95 | 4:eb89733b8642 | 27 | FXOS8700 accel(PTC11, PTC10); |
catchvibes95 | 4:eb89733b8642 | 28 | |
catchvibes95 | 4:eb89733b8642 | 29 | /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ |
catchvibes95 | 4:eb89733b8642 | 30 | KW40Z kw40z_device(PTE24, PTE25); |
catchvibes95 | 4:eb89733b8642 | 31 | |
catchvibes95 | 4:eb89733b8642 | 32 | /* Instantiate the SSD1351 OLED Driver */ |
trhackett | 7:e5fcb30509ea | 33 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); |
trhackett | 7:e5fcb30509ea | 34 | |
trhackett | 7:e5fcb30509ea | 35 | RtosTimer hapticTimer(StopHaptic, osTimerOnce); |
trhackett | 7:e5fcb30509ea | 36 | DigitalOut haptic(PTB9); |
catchvibes95 | 4:eb89733b8642 | 37 | |
trhackett | 7:e5fcb30509ea | 38 | /* Create a Thread to handle sending BLE Sensor Data */ |
trhackett | 7:e5fcb30509ea | 39 | Thread bluetoothThread; |
trhackett | 7:e5fcb30509ea | 40 | Thread dataThread; |
catchvibes95 | 4:eb89733b8642 | 41 | |
trhackett | 7:e5fcb30509ea | 42 | Thread displayThread; |
trhackett | 7:e5fcb30509ea | 43 | EventQueue displayEventQueue; |
xihan94 | 0:33686dd26bf9 | 44 | |
catchvibes95 | 4:eb89733b8642 | 45 | char text[20]; |
catchvibes95 | 4:eb89733b8642 | 46 | |
catchvibes95 | 4:eb89733b8642 | 47 | // Variables |
catchvibes95 | 4:eb89733b8642 | 48 | float accel_data[3]; // Storage for the data from the sensor |
catchvibes95 | 4:eb89733b8642 | 49 | float accel_rms=0.0; // RMS value from the sensor |
catchvibes95 | 4:eb89733b8642 | 50 | float ax, ay, az; // Integer value from the sensor to be displayed |
catchvibes95 | 4:eb89733b8642 | 51 | const uint8_t *image1; // Pointer for the image1 to be displayed |
catchvibes95 | 4:eb89733b8642 | 52 | char text1[20]; // Text Buffer for dynamic value displayed |
catchvibes95 | 4:eb89733b8642 | 53 | char text2[20]; // Text Buffer for dynamic value displayed |
catchvibes95 | 4:eb89733b8642 | 54 | char text3[20]; // Text Buffer for dynamic value displayed |
catchvibes95 | 4:eb89733b8642 | 55 | float dot; |
catchvibes95 | 4:eb89733b8642 | 56 | float old_acc=0; |
catchvibes95 | 4:eb89733b8642 | 57 | float new_acc=0; |
catchvibes95 | 4:eb89733b8642 | 58 | float old_accx, old_accy, old_accz, old_dot=0.0; |
catchvibes95 | 4:eb89733b8642 | 59 | uint8_t StepNum = 0, StepNumber = 0; |
catchvibes95 | 4:eb89733b8642 | 60 | |
catchvibes95 | 4:eb89733b8642 | 61 | float filter_buf[75]; |
catchvibes95 | 4:eb89733b8642 | 62 | |
trhackett | 7:e5fcb30509ea | 63 | uint8_t result[1]={0}; |
angelasnail | 6:328ef7a29083 | 64 | |
catchvibes95 | 4:eb89733b8642 | 65 | |
trhackett | 7:e5fcb30509ea | 66 | // variable to assist with viewing the other users' data |
trhackett | 7:e5fcb30509ea | 67 | char user[2]; |
trhackett | 7:e5fcb30509ea | 68 | char mean[4]; |
trhackett | 7:e5fcb30509ea | 69 | char max[4]; |
trhackett | 7:e5fcb30509ea | 70 | char min[4]; |
trhackett | 7:e5fcb30509ea | 71 | char steps[6]; |
trhackett | 7:e5fcb30509ea | 72 | // boolean flag that says whether or not we've processed new data from pi |
trhackett | 7:e5fcb30509ea | 73 | // default to true because not data from pi until we ask for it |
trhackett | 7:e5fcb30509ea | 74 | bool processedReceivedData = true; |
catchvibes95 | 4:eb89733b8642 | 75 | |
trhackett | 7:e5fcb30509ea | 76 | int flag = 0; |
trhackett | 7:e5fcb30509ea | 77 | int userChosen = flag; |
catchvibes95 | 4:eb89733b8642 | 78 | |
trhackett | 5:a2f68bbb5400 | 79 | // main() runs in its own thread in the OS |
trhackett | 5:a2f68bbb5400 | 80 | int main() { |
trhackett | 7:e5fcb30509ea | 81 | kw40z_device.attach_buttonLeft(&ButtonLeft); |
trhackett | 7:e5fcb30509ea | 82 | kw40z_device.attach_buttonRight(&ButtonRight); |
trhackett | 7:e5fcb30509ea | 83 | kw40z_device.attach_buttonDown(&ButtonDown); |
trhackett | 7:e5fcb30509ea | 84 | kw40z_device.attach_buttonUp(&ButtonUp); |
trhackett | 7:e5fcb30509ea | 85 | kw40z_device.attach_alert(&AlertReceived); |
trhackett | 7:e5fcb30509ea | 86 | |
trhackett | 5:a2f68bbb5400 | 87 | accel.accel_config(); |
catchvibes95 | 4:eb89733b8642 | 88 | |
trhackett | 7:e5fcb30509ea | 89 | // open up the display queue so that at any point in the program, |
trhackett | 7:e5fcb30509ea | 90 | // we can put things inside of it and they'll be executed eventually |
trhackett | 7:e5fcb30509ea | 91 | displayThread.start(callback(&displayEventQueue, &EventQueue::dispatch_forever)); |
trhackett | 7:e5fcb30509ea | 92 | displayEventQueue.call(&clearScreen); |
catchvibes95 | 4:eb89733b8642 | 93 | |
trhackett | 7:e5fcb30509ea | 94 | dataThread.start(BTTask); |
trhackett | 7:e5fcb30509ea | 95 | bluetoothThread.start(dataTask); |
trhackett | 7:e5fcb30509ea | 96 | |
trhackett | 7:e5fcb30509ea | 97 | wait(osWaitForever); |
catchvibes95 | 4:eb89733b8642 | 98 | } |
xihan94 | 0:33686dd26bf9 | 99 | |
trhackett | 7:e5fcb30509ea | 100 | void BTTask(void){ |
catchvibes95 | 4:eb89733b8642 | 101 | |
catchvibes95 | 4:eb89733b8642 | 102 | while (true) |
catchvibes95 | 4:eb89733b8642 | 103 | { |
angelasnail | 6:328ef7a29083 | 104 | if (kw40z_device.GetLinkState() == 0) { |
angelasnail | 6:328ef7a29083 | 105 | kw40z_device.ToggleAdvertisementMode(); |
angelasnail | 6:328ef7a29083 | 106 | } |
angelasnail | 6:328ef7a29083 | 107 | |
catchvibes95 | 4:eb89733b8642 | 108 | /*Notify Hexiwear App that it is running Sensor Tag mode*/ |
catchvibes95 | 4:eb89733b8642 | 109 | kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); |
angelasnail | 6:328ef7a29083 | 110 | |
angelasnail | 6:328ef7a29083 | 111 | kw40z_device.SendAlert(result, 2); |
catchvibes95 | 4:eb89733b8642 | 112 | kw40z_device.SendBatteryLevel(StepNumber); |
catchvibes95 | 4:eb89733b8642 | 113 | |
trhackett | 7:e5fcb30509ea | 114 | Thread::wait(1000); |
catchvibes95 | 4:eb89733b8642 | 115 | } |
catchvibes95 | 4:eb89733b8642 | 116 | } |
catchvibes95 | 4:eb89733b8642 | 117 | |
trhackett | 7:e5fcb30509ea | 118 | void dataTask(void) { |
trhackett | 7:e5fcb30509ea | 119 | while (true) { |
trhackett | 7:e5fcb30509ea | 120 | if(flag == 1) { |
trhackett | 7:e5fcb30509ea | 121 | result[1] = 1; |
trhackett | 7:e5fcb30509ea | 122 | flag = 0; |
trhackett | 7:e5fcb30509ea | 123 | userChosen = 1; |
trhackett | 7:e5fcb30509ea | 124 | } |
trhackett | 7:e5fcb30509ea | 125 | |
trhackett | 7:e5fcb30509ea | 126 | if(flag == 2) { |
trhackett | 7:e5fcb30509ea | 127 | result[1] = 2; |
trhackett | 7:e5fcb30509ea | 128 | flag = 0; |
trhackett | 7:e5fcb30509ea | 129 | userChosen = 2; |
trhackett | 7:e5fcb30509ea | 130 | } |
trhackett | 7:e5fcb30509ea | 131 | |
trhackett | 7:e5fcb30509ea | 132 | if(flag == 3){ |
trhackett | 7:e5fcb30509ea | 133 | result[1] = 3; |
trhackett | 7:e5fcb30509ea | 134 | flag = 0; |
trhackett | 7:e5fcb30509ea | 135 | userChosen = 3; |
trhackett | 7:e5fcb30509ea | 136 | } |
trhackett | 7:e5fcb30509ea | 137 | |
trhackett | 7:e5fcb30509ea | 138 | accel.acquire_accel_data_g(accel_data); |
trhackett | 7:e5fcb30509ea | 139 | ax = Filter(0); |
trhackett | 7:e5fcb30509ea | 140 | ay = Filter(1); |
trhackett | 7:e5fcb30509ea | 141 | az = Filter(2); |
trhackett | 7:e5fcb30509ea | 142 | wait(0.02); |
trhackett | 7:e5fcb30509ea | 143 | accel_rms = sqrt((ax*ax)+(ay*ay)+(az*az)/3); |
trhackett | 7:e5fcb30509ea | 144 | dot = (old_accx * ax)+(old_accy * ay)+(old_accz * az); |
trhackett | 7:e5fcb30509ea | 145 | old_acc = abs(sqrt(old_accx*old_accx+old_accy*old_accy+old_accz*old_accz)); |
trhackett | 7:e5fcb30509ea | 146 | new_acc = abs(sqrt(ax*ax+ay*ay+az*az)); |
trhackett | 7:e5fcb30509ea | 147 | dot /= (old_acc * new_acc); |
trhackett | 7:e5fcb30509ea | 148 | |
trhackett | 7:e5fcb30509ea | 149 | /* Display Legends */ |
trhackett | 7:e5fcb30509ea | 150 | StepNum = StepNumber; |
trhackett | 7:e5fcb30509ea | 151 | if(abs(dot - old_dot) >= 0.05 && abs(dot - old_dot) <= 0.10) { |
trhackett | 7:e5fcb30509ea | 152 | StepNumber += 1; |
trhackett | 7:e5fcb30509ea | 153 | } |
trhackett | 7:e5fcb30509ea | 154 | |
trhackett | 7:e5fcb30509ea | 155 | old_accx = ax; |
trhackett | 7:e5fcb30509ea | 156 | old_accy = ay; |
trhackett | 7:e5fcb30509ea | 157 | old_accz = az; |
trhackett | 7:e5fcb30509ea | 158 | old_dot = dot; |
trhackett | 7:e5fcb30509ea | 159 | |
trhackett | 7:e5fcb30509ea | 160 | Thread::wait(250); |
angelasnail | 6:328ef7a29083 | 161 | } |
angelasnail | 6:328ef7a29083 | 162 | } |
angelasnail | 6:328ef7a29083 | 163 | |
angelasnail | 6:328ef7a29083 | 164 | void StartHaptic(void) { |
angelasnail | 6:328ef7a29083 | 165 | hapticTimer.start(50); |
angelasnail | 6:328ef7a29083 | 166 | haptic = 1; |
angelasnail | 6:328ef7a29083 | 167 | } |
angelasnail | 6:328ef7a29083 | 168 | |
angelasnail | 6:328ef7a29083 | 169 | void StopHaptic(void const *n) { |
angelasnail | 6:328ef7a29083 | 170 | haptic = 0; |
angelasnail | 6:328ef7a29083 | 171 | hapticTimer.stop(); |
angelasnail | 6:328ef7a29083 | 172 | } |
angelasnail | 6:328ef7a29083 | 173 | |
angelasnail | 6:328ef7a29083 | 174 | float Filter(int s) { |
angelasnail | 6:328ef7a29083 | 175 | accel.acquire_accel_data_g(accel_data); |
angelasnail | 6:328ef7a29083 | 176 | float filter_sum = 0.0; |
angelasnail | 6:328ef7a29083 | 177 | //printf("%d\n\r",s); |
angelasnail | 6:328ef7a29083 | 178 | for(int i = 0; i < 75; i++) |
angelasnail | 6:328ef7a29083 | 179 | { |
angelasnail | 6:328ef7a29083 | 180 | filter_buf[i] = accel_data[s]; |
angelasnail | 6:328ef7a29083 | 181 | //printf("%4.2f\n\r",filter_buf[i]); |
angelasnail | 6:328ef7a29083 | 182 | filter_sum += filter_buf[i]; |
angelasnail | 6:328ef7a29083 | 183 | } |
angelasnail | 6:328ef7a29083 | 184 | return (float)(filter_sum / 75); |
angelasnail | 6:328ef7a29083 | 185 | } |
angelasnail | 6:328ef7a29083 | 186 | |
angelasnail | 6:328ef7a29083 | 187 | // Key modification: use the alert functionality enabled by the host-ble interface |
angelasnail | 6:328ef7a29083 | 188 | // to define our own command. |
angelasnail | 6:328ef7a29083 | 189 | void AlertReceived(uint8_t *data, uint8_t length) { |
angelasnail | 8:f5a21cfd3833 | 190 | processedReceivedData = false; |
angelasnail | 6:328ef7a29083 | 191 | |
angelasnail | 6:328ef7a29083 | 192 | StartHaptic(); |
angelasnail | 6:328ef7a29083 | 193 | data[19] = 0; |
angelasnail | 6:328ef7a29083 | 194 | |
angelasnail | 6:328ef7a29083 | 195 | user[0] = '0' + userChosen; |
angelasnail | 6:328ef7a29083 | 196 | |
angelasnail | 6:328ef7a29083 | 197 | mean[0] = data[0]; |
angelasnail | 6:328ef7a29083 | 198 | mean[1] = data[1]; |
angelasnail | 6:328ef7a29083 | 199 | mean[2] = data[2]; |
angelasnail | 6:328ef7a29083 | 200 | |
angelasnail | 6:328ef7a29083 | 201 | max[0] = data[5]; |
angelasnail | 6:328ef7a29083 | 202 | max[1] = data[6]; |
angelasnail | 6:328ef7a29083 | 203 | max[2] = data[7]; |
angelasnail | 6:328ef7a29083 | 204 | |
angelasnail | 6:328ef7a29083 | 205 | min[0] = data[10]; |
angelasnail | 6:328ef7a29083 | 206 | min[1] = data[11]; |
angelasnail | 6:328ef7a29083 | 207 | min[2] = data[12]; |
angelasnail | 6:328ef7a29083 | 208 | |
angelasnail | 6:328ef7a29083 | 209 | steps[0] = data[15]; |
angelasnail | 6:328ef7a29083 | 210 | steps[1] = data[16]; |
angelasnail | 6:328ef7a29083 | 211 | steps[2] = data[17]; |
angelasnail | 6:328ef7a29083 | 212 | steps[3] = data[18]; |
angelasnail | 6:328ef7a29083 | 213 | steps[4] = data[19]; |
angelasnail | 6:328ef7a29083 | 214 | |
angelasnail | 6:328ef7a29083 | 215 | user[1] = 0; |
angelasnail | 6:328ef7a29083 | 216 | mean[3] = 0; |
angelasnail | 6:328ef7a29083 | 217 | max[3] = 0; |
angelasnail | 6:328ef7a29083 | 218 | min[3] = 0; |
angelasnail | 6:328ef7a29083 | 219 | steps[5] = 0; |
angelasnail | 6:328ef7a29083 | 220 | |
angelasnail | 6:328ef7a29083 | 221 | // if you haven't yet processed the data that pi sent |
angelasnail | 6:328ef7a29083 | 222 | // you in the past, then don't do anything. |
angelasnail | 6:328ef7a29083 | 223 | |
angelasnail | 6:328ef7a29083 | 224 | // 2: queue up the displaying of that string |
angelasnail | 6:328ef7a29083 | 225 | displayEventQueue.call(&displayString); |
trhackett | 7:e5fcb30509ea | 226 | } |
trhackett | 7:e5fcb30509ea | 227 | |
trhackett | 7:e5fcb30509ea | 228 | |
trhackett | 7:e5fcb30509ea | 229 | /****************************Call Back Functions*******************************/ |
trhackett | 7:e5fcb30509ea | 230 | |
trhackett | 7:e5fcb30509ea | 231 | // just write the received data to the screen |
trhackett | 7:e5fcb30509ea | 232 | void displayString() { |
trhackett | 7:e5fcb30509ea | 233 | if (!processedReceivedData) { |
trhackett | 7:e5fcb30509ea | 234 | clearScreen(); |
trhackett | 7:e5fcb30509ea | 235 | |
trhackett | 7:e5fcb30509ea | 236 | processedReceivedData = true; |
trhackett | 7:e5fcb30509ea | 237 | oled_text_properties_t textProperties = {0}; |
trhackett | 7:e5fcb30509ea | 238 | oled.GetTextProperties(&textProperties); |
trhackett | 7:e5fcb30509ea | 239 | |
trhackett | 7:e5fcb30509ea | 240 | textProperties.fontColor = COLOR_BLUE; |
trhackett | 7:e5fcb30509ea | 241 | oled.SetTextProperties(&textProperties); |
trhackett | 7:e5fcb30509ea | 242 | |
trhackett | 7:e5fcb30509ea | 243 | sprintf(text, "USER: %s\0",user); |
trhackett | 7:e5fcb30509ea | 244 | oled.Label((uint8_t*)text,0,0); |
trhackett | 7:e5fcb30509ea | 245 | |
trhackett | 7:e5fcb30509ea | 246 | sprintf(text, "MEAN HR: %s\0",mean); |
trhackett | 7:e5fcb30509ea | 247 | oled.Label((uint8_t*)text,0,15); |
trhackett | 7:e5fcb30509ea | 248 | |
trhackett | 7:e5fcb30509ea | 249 | sprintf(text, "MAX HR: %s\0",max); |
trhackett | 7:e5fcb30509ea | 250 | oled.Label((uint8_t*)text,0,30); |
trhackett | 7:e5fcb30509ea | 251 | |
trhackett | 7:e5fcb30509ea | 252 | sprintf(text, "MIN HR: %s\0",min); |
trhackett | 7:e5fcb30509ea | 253 | oled.Label((uint8_t*)text,0,45); |
trhackett | 7:e5fcb30509ea | 254 | |
trhackett | 7:e5fcb30509ea | 255 | sprintf(text, "STEPS: %s\0",steps); |
trhackett | 7:e5fcb30509ea | 256 | oled.Label((uint8_t*)text,0,60); |
trhackett | 7:e5fcb30509ea | 257 | } |
trhackett | 7:e5fcb30509ea | 258 | } |
trhackett | 7:e5fcb30509ea | 259 | |
trhackett | 7:e5fcb30509ea | 260 | void ButtonUp(void) { |
trhackett | 7:e5fcb30509ea | 261 | StartHaptic(); |
trhackett | 7:e5fcb30509ea | 262 | flag = 1; |
angelasnail | 8:f5a21cfd3833 | 263 | // processedReceivedData = false; |
trhackett | 7:e5fcb30509ea | 264 | } |
trhackett | 7:e5fcb30509ea | 265 | |
trhackett | 7:e5fcb30509ea | 266 | void ButtonDown(void) { |
trhackett | 7:e5fcb30509ea | 267 | StartHaptic(); |
trhackett | 7:e5fcb30509ea | 268 | flag = 2; |
angelasnail | 8:f5a21cfd3833 | 269 | // processedReceivedData = false; |
trhackett | 7:e5fcb30509ea | 270 | } |
trhackett | 7:e5fcb30509ea | 271 | |
trhackett | 7:e5fcb30509ea | 272 | void ButtonRight(void) { |
trhackett | 7:e5fcb30509ea | 273 | StartHaptic(); |
trhackett | 7:e5fcb30509ea | 274 | flag = 3; |
angelasnail | 8:f5a21cfd3833 | 275 | // processedReceivedData = false; |
trhackett | 7:e5fcb30509ea | 276 | } |
trhackett | 7:e5fcb30509ea | 277 | |
trhackett | 7:e5fcb30509ea | 278 | void ButtonLeft(void) { |
trhackett | 7:e5fcb30509ea | 279 | StartHaptic(); |
trhackett | 7:e5fcb30509ea | 280 | kw40z_device.ToggleAdvertisementMode(); |
trhackett | 7:e5fcb30509ea | 281 | } |
trhackett | 7:e5fcb30509ea | 282 | |
trhackett | 7:e5fcb30509ea | 283 | |
trhackett | 7:e5fcb30509ea | 284 | |
trhackett | 7:e5fcb30509ea | 285 | // initialize the screen to black |
trhackett | 7:e5fcb30509ea | 286 | void clearScreen() { |
trhackett | 7:e5fcb30509ea | 287 | oled.FillScreen(COLOR_BLACK); |
trhackett | 5:a2f68bbb5400 | 288 | } |