Ver código fonte

Reworked the handling of button presses.

Vladimir N. Shilov 2 anos atrás
pai
commit
762a96fa94
3 arquivos alterados com 143 adições e 155 exclusões
  1. 6 9
      inc/buttons.h
  2. 16 24
      src/buttons.c
  3. 121 122
      src/main.c

+ 6 - 9
inc/buttons.h

@@ -2,26 +2,20 @@
 #ifndef _BUTTONS_H_
 #define _BUTTONS_H_
 
-/* Events */
-#define EVT_BTN1_PRS EVENT_MASK(0)
-#define EVT_BTN2_PRS EVENT_MASK(1)
-#define EVT_BTN3_PRS EVENT_MASK(2)
-#define EVT_BTN4_PRS EVENT_MASK(3)
-
 /* time constant in ms */
 #define BTN_SCAN_PERIOD     10
 #define BTN_SCAN_PAUSE      200
 #define BTN_TIME_PRESSED    30
 #define BTN_TIME_HOLDED     500
 #define BTN_TIME_REPEATED   50
-#define BUTTON_NUMS         4
 
 /* type defs */
 typedef enum btn_num {
   Button1 = 0,
   Button2 = 1,
   Button3 = 2,
-  Button4 = 3
+  Button4 = 3,
+  Button_Num = 4
 } button_num_t;
 
 typedef enum btn_state {
@@ -31,11 +25,14 @@ typedef enum btn_state {
   BTN_st_Released
 } button_state_t;
 
+/* Handler function type */
+typedef void (*btn_hndlr)(const button_state_t);
+
 /* function prototypes */
 #ifdef __cplusplus
 extern "C" {
 #endif
-  void buttons_Init(thread_t * thread);
+  void buttons_Init(btn_hndlr ha[]);
   button_state_t buttons_GetState(const button_num_t btn);
 #ifdef __cplusplus
 }

+ 16 - 24
src/buttons.c

@@ -2,27 +2,22 @@
 #include <hal.h>
 #include "buttons.h"
 
+static btn_hndlr btnh[4];
+
 /* Constants */
-static const ioline_t button_Lines[BUTTON_NUMS] = {
+static const ioline_t button_Lines[Button_Num] = {
   LINE_BUTTON1, LINE_BUTTON2, LINE_BUTTON3, LINE_BUTTON4
 };
-static const eventmask_t button_Events[BUTTON_NUMS] = {
-  EVT_BTN1_PRS, EVT_BTN2_PRS, EVT_BTN3_PRS, EVT_BTN4_PRS
-};
 
 /* Variables */
-static thread_t * evt_thread;
 static virtual_timer_t button_vt;
-static button_state_t button_States[BUTTON_NUMS] = {
-  BTN_st_Clear, BTN_st_Clear, BTN_st_Clear, BTN_st_Clear
-};
-static uint8_t button_Time[BUTTON_NUMS] = {0, 0, 0, 0};
+static button_state_t button_States[Button_Num] = {0};
+static uint8_t button_Time[Button_Num] = {0};
 
 /* Functions */
 static void button_Process(virtual_timer_t *vtp, void *p) {
   (void)vtp;
   (void)p;
-  eventmask_t events = 0;
   static int pause = 0;
 
   if (pause != 0) {
@@ -30,7 +25,7 @@ static void button_Process(virtual_timer_t *vtp, void *p) {
   } else {
 
     int i;
-    for (i=0; i<BUTTON_NUMS; i++) {
+    for (i=0; i<Button_Num; i++) {
 
       if (palReadLine(button_Lines[(button_num_t)i]) == BUTTON_PRESSED) {
       /* button pressed */
@@ -39,7 +34,7 @@ static void button_Process(virtual_timer_t *vtp, void *p) {
           /* process long press */
           button_Time[i] -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
           button_States[i] = BTN_st_Holded;
-          events |= button_Events[i]; // autorepeat
+          btnh[i](BTN_st_Pressed); // autorepeat
         }
 
       } else if (button_Time[i] != 0) {
@@ -47,30 +42,27 @@ static void button_Process(virtual_timer_t *vtp, void *p) {
         if (button_Time[i] >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
           /* process short press */
           button_States[i] = BTN_st_Pressed;
-          events |= button_Events[i];
+          btnh[i](BTN_st_Pressed);
         }
         button_Time[i] = 0;
         button_States[i] = BTN_st_Released;
         pause = (BTN_SCAN_PAUSE/BTN_SCAN_PERIOD);
-      }
-
-      /* Segnaling events to the thread, if any. */
-      if (events) {
-        chSysLockFromISR();
-        chEvtSignalI(evt_thread, events);
-        chSysUnlockFromISR();
-        events = 0;
+        btnh[i](BTN_st_Pressed);
       }
     } /* end FOR */
 
   } /* end Pause check */
 }
 
-void buttons_Init(thread_t * thread) {
-  evt_thread = thread;
+void buttons_Init(btn_hndlr ha[]) {
+  btnh[Button1] = ha[Button1];
+  btnh[Button2] = ha[Button2];
+  btnh[Button3] = ha[Button3];
+  btnh[Button4] = ha[Button4];
+
   chVTSetContinuous(&button_vt, TIME_MS2I(BTN_SCAN_PERIOD), button_Process, NULL);
 }
 
 button_state_t buttons_GetState(const button_num_t btn) {
   return button_States[btn];
-}
+}

+ 121 - 122
src/main.c

@@ -21,6 +21,8 @@
 #include "buttons.h"
 #include "INA3221.h"
 
+#define INA_SCAN_PERIOS_MS    100
+
 /* Type definitions */
 typedef enum chrgr_state {
   Stop = 0,
@@ -73,11 +75,21 @@ static const charger_channel_t charger_Channels[INA3221_CH_NUM] = {
   {20, 8190}, {50, 3276}, {10, 16380}
 };
 
+/* Privae functions */
+static void prepare_Screen(void);
+static void ina_Process(virtual_timer_t *vtp, void *p);
+static void btn1_handler(const button_state_t);
+static void btn2_handler(const button_state_t);
+static void btn3_handler(const button_state_t);
+static void btn4_handler(const button_state_t);
+
 /* Private variables */
+static virtual_timer_t ina_vt;
 static charger_state_t charger_State;
 static uint8_t current_Channel;
-static int32_t Current, sumCurrent=0;
-static uint32_t Voltage, sumVoltage=0;
+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 int INA_Present = 0;
 static BaseSequentialStream * chp = (BaseSequentialStream*) &SD1;
 GHandle GW1;  // The handle for our console
@@ -85,53 +97,7 @@ ina3221_t ina3221;
 static gFont font1;
 static gFont font2;
 static time_cnt_t Timer = {0};
-
-/* Privae functions */
-static void prepareScreen(void);
-static void btn1_handler(void);
-static void btn2_handler(void);
-static void btn3_handler(void);
-static void btn4_handler(void);
-
-/*
- * Read Voltage, Current, update screen info.
- */
-static THD_WORKING_AREA(waInaThread, 128);
-static THD_FUNCTION(InaThread, arg) {
-  (void)arg;
-  systime_t time;
-  int32_t tmp1, tmp2;
-  char buf[12];
-
-  while (true) {
-    time = chVTGetSystemTimeX();
-
-    if (INA_Present != 0) {
-      Current = INA3221_getCurrent(&ina3221, (ina3221_ch_t)current_Channel);
-      Voltage = 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;
-      }
-      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;
-      }
-    }
-
-    chThdSleepUntil(time += TIME_MS2I(100));
-  }
-}
+static btn_hndlr bha[Button_Num] = {btn1_handler, btn2_handler, btn3_handler, btn4_handler};
 
 /*
  * Green LED blinker thread, times are in milliseconds.
@@ -148,41 +114,6 @@ static THD_FUNCTION(Thread1, arg) {
   }
 }
 
-/*
- * Pointer to the Buton handler thread.
- */
-thread_t * btn_thread;
-
-static THD_WORKING_AREA(waBTNThread, 128);
-static THD_FUNCTION(BTNThread, arg) {
-  (void)arg;
-
-  /* Thread activity.*/
-  while (true) {
-    /* Waiting for any event.*/
-    eventmask_t evt = chEvtWaitAny(ALL_EVENTS);
-    palToggleLine(LINE_LED2);
- 
-    /* Serving events.*/
-    if (evt & EVT_BTN1_PRS) {
-      /* BTN1 event.*/
-      btn1_handler();
-    }
-    if (evt & EVT_BTN2_PRS) {
-      /* BTN2 event.*/
-      btn2_handler();
-    }
-    if (evt & EVT_BTN3_PRS) {
-      /* BTN3 event.*/
-      btn3_handler();
-    }
-    if (evt & EVT_BTN4_PRS) {
-      /* BTN4 event.*/
-      btn4_handler();
-    }
-  }
-}
-
 /*
  * Application entry point.
  */
@@ -204,7 +135,7 @@ int main(void) {
    * Initialize uGFX and the underlying system
    */
   gfxInit();
-  prepareScreen();
+  prepare_Screen();
 
   gptStart(&GPTD5, &gpt_cfg);
 
@@ -223,14 +154,13 @@ int main(void) {
   /*
    * Starting the button handle thread.
    */
-  btn_thread = chThdCreateStatic(waBTNThread, sizeof(waBTNThread),
-                                  NORMALPRIO+1, BTNThread, NULL);
-  buttons_Init(btn_thread);
+  buttons_Init(bha);
 
   /*
    * Creates the INA thread.
    */
-  chThdCreateStatic(waInaThread, sizeof(waInaThread), NORMALPRIO, InaThread, NULL);
+  //chThdCreateStatic(waInaThread, sizeof(waInaThread), NORMALPRIO, InaThread, NULL);
+  chVTSetContinuous(&ina_vt, TIME_MS2I(INA_SCAN_PERIOS_MS), ina_Process, NULL);
 
   gwinPrintf(GW1, "Try to init INA3221....\n");
   ina3221.i2c_addr = INA3221_ADDR40_GND;
@@ -278,7 +208,7 @@ int main(void) {
   }
 }
 
-static void prepareScreen(void) {
+static void prepare_Screen(void) {
   /* Set some fonts */
   font1 = gdispOpenFont("DejaVu Sans Book 12");
   font2 = gdispOpenFont("DejaVu Sans Book 24");
@@ -328,51 +258,120 @@ static void prepareScreen(void) {
   gwinPrintf(GW1, "\033bConsole\033B started sucessful.\n");
 }
 
-static void btn1_handler(void) {
-  gwinPrintf(GW1, "Button 1 pressed\n");
+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);
+    current_Channel ++;
+    if (current_Channel >= INA3221_CH_NUM) {
+      current_Channel = INA3221_CH1;
+    }
+    int tmp = 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);
+  }
 }
 
-static void btn2_handler(void) {
-  gwinPrintf(GW1, "Button 2 pressed\n");
+static void btn2_handler(button_state_t state) {
+  if (state == BTN_st_Pressed) {
+    gwinPrintf(GW1, "Button 2 pressed\n");
+  }
 }
 
-static void btn3_handler(void) {
-  gwinPrintf(GW1, "Button 3 pressed\n");
+static void btn3_handler(button_state_t state) {
+  if (state == BTN_st_Pressed) {
+    gwinPrintf(GW1, "Button 3 pressed\n");
+  }
 }
 
-static void btn4_handler(void) {
-  gwinPrintf(GW1, "Button 4 pressed\n");
-
-  switch(charger_State) {
-  case Stop:
-    charger_State = Charge1;
-    gwinPrintf(GW1, "Change state to Charge#1\n");
-    gdispFillStringBox(1, 91, 78, 15, "[4] Stop", font1, Yellow, Blue, gJustifyLeft);
-    break;
-  default:
-    charger_State = Stop;
-    gwinPrintf(GW1, "Change state to Stop\n");
-    gdispFillStringBox(1, 91, 78, 15, "[4] Start", font1, Yellow, Blue, gJustifyLeft);
-    break;
+static void btn4_handler(button_state_t state) {
+  if (state == BTN_st_Pressed) {
+    gwinPrintf(GW1, "Button 4 pressed\n");
+
+    switch(charger_State) {
+    case Stop:
+      charger_State = Charge1;
+      gwinPrintf(GW1, "Change state to Charge#1\n");
+      gdispFillStringBox(1, 91, 78, 15, "[4] Stop", font1, Yellow, Blue, gJustifyLeft);
+      break;
+    default:
+      charger_State = Stop;
+      gwinPrintf(GW1, "Change state to Stop\n");
+      gdispFillStringBox(1, 91, 78, 15, "[4] Start", font1, Yellow, Blue, gJustifyLeft);
+      break;
+    }
+  }
+}
+
+/*
+ * Read Voltage & Current, update screen info.
+ */
+static void ina_Process(virtual_timer_t *vtp, void *p) {
+  (void)vtp;
+  (void)p;
+
+  int32_t tmp1, tmp2;
+  char buf[12];
+  static int idx = 0;
+
+  palToggleLine(LINE_LED2);
+  if (INA_Present != 0) {
+    Current = INA3221_getCurrent(&ina3221, (ina3221_ch_t)current_Channel);
+    Voltage = 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;
+    }
+    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;
+      if (idx < 9) {
+        idx ++;
+      } else {
+        secCurrent = (sumCurrent + 5) / 10;
+        sumCurrent = 0;
+        secVoltage = (sumVoltage + 5) / 10;
+        sumVoltage = 0;
+
+        secPower = ((secVoltage * secCurrent) + 500) / 1000;
+        Capacity_I += (secCurrent + 1800) / 3600;
+        Capacity_P += (secPower + 1800) / 3600;
+
+        tmp1 = secVoltage / 1000;
+        tmp2 = secVoltage % 1000;
+        chsnprintf(buf, 10, "V:%2d.%03u", 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, "I:%2d.%03u", tmp1, tmp2);
+        gdispFillStringBox(160, 183, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
+      }
+    }
   }
 }
 
 /*
- * GPT callback.
+ * GPT callback. One second pulse.
  */
 static void gpt_cb(GPTDriver *gptp) {
   (void)gptp;