Advanced magnetometer program example for Hexiwear featuring OLED Display

Dependencies:   FXOS8700 Hexi_OLED_SSD1351

This project demonstrates the use of the FXOS8700CQ Magnetometer sensor embedded in Hexiwear

Compile the project and copy the binary "Hexi_Magneto-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 magnitude values for the axis X, Y and Z will be displayed in real-time on the OLED Display.

Committer:
GregC
Date:
Wed Oct 19 22:55:10 2016 +0000
Revision:
0:3fcdc27ab845
Advanced magnetometer program example for Hexiwear featuring OLED Display

Who changed what in which revision?

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