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@5:a2f68bbb5400, 2018-06-15 (annotated)
- Committer:
- trhackett
- Date:
- Fri Jun 15 03:12:45 2018 +0000
- Revision:
- 5:a2f68bbb5400
- Parent:
- 4:eb89733b8642
- Child:
- 6:328ef7a29083
nothing works and everything sucks
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 | |
catchvibes95 | 4:eb89733b8642 | 10 | void UpdateSensorData(void); |
catchvibes95 | 4:eb89733b8642 | 11 | void txTask(void); |
catchvibes95 | 4:eb89733b8642 | 12 | |
catchvibes95 | 4:eb89733b8642 | 13 | FXOS8700 accel(PTC11, PTC10); |
catchvibes95 | 4:eb89733b8642 | 14 | |
catchvibes95 | 4:eb89733b8642 | 15 | /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ |
catchvibes95 | 4:eb89733b8642 | 16 | KW40Z kw40z_device(PTE24, PTE25); |
catchvibes95 | 4:eb89733b8642 | 17 | |
catchvibes95 | 4:eb89733b8642 | 18 | /* Instantiate the SSD1351 OLED Driver */ |
catchvibes95 | 4:eb89733b8642 | 19 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */ |
catchvibes95 | 4:eb89733b8642 | 20 | |
catchvibes95 | 4:eb89733b8642 | 21 | /*Create a Thread to handle sending BLE Sensor Data */ |
catchvibes95 | 4:eb89733b8642 | 22 | Thread txThread; |
catchvibes95 | 4:eb89733b8642 | 23 | |
xihan94 | 0:33686dd26bf9 | 24 | Thread t; |
xihan94 | 0:33686dd26bf9 | 25 | |
catchvibes95 | 4:eb89733b8642 | 26 | char text[20]; |
catchvibes95 | 4:eb89733b8642 | 27 | |
catchvibes95 | 4:eb89733b8642 | 28 | // Variables |
catchvibes95 | 4:eb89733b8642 | 29 | float accel_data[3]; // Storage for the data from the sensor |
catchvibes95 | 4:eb89733b8642 | 30 | float accel_rms=0.0; // RMS value from the sensor |
catchvibes95 | 4:eb89733b8642 | 31 | float ax, ay, az; // Integer value from the sensor to be displayed |
catchvibes95 | 4:eb89733b8642 | 32 | const uint8_t *image1; // Pointer for the image1 to be displayed |
catchvibes95 | 4:eb89733b8642 | 33 | char text1[20]; // Text Buffer for dynamic value displayed |
catchvibes95 | 4:eb89733b8642 | 34 | char text2[20]; // Text Buffer for dynamic value displayed |
catchvibes95 | 4:eb89733b8642 | 35 | char text3[20]; // Text Buffer for dynamic value displayed |
catchvibes95 | 4:eb89733b8642 | 36 | float dot; |
catchvibes95 | 4:eb89733b8642 | 37 | float old_acc=0; |
catchvibes95 | 4:eb89733b8642 | 38 | float new_acc=0; |
catchvibes95 | 4:eb89733b8642 | 39 | float old_accx, old_accy, old_accz, old_dot=0.0; |
catchvibes95 | 4:eb89733b8642 | 40 | uint8_t StepNum = 0, StepNumber = 0; |
catchvibes95 | 4:eb89733b8642 | 41 | |
catchvibes95 | 4:eb89733b8642 | 42 | float filter_buf[75]; |
catchvibes95 | 4:eb89733b8642 | 43 | |
catchvibes95 | 4:eb89733b8642 | 44 | /****************************Call Back Functions*******************************/ |
catchvibes95 | 4:eb89733b8642 | 45 | |
catchvibes95 | 4:eb89733b8642 | 46 | float Filter(int s) |
catchvibes95 | 4:eb89733b8642 | 47 | { |
catchvibes95 | 4:eb89733b8642 | 48 | accel.acquire_accel_data_g(accel_data); |
catchvibes95 | 4:eb89733b8642 | 49 | float filter_sum = 0.0; |
catchvibes95 | 4:eb89733b8642 | 50 | //printf("%d\n\r",s); |
catchvibes95 | 4:eb89733b8642 | 51 | for(int i = 0; i < 75; i++) |
catchvibes95 | 4:eb89733b8642 | 52 | { |
catchvibes95 | 4:eb89733b8642 | 53 | filter_buf[i] = accel_data[s]; |
catchvibes95 | 4:eb89733b8642 | 54 | //printf("%4.2f\n\r",filter_buf[i]); |
catchvibes95 | 4:eb89733b8642 | 55 | filter_sum += filter_buf[i]; |
catchvibes95 | 4:eb89733b8642 | 56 | } |
catchvibes95 | 4:eb89733b8642 | 57 | return (float)(filter_sum / 75); |
catchvibes95 | 4:eb89733b8642 | 58 | } |
catchvibes95 | 4:eb89733b8642 | 59 | |
catchvibes95 | 4:eb89733b8642 | 60 | |
trhackett | 5:a2f68bbb5400 | 61 | // main() runs in its own thread in the OS |
trhackett | 5:a2f68bbb5400 | 62 | int main() { |
trhackett | 5:a2f68bbb5400 | 63 | accel.accel_config(); |
catchvibes95 | 4:eb89733b8642 | 64 | |
trhackett | 5:a2f68bbb5400 | 65 | txThread.start(txTask); /*Start transmitting Sensor Tag Data */ |
catchvibes95 | 4:eb89733b8642 | 66 | |
trhackett | 5:a2f68bbb5400 | 67 | while (true) { |
trhackett | 5:a2f68bbb5400 | 68 | Thread::wait(50); |
catchvibes95 | 4:eb89733b8642 | 69 | } |
catchvibes95 | 4:eb89733b8642 | 70 | } |
xihan94 | 0:33686dd26bf9 | 71 | |
catchvibes95 | 4:eb89733b8642 | 72 | void txTask(void){ |
catchvibes95 | 4:eb89733b8642 | 73 | |
catchvibes95 | 4:eb89733b8642 | 74 | while (true) |
catchvibes95 | 4:eb89733b8642 | 75 | { |
catchvibes95 | 4:eb89733b8642 | 76 | UpdateSensorData(); |
catchvibes95 | 4:eb89733b8642 | 77 | |
catchvibes95 | 4:eb89733b8642 | 78 | /*Notify Hexiwear App that it is running Sensor Tag mode*/ |
catchvibes95 | 4:eb89733b8642 | 79 | kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); |
catchvibes95 | 4:eb89733b8642 | 80 | |
trhackett | 5:a2f68bbb5400 | 81 | /*Send Battery Level for 20% */ |
catchvibes95 | 4:eb89733b8642 | 82 | kw40z_device.SendBatteryLevel(StepNumber); |
catchvibes95 | 4:eb89733b8642 | 83 | |
catchvibes95 | 4:eb89733b8642 | 84 | Thread::wait(10); |
catchvibes95 | 4:eb89733b8642 | 85 | } |
catchvibes95 | 4:eb89733b8642 | 86 | } |
catchvibes95 | 4:eb89733b8642 | 87 | |
catchvibes95 | 4:eb89733b8642 | 88 | void UpdateSensorData(void) |
catchvibes95 | 4:eb89733b8642 | 89 | { |
trhackett | 5:a2f68bbb5400 | 90 | accel.acquire_accel_data_g(accel_data); |
trhackett | 5:a2f68bbb5400 | 91 | ax = Filter(0); |
trhackett | 5:a2f68bbb5400 | 92 | ay = Filter(1); |
trhackett | 5:a2f68bbb5400 | 93 | az = Filter(2); |
trhackett | 5:a2f68bbb5400 | 94 | wait(0.02); |
trhackett | 5:a2f68bbb5400 | 95 | accel_rms = sqrt((ax*ax)+(ay*ay)+(az*az)/3); |
trhackett | 5:a2f68bbb5400 | 96 | dot = (old_accx * ax)+(old_accy * ay)+(old_accz * az); |
trhackett | 5:a2f68bbb5400 | 97 | old_acc = abs(sqrt(old_accx*old_accx+old_accy*old_accy+old_accz*old_accz)); |
trhackett | 5:a2f68bbb5400 | 98 | new_acc = abs(sqrt(ax*ax+ay*ay+az*az)); |
trhackett | 5:a2f68bbb5400 | 99 | dot /= (old_acc * new_acc); |
trhackett | 5:a2f68bbb5400 | 100 | |
trhackett | 5:a2f68bbb5400 | 101 | /* Display Legends */ |
trhackett | 5:a2f68bbb5400 | 102 | StepNum = StepNumber; |
trhackett | 5:a2f68bbb5400 | 103 | if(abs(dot - old_dot) >= 0.05 && abs(dot - old_dot) <= 0.10) { |
trhackett | 5:a2f68bbb5400 | 104 | StepNumber += 1; |
trhackett | 5:a2f68bbb5400 | 105 | } |
trhackett | 5:a2f68bbb5400 | 106 | |
trhackett | 5:a2f68bbb5400 | 107 | old_accx = ax; |
trhackett | 5:a2f68bbb5400 | 108 | old_accy = ay; |
trhackett | 5:a2f68bbb5400 | 109 | old_accz = az; |
trhackett | 5:a2f68bbb5400 | 110 | old_dot = dot; |
trhackett | 5:a2f68bbb5400 | 111 | |
trhackett | 5:a2f68bbb5400 | 112 | Thread::wait(250); |
trhackett | 5:a2f68bbb5400 | 113 | } |