Code to be run for the project Group Fitness
Dependencies: FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351 MAX30101
Fork of final_project_ee119 by
Diff: main.cpp
- Revision:
- 6:328ef7a29083
- Parent:
- 5:a2f68bbb5400
- Child:
- 7:e5fcb30509ea
--- a/main.cpp Fri Jun 15 03:12:45 2018 +0000 +++ b/main.cpp Fri Jun 15 03:38:32 2018 +0000 @@ -7,6 +7,11 @@ #include "string.h" #include "FXOS8700.h" +void StartHaptic(void); +void StopHaptic(void const *n); +float Filter(int s); +void AlertReceived(uint8_t *data, uint8_t length); + void UpdateSensorData(void); void txTask(void); @@ -41,20 +46,52 @@ float filter_buf[75]; + + /****************************Call Back Functions*******************************/ +void ButtonUp(void) { + StartHaptic(); + flag=1; +} + +void ButtonDown(void) { + StartHaptic(); + flag=2; +} + +void ButtonRight(void) { + StartHaptic(); + flag=3; +} + -float Filter(int s) -{ - accel.acquire_accel_data_g(accel_data); - float filter_sum = 0.0; - //printf("%d\n\r",s); - for(int i = 0; i < 75; i++) - { - filter_buf[i] = accel_data[s]; - //printf("%4.2f\n\r",filter_buf[i]); - filter_sum += filter_buf[i]; +// just write the received data to the screen +void displayString() { + if (!processedReceivedData) { + clearScreen(); + + processedReceivedData = true; + oled_text_properties_t textProperties = {0}; + oled.GetTextProperties(&textProperties); + + textProperties.fontColor = COLOR_BLUE; + oled.SetTextProperties(&textProperties); + + sprintf(text, "USER: %s\0",user); + oled.Label((uint8_t*)text,0,0); + + sprintf(text, "MEAN HR: %s\0",mean); + oled.Label((uint8_t*)text,0,15); + + sprintf(text, "MAX HR: %s\0",max); + oled.Label((uint8_t*)text,0,30); + + sprintf(text, "MIN HR: %s\0",min); + oled.Label((uint8_t*)text,0,45); + + sprintf(text, "STEPS: %s\0",steps); + oled.Label((uint8_t*)text,0,60); } - return (float)(filter_sum / 75); } @@ -73,12 +110,16 @@ while (true) { + if (kw40z_device.GetLinkState() == 0) { + kw40z_device.ToggleAdvertisementMode(); + } + UpdateSensorData(); /*Notify Hexiwear App that it is running Sensor Tag mode*/ kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); - - /*Send Battery Level for 20% */ + + kw40z_device.SendAlert(result, 2); kw40z_device.SendBatteryLevel(StepNumber); Thread::wait(10); @@ -87,6 +128,24 @@ void UpdateSensorData(void) { + if(flag == 1) { + result[1]=1; + flag=0; + userChosen = 1; + } + + if(flag == 2) { + result[1]=2; + flag=0; + userChosen = 2; + } + + if(flag == 3){ + result[1]=3; + flag=0; + userChosen = 3; + } + accel.acquire_accel_data_g(accel_data); ax = Filter(0); ay = Filter(1); @@ -110,4 +169,82 @@ old_dot = dot; Thread::wait(250); +} + +void StartHaptic(void) { + hapticTimer.start(50); + haptic = 1; +} + +void StopHaptic(void const *n) { + haptic = 0; + hapticTimer.stop(); +} + +float Filter(int s) { + accel.acquire_accel_data_g(accel_data); + float filter_sum = 0.0; + //printf("%d\n\r",s); + for(int i = 0; i < 75; i++) + { + filter_buf[i] = accel_data[s]; + //printf("%4.2f\n\r",filter_buf[i]); + filter_sum += filter_buf[i]; + } + return (float)(filter_sum / 75); +} + +// Key modification: use the alert functionality enabled by the host-ble interface +// to define our own command. +void AlertReceived(uint8_t *data, uint8_t length) { + + StartHaptic(); + data[19] = 0; + + // 1: copy data into a global array + if (processedReceivedData) { + processedReceivedData = false; + for (int i = 0; i < 9; i++) { + receivedSummaryData1[i] = data[i]; + } + + for (int i = 9; i < 18; i++) { + receivedSummaryData2[i-9] = data[i]; + } + } + + user[0] = '0' + userChosen; + + mean[0] = data[0]; + mean[1] = data[1]; + mean[2] = data[2]; + + max[0] = data[5]; + max[1] = data[6]; + max[2] = data[7]; + + min[0] = data[10]; + min[1] = data[11]; + min[2] = data[12]; + + steps[0] = data[15]; + steps[1] = data[16]; + steps[2] = data[17]; + steps[3] = data[18]; + steps[4] = data[19]; + + user[1] = 0; + mean[3] = 0; + max[3] = 0; + min[3] = 0; + steps[5] = 0; + + receivedSummaryData1[9] = 0; + receivedSummaryData2[9] = 0; + + // if you haven't yet processed the data that pi sent + // you in the past, then don't do anything. + + // 2: queue up the displaying of that string + displayEventQueue.call(&displayString); } \ No newline at end of file