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.

Committer:
GregC
Date:
Wed Oct 19 22:46:58 2016 +0000
Revision:
2:a041eec9ea8f
Parent:
1:6bd53a378639
Advanced gyroscope program example for Hexiwear featuring Display

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GregC 0:2ebb70d2b0d9 1 #include "mbed.h"
GregC 0:2ebb70d2b0d9 2 #include "FXAS21002.h"
GregC 0:2ebb70d2b0d9 3 #include "Hexi_OLED_SSD1351.h"
GregC 0:2ebb70d2b0d9 4 #include "images.h"
GregC 0:2ebb70d2b0d9 5 #include "string.h"
GregC 0:2ebb70d2b0d9 6
GregC 0:2ebb70d2b0d9 7 // Pin connections
GregC 0:2ebb70d2b0d9 8 DigitalOut led1(LED_GREEN); // RGB LED
GregC 0:2ebb70d2b0d9 9 Serial pc(USBTX, USBRX); // Serial interface
GregC 0:2ebb70d2b0d9 10 FXAS21002 gyro(PTC11,PTC10); // Gyroscope
GregC 0:2ebb70d2b0d9 11 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC)
GregC 0:2ebb70d2b0d9 12
GregC 0:2ebb70d2b0d9 13 // Variables
GregC 0:2ebb70d2b0d9 14 float gyro_data[3]; // Storage for the data from the sensor
GregC 2:a041eec9ea8f 15 float gyro_rms=0.0; // RMS value from the sensor
GregC 2:a041eec9ea8f 16 float gx, gy, gz; // Integer value from the sensor to be displayed
GregC 2:a041eec9ea8f 17 const uint8_t *image1; // Pointer for the image1 to be displayed
GregC 2:a041eec9ea8f 18 char text1[20]; // Text Buffer for dynamic value displayed
GregC 2:a041eec9ea8f 19 char text2[20]; // Text Buffer for dynamic value displayed
GregC 2:a041eec9ea8f 20 char text3[20]; // Text Buffer for dynamic value displayed
GregC 0:2ebb70d2b0d9 21
GregC 0:2ebb70d2b0d9 22 int main() {
GregC 2:a041eec9ea8f 23
GregC 2:a041eec9ea8f 24 // Configure Gyroscope FXAS21002
GregC 0:2ebb70d2b0d9 25 gyro.gyro_config();
GregC 0:2ebb70d2b0d9 26
GregC 0:2ebb70d2b0d9 27 /* Setting pointer location of the 96 by 96 pixel bitmap */
GregC 2:a041eec9ea8f 28 image1 = Gyro;
GregC 0:2ebb70d2b0d9 29
GregC 2:a041eec9ea8f 30 // Dimm Down OLED backlight
GregC 0:2ebb70d2b0d9 31 // oled.DimScreenON();
GregC 0:2ebb70d2b0d9 32
GregC 0:2ebb70d2b0d9 33 /* Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0 */
GregC 0:2ebb70d2b0d9 34 oled.DrawImage(image1,0,0);
GregC 0:2ebb70d2b0d9 35
GregC 0:2ebb70d2b0d9 36
GregC 2:a041eec9ea8f 37 while (true)
GregC 2:a041eec9ea8f 38 {
GregC 0:2ebb70d2b0d9 39
GregC 0:2ebb70d2b0d9 40 gyro.acquire_gyro_data_dps(gyro_data);
GregC 2:a041eec9ea8f 41 gyro_rms = sqrt(((gyro_data[0]*gyro_data[0])+(gyro_data[1]*gyro_data[1])+(gyro_data[2]*gyro_data[2]))/3);
GregC 2:a041eec9ea8f 42 pc.printf("Gyroscope \tRoll (G) %4.2f \tPitch (G) %4.2f \tYaw (G) %4.2f \tRMS %4.2f\n\r",gyro_data[0],gyro_data[1],gyro_data[2],gyro_rms);
GregC 2:a041eec9ea8f 43 wait(0.01);
GregC 2:a041eec9ea8f 44 gx = gyro_data[0];
GregC 2:a041eec9ea8f 45 gy = gyro_data[1];
GregC 2:a041eec9ea8f 46 gz = gyro_data[2];
GregC 0:2ebb70d2b0d9 47
GregC 0:2ebb70d2b0d9 48 /* Get OLED Class Default Text Properties */
GregC 0:2ebb70d2b0d9 49 oled_text_properties_t textProperties = {0};
GregC 0:2ebb70d2b0d9 50 oled.GetTextProperties(&textProperties);
GregC 0:2ebb70d2b0d9 51
GregC 0:2ebb70d2b0d9 52 /* Set text properties to white and right aligned for the dynamic text */
GregC 0:2ebb70d2b0d9 53 textProperties.fontColor = COLOR_BLUE;
GregC 0:2ebb70d2b0d9 54 textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
GregC 0:2ebb70d2b0d9 55 oled.SetTextProperties(&textProperties);
GregC 0:2ebb70d2b0d9 56
GregC 2:a041eec9ea8f 57 /* Display Legends */
GregC 2:a041eec9ea8f 58 strcpy((char *) text1,"Roll (dps):");
GregC 2:a041eec9ea8f 59 oled.Label((uint8_t *)text1,3,45);
GregC 1:6bd53a378639 60
GregC 0:2ebb70d2b0d9 61 /* Format the value */
GregC 2:a041eec9ea8f 62 sprintf(text1,"%4.2f",gx);
GregC 0:2ebb70d2b0d9 63 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
GregC 2:a041eec9ea8f 64 oled.TextBox((uint8_t *)text1,70,45,20,15); //Increase textbox for more digits
GregC 0:2ebb70d2b0d9 65
GregC 0:2ebb70d2b0d9 66 /* Set text properties to white and right aligned for the dynamic text */
GregC 0:2ebb70d2b0d9 67 textProperties.fontColor = COLOR_GREEN;
GregC 2:a041eec9ea8f 68 textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
GregC 0:2ebb70d2b0d9 69 oled.SetTextProperties(&textProperties);
GregC 1:6bd53a378639 70
GregC 2:a041eec9ea8f 71 /* Display Legends */
GregC 2:a041eec9ea8f 72 strcpy((char *) text2,"Pitch (dps):");
GregC 2:a041eec9ea8f 73 oled.Label((uint8_t *)text2,3,62);
GregC 0:2ebb70d2b0d9 74
GregC 0:2ebb70d2b0d9 75 /* Format the value */
GregC 2:a041eec9ea8f 76 sprintf(text2,"%4.2f",gy);
GregC 0:2ebb70d2b0d9 77 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
GregC 2:a041eec9ea8f 78 oled.TextBox((uint8_t *)text2,70,62,20,15); //Increase textbox for more digits
GregC 0:2ebb70d2b0d9 79
GregC 0:2ebb70d2b0d9 80 /* Set text properties to white and right aligned for the dynamic text */
GregC 0:2ebb70d2b0d9 81 textProperties.fontColor = COLOR_RED;
GregC 2:a041eec9ea8f 82 textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
GregC 0:2ebb70d2b0d9 83 oled.SetTextProperties(&textProperties);
GregC 0:2ebb70d2b0d9 84
GregC 2:a041eec9ea8f 85 /* Display Legends */
GregC 2:a041eec9ea8f 86 strcpy((char *) text3,"Yaw (dps):");
GregC 2:a041eec9ea8f 87 oled.Label((uint8_t *)text3,3,79);
GregC 1:6bd53a378639 88
GregC 0:2ebb70d2b0d9 89 /* Format the value */
GregC 2:a041eec9ea8f 90 sprintf(text3,"%4.2f",gz);
GregC 0:2ebb70d2b0d9 91 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
GregC 2:a041eec9ea8f 92 oled.TextBox((uint8_t *)text3,70,79,20,15); //Increase textbox for more digits
GregC 0:2ebb70d2b0d9 93
GregC 0:2ebb70d2b0d9 94 led1 = !led1;
GregC 0:2ebb70d2b0d9 95 Thread::wait(250);
GregC 2:a041eec9ea8f 96 }
GregC 0:2ebb70d2b0d9 97 }