voor Arne
Dependencies: ESP8266Interface FXAS21002 FXOS8700 Hexi_OLED_SSD1351
Fork of Hexi_Magneto-v2_Example by
main.cpp@3:e259f976e1fc, 2017-02-06 (annotated)
- Committer:
- blownelco
- Date:
- Mon Feb 06 08:21:05 2017 +0000
- Revision:
- 3:e259f976e1fc
- Parent:
- 2:3c1fe1657db9
voor arne;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GregC | 0:3fcdc27ab845 | 1 | #include "mbed.h" |
GregC | 0:3fcdc27ab845 | 2 | #include "FXOS8700.h" |
blownelco | 3:e259f976e1fc | 3 | #include "FXAS21002.h" |
GregC | 0:3fcdc27ab845 | 4 | #include "Hexi_OLED_SSD1351.h" |
GregC | 0:3fcdc27ab845 | 5 | #include "images.h" |
GregC | 0:3fcdc27ab845 | 6 | #include "string.h" |
blownelco | 2:3c1fe1657db9 | 7 | #include "ESP8266Interface.h" |
GregC | 0:3fcdc27ab845 | 8 | |
GregC | 0:3fcdc27ab845 | 9 | // Pin connections |
GregC | 0:3fcdc27ab845 | 10 | DigitalOut led1(LED_GREEN); // RGB LED |
blownelco | 2:3c1fe1657db9 | 11 | DigitalOut led2(PTA12); // RGB LED |
blownelco | 2:3c1fe1657db9 | 12 | |
blownelco | 3:e259f976e1fc | 13 | AnalogIn ain(PTB6); |
blownelco | 1:31908216b9ac | 14 | DigitalOut PWD(PTB2); |
blownelco | 1:31908216b9ac | 15 | DigitalOut G15(PTB11); |
blownelco | 1:31908216b9ac | 16 | |
blownelco | 3:e259f976e1fc | 17 | // Storage for the data from the sensor |
blownelco | 3:e259f976e1fc | 18 | float gyro_data[3]; float gyro_rms=0.0; |
blownelco | 1:31908216b9ac | 19 | |
blownelco | 1:31908216b9ac | 20 | |
blownelco | 3:e259f976e1fc | 21 | Serial pc(USBTX, USBRX); // Serial interface |
blownelco | 2:3c1fe1657db9 | 22 | //RawSerial wifi(PTD3, PTD2); |
GregC | 0:3fcdc27ab845 | 23 | FXOS8700 mag(PTC11, PTC10); |
blownelco | 3:e259f976e1fc | 24 | FXOS8700 accel(PTC11, PTC10); |
blownelco | 3:e259f976e1fc | 25 | //FXAS21002 gyro(PTC11,PTC10); werkt nog niet |
blownelco | 3:e259f976e1fc | 26 | |
blownelco | 3:e259f976e1fc | 27 | |
GregC | 0:3fcdc27ab845 | 28 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC) |
GregC | 0:3fcdc27ab845 | 29 | |
GregC | 0:3fcdc27ab845 | 30 | // Variables |
GregC | 0:3fcdc27ab845 | 31 | float mag_data[3]; // Storage for the data from the sensor |
GregC | 0:3fcdc27ab845 | 32 | float mag_rms=0.0; // RMS value from the sensor |
blownelco | 3:e259f976e1fc | 33 | |
blownelco | 3:e259f976e1fc | 34 | float accel_data[3]; float accel_rms=0.0; |
blownelco | 3:e259f976e1fc | 35 | |
GregC | 0:3fcdc27ab845 | 36 | float mx, my, mz; // Integer value from the sensor to be displayed |
GregC | 0:3fcdc27ab845 | 37 | const uint8_t *image1; // Pointer for the image to be displayed |
GregC | 0:3fcdc27ab845 | 38 | char text1[20]; // Text Buffer for dynamic value displayed |
GregC | 0:3fcdc27ab845 | 39 | char text2[20]; // Text Buffer for dynamic value displayed |
GregC | 0:3fcdc27ab845 | 40 | char text3[20]; // Text Buffer for dynamic value displayed |
blownelco | 3:e259f976e1fc | 41 | int steps; |
blownelco | 2:3c1fe1657db9 | 42 | |
blownelco | 2:3c1fe1657db9 | 43 | //void Rx_interrupt(); |
blownelco | 2:3c1fe1657db9 | 44 | //void read_line(); |
blownelco | 2:3c1fe1657db9 | 45 | |
blownelco | 2:3c1fe1657db9 | 46 | |
blownelco | 2:3c1fe1657db9 | 47 | // Circular buffers for serial TX and RX data - used by interrupt routines |
blownelco | 2:3c1fe1657db9 | 48 | const int buffer_size = 255; |
blownelco | 2:3c1fe1657db9 | 49 | // might need to increase buffer size for high baud rates |
blownelco | 2:3c1fe1657db9 | 50 | char rx_buffer[buffer_size+1]; |
blownelco | 2:3c1fe1657db9 | 51 | // Circular buffer pointers |
blownelco | 2:3c1fe1657db9 | 52 | // volatile makes read-modify-write atomic |
blownelco | 2:3c1fe1657db9 | 53 | volatile int rx_in=0; |
blownelco | 2:3c1fe1657db9 | 54 | volatile int rx_out=0; |
blownelco | 2:3c1fe1657db9 | 55 | // Line buffers for sprintf and sscanf |
blownelco | 2:3c1fe1657db9 | 56 | char tx_line[80]; |
blownelco | 2:3c1fe1657db9 | 57 | char rx_line[80]; |
blownelco | 2:3c1fe1657db9 | 58 | |
blownelco | 3:e259f976e1fc | 59 | //ESP8266Interface wifi(PTD3,PTD2,PTD11,"QBMT","zoranetwerk",9600); // TX,RX,Reset,SSID,Password,Baud |
blownelco | 2:3c1fe1657db9 | 60 | |
GregC | 0:3fcdc27ab845 | 61 | int main() { |
blownelco | 1:31908216b9ac | 62 | |
blownelco | 3:e259f976e1fc | 63 | steps = 0; |
blownelco | 2:3c1fe1657db9 | 64 | /* wifi3 board reset etc */ |
blownelco | 3:e259f976e1fc | 65 | // PWD = 1; |
blownelco | 3:e259f976e1fc | 66 | // G15 = 0; |
blownelco | 3:e259f976e1fc | 67 | |
blownelco | 3:e259f976e1fc | 68 | // printf("test"); |
blownelco | 2:3c1fe1657db9 | 69 | //ebsocket ws("ws://192.168.1.20:8888/ws"); |
blownelco | 1:31908216b9ac | 70 | |
blownelco | 2:3c1fe1657db9 | 71 | |
blownelco | 3:e259f976e1fc | 72 | accel.accel_config(); |
blownelco | 3:e259f976e1fc | 73 | |
GregC | 0:3fcdc27ab845 | 74 | // Configure Accelerometer FXOS8700, Magnetometer FXOS8700 |
GregC | 0:3fcdc27ab845 | 75 | mag.mag_config(); |
GregC | 0:3fcdc27ab845 | 76 | |
GregC | 0:3fcdc27ab845 | 77 | /* Setting pointer location of the 96 by 96 pixel bitmap */ |
GregC | 0:3fcdc27ab845 | 78 | image1 = Magneto; |
GregC | 0:3fcdc27ab845 | 79 | |
GregC | 0:3fcdc27ab845 | 80 | /* Turn on the backlight of the OLED Display */ |
GregC | 0:3fcdc27ab845 | 81 | // oled.DimScreenON(); |
GregC | 0:3fcdc27ab845 | 82 | |
GregC | 0:3fcdc27ab845 | 83 | /* Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0 */ |
blownelco | 3:e259f976e1fc | 84 | //oled.DrawImage(image1,0,0); |
blownelco | 3:e259f976e1fc | 85 | |
blownelco | 3:e259f976e1fc | 86 | /*Wifi initialisation commands */ |
blownelco | 3:e259f976e1fc | 87 | |
blownelco | 3:e259f976e1fc | 88 | // wifi.init(); //Reset |
blownelco | 3:e259f976e1fc | 89 | // printf("init done"); |
blownelco | 3:e259f976e1fc | 90 | // wifi.connect(); |
blownelco | 3:e259f976e1fc | 91 | // wifi.getIPAddress(); |
blownelco | 3:e259f976e1fc | 92 | |
blownelco | 3:e259f976e1fc | 93 | |
blownelco | 3:e259f976e1fc | 94 | |
blownelco | 3:e259f976e1fc | 95 | |
GregC | 0:3fcdc27ab845 | 96 | |
GregC | 0:3fcdc27ab845 | 97 | |
GregC | 0:3fcdc27ab845 | 98 | while (true) |
GregC | 0:3fcdc27ab845 | 99 | { |
blownelco | 3:e259f976e1fc | 100 | //gyro.acquire_gyro_data_dps(gyro_data); |
blownelco | 3:e259f976e1fc | 101 | //gyro_rms = sqrt(((gyro_data[0]*gyro_data[0])+(gyro_data[1]*gyro_data[1])+(gyro_data[2]*gyro_data[2]))/3); |
blownelco | 3:e259f976e1fc | 102 | |
blownelco | 3:e259f976e1fc | 103 | //pc.printf("gyro: %d",gyro_data[0]); |
blownelco | 3:e259f976e1fc | 104 | //pc.printf("gyro: %d",gyro_data[1]); |
blownelco | 3:e259f976e1fc | 105 | //pc.printf("gyro: %d",gyro_data[2]); |
blownelco | 3:e259f976e1fc | 106 | |
blownelco | 3:e259f976e1fc | 107 | accel.acquire_accel_data_g(accel_data); |
blownelco | 3:e259f976e1fc | 108 | accel_rms = sqrt(((accel_data[0]*accel_data[0])+(accel_data[1]*accel_data[1])+(accel_data[2]*accel_data[2]))/3); |
blownelco | 3:e259f976e1fc | 109 | 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); |
blownelco | 3:e259f976e1fc | 110 | wait(0.2); |
blownelco | 3:e259f976e1fc | 111 | if ( accel_data[0] > 1 ) |
blownelco | 3:e259f976e1fc | 112 | { |
blownelco | 3:e259f976e1fc | 113 | steps++; |
blownelco | 3:e259f976e1fc | 114 | } |
blownelco | 3:e259f976e1fc | 115 | |
blownelco | 3:e259f976e1fc | 116 | |
GregC | 0:3fcdc27ab845 | 117 | mag.acquire_mag_data_uT(mag_data); |
GregC | 0:3fcdc27ab845 | 118 | mag_rms = sqrt(((mag_data[0]*mag_data[0])+(mag_data[1]*mag_data[1])+(mag_data[2]*mag_data[2]))/3); |
blownelco | 3:e259f976e1fc | 119 | //printf("normalized: 0x%04X \n", ain.read_u16()); |
blownelco | 3:e259f976e1fc | 120 | //printf("Magnetometer \tX-Axis %4.2f \tY-Axis %4.2f \tZ-Axis %4.2f \tRMS %4.2f adc %d \n\n\r",mag_data[0],mag_data[1],mag_data[2],mag_rms,ain.read_u16()); |
blownelco | 3:e259f976e1fc | 121 | wait(0.2); |
GregC | 0:3fcdc27ab845 | 122 | mx = mag_data[0]; |
GregC | 0:3fcdc27ab845 | 123 | my = mag_data[1]; |
GregC | 0:3fcdc27ab845 | 124 | mz = mag_data[2]; |
GregC | 0:3fcdc27ab845 | 125 | |
GregC | 0:3fcdc27ab845 | 126 | /* Get OLED Class Default Text Properties */ |
GregC | 0:3fcdc27ab845 | 127 | oled_text_properties_t textProperties = {0}; |
GregC | 0:3fcdc27ab845 | 128 | oled.GetTextProperties(&textProperties); |
GregC | 0:3fcdc27ab845 | 129 | |
GregC | 0:3fcdc27ab845 | 130 | /* Set text properties to white and right aligned for the dynamic text */ |
GregC | 0:3fcdc27ab845 | 131 | textProperties.fontColor = COLOR_BLUE; |
GregC | 0:3fcdc27ab845 | 132 | textProperties.alignParam = OLED_TEXT_ALIGN_LEFT; |
GregC | 0:3fcdc27ab845 | 133 | oled.SetTextProperties(&textProperties); |
blownelco | 3:e259f976e1fc | 134 | |
blownelco | 3:e259f976e1fc | 135 | strcpy((char *) text1,"QBMT"); |
blownelco | 3:e259f976e1fc | 136 | oled.Label((uint8_t *)text1,35,0); |
blownelco | 3:e259f976e1fc | 137 | |
blownelco | 3:e259f976e1fc | 138 | /* Display Legends */ |
blownelco | 3:e259f976e1fc | 139 | |
blownelco | 3:e259f976e1fc | 140 | textProperties.fontColor = COLOR_WHITE; |
blownelco | 3:e259f976e1fc | 141 | strcpy((char *) text1,"STEPS:"); |
blownelco | 3:e259f976e1fc | 142 | oled.Label((uint8_t *)text1,3,15); |
blownelco | 3:e259f976e1fc | 143 | |
blownelco | 3:e259f976e1fc | 144 | /* Format the value */ |
blownelco | 3:e259f976e1fc | 145 | sprintf(text1,"%d",steps); |
blownelco | 3:e259f976e1fc | 146 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
blownelco | 3:e259f976e1fc | 147 | oled.TextBox((uint8_t *)text1,60,15,20,15); //Increase textbox for more digits |
blownelco | 3:e259f976e1fc | 148 | |
blownelco | 3:e259f976e1fc | 149 | |
GregC | 0:3fcdc27ab845 | 150 | |
GregC | 0:3fcdc27ab845 | 151 | /* Display Legends */ |
blownelco | 3:e259f976e1fc | 152 | strcpy((char *) text1,"Pressure:"); |
blownelco | 3:e259f976e1fc | 153 | oled.Label((uint8_t *)text1,3,32); |
blownelco | 3:e259f976e1fc | 154 | |
blownelco | 3:e259f976e1fc | 155 | /* Format the value */ |
blownelco | 3:e259f976e1fc | 156 | sprintf(text1,"%d",ain.read_u16()); |
blownelco | 3:e259f976e1fc | 157 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
blownelco | 3:e259f976e1fc | 158 | oled.TextBox((uint8_t *)text1,65,32,20,15); //Increase textbox for more digits |
blownelco | 3:e259f976e1fc | 159 | |
blownelco | 3:e259f976e1fc | 160 | |
blownelco | 3:e259f976e1fc | 161 | |
blownelco | 3:e259f976e1fc | 162 | |
blownelco | 3:e259f976e1fc | 163 | |
blownelco | 3:e259f976e1fc | 164 | /* Set text properties to white and right aligned for the dynamic text */ |
blownelco | 3:e259f976e1fc | 165 | textProperties.fontColor = COLOR_WHITE; |
blownelco | 3:e259f976e1fc | 166 | textProperties.alignParam = OLED_TEXT_ALIGN_LEFT; |
blownelco | 3:e259f976e1fc | 167 | oled.SetTextProperties(&textProperties); |
blownelco | 3:e259f976e1fc | 168 | |
blownelco | 3:e259f976e1fc | 169 | |
GregC | 0:3fcdc27ab845 | 170 | strcpy((char *) text1,"X-Axis (uT):"); |
GregC | 0:3fcdc27ab845 | 171 | oled.Label((uint8_t *)text1,3,45); |
GregC | 0:3fcdc27ab845 | 172 | |
GregC | 0:3fcdc27ab845 | 173 | /* Format the value */ |
GregC | 0:3fcdc27ab845 | 174 | sprintf(text1,"%4.2f",mx); |
GregC | 0:3fcdc27ab845 | 175 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
GregC | 0:3fcdc27ab845 | 176 | oled.TextBox((uint8_t *)text1,70,45,20,15); //Increase textbox for more digits |
GregC | 0:3fcdc27ab845 | 177 | |
GregC | 0:3fcdc27ab845 | 178 | /* Set text properties to white and right aligned for the dynamic text */ |
GregC | 0:3fcdc27ab845 | 179 | textProperties.fontColor = COLOR_GREEN; |
GregC | 0:3fcdc27ab845 | 180 | textProperties.alignParam = OLED_TEXT_ALIGN_LEFT; |
GregC | 0:3fcdc27ab845 | 181 | oled.SetTextProperties(&textProperties); |
GregC | 0:3fcdc27ab845 | 182 | |
GregC | 0:3fcdc27ab845 | 183 | /* Display Legends */ |
GregC | 0:3fcdc27ab845 | 184 | strcpy((char *) text2,"Y-Axis (uT):"); |
GregC | 0:3fcdc27ab845 | 185 | oled.Label((uint8_t *)text2,3,62); |
GregC | 0:3fcdc27ab845 | 186 | |
GregC | 0:3fcdc27ab845 | 187 | /* Format the value */ |
GregC | 0:3fcdc27ab845 | 188 | sprintf(text2,"%4.2f",my); |
GregC | 0:3fcdc27ab845 | 189 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
GregC | 0:3fcdc27ab845 | 190 | oled.TextBox((uint8_t *)text2,70,62,20,15); //Increase textbox for more digits |
GregC | 0:3fcdc27ab845 | 191 | |
GregC | 0:3fcdc27ab845 | 192 | /* Set text properties to white and right aligned for the dynamic text */ |
GregC | 0:3fcdc27ab845 | 193 | textProperties.fontColor = COLOR_RED; |
GregC | 0:3fcdc27ab845 | 194 | textProperties.alignParam = OLED_TEXT_ALIGN_LEFT; |
GregC | 0:3fcdc27ab845 | 195 | oled.SetTextProperties(&textProperties); |
GregC | 0:3fcdc27ab845 | 196 | |
GregC | 0:3fcdc27ab845 | 197 | /* Display Legends */ |
GregC | 0:3fcdc27ab845 | 198 | strcpy((char *) text3,"Z-Axis (uT):"); |
GregC | 0:3fcdc27ab845 | 199 | oled.Label((uint8_t *)text3,3,79); |
GregC | 0:3fcdc27ab845 | 200 | |
GregC | 0:3fcdc27ab845 | 201 | /* Format the value */ |
GregC | 0:3fcdc27ab845 | 202 | sprintf(text3,"%4.2f",mz); |
GregC | 0:3fcdc27ab845 | 203 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
GregC | 0:3fcdc27ab845 | 204 | oled.TextBox((uint8_t *)text3,70,79,20,15); //Increase textbox for more digits |
GregC | 0:3fcdc27ab845 | 205 | |
GregC | 0:3fcdc27ab845 | 206 | led1 = !led1; |
GregC | 0:3fcdc27ab845 | 207 | Thread::wait(250); |
GregC | 0:3fcdc27ab845 | 208 | } |
blownelco | 2:3c1fe1657db9 | 209 | } |
blownelco | 2:3c1fe1657db9 | 210 | |
blownelco | 2:3c1fe1657db9 | 211 | |
blownelco | 2:3c1fe1657db9 | 212 | |
blownelco | 2:3c1fe1657db9 | 213 | |
blownelco | 2:3c1fe1657db9 | 214 | |
blownelco | 2:3c1fe1657db9 | 215 | |
blownelco | 2:3c1fe1657db9 | 216 |