ROM Comm / Mbed 2 deprecated espar_mini_control_CAN

Dependencies:   mbed mbed-STM32F103C8T6

Revision:
9:6126b83608be
Parent:
8:2b0d18ebbba9
Child:
10:a82e51837e2b
--- a/main.cpp	Wed Jun 10 22:31:30 2020 +0000
+++ b/main.cpp	Thu Jun 11 21:57:29 2020 +0000
@@ -16,7 +16,7 @@
 //#include "MapleMini.h"
 #include "stm32f103c8t6.h"
 #include "mbed.h"
-#include "ecu_simulator.h"
+//#include "ecu_simulator.h"
 #include "globals.h"
 #include "heatcontrol.h"
 
@@ -29,12 +29,14 @@
 //DigitalIn left(p24);
 //DigitalIn up(p25);
 
-ecu_sim sim(CANSPEED_500);     //Create object and set CAN speed
-ecu_t ecu;
+volatile unsigned long lifetimer = 0;
+volatile int secondFlag = 0;
 
 Ticker timer;
+Ticker quartersecond_timed_event;
+Ticker onesecond_timed_event;
 
-void attime() 
+void attime()
 {
     //second_tick = true;
     //myled = !myled;
@@ -48,28 +50,80 @@
 
 // external variables
 volatile unsigned char idletimer;
+extern char otherControllerDetected;
+
 char printbuff[256];
 
-void DebugWrite(const char* str) {
-    pc.printf(str);    
+void DebugWrite(const char* str)
+{
+    pc.printf(str);
 }
-void resetTimer(struct sHeatVars *s) {
+void resetTimer(struct sHeatVars *s)
+{
+    s->resettick = 1;
+    //TODO: This.
+}
+void resetTimerAsync(struct sHeatVars *s)
+{
+    s->resettick = 1;
     //TODO: This.
 }
-void resetTimerAsync(struct sHeatVars *s) {
-    //TODO: This.
+void one_second_tick()
+{
+    int i;
+    lifetimer++; // used for wifi reset test.
+    //wiced_rtos_lock_mutex( &xively_data.mutex );
+
+    for (i = 0; i < HEATERSTATECOUNT; i++) {
+        if (heaterState[i].heattime > 0 && heaterState[i].heattime < 59995) { // count down the heat timer
+            // tick,tick...
+            heaterState[i].heattime--;
+        }
+        if (heaterState[i].heatresettime > 0) {
+            heaterState[i].heatresettime--;
+        }
+        if (heaterState[i].preheattime > 0) {
+            heaterState[i].preheattime--;
+        }
+        if (heaterState[i].heaterDetected > 0) {
+            heaterState[i].heaterDetected--;
+        }
+    }
+
+    if (idletimer > 0) {
+        idletimer--;
+    }
+    if (otherControllerDetected > 0) {
+        otherControllerDetected--;
+    }
+    secondFlag = 1;
+
 }
-int main() 
+
+void quarter_second_tick()
+{
+    int i;
+    for (i = 0; i < HEATERSTATECOUNT; i++) {
+        heaterState[i].tickcount += 250;
+        if (heaterState[i].resettick == 1) {
+            heaterState[i].tickcount = 0;
+            heaterState[i].resettick = 0;
+        }
+    }
+
+}
+
+int main()
 {
     confSysClock();     //Configure system clock (72MHz HSE clock, 48MHz USB clock)
     pc.baud(115200);
-    
-    //Enable Pullup 
+
+    //Enable Pullup
     click.mode(PullUp);
 
-    
-    pc.printf("\n\nRIXEN Simulator v1.1 \r\n"); 
-      
+
+    pc.printf("\n\nRIXEN Simulator v1.1 \r\n");
+
     led1 = 1;
     wait(0.1);
     led2 = 1;
@@ -78,49 +132,79 @@
     wait(0.1);
     led4 = 1;
     wait(0.2);
-    led1 = 0; led2 = 0; led3 = 0; led4 = 0;
-       
-    
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+    led4 = 0;
+
+
     wait(1);
-    
+
     //canspeed = CAN500;
-    ecu.dtc = false;
-    ecu.active_fault = false;
-    
-    sim.canspeed(CANSPEED_500); 
-    
+//    ecu.dtc = false;
+//   ecu.active_fault = false;
+
+//   sim.canspeed(CANSPEED_500);
+
     wait(0.2);
     led1 = 1;
-    
-    timer.attach(&attime, 1);
-    
-    
+
+    //timer.attach(&attime, 1);
+    quartersecond_timed_event.attach(&quarter_second_tick, 0.250);
+    onesecond_timed_event.attach(&one_second_tick, 1.0);
+
+
     initHeaterState(&heaterState[0]);
     initHeaterState(&heaterState[1]);
 
     heaterState[0].bustype = HEATERTYPECAN;
     heaterState[0].heaternum = 1;
-    
+
     heaterState[1].bustype = HEATERTYPENONE;
-    
+
 
-    while(1)  // Main CAN loop
-    {
-        
-        for (int i = 0; i < HEATERSTATECOUNT; i++)
-        {
+    InitCAN();
+
+
+    while(1) { // Main CAN loop
+
+        for (int i = 0; i < HEATERSTATECOUNT; i++) {
 //            wiced_update_system_monitor(&main_thread_monitor, 10 * 1000);
             doHeatTaskCAN(&heaterState[i]);
 //            wiced_update_system_monitor(&main_thread_monitor, 10 * 1000);
             doHeatLogicTask(&heaterState[i]);
-            if (i < HEATERSTATECOUNT)
-            {
+            if (i < HEATERSTATECOUNT) {
                 wait_ms(50);
-            }
-            else
-            {
+            } else {
                 //wiced_rtos_delay_milliseconds(50);
             }
         }
+        if (!click) {
+            if (heaterState[0].heatOn == HEATCALLOFF) {
+                set_heat_con(&heaterState[0], HEATCALLINIT);
+            }
+        } else {
+            if (heaterState[0].heatOn != HEATCALLOFF) {
+                set_heat_con(&heaterState[0], HEATCALLOFF);
+            }
+        }
+
+        if (secondFlag > 0) {
+            secondFlag = 0;
+            if (heaterState[0].initTimer > 0) {
+                heaterState[0].initTimer--;
+            }
+            if (heaterState[1].initTimer > 0) {
+                heaterState[1].initTimer--;
+            }
+//            DebugWrite("Doing re-init of I2c\r\n");
+
+            // check I2C
+            //          DeInit_SC16IS740();
+            //          DeInit_SPL0601();
+            //          cleanI2CLines();
+            //          Init_SC16IS740(2);
+            //          Init_SPL0601();
+        }
     }
 }