![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Swim-It, is a waterproof counter for arm movement
Dependencies: Hexi_KW40Z Hexi_OLED_SSD1351
Fork of Hexi_BLE_Example by
main.cpp@3:f9d351e32e78, 2016-10-09 (annotated)
- Committer:
- fyaocn
- Date:
- Sun Oct 09 03:19:16 2016 +0000
- Revision:
- 3:f9d351e32e78
- Parent:
- 1:a0d9eeedb771
Swim-It, is a waterproof counter for arm movement
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fyaocn | 3:f9d351e32e78 | 1 | //This file is based on example of Hexi_Accelero_Magnet and Hexi_BLE_Example, logic of swim it is added as core lines for arm stroke counting in |
khuang | 0:c80666325948 | 2 | #include "mbed.h" |
khuang | 0:c80666325948 | 3 | #include "Hexi_KW40Z.h" |
khuang | 0:c80666325948 | 4 | #include "Hexi_OLED_SSD1351.h" |
fyaocn | 3:f9d351e32e78 | 5 | #include "./FXOS8700/FXOS8700.h" |
khuang | 0:c80666325948 | 6 | #include "OLED_types.h" |
khuang | 0:c80666325948 | 7 | #include "OpenSans_Font.h" |
khuang | 0:c80666325948 | 8 | #include "string.h" |
khuang | 0:c80666325948 | 9 | |
khuang | 0:c80666325948 | 10 | #define LED_ON 0 |
khuang | 0:c80666325948 | 11 | #define LED_OFF 1 |
fyaocn | 3:f9d351e32e78 | 12 | |
fyaocn | 3:f9d351e32e78 | 13 | #define MAG_THREHOLD 0.5 |
fyaocn | 3:f9d351e32e78 | 14 | #define ACCEL_THREHOLD 0.5 |
fyaocn | 3:f9d351e32e78 | 15 | |
fyaocn | 3:f9d351e32e78 | 16 | Timeout swimit_reset; |
fyaocn | 3:f9d351e32e78 | 17 | static uint16_t swimit_count; |
khuang | 0:c80666325948 | 18 | |
khuang | 0:c80666325948 | 19 | void StartHaptic(void); |
khuang | 0:c80666325948 | 20 | void StopHaptic(void const *n); |
khuang | 1:a0d9eeedb771 | 21 | void txTask(void); |
fyaocn | 3:f9d351e32e78 | 22 | void swimit_reseter(void); |
khuang | 0:c80666325948 | 23 | |
khuang | 0:c80666325948 | 24 | DigitalOut redLed(LED1,1); |
khuang | 0:c80666325948 | 25 | DigitalOut greenLed(LED2,1); |
khuang | 0:c80666325948 | 26 | DigitalOut blueLed(LED3,1); |
khuang | 0:c80666325948 | 27 | DigitalOut haptic(PTB9); |
khuang | 0:c80666325948 | 28 | |
fyaocn | 3:f9d351e32e78 | 29 | // Pin connections & address for Hexiwear |
fyaocn | 3:f9d351e32e78 | 30 | FXOS8700 accel(PTC11, PTC10); |
fyaocn | 3:f9d351e32e78 | 31 | FXOS8700 mag(PTC11, PTC10); |
fyaocn | 3:f9d351e32e78 | 32 | |
khuang | 0:c80666325948 | 33 | /* Define timer for haptic feedback */ |
khuang | 0:c80666325948 | 34 | RtosTimer hapticTimer(StopHaptic, osTimerOnce); |
khuang | 0:c80666325948 | 35 | |
khuang | 0:c80666325948 | 36 | /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ |
khuang | 0:c80666325948 | 37 | KW40Z kw40z_device(PTE24, PTE25); |
khuang | 0:c80666325948 | 38 | |
khuang | 0:c80666325948 | 39 | /* Instantiate the SSD1351 OLED Driver */ |
khuang | 0:c80666325948 | 40 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */ |
khuang | 0:c80666325948 | 41 | |
khuang | 1:a0d9eeedb771 | 42 | /*Create a Thread to handle sending BLE Sensor Data */ |
khuang | 1:a0d9eeedb771 | 43 | Thread txThread; |
khuang | 0:c80666325948 | 44 | |
khuang | 1:a0d9eeedb771 | 45 | /* Text Buffer */ |
khuang | 1:a0d9eeedb771 | 46 | char text[20]; |
khuang | 1:a0d9eeedb771 | 47 | |
fyaocn | 3:f9d351e32e78 | 48 | |
fyaocn | 3:f9d351e32e78 | 49 | |
khuang | 1:a0d9eeedb771 | 50 | /****************************Call Back Functions*******************************/ |
khuang | 0:c80666325948 | 51 | void ButtonRight(void) |
khuang | 0:c80666325948 | 52 | { |
khuang | 0:c80666325948 | 53 | StartHaptic(); |
khuang | 0:c80666325948 | 54 | kw40z_device.ToggleAdvertisementMode(); |
khuang | 0:c80666325948 | 55 | } |
khuang | 0:c80666325948 | 56 | |
khuang | 0:c80666325948 | 57 | void ButtonLeft(void) |
khuang | 0:c80666325948 | 58 | { |
khuang | 0:c80666325948 | 59 | StartHaptic(); |
khuang | 0:c80666325948 | 60 | kw40z_device.ToggleAdvertisementMode(); |
khuang | 0:c80666325948 | 61 | } |
khuang | 0:c80666325948 | 62 | |
khuang | 0:c80666325948 | 63 | void PassKey(void) |
khuang | 0:c80666325948 | 64 | { |
khuang | 0:c80666325948 | 65 | StartHaptic(); |
khuang | 0:c80666325948 | 66 | strcpy((char *) text,"PAIR CODE"); |
khuang | 1:a0d9eeedb771 | 67 | oled.TextBox((uint8_t *)text,0,25,95,18); |
khuang | 0:c80666325948 | 68 | |
khuang | 0:c80666325948 | 69 | /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */ |
khuang | 0:c80666325948 | 70 | sprintf(text,"%d", kw40z_device.GetPassKey()); |
khuang | 0:c80666325948 | 71 | oled.TextBox((uint8_t *)text,0,40,95,18); |
khuang | 0:c80666325948 | 72 | } |
khuang | 1:a0d9eeedb771 | 73 | |
khuang | 1:a0d9eeedb771 | 74 | /***********************End of Call Back Functions*****************************/ |
khuang | 1:a0d9eeedb771 | 75 | |
khuang | 1:a0d9eeedb771 | 76 | /********************************Main******************************************/ |
khuang | 1:a0d9eeedb771 | 77 | |
khuang | 0:c80666325948 | 78 | int main() |
khuang | 0:c80666325948 | 79 | { |
fyaocn | 3:f9d351e32e78 | 80 | // Configure Accelerometer FXOS8700, Magnetometer FXOS8700 |
fyaocn | 3:f9d351e32e78 | 81 | accel.accel_config(); |
fyaocn | 3:f9d351e32e78 | 82 | mag.mag_config(); |
fyaocn | 3:f9d351e32e78 | 83 | |
fyaocn | 3:f9d351e32e78 | 84 | |
fyaocn | 3:f9d351e32e78 | 85 | |
khuang | 0:c80666325948 | 86 | /* Get OLED Class Default Text Properties */ |
khuang | 0:c80666325948 | 87 | oled_text_properties_t textProperties = {0}; |
khuang | 0:c80666325948 | 88 | oled.GetTextProperties(&textProperties); |
khuang | 0:c80666325948 | 89 | |
khuang | 0:c80666325948 | 90 | /* Turn on the backlight of the OLED Display */ |
khuang | 0:c80666325948 | 91 | oled.DimScreenON(); |
khuang | 0:c80666325948 | 92 | |
khuang | 0:c80666325948 | 93 | /* Fills the screen with solid black */ |
khuang | 0:c80666325948 | 94 | oled.FillScreen(COLOR_BLACK); |
khuang | 0:c80666325948 | 95 | |
khuang | 0:c80666325948 | 96 | /* Register callbacks to application functions */ |
khuang | 0:c80666325948 | 97 | kw40z_device.attach_buttonLeft(&ButtonLeft); |
khuang | 0:c80666325948 | 98 | kw40z_device.attach_buttonRight(&ButtonRight); |
khuang | 0:c80666325948 | 99 | kw40z_device.attach_passkey(&PassKey); |
khuang | 1:a0d9eeedb771 | 100 | |
khuang | 0:c80666325948 | 101 | /* Change font color to Blue */ |
khuang | 0:c80666325948 | 102 | textProperties.fontColor = COLOR_BLUE; |
khuang | 0:c80666325948 | 103 | oled.SetTextProperties(&textProperties); |
khuang | 0:c80666325948 | 104 | |
khuang | 0:c80666325948 | 105 | /* Display Bluetooth Label at x=17,y=65 */ |
khuang | 0:c80666325948 | 106 | strcpy((char *) text,"BLUETOOTH"); |
khuang | 0:c80666325948 | 107 | oled.Label((uint8_t *)text,17,65); |
khuang | 0:c80666325948 | 108 | |
khuang | 0:c80666325948 | 109 | /* Change font color to white */ |
khuang | 0:c80666325948 | 110 | textProperties.fontColor = COLOR_WHITE; |
khuang | 0:c80666325948 | 111 | textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; |
khuang | 0:c80666325948 | 112 | oled.SetTextProperties(&textProperties); |
khuang | 0:c80666325948 | 113 | |
khuang | 1:a0d9eeedb771 | 114 | /* Display Label at x=22,y=80 */ |
khuang | 0:c80666325948 | 115 | strcpy((char *) text,"Tap Below"); |
khuang | 0:c80666325948 | 116 | oled.Label((uint8_t *)text,22,80); |
khuang | 0:c80666325948 | 117 | |
khuang | 1:a0d9eeedb771 | 118 | uint8_t prevLinkState = 0; |
khuang | 1:a0d9eeedb771 | 119 | uint8_t currLinkState = 0; |
khuang | 1:a0d9eeedb771 | 120 | txThread.start(txTask); /*Start transmitting Sensor Tag Data */ |
khuang | 0:c80666325948 | 121 | |
khuang | 1:a0d9eeedb771 | 122 | while (true) |
khuang | 1:a0d9eeedb771 | 123 | { |
khuang | 1:a0d9eeedb771 | 124 | blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
khuang | 1:a0d9eeedb771 | 125 | Thread::wait(50); |
fyaocn | 3:f9d351e32e78 | 126 | |
khuang | 1:a0d9eeedb771 | 127 | } |
khuang | 1:a0d9eeedb771 | 128 | } |
khuang | 1:a0d9eeedb771 | 129 | |
khuang | 1:a0d9eeedb771 | 130 | /******************************End of Main*************************************/ |
khuang | 1:a0d9eeedb771 | 131 | |
khuang | 1:a0d9eeedb771 | 132 | |
khuang | 1:a0d9eeedb771 | 133 | /* txTask() transmits the sensor data */ |
khuang | 1:a0d9eeedb771 | 134 | void txTask(void){ |
fyaocn | 3:f9d351e32e78 | 135 | |
fyaocn | 3:f9d351e32e78 | 136 | float accel_data[3]; |
fyaocn | 3:f9d351e32e78 | 137 | float accel_rms=0.0; |
fyaocn | 3:f9d351e32e78 | 138 | float mag_data[3]; |
fyaocn | 3:f9d351e32e78 | 139 | float mag_rms=0.0; |
fyaocn | 3:f9d351e32e78 | 140 | uint16_t steps=0; |
fyaocn | 3:f9d351e32e78 | 141 | float accel_rms_ave=accel_rms; |
fyaocn | 3:f9d351e32e78 | 142 | |
khuang | 1:a0d9eeedb771 | 143 | while (true) |
khuang | 1:a0d9eeedb771 | 144 | { |
fyaocn | 3:f9d351e32e78 | 145 | |
fyaocn | 3:f9d351e32e78 | 146 | |
fyaocn | 3:f9d351e32e78 | 147 | |
fyaocn | 3:f9d351e32e78 | 148 | /*acquire accel_data and mag_data, running Sensor Tag mode*/ |
fyaocn | 3:f9d351e32e78 | 149 | accel.acquire_accel_data_g(accel_data); |
fyaocn | 3:f9d351e32e78 | 150 | accel_rms = sqrt(((accel_data[0]*accel_data[0])+(accel_data[1]*accel_data[1])+(accel_data[2]*accel_data[2]))/3); |
fyaocn | 3:f9d351e32e78 | 151 | Thread::wait(0.01); |
fyaocn | 3:f9d351e32e78 | 152 | |
fyaocn | 3:f9d351e32e78 | 153 | mag.acquire_mag_data_uT(mag_data); |
fyaocn | 3:f9d351e32e78 | 154 | mag_rms = sqrt(((mag_data[0]*mag_data[0])+(mag_data[1]*mag_data[1])+(mag_data[2]*mag_data[2]))/3); |
fyaocn | 3:f9d351e32e78 | 155 | Thread::wait(0.01); |
fyaocn | 3:f9d351e32e78 | 156 | |
fyaocn | 3:f9d351e32e78 | 157 | //rm Strok counting by change of MAG and ACCEL at the same time, |
fyaocn | 3:f9d351e32e78 | 158 | if (accel_rms>ACCEL_THREHOLD ||mag_rms>MAG_THREHOLD) swimit_count++; |
fyaocn | 3:f9d351e32e78 | 159 | steps = swimit_count; |
fyaocn | 3:f9d351e32e78 | 160 | accel_rms_ave=accel_rms; |
fyaocn | 3:f9d351e32e78 | 161 | |
fyaocn | 3:f9d351e32e78 | 162 | |
fyaocn | 3:f9d351e32e78 | 163 | // Reset the step after 60 second without any movement; |
fyaocn | 3:f9d351e32e78 | 164 | if (abs(accel_rms_ave-accel_rms)<0.1) swimit_reset.attach(&swimit_reseter, 60.0); |
fyaocn | 3:f9d351e32e78 | 165 | |
fyaocn | 3:f9d351e32e78 | 166 | |
khuang | 1:a0d9eeedb771 | 167 | /*Notify Hexiwear App that it is running Sensor Tag mode*/ |
khuang | 0:c80666325948 | 168 | kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); |
khuang | 0:c80666325948 | 169 | |
fyaocn | 3:f9d351e32e78 | 170 | |
fyaocn | 3:f9d351e32e78 | 171 | |
fyaocn | 3:f9d351e32e78 | 172 | /*The following is sending Mag,Accel,Gyro Data over BLE.*/ |
khuang | 1:a0d9eeedb771 | 173 | |
fyaocn | 3:f9d351e32e78 | 174 | kw40z_device.SendAccel(accel_data[0],accel_data[1],accel_data[2]); |
fyaocn | 3:f9d351e32e78 | 175 | kw40z_device.SendSteps(steps); |
fyaocn | 3:f9d351e32e78 | 176 | Thread::wait(10); |
khuang | 0:c80666325948 | 177 | } |
khuang | 0:c80666325948 | 178 | } |
khuang | 0:c80666325948 | 179 | |
khuang | 1:a0d9eeedb771 | 180 | void StartHaptic(void) { |
khuang | 0:c80666325948 | 181 | hapticTimer.start(50); |
khuang | 0:c80666325948 | 182 | haptic = 1; |
khuang | 0:c80666325948 | 183 | } |
khuang | 0:c80666325948 | 184 | |
khuang | 0:c80666325948 | 185 | void StopHaptic(void const *n) { |
khuang | 0:c80666325948 | 186 | haptic = 0; |
khuang | 0:c80666325948 | 187 | hapticTimer.stop(); |
khuang | 0:c80666325948 | 188 | } |
khuang | 1:a0d9eeedb771 | 189 | |
fyaocn | 3:f9d351e32e78 | 190 | void swimit_reseter() { |
fyaocn | 3:f9d351e32e78 | 191 | swimit_count=0; |
fyaocn | 3:f9d351e32e78 | 192 | } |