Anand vaidyanathan / Mbed OS Racket_VibrAnalyzer

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 * Bitmaps need to be formatted as 16bppRgb565, flipped vertically
00003 * and a 6 byte header needs to be appended at the start of the array.
00004 * Use the following tool to create arrays for images. 
00005 * https://github.com/MikroElektronika/HEXIWEAR/tree/master/SW/ResourceCollectionTool
00006 * It takes an image and outputs the array. It handles the flipping and the 6 byte header.  
00007 * Image needs to be converted outside the tool to fit the screen 
00008 * (Max Dimensions:96px by 96px).
00009 */
00010 
00011 #include "mbed.h"
00012 #include "Hexi_OLED_SSD1351.h"
00013 #include "images.h"
00014 #include "tennis.h"
00015 #include "Hexi_KW40Z.h"
00016 #include "FXOS8700CQ.h"
00017 
00018 #define LED_ON      0
00019 #define LED_OFF     1
00020    
00021 void StartHaptic(void);
00022 void StopHaptic(void const *n);
00023 
00024 /* Instantiate the RGB LED and Haptic motor pinout */
00025 DigitalOut redLed(LED1);
00026 DigitalOut greenLed(LED2);
00027 DigitalOut blueLed(LED3);
00028 DigitalOut haptic(PTB9);
00029 
00030 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ 
00031 KW40Z kw40z_device(PTE24, PTE25);
00032 
00033 /* Instantiate the SSD1351 OLED Driver */
00034 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // (MOSI,SCLK,POWER,CS,RST,DC)
00035 
00036 /* Define timer for haptic feedback */
00037 RtosTimer hapticTimer(StopHaptic, osTimerOnce);
00038 
00039 /* Accelerator Functions */
00040 FXOS8700CQ fxos(PTC11 /* sda */, PTC10 /* scl */);
00041 
00042 /* Accelerator interrupt INT1 */
00043 InterruptIn accelIntPin(PTC1);
00044 
00045 /* Serial interface */
00046 Serial pc(USBTX, USBRX);
00047 
00048 SRAWDATA accelData;
00049 
00050 int accelReady=0;
00051 
00052 int flag=0;
00053 
00054 uint32_t cntVib=0, maxVib=0, minVib=0xffffffff;
00055 
00056 void ButtonLeft(void)
00057 {
00058     StartHaptic();
00059     
00060     redLed      = LED_ON;
00061     greenLed    = LED_ON;
00062     blueLed     = LED_OFF;
00063     
00064     flag = 1;
00065 }
00066 
00067 void ButtonRight(void)
00068 {
00069     StartHaptic();
00070     
00071     redLed      = LED_ON;
00072     greenLed    = LED_OFF;
00073     blueLed     = LED_OFF;    
00074     
00075     flag = 2;
00076 }
00077 
00078 void StartHaptic(void)
00079 {
00080     hapticTimer.start(75);
00081     haptic = 1;
00082 }
00083 
00084 void StopHaptic(void const *n) {
00085     haptic = 0;
00086     hapticTimer.stop();
00087     redLed      = LED_OFF;
00088     greenLed    = LED_OFF;
00089     blueLed     = LED_OFF;
00090 }
00091 
00092 void AccelIntCallback() {
00093     accelReady = 1;
00094 }
00095 
00096 void DisplayResult() {
00097     char text[15];
00098 
00099     oled_text_properties_t textProps={0};
00100     oled.GetTextProperties(&textProps);
00101     textProps.fontColor = COLOR_GREEN;
00102     oled.SetTextProperties(&textProps);
00103 
00104     sprintf(text, "TOT=%08x", cntVib); 
00105     oled.Label((uint8_t*) text, 5, 48);
00106 
00107     sprintf(text, "MAX=%08x", maxVib); 
00108     oled.Label((uint8_t*) text, 5, 60);
00109 
00110     sprintf(text, "MIN=%08x", minVib);
00111     oled.Label((uint8_t*) text, 5, 72);
00112 }
00113 
00114 int main() {
00115     
00116     pc.baud(9600);
00117 
00118     pc.printf("VibrAnalyzer Start \n");
00119 
00120     redLed      = LED_OFF;
00121     greenLed    = LED_OFF;
00122     blueLed     = LED_OFF;
00123     
00124     /* Pointer for the image to be displayed  */  
00125     const uint8_t *image1;
00126     const uint8_t *image2;
00127     const uint8_t *image3;
00128 
00129     /* Setting pointer location of the 96 by 96 pixel bitmap */
00130     image1  = /*Relay_OFF*/ tennis_app_image2;
00131     image2  = Button_OFF;
00132     image3  = Button_ON;
00133 
00134     /* Accel Int setup */
00135     accelIntPin.mode(PullUp);
00136     
00137     /* Accel Int callback setup */
00138     accelIntPin.fall(&AccelIntCallback);
00139 
00140     /* Turn on the backlight of the OLED Display */
00141     oled.DimScreenON();
00142     
00143     /* Register callbacks to application functions */
00144     kw40z_device.attach_buttonLeft(&ButtonLeft);
00145     kw40z_device.attach_buttonRight(&ButtonRight);
00146 
00147     /* Fill screen with black */
00148     oled.FillScreen(COLOR_BLACK);
00149         
00150     oled.DrawImage(image1,0,0);
00151 
00152     /* Accel data enable */
00153     fxos.enable_trans_accel();
00154     
00155     while (true) 
00156     {
00157         if (flag == 1) 
00158         {
00159             if (accelReady == 1)
00160             {
00161                 accelReady = 0;
00162 
00163                 ++cntVib;
00164 
00165                 if (I2C_SUCCESS == fxos.read_accel(&accelData))
00166                 {
00167                   /* Get Vector magnitude of each axis */
00168                   uint16_t xAxis = (accelData.x & 0x7fff);
00169                   uint16_t yAxis = (accelData.y & 0x7fff);
00170                   uint16_t zAxis = (accelData.z & 0x7fff);
00171                   /* Magnitude rounded-up */
00172                   uint32_t accMag = (uint32_t)(0.5 + ((xAxis*xAxis) + 
00173                                       (yAxis*yAxis) + (zAxis*zAxis)));
00174                   /* Update Min and Max Vibration */
00175                   if (accMag > maxVib) {
00176                     maxVib = accMag;
00177                   }
00178                   if (accMag < minVib) {
00179                     minVib = accMag;
00180                   }
00181                 }
00182             }
00183 
00184         }
00185         else if (flag == 2)
00186         {
00187             pc.printf("Count=%X Min=%X Max=%X\n",
00188                       cntVib, minVib, maxVib);
00189         }
00190         Thread::wait(50);
00191     }
00192 }