version 1.0

Dependencies:   CMSIS_DSP_401 GPS MPU9150_DMP PID QuaternionMath Servo mbed

Fork of SolarOnFoils_MainModule_20150518 by Dannis Brugman

Revision:
2:f6d058931b17
Parent:
1:b4a0d63db637
--- a/main.cpp	Fri Jun 26 09:21:33 2015 +0000
+++ b/main.cpp	Tue Aug 11 08:38:55 2015 +0000
@@ -21,7 +21,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include "MCP23017.h"
 #include "LCD_I2C.h"
 #include "uart.h"
 #include "GPS.h"
@@ -31,7 +30,6 @@
 #include "Servo.h"
 #include "mRotaryEncoder.h"
 #include "MainModule.h"
-//#include "LCD_Menu.h"
 #include "menu.h"
 #include "systemVar.h"
 
@@ -108,11 +106,12 @@
 //mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode=PullUp, int debounceTime_us=1000)
 mRotaryEncoder  rSwitch(p12, p11, p7);
 CAN CANbus(p30, p29);
-MCP23017 mcp23017 = MCP23017(i2c, 0x40);
 Serial debug(USBTX, USBRX);
 
 PID rollPID(Kc,Ti,Td,RATE); // declare a PID for roll adjustment
 
+LCD_I2C LCD(p9, p10, 0x40);
+
 //////////////////////////////////////////////////////////////////////////////////////
 // function prototypes                                                              //
 //////////////////////////////////////////////////////////////////////////////////////
@@ -134,13 +133,11 @@
 bool enc_rotated = false;       // rotary encoder was totaded left or right
 bool bSystemFail = false;       // sytem fail
 
-char buffer_GPIOB;
 char cBuffer[200];
 char cMessage;
 char text[16];
 char cPTurns[2] ={0};
 char cSTurns[2] ={0};
-//char buffer[16];
 
 float   fRoll, fPitch, fEulerRoll;
 float   fRollDiff;
@@ -196,12 +193,7 @@
     timer.start();      //timer to test imu roll angle
     actualTime.start(); //crash timer 
     // clear line -1-
-    vLCD_printPos_I2C((unsigned char*)"                 ", 1, 1);
-    
-    // put text on lcd
-    
-    //mLCDMenu.vShowScreen(_FOILBORNE);
-    //vLCD_printPos_I2C((unsigned char*)mLCDMenu.getLine1(), 2, 1);
+    LCD.vLCD_printPos_I2C((unsigned char*)"                 ", 1, 1);
     
 //////////////////////////////////////////////////////////////////////////////////////
 // endless loop                                                                     //
@@ -255,7 +247,7 @@
             svSoF.vSetPitch(q1);                                 // call function class SystemVar
             svSoF.vSetRoll(q1);
             mLCDMenu.vShowScreen(mLCDMenu.getScreen());
-            vLCD_update();
+            LCD.vLCD_update();
             
             //debug.printf("Roll: %c\t", svSoF.getValue());
             //debug.printf("Roll: %f\t", getRollAngle(q1));
@@ -277,218 +269,6 @@
 } // END MAIN 
 
 //////////////////////////////////////////////////////////////////////////////////////
-// delay function for LCD                                                           //
-//////////////////////////////////////////////////////////////////////////////////////
-
-void vLCD_delay_I2C (unsigned int t_delay)
-{
-    unsigned int i;
-    
-    for (i=0; i < t_delay; i++);
-}
-
-//////////////////////////////////////////////////////////////////////////////////////
-// write commando to LCD                                                            //
-//////////////////////////////////////////////////////////////////////////////////////
-
-void vLCD_cmd_I2C   (unsigned char commando)
-{
-__disable_irq();    // Disable Interrupts
-
-    // write commando to LCD
-    mcp23017.write(PORT_A, commando);
-    
-    // LCD_RS = 0
-    buffer_GPIOB = buffer_GPIOB & 0xFD;   // RS = 0
-    // write to LCD
-    mcp23017.write(PORT_B, buffer_GPIOB);
-    
-    // LCD_EN = 1
-    buffer_GPIOB = buffer_GPIOB | 0x01;   // EN = 1
-    // write to LCD
-    mcp23017.write(PORT_B, buffer_GPIOB);
-    
-    // LCD_EN = 0
-    buffer_GPIOB = buffer_GPIOB & 0xFE;   // EN = 0
-    // write to LCD
-    mcp23017.write(PORT_B, buffer_GPIOB);
-    
-__enable_irq();     // Enable Interrupts
-}
-
-//////////////////////////////////////////////////////////////////////////////////////
-// write single char to LCD                                                         //
-//////////////////////////////////////////////////////////////////////////////////////
-
-void vLCD_data_I2C  (unsigned char data)
-{
-__disable_irq();    // Disable Interrupts
- 
-    // write data to LCD
-    mcp23017.write(PORT_A, data);
-    
-    // LCD_RS = 1
-    buffer_GPIOB = buffer_GPIOB | 0x02;   // RS = 1
-    // write to LCD
-    mcp23017.write(PORT_B, buffer_GPIOB);
-    
-    // LCD_EN = 1
-    buffer_GPIOB = buffer_GPIOB | 0x01;   // EN = 1
-    // write to LCD
-    mcp23017.write(PORT_B, buffer_GPIOB);
-    
-    // LCD_EN = 0
-    buffer_GPIOB = buffer_GPIOB & 0xFE;   // EN = 0
-    // write to LCD
-    mcp23017.write(PORT_B, buffer_GPIOB);
-    
-__enable_irq();     // Enable Interrupts
-}
-
-//////////////////////////////////////////////////////////////////////////////////////
-// Initialize LCD                                                                   //
-//////////////////////////////////////////////////////////////////////////////////////
-
-void vLCD_init_I2C  (void)
-{
-    vLCD_cmd_I2C (0x00);
-    // vLCD_delay_I2C (65535)
-    vLCD_delay_I2C (100);
-    
-    vLCD_cmd_I2C (0x38);
-    // vLCD_delay_I2C (65535)
-    vLCD_delay_I2C (100);
-    
-    vLCD_cmd_I2C (0x38);
-    //vLCD_delay_I2C (65535)
-    vLCD_delay_I2C (100);
-    
-    vLCD_cmd_I2C (0x38);
-    //vLCD_delay_I2C (65535)
-    vLCD_delay_I2C (100);
-    
-    // Display ON / OFF
-    vLCD_cmd_I2C (0x08);
-    
-    //vLCD_delay_I2C (65535)
-    vLCD_delay_I2C (100);
-    
-    // Clear Display
-    vLCD_cmd_I2C (0x01);
-    
-    //vLCD_delay_I2C (65535)
-    vLCD_delay_I2C (100);
-    
-    // Entry Mode Set
-    vLCD_cmd_I2C (0x06);
-    
-    //vLCD_delay_I2C (65535)
-    vLCD_delay_I2C (100);
-    
-    // Display ON / OFF
-    vLCD_cmd_I2C (0x0C);
-    
-    //vLCD_delay_I2C (65535)
-    vLCD_delay_I2C (100);
-}
-
-//////////////////////////////////////////////////////////////////////////////////////
-// Write string to LCD                                                              //
-//////////////////////////////////////////////////////////////////////////////////////
-
-void vLCD_print_I2C (unsigned char *string, unsigned char line)
-{
-    unsigned int i;
-    
-    // select target line to write to
-    if (line == 1)
-    {
-        vLCD_cmd_I2C (0x80);
-    }
-    else if (line == 2)
-    {
-        vLCD_cmd_I2C (0xC0);
-    }
-    else
-    {
-        return;    // End of function: Error in the past value of line
-    }
-    
-    // write data to selected line
-    for (i=0; i<16; i++)
-    {
-        if (string [i] != 0x00) 
-        {
-            vLCD_data_I2C (string [i]);
-        }
-        else
-            vLCD_data_I2C (' ');
-    }
-}
-
-//////////////////////////////////////////////////////////////////////////////////////
-// Write string to LCD                                                              //
-//////////////////////////////////////////////////////////////////////////////////////
-
-void vLCD_printPos_I2C (unsigned char *string, unsigned char line, unsigned char character)
-{
-    unsigned int i;
-    
-    // select target line and position to write to
-    if (line == 1)
-    {
-        vLCD_cmd_I2C (0x80+(character-1));
-    }
-    
-    else if (line == 2)
-    {
-        vLCD_cmd_I2C (0xC0+(character-1));
-    }
-    
-    else
-    {
-        return;    // End of function: Error in the past value of line
-    }
-    
-    // write data to selected line and position
-    for (i=0; i<16; i++)
-    {
-        if (string [i] != 0x00)
-        {
-            vLCD_data_I2C (string [i]);
-        }
-        else
-            vLCD_data_I2C (' ');
-    }
-}
-
-//////////////////////////////////////////////////////////////////////////////////////
-// update LCD                                                                       //
-//////////////////////////////////////////////////////////////////////////////////////
-void vLCD_clear_I2C (void)
-{
-    // Clear Display
-    vLCD_cmd_I2C (0x01);
-    //vLCD_delay_I2C (65535)
-    vLCD_delay_I2C (100);
-    // Display ON / OFF
-    vLCD_cmd_I2C (0x0C);
-    //vLCD_delay_I2C (65535)
-    vLCD_delay_I2C (100);
-}
-
-//////////////////////////////////////////////////////////////////////////////////////
-// update LCD                                                                       //
-//////////////////////////////////////////////////////////////////////////////////////
-void vLCD_update (void)
-{
-   vLCD_print_I2C((unsigned char*)mLCDMenu.getLine1(), 1);
-   vLCD_print_I2C((unsigned char*)mLCDMenu.getLine2(), 2);
-   //vLCD_printPos_I2C((unsigned char*)mLCDMenu.getLine1(), 1, 1);
-   //vLCD_printPos_I2C((unsigned char*)mLCDMenu.getLine2(), 2, 1);
-}
-
-//////////////////////////////////////////////////////////////////////////////////////
 // Init SOF module                                                                  //
 //////////////////////////////////////////////////////////////////////////////////////
 void vInit (void)
@@ -496,15 +276,15 @@
     // I2C init
     i2c.frequency(100000);
     //Port A is databus - Output
-    mcp23017.direction(PORT_A, PORT_DIR_OUT);
+//    mcp23017.direction(PORT_A, PORT_DIR_OUT);
     //Port B is controlbus - Output
-    mcp23017.direction(PORT_B, PORT_DIR_OUT);
+//    mcp23017.direction(PORT_B, PORT_DIR_OUT);
    
     debug.baud(115200);
     // initialize LCD
-    vLCD_init_I2C();
+    //vLCD_init_I2C();
     mLCDMenu.vSelectMenu(_MENU0_0);
-    vLCD_update();
+    LCD.vLCD_update();
       
     // led's out
     aliveLed = 1; wait(0.1); aliveLed = 0;
@@ -537,7 +317,7 @@
 {
     if(imu.isReady()) mLCDMenu.vShowScreen(_IMUREADY);
     else  mLCDMenu.vShowScreen(_IMUFAIL);
-    vLCD_update();
+    LCD.vLCD_update();
         
     imu.initialiseDMP();
     imu.setFifoReset(true);    
@@ -588,7 +368,7 @@
                     case GYRO_GPS_DIAGN:        if((msg.id == GYRO_GPS_ID)&&(msg.data[0] == 0xFF)) mLCDMenu.vShowScreen(_CANID205OK); break;
                     case EXT_UI_DIAGN:          if((msg.id == EXT_UI_ID)&&(msg.data[0] == 0xFF)) mLCDMenu.vShowScreen(_CANID1001OK); break;                                                                                      
                 } //end switch
-                vLCD_update();
+                LCD.vLCD_update();
                 bReceived = true;
             } //end if
             else if(iTry == 1){
@@ -600,7 +380,7 @@
                     case GYRO_GPS_DIAGN:        mLCDMenu.vShowScreen(_CANID205FAIL); break;
                     case EXT_UI_DIAGN:          mLCDMenu.vShowScreen(_CANID1001FAIL); break;                                                                                      
                 } //end switch
-                vLCD_update();
+                LCD.vLCD_update();
             } //end if
             if(CANbus.read(msg)){}//unwanted message
             if(!bReceived) iTry--;
@@ -622,7 +402,7 @@
     CANbus.write(CANMessage(EXT_UI_HEIGHT, &cMessage, 1));
     
     mLCDMenu.vSelectMenu(_MENU0_0);
-    vLCD_update();
+    LCD.vLCD_update();
     debug.printf("end can init");  
 }
 
@@ -683,18 +463,6 @@
 }
 
 //////////////////////////////////////////////////////////////////////////////////////
-// Writing integer to LCD                                                           //
-//////////////////////////////////////////////////////////////////////////////////////
-void vLCD_printInt_I2C (int value, unsigned char line, unsigned char character)
-{
-        static char buffer[32];
-        // convert int to char
-        sprintf(buffer, "%.2d", value);
-        // write converted int to LCD
-    vLCD_printPos_I2C((unsigned char*)buffer, line, character);
-}
-
-//////////////////////////////////////////////////////////////////////////////////////
 // calculate Roll from quaternion                                                   //
 //////////////////////////////////////////////////////////////////////////////////////
 float getRollAngle(Quaternion q1)
@@ -769,7 +537,7 @@
     
     // reset switch count
     vResetSwitch();
-    vLCD_update();
+    LCD.vLCD_update();
 }
 
 //////////////////////////////////////////////////////////////////////////////////////
@@ -784,7 +552,7 @@
     // reset switch count
     vResetSwitch();
     
-    vLCD_update();
+    LCD.vLCD_update();
 }
 
 //////////////////////////////////////////////////////////////////////////////////////