Advanced gyroscope program example for Hexiwear featuring OLED Display
Dependencies: FXAS21002 Hexi_OLED_SSD1351
Fork of Hexi_Gyro-v2_Example by
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@1:6bd53a378639, 2016-09-24 (annotated)
- Committer:
- GregC
- Date:
- Sat Sep 24 19:51:05 2016 +0000
- Revision:
- 1:6bd53a378639
- Parent:
- 0:2ebb70d2b0d9
- Child:
- 2:a041eec9ea8f
test w display
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:6bd53a378639 | 59 | /* Display Legends */ |
GregC | 1:6bd53a378639 | 60 | strcpy((char *) text,"Roll"); |
GregC | 1:6bd53a378639 | 61 | oled.Label((uint8_t *)text,5,67); |
GregC | 1:6bd53a378639 | 62 | |
GregC | 0:2ebb70d2b0d9 | 63 | /* Format the value */ |
GregC | 0:2ebb70d2b0d9 | 64 | sprintf(text,"%i",x); |
GregC | 0:2ebb70d2b0d9 | 65 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
GregC | 1:6bd53a378639 | 66 | oled.TextBox((uint8_t *)text,5,81,20,15); //Increase textbox for more digits |
GregC | 0:2ebb70d2b0d9 | 67 | |
GregC | 0:2ebb70d2b0d9 | 68 | /* Set text properties to white and right aligned for the dynamic text */ |
GregC | 0:2ebb70d2b0d9 | 69 | textProperties.fontColor = COLOR_GREEN; |
GregC | 0:2ebb70d2b0d9 | 70 | textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; |
GregC | 0:2ebb70d2b0d9 | 71 | oled.SetTextProperties(&textProperties); |
GregC | 1:6bd53a378639 | 72 | |
GregC | 1:6bd53a378639 | 73 | /* Display Legends */ |
GregC | 1:6bd53a378639 | 74 | strcpy((char *) text,"Pitch"); |
GregC | 1:6bd53a378639 | 75 | oled.Label((uint8_t *)text,37,67); |
GregC | 0:2ebb70d2b0d9 | 76 | |
GregC | 0:2ebb70d2b0d9 | 77 | /* Format the value */ |
GregC | 0:2ebb70d2b0d9 | 78 | sprintf(text,"%i",y); |
GregC | 0:2ebb70d2b0d9 | 79 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
GregC | 1:6bd53a378639 | 80 | oled.TextBox((uint8_t *)text,37,81,20,15); //Increase textbox for more digits |
GregC | 0:2ebb70d2b0d9 | 81 | |
GregC | 0:2ebb70d2b0d9 | 82 | /* Set text properties to white and right aligned for the dynamic text */ |
GregC | 0:2ebb70d2b0d9 | 83 | textProperties.fontColor = COLOR_RED; |
GregC | 0:2ebb70d2b0d9 | 84 | textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; |
GregC | 0:2ebb70d2b0d9 | 85 | oled.SetTextProperties(&textProperties); |
GregC | 0:2ebb70d2b0d9 | 86 | |
GregC | 1:6bd53a378639 | 87 | /* Display Legends */ |
GregC | 1:6bd53a378639 | 88 | strcpy((char *) text,"Yaw"); |
GregC | 1:6bd53a378639 | 89 | oled.Label((uint8_t *)text,70,67); |
GregC | 1:6bd53a378639 | 90 | |
GregC | 0:2ebb70d2b0d9 | 91 | /* Format the value */ |
GregC | 0:2ebb70d2b0d9 | 92 | sprintf(text,"%i",z); |
GregC | 0:2ebb70d2b0d9 | 93 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
GregC | 1:6bd53a378639 | 94 | oled.TextBox((uint8_t *)text,70,81,20,15); //Increase textbox for more digits |
GregC | 0:2ebb70d2b0d9 | 95 | |
GregC | 0:2ebb70d2b0d9 | 96 | led1 = !led1; |
GregC | 0:2ebb70d2b0d9 | 97 | Thread::wait(250); |
GregC | 0:2ebb70d2b0d9 | 98 | } |
GregC | 0:2ebb70d2b0d9 | 99 | } |