Jelajahi Sumber

Organize and reorder values.

Vladimir N. Shilov 2 tahun lalu
induk
melakukan
4781294b9e
1 mengubah file dengan 115 tambahan dan 72 penghapusan
  1. 115 72
      src/main.c

+ 115 - 72
src/main.c

@@ -24,6 +24,9 @@
 #define INA_SCAN_PERIOS_US    62500
 #define INA_AVG_FACTOR        16
 
+#define CHRGR_PAUSE1_S        60
+#define CHRGR_PAUSE2_S        60
+
 /* Type definitions */
 typedef enum chrgr_state {
   Stop = 0,
@@ -78,7 +81,8 @@ static const charger_channel_t charger_Channels[INA3221_CH_NUM] = {
 
 /* Privae functions */
 static void prepare_Screen(void);
-static void ina_Process(void); //(virtual_timer_t *vtp, void *p);
+static void ina_Process(void);
+static void mode_vt_cb(virtual_timer_t *vtp, charger_state_t st);
 static void btn1_handler(const button_state_t);
 static void btn2_handler(const button_state_t);
 static void btn3_handler(const button_state_t);
@@ -86,11 +90,13 @@ static void btn4_handler(const button_state_t);
 
 /* Private variables */
 static binary_semaphore_t ina_bsem, charger_bsem;
+static virtual_timer_t mode_vt;
+static thread_t *uart_thread;
 static charger_state_t charger_State;
-static uint8_t current_Channel;
-static int32_t Current, sumCurrent=0, secCurrent;
-static uint32_t Voltage, sumVoltage=0, secVoltage;
-static uint32_t secPower, Capacity_I=0, Capacity_P=0;
+static ina3221_ch_t current_Channel;
+static uint32_t Current;
+static uint32_t Voltage;
+static uint32_t Power, Capacity_I=0, Capacity_P=0;
 static int INA_Present = 0;
 static BaseSequentialStream * chp = (BaseSequentialStream*) &SD1;
 GHandle GW1;  // The handle for our console
@@ -124,24 +130,12 @@ static THD_FUNCTION(ChrgThread, arg) {
   while (true) {
     chBSemWait(&charger_bsem);
 
-    if (Timer.ss < 59) {
-      Timer.ss ++;
-    } else {
-      Timer.ss = 0;
-      if (Timer.mm < 59) {
-        Timer.mm ++;
-      } else {
-        Timer.mm = 0;
-        Timer.hh ++;
-      }
-    }
-
     if (charger_State != Stop) {
       chsnprintf(buf, 12, "T:%02u:%02u:%02u", Timer.hh, Timer.mm, Timer.ss);
     } else {
       chsnprintf(buf, 12, "T:00:00:00");
     }
-    gdispFillStringBox(1, 153, 158, 29, buf, font2, Red, Gray, gJustifyCenter);
+    gdispFillStringBox(160, 123, 158, 29, buf, font2, Red, Gray, gJustifyCenter);
 
   }
 }
@@ -310,22 +304,36 @@ static void prepare_Screen(void) {
 
 static void btn1_handler(button_state_t state) {
   if (state == BTN_st_Pressed) {
-
     gwinPrintf(GW1, "Button 1 pressed\n");
 
-    current_Channel ++;
-    if (current_Channel >= INA3221_CH_NUM) {
-      current_Channel = INA3221_CH1;
-    }
-    int tmp = current_Channel + 1;
-    gwinPrintf(GW1, "Channel #%u selected.\n", tmp);
+    if (charger_State == Stop) {
+      switch (current_Channel)
+      {
+      case INA3221_CH1:
+        current_Channel = INA3221_CH2;
+        break;
+      case INA3221_CH2:
+        current_Channel = INA3221_CH3;
+        break;
+      case INA3221_CH3:
+        current_Channel = INA3221_CH1;
+        break;
+      
+      default:
+        current_Channel = INA3221_CH1;
+        break;
+      }
+
+      int tmp = (int)current_Channel + 1;
+      gwinPrintf(GW1, "Channel #%u selected.\n", tmp);
 
-    char buf[16];
-    chsnprintf(buf, 10, "[1] ch.%u", tmp);
-    gdispFillStringBox(1, 1, 78, 15,  buf, font1, Yellow, Blue, gJustifyLeft);
+      char buf[16];
+      chsnprintf(buf, 10, "[1] ch.%u", tmp);
+      gdispFillStringBox(1, 1, 78, 15,  buf, font1, Yellow, Blue, gJustifyLeft);
 
-    chsnprintf(buf, 12, "   - %2uA ", charger_Channels[current_Channel].max_current/1000);
-    gdispFillStringBox(1, 16, 78, 15, buf, font1, Yellow, Blue, gJustifyLeft);
+      chsnprintf(buf, 12, "   - %2uA ", charger_Channels[current_Channel].max_current/1000);
+      gdispFillStringBox(1, 16, 78, 15, buf, font1, Yellow, Blue, gJustifyLeft);
+    }
   }
 }
 
@@ -348,11 +356,15 @@ static void btn4_handler(button_state_t state) {
     switch(charger_State) {
     case Stop:
       charger_State = Charge1;
-      gwinPrintf(GW1, "Change state to Charge#1\n");
+      gwinPrintf(GW1, "Change state to Charge_1\n");
       gdispFillStringBox(1, 91, 78, 15, "[4] Stop", font1, Yellow, Blue, gJustifyLeft);
       break;
+
     default:
       charger_State = Stop;
+      Current = 0;
+      Voltage = 0;
+      Power = 0;
       gwinPrintf(GW1, "Change state to Stop\n");
       gdispFillStringBox(1, 91, 78, 15, "[4] Start", font1, Yellow, Blue, gJustifyLeft);
       break;
@@ -360,63 +372,83 @@ static void btn4_handler(button_state_t state) {
   }
 }
 
+static void mode_vt_cb(virtual_timer_t *vtp, charger_state_t st) {
+  (void)vtp;
+  charger_State = st;
+}
+
 /*
  * Read Voltage & Current, update screen info.
  */
-//static void ina_Process(virtual_timer_t *vtp, void *p) {
-//  (void)vtp;
-//  (void)p;
-
 static void ina_Process(void) {
-  int32_t tmp1, tmp2;
+  int32_t tmp1, tmp2, current;
+  uint32_t volt;
   char buf[12];
   static int idx = 0;
+  static uint32_t sumCurrent=0, sumVoltage=0;
 
   if (INA_Present != 0) {
-    Current = INA3221_getCurrent(&ina3221, (ina3221_ch_t)current_Channel);
-    Voltage = INA3221_getVoltage(&ina3221, (ina3221_ch_t)current_Channel);
+    current = INA3221_getCurrent(&ina3221, (ina3221_ch_t)current_Channel);
+    volt = INA3221_getVoltage(&ina3221, (ina3221_ch_t)current_Channel);
 
-    tmp1 = Voltage / 1000;
-    tmp2 = Voltage % 1000;
-    chsnprintf(buf, 10, "V:%2d.%03u", tmp1, tmp2);
-    gdispFillStringBox(1, 123, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
-
-    if (Current < 0) {
-      Current = 1 - Current;
+    if (current < 0) {
+      current = 1 - current;
     }
-    tmp1 = Current / 1000;
-    tmp2 = Current % 1000;
-    chsnprintf(buf, 10, "I:%2d.%03u", tmp1, tmp2);
-    gdispFillStringBox(160, 123, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
 
-    if (charger_State != Stop) {
-      sumCurrent += Current;
-      sumVoltage += Voltage;
-      
-      idx ++;
-      if (idx >= INA_AVG_FACTOR) {
-        idx = 0;
-        secCurrent = (sumCurrent + (INA_AVG_FACTOR/2)) / INA_AVG_FACTOR;
+    sumCurrent += current;
+    sumVoltage += volt;
+
+    idx ++;
+    if (idx >= INA_AVG_FACTOR) {
+      idx = 0;
+      /* Count, summarize & show values */
+      if (charger_State != Stop) {
+        Current = (sumCurrent + (INA_AVG_FACTOR/2)) / INA_AVG_FACTOR;
+        Voltage = (sumVoltage + (INA_AVG_FACTOR/2)) / INA_AVG_FACTOR;
         sumCurrent = 0;
-        secVoltage = (sumVoltage + (INA_AVG_FACTOR/2)) / INA_AVG_FACTOR;
         sumVoltage = 0;
 
-        secPower = ((secVoltage * secCurrent) + 500) / 1000;
-        Capacity_I += (secCurrent + 1800) / 3600;
-        Capacity_P += (secPower + 1800) / 3600;
+        Power = ((Voltage * Current) + 500) / 1000;
+
+        Capacity_I += Current;
+        Capacity_P += Power;
+
+        tmp1 = Voltage / 1000;
+        tmp2 = Voltage % 1000;
+        chsnprintf(buf, 10, "V:%2d.%03uV", tmp1, tmp2);
+        gdispFillStringBox(1, 123, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
 
-        tmp1 = secVoltage / 1000;
-        tmp2 = secVoltage % 1000;
-        chsnprintf(buf, 10, "Vs:%2d.%03u", tmp1, tmp2);
+        tmp1 = Current / 1000;
+        tmp2 = Current % 1000;
+        chsnprintf(buf, 10, "I:%2d.%03uA", tmp1, tmp2);
+        gdispFillStringBox(1, 153, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+
+        tmp1 = Power / 1000;
+        tmp2 = Power % 1000;
+        chsnprintf(buf, 10, "P:%2d.%03uW", tmp1, tmp2);
+        gdispFillStringBox(1, 183, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+
+        uint32_t tmp0 = (Capacity_I + 1800) / 3600;
+        tmp1 = tmp0 / 1000;
+        tmp2 = tmp0 % 1000;
+        chsnprintf(buf, 10, "CI:%2d.%03uAh", tmp1, tmp2);
+        gdispFillStringBox(1, 153, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+
+        tmp0 = (Capacity_P + 1800) / 3600;
+        tmp1 = tmp0 / 1000;
+        tmp2 = tmp0 % 1000;
+        chsnprintf(buf, 10, "CP:%2d.%03uWh", tmp1, tmp2);
         gdispFillStringBox(1, 183, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+      }
 
-        if (secCurrent < 0) {
-          secCurrent = 1 - secCurrent;
-        }
-        tmp1 = secCurrent / 1000;
-        tmp2 = secCurrent % 1000;
-        chsnprintf(buf, 10, "Is:%2d.%03u", tmp1, tmp2);
-        gdispFillStringBox(160, 183, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+      /* Show bus voltage in STOP mode */
+      if (charger_State == Stop) {
+        tmp1 = sumVoltage / 1000;
+        tmp2 = sumVoltage % 1000;
+        chsnprintf(buf, 10, "V:%2d.%03uV", tmp1, tmp2);
+        gdispFillStringBox(1, 123, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+        sumCurrent=0;
+        sumVoltage=0;
       }
     }
   }
@@ -438,9 +470,20 @@ static void gpt_cb(GPTDriver *gptp) {
     /* One second */
     cnt = 0;
   
+    if (Timer.ss < 59) {
+      Timer.ss ++;
+    } else {
+      Timer.ss = 0;
+      if (Timer.mm < 59) {
+        Timer.mm ++;
+      } else {
+        Timer.mm = 0;
+        Timer.hh ++;
+      }
+    }
+
     chSysLockFromISR();
     chBSemSignalI(&charger_bsem);
     chSysUnlockFromISR();
   }
-//  chSysUnlockFromISR();
 }