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:
Sat Sep 24 19:19:42 2016 +0000
Revision:
0:2ebb70d2b0d9
Child:
1:6bd53a378639
test

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 0:2ebb70d2b0d9 15 //float gyro_rms=0.0; // RMS value from the sensor
GregC 0:2ebb70d2b0d9 16 int x, y, z; // Integer value from the sensor to be displayed
GregC 0:2ebb70d2b0d9 17 const uint8_t *image1; // Pointer for the image to be displayed
GregC 0:2ebb70d2b0d9 18 char text[20]; // Text Buffer for dynamic value displayed
GregC 0:2ebb70d2b0d9 19
GregC 0:2ebb70d2b0d9 20
GregC 0:2ebb70d2b0d9 21
GregC 0:2ebb70d2b0d9 22
GregC 0:2ebb70d2b0d9 23 int main() {
GregC 0:2ebb70d2b0d9 24
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 0:2ebb70d2b0d9 28 image1 = Gyroscope;
GregC 0:2ebb70d2b0d9 29
GregC 0:2ebb70d2b0d9 30 /* Turn on the backlight of the OLED Display */
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 0:2ebb70d2b0d9 37
GregC 0:2ebb70d2b0d9 38
GregC 0:2ebb70d2b0d9 39
GregC 0:2ebb70d2b0d9 40 while (true) {
GregC 0:2ebb70d2b0d9 41
GregC 0:2ebb70d2b0d9 42 gyro.acquire_gyro_data_dps(gyro_data);
GregC 0:2ebb70d2b0d9 43 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]);
GregC 0:2ebb70d2b0d9 44 // gyro_rms = sqrt(((gyro_data[0]*gyro_data[0])+(gyro_data[1]*gyro_data[1])+(gyro_data[2]*gyro_data[2]))/3);
GregC 0:2ebb70d2b0d9 45 // pc.printf("Gyroscope RMS %f \r\n\n",gyro_rms);
GregC 0:2ebb70d2b0d9 46 x = gyro_data[0];
GregC 0:2ebb70d2b0d9 47 y = gyro_data[1];
GregC 0:2ebb70d2b0d9 48 z = gyro_data[2];
GregC 0:2ebb70d2b0d9 49
GregC 0:2ebb70d2b0d9 50 /* Get OLED Class Default Text Properties */
GregC 0:2ebb70d2b0d9 51 oled_text_properties_t textProperties = {0};
GregC 0:2ebb70d2b0d9 52 oled.GetTextProperties(&textProperties);
GregC 0:2ebb70d2b0d9 53
GregC 0:2ebb70d2b0d9 54 /* Set text properties to white and right aligned for the dynamic text */
GregC 0:2ebb70d2b0d9 55 textProperties.fontColor = COLOR_BLUE;
GregC 0:2ebb70d2b0d9 56 textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
GregC 0:2ebb70d2b0d9 57 oled.SetTextProperties(&textProperties);
GregC 0:2ebb70d2b0d9 58
GregC 0:2ebb70d2b0d9 59 /* Format the value */
GregC 0:2ebb70d2b0d9 60 sprintf(text,"%i",x);
GregC 0:2ebb70d2b0d9 61 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
GregC 0:2ebb70d2b0d9 62 oled.TextBox((uint8_t *)text,5,75,20,15); //Increase textbox for more digits
GregC 0:2ebb70d2b0d9 63
GregC 0:2ebb70d2b0d9 64 /* Set text properties to white and right aligned for the dynamic text */
GregC 0:2ebb70d2b0d9 65 textProperties.fontColor = COLOR_GREEN;
GregC 0:2ebb70d2b0d9 66 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
GregC 0:2ebb70d2b0d9 67 oled.SetTextProperties(&textProperties);
GregC 0:2ebb70d2b0d9 68
GregC 0:2ebb70d2b0d9 69 /* Format the value */
GregC 0:2ebb70d2b0d9 70 sprintf(text,"%i",y);
GregC 0:2ebb70d2b0d9 71 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
GregC 0:2ebb70d2b0d9 72 oled.TextBox((uint8_t *)text,37,75,20,15); //Increase textbox for more digits
GregC 0:2ebb70d2b0d9 73
GregC 0:2ebb70d2b0d9 74 /* Set text properties to white and right aligned for the dynamic text */
GregC 0:2ebb70d2b0d9 75 textProperties.fontColor = COLOR_RED;
GregC 0:2ebb70d2b0d9 76 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
GregC 0:2ebb70d2b0d9 77 oled.SetTextProperties(&textProperties);
GregC 0:2ebb70d2b0d9 78
GregC 0:2ebb70d2b0d9 79 /* Format the value */
GregC 0:2ebb70d2b0d9 80 sprintf(text,"%i",z);
GregC 0:2ebb70d2b0d9 81 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
GregC 0:2ebb70d2b0d9 82 oled.TextBox((uint8_t *)text,70,75,20,15); //Increase textbox for more digits
GregC 0:2ebb70d2b0d9 83
GregC 0:2ebb70d2b0d9 84 led1 = !led1;
GregC 0:2ebb70d2b0d9 85 Thread::wait(250);
GregC 0:2ebb70d2b0d9 86 }
GregC 0:2ebb70d2b0d9 87 }