pub

Dependencies:   CANnucleo mbed-dev

Fork of CANnucleo_Hello by Zoltan Hudak

Revision:
13:37c502ebc543
Parent:
12:e91e44924194
--- a/main.cpp	Sat Jul 23 08:29:31 2016 +0000
+++ b/main.cpp	Tue Mar 13 20:54:24 2018 +0000
@@ -1,123 +1,345 @@
-/*
- * An example showing how to use the CANnucleo library:
- *
- * Two affordable (less than $4 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash), 
- * compatible with the NUCLEO-F103RB platform (20kB SRAM, 128kB Flash), 
- * are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.). 
- * CAN transceivers are not part of NUCLEO boards, therefore must be added by you. 
- * Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends.
- *
- * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/>
- *
- * NOTE: If you'd like to use the official NUCLEO boards comment out line 25
- *
- * The same code is used for both NUCLEO boards, but:
- *      For board #1 compile the example without any change.
- *      For board #2 comment out line 26 before compiling 
- *
- * Once the binaries have been downloaded to the boards reset board #1.
- *
- */ 
-
 #include "mbed.h"
 #include "CANnucleo.h"
+#include "myCan.hpp"
+#include "lcd.hpp"
+#include "printGear.hpp"
+#include "printIntValue.hpp"
+#include "printFloatValue.hpp"
+#include "displaySettings.hpp"
 
-//#define TARGET_STM32F103C8T6  1     // comment out this line when using official NUCLEO boards!                                    
-#define BOARD1                1     // comment out this line when compiling for board #2
+//Temperatur definitionen
+#define tmot_max 95
+#define toil_max 150
+#define tfuel_max 40
+#define poil_min 1.0
+#define ubatt_min 14
+
+#define tmot_max2 105
+#define toil_max2 155
+#define tfuel_max2 50
+#define poil_min2 0.8
+#define ubatt_min2 13.2
+
+
+
+#define TARGET_NUCLEO_F303K8 1
+
+#define   LED_PIN   LED1
 
-#if defined(TARGET_STM32F103C8T6) 
-    #define   LED_PIN   PC_13 
-    const int OFF = 1;
-    const int ON  = 0;
-#else
-    #define   LED_PIN   LED1
-    const int OFF = 0;
-    const int ON  = 1;
-#endif
+const int OFF = 0;
+const int ON  = 1;
+
+const unsigned int TX_ID = 0x7AA;
+
+
+//Ein- und Ausgänge
+DigitalOut mil_ubatt(A0);
+DigitalOut mil_tmot(D13);
+DigitalOut mil_toil(D1);
+DigitalOut mil_poil(D0);
+
+DigitalOut shift(D8);
 
-#if defined(BOARD1)
-    const unsigned int RX_ID = 0x100;
-    const unsigned int TX_ID = 0x101;
-#else
-    const unsigned int RX_ID = 0x101;
-    const unsigned int TX_ID = 0x100;
-#endif
+DigitalOut backl_red(D9);
+DigitalOut backl_yellow(D11);
+
+DigitalOut decoder_a(D3);
+DigitalOut decoder_b(D4);
+DigitalOut decoder_c(D5);
+DigitalOut decoder_d(D7);
 
-DigitalOut      led(LED_PIN);
-int             ledState;
-Timer           timer;
+AnalogIn brightnes(D6);
+DigitalIn pageSelect(D12);
+
+
+
 CAN             can(PA_11, PA_12);  // CAN Rx pin name, CAN Tx pin name
+
 CANMessage      rxMsg;
 CANMessage      txMsg;
-int             counter = 0;
+//for debuging
+Serial pc(USBTX, USBRX); // tx, rx
+
+
 volatile bool   msgAvailable = false;
 
-/**
- * @brief   'CAN receive-complete' interrup handler.
- * @note    Called on arrival of new CAN message.
- *          Keep it as short as possible.
- * @param   
- * @retval  
- */
-void onMsgReceived() {
+void onMsgReceived()
+{
     msgAvailable = true;
 }
 
-/**
- * @brief   Main
- * @note
- * @param 
- * @retval
- */
-int main() {
-    can.frequency(1000000);                     // set bit rate to 1Mbps
-    can.attach(&onMsgReceived);                 // attach 'CAN receive-complete' interrupt handler
-    
-#if defined(BOARD1)
-    led = ON;       // turn LED on
-    timer.start();  // start timer
-#else
-    led = OFF;      // turn LED off
-#endif
+int main()
+{
+    pc.baud(230400);
+    pc.printf("Ready\n");
+
+    pageSelect.mode(PullUp);        //button is an GND switch
+    bool pageSelectOld = false;     //because if button pressed since beginn
+
+    //Init Display
+    initSpi();
+    initDisplay();
+
+    backl_red = 1;
+    backl_yellow = 0;
+
+    mil_ubatt = 0;
+    mil_tmot = 0;
+    mil_toil = 0;
+    mil_poil = 0;
+
+    shift = 1;
+
+ 
+    //init Variable
+    char pageSelectCan = 0;
+    int mappos = 10;
+    int gear = 0;
+    int tmot = 0;
+    int toil = 0;
+    int tfuel = 0;
+    int rev = 0;
+    float poil = 0;
+    float ath = 0;
+    float ubatt = 0;
+    float lam = 0.0;
+
+    char currentPage = 0;
+    int page=0;
+    int windowNr=0;
+    char x = 0;
+
+    Timer printTimer;
+    printTimer.start();
+
+    Timer ledTimer;
+    ledTimer.start();
+    char ledCounter = 0;
+    bool ledStatus = false;
+
+    decoder_a = 0;
+    decoder_b = 0;
+    decoder_c = 0;
+    decoder_d = 0;
+
+
+    can.frequency(1000000);                       // set bit rate to 1Mbps
+    can.attach(&onMsgReceived);
 
     while(1) {
-        if(timer.read() >= 1.0) {               // check for timeout
-            timer.stop();                       // stop timer
-            timer.reset();                      // reset timer
-            counter++;                          // increment counter
-            ledState = led.read();              // get led state
-            txMsg.clear();                      // clear Tx message storage
-            txMsg.id = TX_ID;                   // set ID
-            txMsg << counter;                   // append first data item
-            txMsg << ledState;                  // append second data item (total data lenght must be <= 8 bytes!)
-            led = OFF;                          // turn LED off
-            if(can.write(txMsg))                // transmit message
-                printf("CAN message sent\r\n"); 
-            else
-                printf("Transmission error\r\n");
+        if(msgAvailable) {
+            msgAvailable = false;
+            can.read(rxMsg);
+            switch(rxMsg.id) {
+                case 0x100:
+                    pageSelectCan = rxMsg.data[2];
+                    break;
+                case 0x777:
+                    gear = rxMsg.data[0];
+                    break;
+                case 0x77A:
+                    switch(rxMsg.data[0]) {
+                        case 5:
+                            tmot = rxMsg.data[4] - 40;
+                            break;
+                        case 4:
+                            tfuel = rxMsg.data[4] - 40;
+                            toil = rxMsg.data[5] - 40;
+                            break;
+                        case 1:
+                            poil = rxMsg.data[5] * 0.0514;
+                            break;
+                        case 2:
+                            mappos = rxMsg.data[6];
+                            break;
+                    }
+                    break;
+                case 0x779:
+                    ubatt = rxMsg.data[6] * 0.07041875;
+                    break;
+                case 0x773:
+                    ath = rxMsg.data[5] * 0.390625;
+                    rev = (rxMsg.data[3] * 256 + rxMsg.data[4]) / 2;
+                    break;
+                case 0x774:
+                    lam = rxMsg.data[4] * 0.0078431;
+                    break;
+            }
         }
-        if(msgAvailable) {
-            msgAvailable = false;               // reset flag for next use
-            can.read(rxMsg);                    // read message into Rx message storage
-            printf("CAN message received:\r\n");
-            printf("  ID     = %#x\r\n", rxMsg.id);
-            printf("  Type   = %d\r\n", rxMsg.type);
-            printf("  Format = %d\r\n", rxMsg.format);
-            printf("  Length = %d\r\n", rxMsg.len);
-            printf("  Data   =");            
-            for(int i = 0; i < rxMsg.len; i++)
-                printf(" %x", rxMsg.data[i]);
-            printf("\r\n");
-            // Filtering performed by software:           
-            if(rxMsg.id == RX_ID) {             // See comments in CAN.cpp for filtering performed by hardware
-                rxMsg >> counter;               // extract first data item
-                rxMsg >> ledState;              // extract second data item
-                printf("counter = %d\r\n", counter);
-                led = ledState;                 // set LED
-                timer.start();                  // transmission lag
+        
+        if(pageSelectCan == 0) {                        //Check if the button is not pressed
+            pageSelectOld = false;                       //Sets the butten high status also to the pageSelectOld variable
+        }
+        if((pageSelectCan == 1) && (pageSelectOld == false)) {              //Check if there is a rising edge
+            pageSelectOld = true;                 //Set the Old Value also to false(0)
+            if(currentPage >= (1)) {                    //Check if the current page is the last page, here 1
+                currentPage = 0;                        //Set to first page
+            } else {
+                currentPage++;                          //else count one up
+            }
+        }
+        if(ledTimer.read_ms() >= 75) {
+            if(ledCounter >= 2) {
+                ledCounter = 0;
+                ledStatus = true;
+            } else {
+                ledCounter ++;
+                ledStatus = false;
+            }
+            ledTimer.reset();
+        }
+
+
+        if(mappos == 2) {
+            if( (gear == 1 && rev >= 11300) || (gear == 2 && rev >= 10800) || (gear == 3 && rev >= 10800) ) { //Default MAP = Endurance
+                shift = ledStatus;
+            } else {
+                shift = 0;
+            }
+        } else {
+            if( (gear == 1 && rev >= 11300) || (gear == 2 && rev >= 10800) || (gear == 3 && rev >= 10800) ) { //Rest
+                shift = ledStatus;
+            } else {
+                shift = 0;
             }
         }
+
+        if(poil <= poil_min2) {
+            mil_poil = ledStatus;
+        } else if(poil <= poil_min) {
+            mil_poil = 1;
+        } else {
+            mil_poil = 0;
+        }
+
+        if(toil >= toil_max2) {
+            mil_toil = ledStatus;
+        } else if(toil >= toil_max) {
+            mil_toil = 1;
+        } else {
+            mil_toil = 0;
+        }
+
+        if(tmot >= tmot_max2) {
+            mil_tmot = ledStatus;
+        } else if(tmot >= tmot_max) {
+            mil_tmot = 1;
+        } else {
+            mil_tmot = 0;
+        }
+
+        if(ubatt <= ubatt_min2) {
+            mil_ubatt = ledStatus;
+        } else if(ubatt <= ubatt_min) {
+            mil_ubatt = 1;
+        } else {
+            mil_ubatt = 0;
+        }
+        if(100 > printTimer.read_ms() >= 30) {
+            decoder_a = 0;
+            decoder_b = 0;
+            decoder_c = 0;
+            decoder_d = 0;
+        }
+        if(printTimer.read_ms() >= 100) {
+            if(rev >= 14500) {
+                decoder_a = 0;
+                decoder_b = 1;
+                decoder_c = 1;
+                decoder_d = 1;
+            } else if(rev >= 13500) {
+                decoder_a = 1;
+                decoder_b = 0;
+                decoder_c = 1;
+                decoder_d = 1;
+            } else if(rev >= 12500) {
+                decoder_a = 0;
+                decoder_b = 0;
+                decoder_c = 1;
+                decoder_d = 1;
+            } else if(rev >= 11500) {
+                decoder_a = 1;
+                decoder_b = 1;
+                decoder_c = 0;
+                decoder_d = 1;
+            } else if(rev >= 10500) {
+                decoder_a = 0;
+                decoder_b = 1;
+                decoder_c = 0;
+                decoder_d = 1;
+            } else if(rev >= 9500) {
+                decoder_a = 1;
+                decoder_b = 0;
+                decoder_c = 0;
+                decoder_d = 1;
+            } else if(rev >= 8500) {
+                decoder_a = 0;
+                decoder_b = 0;
+                decoder_c = 0;
+                decoder_d = 1;
+            } else if(rev >= 7500) {
+                decoder_a = 1;
+                decoder_b = 1;
+                decoder_c = 1;
+                decoder_d = 0;
+            } else if(rev >= 6500) {
+                decoder_a = 0;
+                decoder_b = 1;
+                decoder_c = 1;
+                decoder_d = 0;
+            } else if(rev >= 5500) {
+                decoder_a = 1;
+                decoder_b = 0;
+                decoder_c = 1;
+                decoder_d = 0;
+            } else if(rev >= 4500) {
+                decoder_a = 0;
+                decoder_b = 0;
+                decoder_c = 1;
+                decoder_d = 0;
+            } else if(rev >= 3500) {
+                decoder_a = 1;
+                decoder_b = 1;
+                decoder_c = 0;
+                decoder_d = 0;
+            } else if(rev >= 2500) {
+                decoder_a = 0;
+                decoder_b = 1;
+                decoder_c = 0;
+                decoder_d = 0;
+            } else if(rev >= 1500) {
+                decoder_a = 1;
+                decoder_b = 0;
+                decoder_c = 0;
+                decoder_d = 0;
+            } else {
+                decoder_a = 0;
+                decoder_b = 0;
+                decoder_c = 0;
+                decoder_d = 0;
+            }
+            printGear(gear);
+            switch(currentPage) {
+                case 0:
+                    printIntValue( 1, "TOIL,", toil, 0, 130);
+                    printFloatValue( 2, "POIL,", poil , 0, 10);
+                    printIntValue( 3, "TMOT,", tmot, 0, 95);
+
+                    printFloatValue( 4, "UBATT", ubatt , 11, 17);
+                    break;
+                case 1:
+                    printIntValue( 1, "RV,,,", rev, 0, 15000);
+                    printIntValue( 2, "TFUEL", tfuel, 0, 40);
+                    printFloatValue( 3, "ATH,,", ath , 0, 101);
+                    printFloatValue( 4, "LAM,,", lam , 0, 10);
+                    break;
+            }
+
+
+            printTimer.reset();
+        }
+
     }
+
 }
 
-