Tennis Racket Vibration Analyzer
Dependencies: Hexi_KW40Z Hexi_OLED_SSD1351
main.cpp@0:f9598a5afb72, 2016-10-02 (annotated)
- Committer:
- v_anand_786
- Date:
- Sun Oct 02 16:08:43 2016 -0400
- Revision:
- 0:f9598a5afb72
Initial Version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
v_anand_786 |
0:f9598a5afb72 | 1 | /* |
v_anand_786 |
0:f9598a5afb72 | 2 | * Bitmaps need to be formatted as 16bppRgb565, flipped vertically |
v_anand_786 |
0:f9598a5afb72 | 3 | * and a 6 byte header needs to be appended at the start of the array. |
v_anand_786 |
0:f9598a5afb72 | 4 | * Use the following tool to create arrays for images. |
v_anand_786 |
0:f9598a5afb72 | 5 | * https://github.com/MikroElektronika/HEXIWEAR/tree/master/SW/ResourceCollectionTool |
v_anand_786 |
0:f9598a5afb72 | 6 | * It takes an image and outputs the array. It handles the flipping and the 6 byte header. |
v_anand_786 |
0:f9598a5afb72 | 7 | * Image needs to be converted outside the tool to fit the screen |
v_anand_786 |
0:f9598a5afb72 | 8 | * (Max Dimensions:96px by 96px). |
v_anand_786 |
0:f9598a5afb72 | 9 | */ |
v_anand_786 |
0:f9598a5afb72 | 10 | |
v_anand_786 |
0:f9598a5afb72 | 11 | #include "mbed.h" |
v_anand_786 |
0:f9598a5afb72 | 12 | #include "Hexi_OLED_SSD1351.h" |
v_anand_786 |
0:f9598a5afb72 | 13 | #include "images.h" |
v_anand_786 |
0:f9598a5afb72 | 14 | #include "tennis.h" |
v_anand_786 |
0:f9598a5afb72 | 15 | #include "Hexi_KW40Z.h" |
v_anand_786 |
0:f9598a5afb72 | 16 | #include "FXOS8700CQ.h" |
v_anand_786 |
0:f9598a5afb72 | 17 | |
v_anand_786 |
0:f9598a5afb72 | 18 | #define LED_ON 0 |
v_anand_786 |
0:f9598a5afb72 | 19 | #define LED_OFF 1 |
v_anand_786 |
0:f9598a5afb72 | 20 | |
v_anand_786 |
0:f9598a5afb72 | 21 | void StartHaptic(void); |
v_anand_786 |
0:f9598a5afb72 | 22 | void StopHaptic(void const *n); |
v_anand_786 |
0:f9598a5afb72 | 23 | |
v_anand_786 |
0:f9598a5afb72 | 24 | /* Instantiate the RGB LED and Haptic motor pinout */ |
v_anand_786 |
0:f9598a5afb72 | 25 | DigitalOut redLed(LED1); |
v_anand_786 |
0:f9598a5afb72 | 26 | DigitalOut greenLed(LED2); |
v_anand_786 |
0:f9598a5afb72 | 27 | DigitalOut blueLed(LED3); |
v_anand_786 |
0:f9598a5afb72 | 28 | DigitalOut haptic(PTB9); |
v_anand_786 |
0:f9598a5afb72 | 29 | |
v_anand_786 |
0:f9598a5afb72 | 30 | /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ |
v_anand_786 |
0:f9598a5afb72 | 31 | KW40Z kw40z_device(PTE24, PTE25); |
v_anand_786 |
0:f9598a5afb72 | 32 | |
v_anand_786 |
0:f9598a5afb72 | 33 | /* Instantiate the SSD1351 OLED Driver */ |
v_anand_786 |
0:f9598a5afb72 | 34 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // (MOSI,SCLK,POWER,CS,RST,DC) |
v_anand_786 |
0:f9598a5afb72 | 35 | |
v_anand_786 |
0:f9598a5afb72 | 36 | /* Define timer for haptic feedback */ |
v_anand_786 |
0:f9598a5afb72 | 37 | RtosTimer hapticTimer(StopHaptic, osTimerOnce); |
v_anand_786 |
0:f9598a5afb72 | 38 | |
v_anand_786 |
0:f9598a5afb72 | 39 | /* Accelerator Functions */ |
v_anand_786 |
0:f9598a5afb72 | 40 | FXOS8700CQ fxos(PTC11 /* sda */, PTC10 /* scl */); |
v_anand_786 |
0:f9598a5afb72 | 41 | |
v_anand_786 |
0:f9598a5afb72 | 42 | /* Accelerator interrupt INT1 */ |
v_anand_786 |
0:f9598a5afb72 | 43 | InterruptIn accelIntPin(PTC1); |
v_anand_786 |
0:f9598a5afb72 | 44 | |
v_anand_786 |
0:f9598a5afb72 | 45 | /* Serial interface */ |
v_anand_786 |
0:f9598a5afb72 | 46 | Serial pc(USBTX, USBRX); |
v_anand_786 |
0:f9598a5afb72 | 47 | |
v_anand_786 |
0:f9598a5afb72 | 48 | SRAWDATA accelData; |
v_anand_786 |
0:f9598a5afb72 | 49 | |
v_anand_786 |
0:f9598a5afb72 | 50 | int accelReady=0; |
v_anand_786 |
0:f9598a5afb72 | 51 | |
v_anand_786 |
0:f9598a5afb72 | 52 | int flag=0; |
v_anand_786 |
0:f9598a5afb72 | 53 | |
v_anand_786 |
0:f9598a5afb72 | 54 | uint32_t cntVib=0, maxVib=0, minVib=0xffffffff; |
v_anand_786 |
0:f9598a5afb72 | 55 | |
v_anand_786 |
0:f9598a5afb72 | 56 | void ButtonLeft(void) |
v_anand_786 |
0:f9598a5afb72 | 57 | { |
v_anand_786 |
0:f9598a5afb72 | 58 | StartHaptic(); |
v_anand_786 |
0:f9598a5afb72 | 59 | |
v_anand_786 |
0:f9598a5afb72 | 60 | redLed = LED_ON; |
v_anand_786 |
0:f9598a5afb72 | 61 | greenLed = LED_ON; |
v_anand_786 |
0:f9598a5afb72 | 62 | blueLed = LED_OFF; |
v_anand_786 |
0:f9598a5afb72 | 63 | |
v_anand_786 |
0:f9598a5afb72 | 64 | flag = 1; |
v_anand_786 |
0:f9598a5afb72 | 65 | } |
v_anand_786 |
0:f9598a5afb72 | 66 | |
v_anand_786 |
0:f9598a5afb72 | 67 | void ButtonRight(void) |
v_anand_786 |
0:f9598a5afb72 | 68 | { |
v_anand_786 |
0:f9598a5afb72 | 69 | StartHaptic(); |
v_anand_786 |
0:f9598a5afb72 | 70 | |
v_anand_786 |
0:f9598a5afb72 | 71 | redLed = LED_ON; |
v_anand_786 |
0:f9598a5afb72 | 72 | greenLed = LED_OFF; |
v_anand_786 |
0:f9598a5afb72 | 73 | blueLed = LED_OFF; |
v_anand_786 |
0:f9598a5afb72 | 74 | |
v_anand_786 |
0:f9598a5afb72 | 75 | flag = 2; |
v_anand_786 |
0:f9598a5afb72 | 76 | } |
v_anand_786 |
0:f9598a5afb72 | 77 | |
v_anand_786 |
0:f9598a5afb72 | 78 | void StartHaptic(void) |
v_anand_786 |
0:f9598a5afb72 | 79 | { |
v_anand_786 |
0:f9598a5afb72 | 80 | hapticTimer.start(75); |
v_anand_786 |
0:f9598a5afb72 | 81 | haptic = 1; |
v_anand_786 |
0:f9598a5afb72 | 82 | } |
v_anand_786 |
0:f9598a5afb72 | 83 | |
v_anand_786 |
0:f9598a5afb72 | 84 | void StopHaptic(void const *n) { |
v_anand_786 |
0:f9598a5afb72 | 85 | haptic = 0; |
v_anand_786 |
0:f9598a5afb72 | 86 | hapticTimer.stop(); |
v_anand_786 |
0:f9598a5afb72 | 87 | redLed = LED_OFF; |
v_anand_786 |
0:f9598a5afb72 | 88 | greenLed = LED_OFF; |
v_anand_786 |
0:f9598a5afb72 | 89 | blueLed = LED_OFF; |
v_anand_786 |
0:f9598a5afb72 | 90 | } |
v_anand_786 |
0:f9598a5afb72 | 91 | |
v_anand_786 |
0:f9598a5afb72 | 92 | void AccelIntCallback() { |
v_anand_786 |
0:f9598a5afb72 | 93 | accelReady = 1; |
v_anand_786 |
0:f9598a5afb72 | 94 | } |
v_anand_786 |
0:f9598a5afb72 | 95 | |
v_anand_786 |
0:f9598a5afb72 | 96 | void DisplayResult() { |
v_anand_786 |
0:f9598a5afb72 | 97 | char text[15]; |
v_anand_786 |
0:f9598a5afb72 | 98 | |
v_anand_786 |
0:f9598a5afb72 | 99 | oled_text_properties_t textProps={0}; |
v_anand_786 |
0:f9598a5afb72 | 100 | oled.GetTextProperties(&textProps); |
v_anand_786 |
0:f9598a5afb72 | 101 | textProps.fontColor = COLOR_GREEN; |
v_anand_786 |
0:f9598a5afb72 | 102 | oled.SetTextProperties(&textProps); |
v_anand_786 |
0:f9598a5afb72 | 103 | |
v_anand_786 |
0:f9598a5afb72 | 104 | sprintf(text, "TOT=%08x", cntVib); |
v_anand_786 |
0:f9598a5afb72 | 105 | oled.Label((uint8_t*) text, 5, 48); |
v_anand_786 |
0:f9598a5afb72 | 106 | |
v_anand_786 |
0:f9598a5afb72 | 107 | sprintf(text, "MAX=%08x", maxVib); |
v_anand_786 |
0:f9598a5afb72 | 108 | oled.Label((uint8_t*) text, 5, 60); |
v_anand_786 |
0:f9598a5afb72 | 109 | |
v_anand_786 |
0:f9598a5afb72 | 110 | sprintf(text, "MIN=%08x", minVib); |
v_anand_786 |
0:f9598a5afb72 | 111 | oled.Label((uint8_t*) text, 5, 72); |
v_anand_786 |
0:f9598a5afb72 | 112 | } |
v_anand_786 |
0:f9598a5afb72 | 113 | |
v_anand_786 |
0:f9598a5afb72 | 114 | int main() { |
v_anand_786 |
0:f9598a5afb72 | 115 | |
v_anand_786 |
0:f9598a5afb72 | 116 | pc.baud(9600); |
v_anand_786 |
0:f9598a5afb72 | 117 | |
v_anand_786 |
0:f9598a5afb72 | 118 | pc.printf("VibrAnalyzer Start \n"); |
v_anand_786 |
0:f9598a5afb72 | 119 | |
v_anand_786 |
0:f9598a5afb72 | 120 | redLed = LED_OFF; |
v_anand_786 |
0:f9598a5afb72 | 121 | greenLed = LED_OFF; |
v_anand_786 |
0:f9598a5afb72 | 122 | blueLed = LED_OFF; |
v_anand_786 |
0:f9598a5afb72 | 123 | |
v_anand_786 |
0:f9598a5afb72 | 124 | /* Pointer for the image to be displayed */ |
v_anand_786 |
0:f9598a5afb72 | 125 | const uint8_t *image1; |
v_anand_786 |
0:f9598a5afb72 | 126 | const uint8_t *image2; |
v_anand_786 |
0:f9598a5afb72 | 127 | const uint8_t *image3; |
v_anand_786 |
0:f9598a5afb72 | 128 | |
v_anand_786 |
0:f9598a5afb72 | 129 | /* Setting pointer location of the 96 by 96 pixel bitmap */ |
v_anand_786 |
0:f9598a5afb72 | 130 | image1 = /*Relay_OFF*/ tennis_app_image2; |
v_anand_786 |
0:f9598a5afb72 | 131 | image2 = Button_OFF; |
v_anand_786 |
0:f9598a5afb72 | 132 | image3 = Button_ON; |
v_anand_786 |
0:f9598a5afb72 | 133 | |
v_anand_786 |
0:f9598a5afb72 | 134 | /* Accel Int setup */ |
v_anand_786 |
0:f9598a5afb72 | 135 | accelIntPin.mode(PullUp); |
v_anand_786 |
0:f9598a5afb72 | 136 | |
v_anand_786 |
0:f9598a5afb72 | 137 | /* Accel Int callback setup */ |
v_anand_786 |
0:f9598a5afb72 | 138 | accelIntPin.fall(&AccelIntCallback); |
v_anand_786 |
0:f9598a5afb72 | 139 | |
v_anand_786 |
0:f9598a5afb72 | 140 | /* Turn on the backlight of the OLED Display */ |
v_anand_786 |
0:f9598a5afb72 | 141 | oled.DimScreenON(); |
v_anand_786 |
0:f9598a5afb72 | 142 | |
v_anand_786 |
0:f9598a5afb72 | 143 | /* Register callbacks to application functions */ |
v_anand_786 |
0:f9598a5afb72 | 144 | kw40z_device.attach_buttonLeft(&ButtonLeft); |
v_anand_786 |
0:f9598a5afb72 | 145 | kw40z_device.attach_buttonRight(&ButtonRight); |
v_anand_786 |
0:f9598a5afb72 | 146 | |
v_anand_786 |
0:f9598a5afb72 | 147 | /* Fill screen with black */ |
v_anand_786 |
0:f9598a5afb72 | 148 | oled.FillScreen(COLOR_BLACK); |
v_anand_786 |
0:f9598a5afb72 | 149 | |
v_anand_786 |
0:f9598a5afb72 | 150 | oled.DrawImage(image1,0,0); |
v_anand_786 |
0:f9598a5afb72 | 151 | |
v_anand_786 |
0:f9598a5afb72 | 152 | /* Accel data enable */ |
v_anand_786 |
0:f9598a5afb72 | 153 | fxos.enable_trans_accel(); |
v_anand_786 |
0:f9598a5afb72 | 154 | |
v_anand_786 |
0:f9598a5afb72 | 155 | while (true) |
v_anand_786 |
0:f9598a5afb72 | 156 | { |
v_anand_786 |
0:f9598a5afb72 | 157 | if (flag == 1) |
v_anand_786 |
0:f9598a5afb72 | 158 | { |
v_anand_786 |
0:f9598a5afb72 | 159 | if (accelReady == 1) |
v_anand_786 |
0:f9598a5afb72 | 160 | { |
v_anand_786 |
0:f9598a5afb72 | 161 | accelReady = 0; |
v_anand_786 |
0:f9598a5afb72 | 162 | |
v_anand_786 |
0:f9598a5afb72 | 163 | ++cntVib; |
v_anand_786 |
0:f9598a5afb72 | 164 | |
v_anand_786 |
0:f9598a5afb72 | 165 | if (I2C_SUCCESS == fxos.read_accel(&accelData)) |
v_anand_786 |
0:f9598a5afb72 | 166 | { |
v_anand_786 |
0:f9598a5afb72 | 167 | /* Get Vector magnitude of each axis */ |
v_anand_786 |
0:f9598a5afb72 | 168 | uint16_t xAxis = (accelData.x & 0x7fff); |
v_anand_786 |
0:f9598a5afb72 | 169 | uint16_t yAxis = (accelData.y & 0x7fff); |
v_anand_786 |
0:f9598a5afb72 | 170 | uint16_t zAxis = (accelData.z & 0x7fff); |
v_anand_786 |
0:f9598a5afb72 | 171 | /* Magnitude rounded-up */ |
v_anand_786 |
0:f9598a5afb72 | 172 | uint32_t accMag = (uint32_t)(0.5 + ((xAxis*xAxis) + |
v_anand_786 |
0:f9598a5afb72 | 173 | (yAxis*yAxis) + (zAxis*zAxis))); |
v_anand_786 |
0:f9598a5afb72 | 174 | /* Update Min and Max Vibration */ |
v_anand_786 |
0:f9598a5afb72 | 175 | if (accMag > maxVib) { |
v_anand_786 |
0:f9598a5afb72 | 176 | maxVib = accMag; |
v_anand_786 |
0:f9598a5afb72 | 177 | } |
v_anand_786 |
0:f9598a5afb72 | 178 | if (accMag < minVib) { |
v_anand_786 |
0:f9598a5afb72 | 179 | minVib = accMag; |
v_anand_786 |
0:f9598a5afb72 | 180 | } |
v_anand_786 |
0:f9598a5afb72 | 181 | } |
v_anand_786 |
0:f9598a5afb72 | 182 | } |
v_anand_786 |
0:f9598a5afb72 | 183 | |
v_anand_786 |
0:f9598a5afb72 | 184 | } |
v_anand_786 |
0:f9598a5afb72 | 185 | else if (flag == 2) |
v_anand_786 |
0:f9598a5afb72 | 186 | { |
v_anand_786 |
0:f9598a5afb72 | 187 | pc.printf("Count=%X Min=%X Max=%X\n", |
v_anand_786 |
0:f9598a5afb72 | 188 | cntVib, minVib, maxVib); |
v_anand_786 |
0:f9598a5afb72 | 189 | } |
v_anand_786 |
0:f9598a5afb72 | 190 | Thread::wait(50); |
v_anand_786 |
0:f9598a5afb72 | 191 | } |
v_anand_786 |
0:f9598a5afb72 | 192 | } |