Advanced gyroscope program example for Hexiwear featuring OLED Display

Dependencies:   FXAS21002 Hexi_OLED_SSD1351

Fork of Hexi_Gyro-v2_Example by Hexiwear

This project demonstrates the use of the FXAS21002CQ Gyroscope sensor embedded in Hexiwear

Compile the project and copy the binary "Hexi_Gyro-V2_Example_HEXIWEAR.bin" in the DAP-LINK drive from your computer file explorer Press the K64F-RESET button on the docking station to start the program on your board

The Roll, the Pitch and the Yaw values will be displayed in real-time on the OLED Display.

main.cpp

Committer:
GregC
Date:
2016-09-24
Revision:
0:2ebb70d2b0d9
Child:
1:6bd53a378639

File content as of revision 0:2ebb70d2b0d9:

#include "mbed.h"
#include "FXAS21002.h"
#include "Hexi_OLED_SSD1351.h"
#include "images.h"
#include "string.h"

// Pin connections
DigitalOut led1(LED_GREEN); // RGB LED
Serial pc(USBTX, USBRX); // Serial interface
FXAS21002 gyro(PTC11,PTC10); // Gyroscope
SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC)

// Variables
float gyro_data[3];  // Storage for the data from the sensor
//float gyro_rms=0.0; // RMS value from the sensor
int x, y, z; // Integer value from the sensor to be displayed
const uint8_t *image1; // Pointer for the image to be displayed
char text[20]; // Text Buffer for dynamic value displayed




int main() {
        
    gyro.gyro_config();
    
    /* Setting pointer location of the 96 by 96 pixel bitmap */
    image1  = Gyroscope;

    /* Turn on the backlight of the OLED Display */
//    oled.DimScreenON();
    
    /* Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0 */
    oled.DrawImage(image1,0,0);  




    
    while (true) {
    
      gyro.acquire_gyro_data_dps(gyro_data);
      pc.printf("Roll (G) %4.2f,\t Pitch (G) %4.2f,\t Yaw (G) %4.2f\r\n",gyro_data[0],gyro_data[1],gyro_data[2]);
//      gyro_rms = sqrt(((gyro_data[0]*gyro_data[0])+(gyro_data[1]*gyro_data[1])+(gyro_data[2]*gyro_data[2]))/3);
//      pc.printf("Gyroscope RMS %f \r\n\n",gyro_rms);
      x = gyro_data[0];
      y = gyro_data[1];
      z = gyro_data[2];  

      /* Get OLED Class Default Text Properties */
      oled_text_properties_t textProperties = {0};
      oled.GetTextProperties(&textProperties); 
      
      /* Set text properties to white and right aligned for the dynamic text */
      textProperties.fontColor = COLOR_BLUE;
      textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
      oled.SetTextProperties(&textProperties);  
      
      /* Format the value */
      sprintf(text,"%i",x);
      /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
      oled.TextBox((uint8_t *)text,5,75,20,15); //Increase textbox for more digits

      /* Set text properties to white and right aligned for the dynamic text */ 
      textProperties.fontColor = COLOR_GREEN;
      textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
      oled.SetTextProperties(&textProperties);  
      
      /* Format the value */
      sprintf(text,"%i",y);
      /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
      oled.TextBox((uint8_t *)text,37,75,20,15); //Increase textbox for more digits
      
      /* Set text properties to white and right aligned for the dynamic text */ 
      textProperties.fontColor = COLOR_RED;
      textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
      oled.SetTextProperties(&textProperties);  
      
      /* Format the value */
      sprintf(text,"%i",z);
      /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
      oled.TextBox((uint8_t *)text,70,75,20,15); //Increase textbox for more digits

      led1 = !led1;
      Thread::wait(250);
      }
}