Works

Dependencies:   BMP180 BNO055_fusion Fonts GPSISR HTU21D SDFileSystem UniGraphic mbed uGUI

Fork of Bicycl_Computer_NUCLEO-F411RE by Darren Ulrich

Revision:
11:869a102dd31c
Parent:
10:a3cb458d7b1c
Child:
12:49600de70963
diff -r a3cb458d7b1c -r 869a102dd31c main.cpp
--- a/main.cpp	Mon Feb 20 19:10:30 2017 +0000
+++ b/main.cpp	Mon Feb 27 22:37:44 2017 +0000
@@ -30,10 +30,13 @@
 #include "GPSISR.h"
 
 
+#include "ArialR16x17.h"
+#include "Arial24x23i.h"
+#include "Arial28x28.h"
+#include "Neu44x36.h"
+#include "SCProSB31x55.h"
 #include "Arial12x12.h"
-#include "Arial24x23.h"
-#include "Arial28x28.h"
-#include "Arial43x48_numb.h"
+#include "ArialR20x20.h"
 
 //#define PIN_CS_TSC      PA_9 
 //#define PIN_TSC_INTR    PA_8
@@ -58,6 +61,8 @@
 #define PIN_RX_GPS      PA_12 //GPS Shield RX pin
 #define PIN_TX_GPS      PA_11 //GPS Shield TX pin
 
+#define PI                  3.14159265358979f
+
 
 BNO055_ID_INF_TypeDef       bno055_id_inf;
 BNO055_EULER_TypeDef        euler_angles;
@@ -90,7 +95,7 @@
 BNO055 imu(I2C_SDA, I2C_SCL, PIN_RESET);  // Reset =D7, addr = BNO055_G_CHIP_ADDR, mode = MODE_NDOF <- as default
 
 // Humidity and Temperature
-HTU21D temphumid(I2C_SDA, I2C_SCL);
+HTU21D humid(I2C_SDA, I2C_SCL);
 
 // Pressure
 BMP180 bmp180(I2C_SDA, I2C_SCL);
@@ -101,145 +106,13 @@
 // Set up serial interrupe service handler for gps characters.
 GPS MyGPS(PIN_TX_GPS,PIN_RX_GPS, 9600);
 
-
-int page = 0 ;
-int numPage = 2 ;
-
-void initTFT(void)
-{
-    //Configure the display driver
-    TFT.BusEnable(true) ;
-    TFT.FastWindow(true) ;
-    TFT.background(Black);
-    TFT.foreground(White);
-    wait(0.01) ;
-    TFT.cls();
-    TFT.BusEnable(false) ;
-}
-
-void screen1(void) // Welcome Screen
-{
-    TFT.BusEnable(true) ;
-    //backlight = 0 ;
-    TFT.background(White) ;
-    wait(0.1) ;
-    TFT.cls() ;
-    wait(0.1) ;
-    
-    TFT.set_font((unsigned char*) Arial24x23);
-    TFT.foreground(Red) ;
-    TFT.locate(80, 40) ;
-    TFT.printf("MBED") ;
-    TFT.foreground(Blue);
-    TFT.locate(60, 80) ;
-    TFT.printf("2.8\"TFT") ; 
-    TFT.locate(40, 120) ;
-    TFT.printf("with touch") ;
-    TFT.foreground(Black);
-    TFT.set_font((unsigned char*) Arial12x12);
-    TFT.foreground(Blue) ;
-    TFT.locate(30, 180) ;
-    TFT.printf("This program is running on") ;
-    TFT.locate(30, 200) ;
-    TFT.printf("ST Nucleo F411RE with") ;
-    TFT.locate(30, 220) ;
-    TFT.printf("a program developed in mbed") ;
-    TFT.foreground(Green) ;
-    TFT.locate(30, 260) ;
-    TFT.printf("To advance demo page, touch") ;
-    TFT.locate(30, 280) ;
-    TFT.printf("and hold right side of screen") ;
-    TFT.locate(30, 300) ;
-    TFT.printf("until the next screen starts") ;
-    TFT.BusEnable(false) ;
-    //backlight = 1 ;
-}
+void Draw_Compass_Rose(void);
+void arrow(int x2, int y2, int x1, int y1, int alength, int awidth, int colour);
 
-void screen2(void) // Graphics
-{
-    //Draw some graphics
-    int i, x[2], y[2] ;
-    //backlight = 0 ;
-    TFT.BusEnable(true) ;
-    TFT.background(Black);
-    wait(0.1) ;
-    TFT.foreground(White);
-    wait(0.1) ;
-    TFT.cls() ;
-    wait(0.1) ;
-    TFT.set_font((unsigned char*) Arial12x12);
-    TFT.locate(90,0);
-    TFT.printf("Graphics");
-    
-    x[0] = 25 ; x[1] = 224 ;
-    y[0] = 20 ; y[1] = 219 ;
-    for (i = 20 ; i < 220 ; i += 10) {
-        TFT.line(i+5, y[0], i+5, y[1], Blue) ;
-        TFT.line(x[0], i, x[1], i, Blue) ;
-    }
-    TFT.line(125, y[0], 125, y[1], Green) ;
-    TFT.line(x[0], 120, x[1], 120, Green) ;
-    TFT.rect(x[0],y[0], x[1], y[1], Green) ;
-    TFT.locate(10, 20) ;
-    TFT.printf("V") ;
-    TFT.locate(0, 115) ;
-    TFT.printf("0.0") ;
-    TFT.locate(115, 225) ;
-    TFT.printf("0.0") ;
-    TFT.locate(215, 225) ;
-    TFT.printf("T") ;
+const int centreX = 120;
+const int centreY = 150;
+const int radius  = 40; 
 
-    double s;
-    for (int i = x[0]; i < 225; i++) {
-        s = 40 * sin((long double)i / 20);
-        TFT.pixel(i, 120 + (int)s, White);
-    }
-    
-    TFT.fillrect(10, 240, 229, 309, White) ;
-    TFT.rect(10, 240, 229, 309, Red) ;
-    TFT.rect(11, 241, 228, 308, Red) ;
-    
-    TFT.background(White) ;
-    TFT.foreground(Black) ;
-    TFT.locate(20, 250) ;
-    TFT.printf("With QVGA resolution") ;
-    TFT.locate(20, 270) ;
-    TFT.printf("simple graphics drawing") ;
-    TFT.locate(20, 290) ;
-    TFT.printf("capability is provided") ;
-    TFT.BusEnable(false) ;
-    //backlight = 1 ;
-}    
-
-double clip(double src)
-{
-    double value ;
-    value = src ;
-    if (value < 0.0) {
-        value = 0.0 ;
-    } else if (value > 2.0) {
-        value = 2.0 ;
-    }
-    return( value ) ;
-}
-
-void incPage(void)
-{
-    page++ ;
-    if (page >= numPage) {
-        page = 0 ;
-    }
-}
-
-void decPage(void) 
-{
-    page-- ;
-    if (page < 0) {
-        page = numPage - 1 ;
-    }
-}
-    
-   
 int main()
 {
     TFT.BusEnable(true) ;
@@ -251,90 +124,161 @@
     const int refresh_Time = 2000; //refresh time in ms
     refresh_Timer.start();  //starts the clock on the timer
     //backlight = 0 ;
-    TFT.set_font((unsigned char*) Arial24x23);
+    TFT.set_font((unsigned char*) Arial24x23i);
     TFT.fillrect(2, 2, 237, 317, White);
     TFT.foreground(Blue);
     TFT.background(White);
     wait(0.1);
-    
    
+    Draw_Compass_Rose(); 
+        
     while (1) { 
-     //check if we recieved a new message from GPS, if so, attempt to parse it,
+             
+                TFT.set_font((unsigned char*) ArialR20x20);
+                //check if we recieved a new message from GPS, if so, attempt to parse it,
         if (refresh_Timer.read_ms() >= refresh_Time) {
             refresh_Timer.reset();
                         
+                        if (bmp180.init() != 0) {
+                                //pc.printf("Error communicating with BMP180\n");
+                                } else {
+                                //pc.printf("Initialized BMP180\n");
+                                bmp180.startTemperature();
+                                wait(0.1);     // Wait for conversion to complete
+                                float temp;
+                                if(bmp180.getTemperature(&temp) != 0) {
+                                        //pc.printf("Error getting temperature\n");
+                                }
+                        //pc.printf("Temperature is : %f", temp);
+                        TFT.locate(4, 26) ;
+                        TFT.printf("%.1fF", ((temp* 9.0) / 5.0 + 32));
+                        }
+                        int ftemp = humid.sample_ftemp();
+                        int humidity = humid.sample_humid();
+                        TFT.locate(140, 26) ;
+                        TFT.printf("%d%%RH",humidity);
+                        //pc.printf("HTU21D Temp: %d",ftemp);
+                        //pc.printf("HTU21D Humidity: %d",humidity);
+                        
+                        
+                        if (imu.chip_ready() == 0){
+                            pc.printf("Bosch BNO055 is NOT avirable!!\r\n");
+                } else {
+                            printf("Cal %d", imu.read_calib_status());
+                            if (imu.read_calib_status() > 0x0){
+                                TFT.foreground(White);
+                                TFT.locate(4, 260) ;
+                                TFT.printf("No Data");
+                                TFT.foreground(Blue);
+                                imu.get_Euler_Angles(&euler_angles);
+                                imu.get_quaternion(&quaternion);
+                                TFT.locate(4, 260) ;
+                                TFT.printf("%.1f",euler_angles.h);
+                                //pc.printf("H %.1f",euler_angles.h);
+                                //pc.printf("R %.1f",euler_angles.r);
+                                //pc.printf("P %.1f",euler_angles.p);
+                            
+                            } else {
+                                    TFT.locate(4, 260) ;
+                                    TFT.printf("No Data");
+                            }
+                        }
+                         
+                         float last_dx;
+                         float last_dy;
+                         float angle = int(euler_angles.h * 180/PI); // Convert radians to degrees for more a more usual result
+                         // For the screen -X = up and +X = down and -Y = left and +Y = right, so does not follow coordinate conventions
+                         float dx = (0.7*radius * cos((angle-90)*PI/180)) + centreX;  // calculate X position for the screen coordinates - can be confusing!
+             float dy = (0.7*radius * sin((angle-90)*PI/180)) + centreY;  // calculate Y position for the screen coordinates - can be confusing!
+             arrow(last_dx,last_dy, centreX, centreY, 2,2,White);      // Erase last arrow      
+             arrow(dx,dy, centreX, centreY, 2, 2,Blue);               // Draw arrow in new position
+             last_dx = dx; 
+             last_dy = dy;
+                        
+                        
             if (MyGPS.dataready()) {
                 MyGPS.read();
-                pc.printf("NMEA has valid data");
-                pc.printf("Sats : %d \n", MyGPS.buffer.satellites);
-                pc.printf("%d-%d-%d\n", MyGPS.buffer.month, MyGPS.buffer.day, MyGPS.buffer.year);
-                pc.printf("%d:%d:%d\n", MyGPS.buffer.hours, MyGPS.buffer.minutes, MyGPS.buffer.seconds);
+                //pc.printf("NMEA has valid data");
+                //pc.printf("Sats : %d \n", MyGPS.buffer.satellites);
+                //pc.printf("%d-%d-%d\n", MyGPS.buffer.month, MyGPS.buffer.day, MyGPS.buffer.year);
+                //pc.printf("%d:%d:%d\n", MyGPS.buffer.hours, MyGPS.buffer.minutes, MyGPS.buffer.seconds);
                 
-                
-                TFT.locate(2, 2) ;
+                                TFT.foreground(White);
+                                TFT.locate(4, 2) ;
+                                TFT.printf("No Data");
+                TFT.foreground(Blue);
+                TFT.locate(4, 2) ;
                 TFT.printf("%d-%d-%d", MyGPS.buffer.month, MyGPS.buffer.day, MyGPS.buffer.year);
-                TFT.locate(2, 26) ;
+                TFT.locate(140, 2) ;
                 TFT.printf("%d:%d:%d", MyGPS.buffer.hours, MyGPS.buffer.minutes, MyGPS.buffer.seconds);
+                                TFT.locate(140, 260) ;
+                TFT.printf("%.1fft", MyGPS.buffer.altitude/0.3048);
+                                TFT.locate(4, 280) ;
+                                int degree;
+                                int minutes;
+                                int seconds;
+                                degree = (int)MyGPS.buffer.longitude;
+                                minutes = (int) ( (MyGPS.buffer.longitude - (double)degree) * 60.0);
+                                seconds = (int) ( (MyGPS.buffer.longitude - (double)degree - (double)minutes / 60.0) * 60.0 * 60.0 );
+                TFT.printf("%d %d' %d\"lon", degree, minutes,seconds);
+                                TFT.locate(4, 300) ;
+                                degree = (int)MyGPS.buffer.latitude;
+                                minutes = (int) ( (MyGPS.buffer.latitude - (double)degree) * 60.0);
+                                seconds = (int) ( (MyGPS.buffer.latitude - (double)degree - (double)minutes / 60.0) * 60.0 * 60.0 );
+                TFT.printf("%d %d' %d\"lat", degree, minutes,seconds);
+                                TFT.set_font((unsigned char*) SCProSB31x55);     
+                TFT.locate(120, 200) ;
+                TFT.printf("%.1f", MyGPS.buffer.speed);    
                                 
             }
             else {
-                 pc.printf("NMEA has no valid data");
+                                TFT.locate(4, 2) ;
+                                TFT.printf("No Data");
+                //pc.printf("NMEA has no valid data");
             }     
-                
         }            
     }  
-   
-    // uint16_t x, y, z ;
-    int prevPage = 99 ;
-    bool waitTouch = false ;
-    point p;
-    
-    printf("Hello World\n") ;
-   
-    // screen0() ;
-        
-    printf("Program Started!\n\r") ;
-    
-    for(;;) {
-//        printf("TFT width = %d, height = %d\n\r", TFT.width(), TFT.height()) ;
-        // tft_cs = 0 ;
-        switch(page) {
-        case 0:
-            if (prevPage != page) {
-                screen1() ;
-            }
-            waitTouch = true ;
-            break ;
-        case 1:
-            if (prevPage != page) {
-                screen2() ; 
-            }
-            waitTouch = true ;
-            break ;
-        default:
-            page = 0 ; 
-            break ;
-        }
-        prevPage = page ;
+}
+
+void arrow(int x2, int y2, int x1, int y1, int alength, int awidth, int colour) {
+  float distance;
+  int dx, dy, x2o,y2o,x3,y3,x4,y4,k;
+  distance = sqrt(pow((double)(x1 - x2),2) + pow((double)(y1 - y2), 2));
+  dx = x2 + (x1 - x2) * alength / distance;
+  dy = y2 + (y1 - y2) * alength / distance;
+  k = awidth / alength;
+  x2o = x2 - dx;
+  y2o = dy - y2;
+  x3 = y2o * k + dx;
+  y3 = x2o * k + dy;
+  x4 = dx - y2o * k;
+  y4 = dy - x2o * k;
+  TFT.line(x1, y1, x2, y2, colour);
+  TFT.line(x1, y1, dx, dy, colour);
+  TFT.line(x3, y3, x4, y4, colour);
+  TFT.line(x3, y3, x2, y2, colour);
+  TFT.line(x2, y2, x4, y4, colour);
+} 
 
-        // tft_cs = 1 ;
-        do {
-            TSC.getTouch(p);
-            //printf("Touched X %d\n\r", p.x) ;
-            //printf("Touched Y %d\n\r", p.y) ;
-            //printf("Touched Z %d\n\r", p.z) ;
-            wait(3) ;
-            if (p.z > __PRESURE) {
-                if (p.y < 30000) { // left
-                    decPage() ;
-                } else if (p.y > 90000) { // right
-                    incPage() ;
-                }
-                waitTouch = false ;
-            }
-            //tsc_cs = 1 ;
-        } 
-        while(waitTouch != false) ;
-        //wait(1) ;
-    }
+void Draw_Compass_Rose(void) {
+  int dxo, dyo, dxi, dyi;
+  TFT.circle(centreX,centreY,radius,Blue);  // Draw compass circle
+  for (float i = 0; i <360; i = i + 22.5) {
+    dxo = radius * cos(i*3.14/180);
+    dyo = radius * sin(i*3.14/180);
+    dxi = dxo * 0.95;
+    dyi = dyo * 0.95;
+    TFT.line(dxi+centreX,dyi+centreY,dxo+centreX,dyo+centreY,Blue);   
+  }
+    TFT.set_font((unsigned char*) Arial12x12);
+  TFT.foreground(Blue);
+  TFT.background(White);
+    TFT.locate((centreX-2),(centreY-24));
+  TFT.printf("N");
+  TFT.locate((centreX-2),(centreY+17));
+  TFT.printf("S");
+  TFT.locate((centreX+19),(centreY-3));
+  TFT.printf("E");
+  TFT.locate((centreX-23),(centreY-3));
+  TFT.printf("W");
 }
\ No newline at end of file