Works

Dependencies:   BMP180 BNO055_fusion Fonts GPSISR HTU21D SDFileSystem UniGraphic mbed uGUI

Fork of Bicycl_Computer_NUCLEO-F411RE by Darren Ulrich

main.cpp

Committer:
trevieze
Date:
2017-03-03
Revision:
14:4f0ebc5a4f00
Parent:
13:1a9c0f9d7128
Child:
15:b174ec6e3ca0

File content as of revision 14:4f0ebc5a4f00:

/* mbed main.cpp to test adafruit 2.8" TFT LCD shiled w Touchscreen
 * Copyright (c) 2014, 2015 Motoo Tanaka @ Design Methodology Lab
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
 
 /* 
  * Note: This program is derived from the SeeeStudioTFTv2 program.
  * Although both program share same ILI9341 TFT driver,
  * the touch sensor was not same with the Display I purchased from Akizuki.
  * http://akizukidenshi.com/catalog/g/gM-07747/
  * The touch sensor on the display is STMPE610,
  * so I hacked the minimum spi driver for it (polling mode only).
  */

#include "mbed.h"
#include <math.h>
#include "ILI9341.h"
#include "SeeedStudioTFTv2.h"
#include "BNO055.h"
#include "HTU21D.h"
#include "BMP180.h"
#include "SDFileSystem.h"
#include "GPSISR.h"
#include "nav.h"


#include "ArialR16x17.h"
#include "Arial24x23i.h"
#include "Arial28x28.h"
#include "Neu44x36.h"
#include "SCProSB31x55.h"
#include "Arial12x12.h"
#include "ArialR20x20.h"

//#define PIN_CS_TSC      PA_9 
//#define PIN_TSC_INTR    PA_8
#define PIN_RESET_TFT   PC_13 /* place holder */
//ILI9341 SPI PINS
#define PIN_XP          A3
#define PIN_XM          A1
#define PIN_YP          A2
#define PIN_YM          A0
#define PIN_MOSI_SPI1   D11 //SPI 1 MOSI
#define PIN_MISO_SPI1   D12 //SPI 1 MISO
#define PIN_SCLK_SPI1   D13 //SPI 1 SLCK
#define PIN_CS_SPI1     D5 // SPI CS D10 Was D5
#define PIN_DC_TFT      D6
#define PIN_CS_SD       D4
#define PIN_RESET       D7
// SD Card on GPS shield PINS
#define PIN_MOSI_SPI3   PB_15 //SPI 1 MOSI
#define PIN_MISO_SPI3   PB_14 //SPI 1 MISO
#define PIN_SCLK_SPI3   PB_13 //SPI 1 SLCK
#define PIN_CS_SPI3     D2 // SPI CS
#define PIN_RX_GPS      PA_12 //GPS Shield RX pin
#define PIN_TX_GPS      PA_11 //GPS Shield TX pin

#define PI                  3.14159265358979f

#define DEVICE_NAME     "F411RE"

#ifndef TARGET_NECLEO_F411RE
#define TARGET_NECLEO_F411RE 
#endif

//DigitalOut backlight(PB_3) ;
// DigitalOut tsc_cs(PA_9, 1) ;
// DigitalOut tft_cs(PB_6, 1) ;

Serial pc(USBTX, USBRX);

// Display
ILI9341 TFT(SPI_8, 10000000, 
    PIN_MOSI_SPI1, PIN_MISO_SPI1,  PIN_SCLK_SPI1, 
    PIN_CS_SPI1, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.8") ;
    
// TouchScreen
TouchScreen TSC(PIN_XP, PIN_XM, PIN_YP, PIN_YM);

// 3 Axis IMU
BNO055 imu(I2C_SDA, I2C_SCL);  // New Library Adafruit

// Humidity and Temperature
HTU21D humid(I2C_SDA, I2C_SCL);

// Pressure
BMP180 bmp180(I2C_SDA, I2C_SCL);

// SD Card Reader On Adafruit GPS Shield
SDFileSystem sd(PIN_MOSI_SPI3, PIN_MISO_SPI3, PIN_SCLK_SPI3, PIN_CS_SPI3, "sd"); // the pinout on the mbed Cool Components workshop board

// Set up serial interrupe service handler for gps characters.
GPS MyGPS(PIN_TX_GPS,PIN_RX_GPS, 9600);

//Navigation Class.
NAV nav;
double plat = 42.826420;
double plon = -84.219413;

void Draw_Compass_Rose(void);
void arrow(int x2, int y2, int x1, int y1, int alength, int awidth, int colour);

const int centreX = 120;
const int centreY = 150;
const int radius  = 40; 
float last_dx;
float last_dy;

int main()
{
    TFT.BusEnable(true) ;
    TFT.FastWindow(true) ;
    wait(0.1);
    TFT.cls();
    wait(0.1);
    Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
    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*) Arial24x23i);
    TFT.fillrect(2, 2, 237, 317, White);
    TFT.foreground(Blue);
    TFT.background(White);
    wait(0.1);
       
    Draw_Compass_Rose(); 
        
    while (1) { 
             
        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.check() == 0){
                pc.printf("Bosch BNO055 is NOT avirable!!\r\n");
            } else {
                //printf("Cal %d", imu.read_calib_status());
                imu.get_calib();
                if (imu.cal.system > 0x0){
                    TFT.foreground(White);
                    TFT.locate(4, 260) ;
                    TFT.printf("No Data");
                    TFT.foreground(Blue);
                    imu.get_angles();
                    TFT.locate(4, 260) ;
                    TFT.printf("%.1f",imu.euler.yaw);
                    //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 angle = int(imu.euler.yaw); // * 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);
                
                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(4, 50) ;
                double waypoint = nav.CalculateDistance(MyGPS.buffer.latitude,MyGPS.buffer.longitude,plat,plon)/double(1609.344);
                TFT.printf("%.1fMI From Perry", waypoint);
                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)abs(MyGPS.buffer.longitude);
                minutes = (int) ( (abs(MyGPS.buffer.longitude) - (double)degree) * 60.0);
                seconds = (int) ( (abs(MyGPS.buffer.longitude) - (double)degree - (double)minutes / 60.0) * 60.0 * 60.0 );
                TFT.printf("%d %d' %d\" %c lon", degree, minutes,seconds, MyGPS.buffer.lonc);
                TFT.locate(4, 300) ;
                degree = (int)abs(MyGPS.buffer.latitude);
                minutes = (int) ( (abs(MyGPS.buffer.latitude) - (double)degree) * 60.0);
                seconds = (int) ( (abs(MyGPS.buffer.latitude) - (double)degree - (double)minutes / 60.0) * 60.0 * 60.0 );
                TFT.printf("%d %d' %d\" %c lat", degree, minutes,seconds, MyGPS.buffer.latc);
                TFT.set_font((unsigned char*) SCProSB31x55);     
                TFT.locate(120, 200) ;
                TFT.printf("%.1f", MyGPS.buffer.speed);    
                                 
                //pc.printf("Dist to Perry %.4f ", nav.CalculateDistance(MyGPS.buffer.latitude,MyGPS.buffer.longitude,plat,plon));
                                
            }
            else {
                TFT.locate(4, 2) ;
                TFT.printf("No Data");
                //pc.printf("NMEA has no valid data");
            }     
        }            
    }  
}

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.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");
    TFT.set_font((unsigned char*) ArialR20x20);
} 

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");
}