
Advanced accelerometer program example for Hexiwear featuring OLED Display
Dependencies: FXOS8700 Hexi_OLED_SSD1351
This project demonstrates the use of the FXOS8700CQ Accelerometer sensor embedded in Hexiwear
Compile the project and copy the binary "Hexi_Accelero-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 value of the linear acceleration for the axis X, Y and Z will be displayed in real-time on the OLED Display.
main.cpp@0:68865a21b70d, 2016-10-19 (annotated)
- Committer:
- GregC
- Date:
- Wed Oct 19 22:51:43 2016 +0000
- Revision:
- 0:68865a21b70d
Advanced accelerometer program example for Hexiwear featuring OLED Display
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GregC | 0:68865a21b70d | 1 | #include "mbed.h" |
GregC | 0:68865a21b70d | 2 | #include "FXOS8700.h" |
GregC | 0:68865a21b70d | 3 | #include "Hexi_OLED_SSD1351.h" |
GregC | 0:68865a21b70d | 4 | #include "images.h" |
GregC | 0:68865a21b70d | 5 | #include "string.h" |
GregC | 0:68865a21b70d | 6 | |
GregC | 0:68865a21b70d | 7 | // Pin connections |
GregC | 0:68865a21b70d | 8 | DigitalOut led1(LED_GREEN); // RGB LED |
GregC | 0:68865a21b70d | 9 | Serial pc(USBTX, USBRX); // Serial interface |
GregC | 0:68865a21b70d | 10 | FXOS8700 accel(PTC11, PTC10); |
GregC | 0:68865a21b70d | 11 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC) |
GregC | 0:68865a21b70d | 12 | |
GregC | 0:68865a21b70d | 13 | // Variables |
GregC | 0:68865a21b70d | 14 | float accel_data[3]; // Storage for the data from the sensor |
GregC | 0:68865a21b70d | 15 | float accel_rms=0.0; // RMS value from the sensor |
GregC | 0:68865a21b70d | 16 | float ax, ay, az; // Integer value from the sensor to be displayed |
GregC | 0:68865a21b70d | 17 | const uint8_t *image1; // Pointer for the image1 to be displayed |
GregC | 0:68865a21b70d | 18 | char text1[20]; // Text Buffer for dynamic value displayed |
GregC | 0:68865a21b70d | 19 | char text2[20]; // Text Buffer for dynamic value displayed |
GregC | 0:68865a21b70d | 20 | char text3[20]; // Text Buffer for dynamic value displayed |
GregC | 0:68865a21b70d | 21 | |
GregC | 0:68865a21b70d | 22 | int main() { |
GregC | 0:68865a21b70d | 23 | |
GregC | 0:68865a21b70d | 24 | // Configure Accelerometer FXOS8700, Magnetometer FXOS8700 |
GregC | 0:68865a21b70d | 25 | accel.accel_config(); |
GregC | 0:68865a21b70d | 26 | |
GregC | 0:68865a21b70d | 27 | // Setting pointer location of the 96 by 96 pixel bitmap |
GregC | 0:68865a21b70d | 28 | image1 = Accelero; |
GregC | 0:68865a21b70d | 29 | |
GregC | 0:68865a21b70d | 30 | // Dimm Down OLED backlight |
GregC | 0:68865a21b70d | 31 | // oled.DimScreenON(); |
GregC | 0:68865a21b70d | 32 | |
GregC | 0:68865a21b70d | 33 | // Fill 96px by 96px Screen with 96px by 96px Image starting at x=0,y=0 |
GregC | 0:68865a21b70d | 34 | oled.DrawImage(image1,0,0); |
GregC | 0:68865a21b70d | 35 | |
GregC | 0:68865a21b70d | 36 | |
GregC | 0:68865a21b70d | 37 | while (true) |
GregC | 0:68865a21b70d | 38 | { |
GregC | 0:68865a21b70d | 39 | |
GregC | 0:68865a21b70d | 40 | accel.acquire_accel_data_g(accel_data); |
GregC | 0:68865a21b70d | 41 | accel_rms = sqrt(((accel_data[0]*accel_data[0])+(accel_data[1]*accel_data[1])+(accel_data[2]*accel_data[2]))/3); |
GregC | 0:68865a21b70d | 42 | printf("Accelerometer \tX-Axis %4.2f \tY-Axis %4.2f \tZ-Axis %4.2f \tRMS %4.2f\n\r",accel_data[0],accel_data[1],accel_data[2],accel_rms); |
GregC | 0:68865a21b70d | 43 | wait(0.01); |
GregC | 0:68865a21b70d | 44 | ax = accel_data[0]; |
GregC | 0:68865a21b70d | 45 | ay = accel_data[1]; |
GregC | 0:68865a21b70d | 46 | az = accel_data[2]; |
GregC | 0:68865a21b70d | 47 | |
GregC | 0:68865a21b70d | 48 | /* Get OLED Class Default Text Properties */ |
GregC | 0:68865a21b70d | 49 | oled_text_properties_t textProperties = {0}; |
GregC | 0:68865a21b70d | 50 | oled.GetTextProperties(&textProperties); |
GregC | 0:68865a21b70d | 51 | |
GregC | 0:68865a21b70d | 52 | /* Set text properties to white and right aligned for the dynamic text */ |
GregC | 0:68865a21b70d | 53 | textProperties.fontColor = COLOR_BLUE; |
GregC | 0:68865a21b70d | 54 | textProperties.alignParam = OLED_TEXT_ALIGN_LEFT; |
GregC | 0:68865a21b70d | 55 | oled.SetTextProperties(&textProperties); |
GregC | 0:68865a21b70d | 56 | |
GregC | 0:68865a21b70d | 57 | /* Display Legends */ |
GregC | 0:68865a21b70d | 58 | strcpy((char *) text1,"X-Axis (g):"); |
GregC | 0:68865a21b70d | 59 | oled.Label((uint8_t *)text1,3,45); |
GregC | 0:68865a21b70d | 60 | |
GregC | 0:68865a21b70d | 61 | /* Format the value */ |
GregC | 0:68865a21b70d | 62 | sprintf(text1,"%4.2f",ax); |
GregC | 0:68865a21b70d | 63 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
GregC | 0:68865a21b70d | 64 | oled.TextBox((uint8_t *)text1,70,45,20,15); //Increase textbox for more digits |
GregC | 0:68865a21b70d | 65 | |
GregC | 0:68865a21b70d | 66 | /* Set text properties to white and right aligned for the dynamic text */ |
GregC | 0:68865a21b70d | 67 | textProperties.fontColor = COLOR_GREEN; |
GregC | 0:68865a21b70d | 68 | textProperties.alignParam = OLED_TEXT_ALIGN_LEFT; |
GregC | 0:68865a21b70d | 69 | oled.SetTextProperties(&textProperties); |
GregC | 0:68865a21b70d | 70 | |
GregC | 0:68865a21b70d | 71 | /* Display Legends */ |
GregC | 0:68865a21b70d | 72 | strcpy((char *) text2,"Y-Axis (g):"); |
GregC | 0:68865a21b70d | 73 | oled.Label((uint8_t *)text2,3,62); |
GregC | 0:68865a21b70d | 74 | |
GregC | 0:68865a21b70d | 75 | /* Format the value */ |
GregC | 0:68865a21b70d | 76 | sprintf(text2,"%4.2f",ay); |
GregC | 0:68865a21b70d | 77 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
GregC | 0:68865a21b70d | 78 | oled.TextBox((uint8_t *)text2,70,62,20,15); //Increase textbox for more digits |
GregC | 0:68865a21b70d | 79 | |
GregC | 0:68865a21b70d | 80 | /* Set text properties to white and right aligned for the dynamic text */ |
GregC | 0:68865a21b70d | 81 | textProperties.fontColor = COLOR_RED; |
GregC | 0:68865a21b70d | 82 | textProperties.alignParam = OLED_TEXT_ALIGN_LEFT; |
GregC | 0:68865a21b70d | 83 | oled.SetTextProperties(&textProperties); |
GregC | 0:68865a21b70d | 84 | |
GregC | 0:68865a21b70d | 85 | /* Display Legends */ |
GregC | 0:68865a21b70d | 86 | strcpy((char *) text3,"Z-Axis (g):"); |
GregC | 0:68865a21b70d | 87 | oled.Label((uint8_t *)text3,3,79); |
GregC | 0:68865a21b70d | 88 | |
GregC | 0:68865a21b70d | 89 | /* Format the value */ |
GregC | 0:68865a21b70d | 90 | sprintf(text3,"%4.2f",az); |
GregC | 0:68865a21b70d | 91 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
GregC | 0:68865a21b70d | 92 | oled.TextBox((uint8_t *)text3,70,79,20,15); //Increase textbox for more digits |
GregC | 0:68865a21b70d | 93 | |
GregC | 0:68865a21b70d | 94 | led1 = !led1; |
GregC | 0:68865a21b70d | 95 | Thread::wait(250); |
GregC | 0:68865a21b70d | 96 | } |
GregC | 0:68865a21b70d | 97 | } |