Hexiwear / Mbed OS Hexi_Accelero-v2_Example

Dependencies:   FXOS8700 Hexi_OLED_SSD1351

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "FXOS8700.h"
00003 #include "Hexi_OLED_SSD1351.h"
00004 #include "images.h"
00005 #include "string.h"
00006 
00007 // Pin connections
00008 DigitalOut led1(LED_GREEN); // RGB LED
00009 Serial pc(USBTX, USBRX); // Serial interface
00010 FXOS8700 accel(PTC11, PTC10);
00011 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC)
00012 
00013 // Variables
00014 float accel_data[3]; // Storage for the data from the sensor
00015 float accel_rms=0.0; // RMS value from the sensor
00016 float ax, ay, az; // Integer value from the sensor to be displayed
00017 const uint8_t *image1; // Pointer for the image1 to be displayed
00018 char text1[20]; // Text Buffer for dynamic value displayed
00019 char text2[20]; // Text Buffer for dynamic value displayed
00020 char text3[20]; // Text Buffer for dynamic value displayed
00021 
00022 int main() {
00023         
00024     // Configure Accelerometer FXOS8700, Magnetometer FXOS8700
00025     accel.accel_config();
00026     
00027     // Setting pointer location of the 96 by 96 pixel bitmap
00028     image1  = Accelero;
00029 
00030     // Dimm Down OLED backlight
00031 //    oled.DimScreenON();
00032     
00033     // Fill 96px by 96px Screen with 96px by 96px Image starting at x=0,y=0
00034     oled.DrawImage(image1,0,0);  
00035 
00036     
00037     while (true) 
00038     {
00039     
00040       accel.acquire_accel_data_g(accel_data);
00041       accel_rms = sqrt(((accel_data[0]*accel_data[0])+(accel_data[1]*accel_data[1])+(accel_data[2]*accel_data[2]))/3);
00042       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);
00043       wait(0.01);
00044       ax = accel_data[0];
00045       ay = accel_data[1];
00046       az = accel_data[2];             
00047 
00048       /* Get OLED Class Default Text Properties */
00049       oled_text_properties_t textProperties = {0};
00050       oled.GetTextProperties(&textProperties); 
00051       
00052       /* Set text properties to white and right aligned for the dynamic text */
00053       textProperties.fontColor = COLOR_BLUE;
00054       textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
00055       oled.SetTextProperties(&textProperties);  
00056       
00057       /* Display Legends */
00058       strcpy((char *) text1,"X-Axis (g):");
00059       oled.Label((uint8_t *)text1,3,45);      
00060       
00061       /* Format the value */
00062       sprintf(text1,"%4.2f",ax);
00063       /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
00064       oled.TextBox((uint8_t *)text1,70,45,20,15); //Increase textbox for more digits
00065 
00066       /* Set text properties to white and right aligned for the dynamic text */ 
00067       textProperties.fontColor = COLOR_GREEN;
00068       textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
00069       oled.SetTextProperties(&textProperties);  
00070 
00071       /* Display Legends */
00072       strcpy((char *) text2,"Y-Axis (g):");
00073       oled.Label((uint8_t *)text2,3,62); 
00074       
00075       /* Format the value */
00076       sprintf(text2,"%4.2f",ay);
00077       /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
00078       oled.TextBox((uint8_t *)text2,70,62,20,15); //Increase textbox for more digits
00079       
00080       /* Set text properties to white and right aligned for the dynamic text */ 
00081       textProperties.fontColor = COLOR_RED;
00082       textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
00083       oled.SetTextProperties(&textProperties);  
00084       
00085       /* Display Legends */
00086       strcpy((char *) text3,"Z-Axis (g):");
00087       oled.Label((uint8_t *)text3,3,79);       
00088       
00089       /* Format the value */
00090       sprintf(text3,"%4.2f",az);
00091       /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
00092       oled.TextBox((uint8_t *)text3,70,79,20,15); //Increase textbox for more digits
00093 
00094       led1 = !led1;
00095       Thread::wait(250);
00096     }
00097 }