Ver código fonte

Charger State changes process.

Vladimir N. Shilov 2 anos atrás
pai
commit
0e35e4785a
1 arquivos alterados com 159 adições e 47 exclusões
  1. 159 47
      src/main.c

+ 159 - 47
src/main.c

@@ -27,6 +27,11 @@
 #define CHRGR_PAUSE1_S        60
 #define CHRGR_PAUSE2_S        60
 
+#define INA_ALL_VALUSE        EVENT_MASK(0)
+#define INA_BUS_VALUES        EVENT_MASK(1)
+#define CHRGR_ST_CHANGE       EVENT_MASK(2)
+#define TIME_CHANGE           EVENT_MASK(3)
+
 /* Type definitions */
 typedef enum chrgr_state {
   Stop = 0,
@@ -82,7 +87,7 @@ static const charger_channel_t charger_Channels[INA3221_CH_NUM] = {
 /* Privae functions */
 static void prepare_Screen(void);
 static void ina_Process(void);
-static void mode_vt_cb(virtual_timer_t *vtp, charger_state_t st);
+static void mode_vt_cb(virtual_timer_t *vtp, void *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);
@@ -91,7 +96,8 @@ 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 event_source_t ina_all_event, ina_bus_event;
+static event_source_t chrgr_st_event, time_event;
 static charger_state_t charger_State;
 static ina3221_ch_t current_Channel;
 static uint32_t Current;
@@ -109,7 +115,7 @@ static btn_hndlr bha[Button_Num] = {btn1_handler, btn2_handler, btn3_handler, bt
 /*
  * INA process thread.
  */
-static THD_WORKING_AREA(waInaThread, 256);
+static THD_WORKING_AREA(waInaThread, 128);
 static THD_FUNCTION(InaThread, arg) {
   (void)arg;
 
@@ -122,21 +128,16 @@ static THD_FUNCTION(InaThread, arg) {
 /*
  * Charger process thread. Once per second.
  */
-static THD_WORKING_AREA(waChrgThread, 256);
+static THD_WORKING_AREA(waChrgThread, 128);
 static THD_FUNCTION(ChrgThread, arg) {
   (void)arg;
-  char buf[16];
 
   while (true) {
     chBSemWait(&charger_bsem);
 
-    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(160, 123, 158, 29, buf, font2, Red, Gray, gJustifyCenter);
-
+    chSysLockFromISR();
+    chEvtBroadcastI(&time_event);
+    chSysUnlockFromISR();
   }
 }
 
@@ -244,12 +245,137 @@ int main(void) {
   charger_State = Stop;
   current_Channel = INA3221_CH1;
 
+  event_listener_t el0, el1, el2, el3;
+
+  /* Events initialization and registration.*/
+  chEvtObjectInit(&ina_all_event);
+  chEvtObjectInit(&ina_bus_event);
+  chEvtObjectInit(&chrgr_st_event);
+  chEvtObjectInit(&time_event);
+  chEvtRegister(&ina_all_event, &el0, 0);
+  chEvtRegister(&ina_bus_event, &el1, 1);
+  chEvtRegister(&chrgr_st_event, &el2, 2);
+  chEvtRegister(&time_event, &el3, 3);
+
+  eventmask_t events;
+  charger_state_t oldState = Stop;
+  char buf[16];
+  uint32_t tmp0, tmp1, tmp2;
+
   /*
    * Normal main() thread activity, in this demo it does nothing except
    * sleeping in a loop and check the button state.
    */
   while (true) {
-    chThdSleepMilliseconds(500);
+    //chThdSleepMilliseconds(500);
+    events = chEvtWaitAny(ALL_EVENTS);
+
+    if (events & INA_ALL_VALUSE) {
+      /* need to control curren/voltage in charge/decharge modes */
+
+      tmp1 = Voltage / 1000;
+      tmp2 = Voltage % 1000;
+      chsnprintf(buf, 11, "V:%2d.%03uV", tmp1, tmp2);
+      gdispFillStringBox(1, 123, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+
+      tmp1 = Current / 1000;
+      tmp2 = Current % 1000;
+      chsnprintf(buf, 11, "I:%2d.%03uA", tmp1, tmp2);
+      gdispFillStringBox(1, 153, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+
+      tmp1 = Power / 1000;
+      tmp2 = Power % 1000;
+      chsnprintf(buf, 11, "P:%2d.%03uW", tmp1, tmp2);
+      gdispFillStringBox(1, 183, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+
+      tmp0 = (Capacity_I + 1800) / 3600;
+      tmp1 = tmp0 / 1000;
+      tmp2 = tmp0 % 1000;
+      chsnprintf(buf, 13, "CI:%2d.%03uAh", tmp1, tmp2);
+      gdispFillStringBox(160, 153, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+
+      tmp0 = (Capacity_P + 1800) / 3600;
+      tmp1 = tmp0 / 1000;
+      tmp2 = tmp0 % 1000;
+      chsnprintf(buf, 13, "CP:%2d.%03uWh", tmp1, tmp2);
+      gdispFillStringBox(160, 183, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+    }
+
+    if (events & INA_BUS_VALUES) {
+      tmp1 = Voltage / 1000;
+      tmp2 = Voltage % 1000;
+      chsnprintf(buf, 11, "V:%2d.%03uV", tmp1, tmp2);
+      gdispFillStringBox(1, 123, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+    }
+
+    if (events & CHRGR_ST_CHANGE) {
+      switch (charger_State) {
+      case Charge1:
+        oldState = Charge1;
+        Capacity_I = 0;
+        Capacity_P = 0;
+        Timer.hh = 0;
+        Timer.mm = 0;
+        Timer.ss = 0;
+        gwinPrintf(GW1, "Begin process: Charge_1\n");
+        // power relay on
+        palClearLine(LINE_RELAY1);
+        break;
+
+      case Pause1:
+        oldState = Pause1;
+        gwinPrintf(GW1, "Pause after Charge_1\n");
+        // power relay off
+        palSetLine(LINE_RELAY1);
+        chVTSet(&mode_vt, CHRGR_PAUSE1_S, mode_vt_cb, (void *)Decharge);
+        break;
+
+      case Decharge:
+        oldState = Decharge;
+        gwinPrintf(GW1, "Begin process: Decharge\n");
+        // load relay on
+        palClearLine(LINE_RELAY2);
+        break;
+
+      case Pause2:
+        oldState = Pause2;
+
+        tmp0 = (Capacity_I + 1800) / 3600;
+        tmp1 = tmp0 / 1000;
+        tmp2 = tmp0 % 1000;
+        chsnprintf(buf, 15, "CI:%2d.%03uAh\n", tmp1, tmp2);
+        gwinPrintf(GW1, buf);
+
+        gwinPrintf(GW1, "Pause after Decharge\n");
+        // load relay off
+        palSetLine(LINE_RELAY2);
+        chVTSet(&mode_vt, CHRGR_PAUSE2_S, mode_vt_cb, (void *)Charge2);
+        break;
+
+      case Charge2:
+        oldState = Charge2;
+        gwinPrintf(GW1, "Begin process: Charge_2\n");
+        // power relay on
+        palClearLine(LINE_RELAY1);
+        break;
+      
+      default:
+        oldState = Stop;
+        // power and load relay off
+        palSetLine(LINE_RELAY1);
+        palSetLine(LINE_RELAY2);
+        break;
+      }
+    }
+
+    if (events & TIME_CHANGE) {
+      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(160, 123, 158, 29, buf, font2, Red, Gray, gJustifyCenter);
+    }
   }
 }
 
@@ -369,21 +495,29 @@ static void btn4_handler(button_state_t state) {
       gdispFillStringBox(1, 91, 78, 15, "[4] Start", font1, Yellow, Blue, gJustifyLeft);
       break;
     }
+
+    chSysLockFromISR();
+    chEvtBroadcastI(&chrgr_st_event);
+    chSysUnlockFromISR();
   }
 }
 
-static void mode_vt_cb(virtual_timer_t *vtp, charger_state_t st) {
+static void mode_vt_cb(virtual_timer_t *vtp, void * st) {
   (void)vtp;
-  charger_State = st;
+
+  charger_State = (charger_state_t)st;
+
+  chSysLockFromISR();
+  chEvtBroadcastI(&chrgr_st_event);
+  chSysUnlockFromISR();
 }
 
 /*
  * Read Voltage & Current, update screen info.
  */
 static void ina_Process(void) {
-  int32_t tmp1, tmp2, current;
+  int32_t current;
   uint32_t volt;
-  char buf[12];
   static int idx = 0;
   static uint32_t sumCurrent=0, sumVoltage=0;
 
@@ -413,42 +547,20 @@ static void ina_Process(void) {
         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 = 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);
+        chSysLockFromISR();
+        chEvtBroadcastI(&ina_all_event);
+        chSysUnlockFromISR();
       }
 
       /* 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);
+        Voltage = sumVoltage;
         sumCurrent=0;
         sumVoltage=0;
+
+        chSysLockFromISR();
+        chEvtBroadcastI(&ina_bus_event);
+        chSysUnlockFromISR();
       }
     }
   }