Subversion Repositories DashDisplay

Rev

Rev 49 | Blame | Last modification | View Log | Download | RSS feed

  1. #include "stm32l1xx_hal.h"
  2.  
  3. #include "switches.h"
  4. #include "main.h"
  5.  
  6. typedef enum
  7. {
  8.   PH_00 = 0, PH_10 = 2, PH_11 = 3, PH_01 = 1,
  9. } eGreyCode;
  10.  
  11. typedef struct
  12. {
  13.   GPIO_TypeDef *GPIO;
  14.   uint16_t Pin;
  15.   uint16_t Debounce;
  16. } sGPIOPin;
  17.  
  18. volatile int push_pos[MAX_PUSHBUTTONS];
  19. volatile int sw_count[MAX_PUSHBUTTONS]; //< debounced switch state
  20. volatile int sw_newstate[MAX_PUSHBUTTONS]; //< debounced switch state
  21.  
  22. volatile int dial_count[MAX_DIALS];
  23. volatile int dial_pos[MAX_DIALS];
  24. volatile int dial_last[MAX_DIALS]; // previous debounced switch position
  25.  
  26. static const sGPIOPin sw_arr[MAX_PUSHBUTTONS] =
  27.   {
  28.     { SW1_PUSH_GPIO_Port, SW1_PUSH_Pin, 10 }, // two debounced switches
  29.         { SW2_PUSH_GPIO_Port, SW2_PUSH_Pin, 10 }, };
  30.  
  31. // call this to setup switch states
  32. void
  33. InitSwitches (void)
  34. {
  35.   int i;
  36.  
  37.   for (i = 0; i < MAX_PUSHBUTTONS; i++)
  38.     {
  39.       sw_newstate[i] = 0;
  40.       sw_count[i] = 0;
  41.       push_pos[i] = 0;
  42.     }
  43. // reset the dial positions
  44.   for (i = 0; i < MAX_DIALS; i++)
  45.     {
  46.       dial_pos[i] = 0;
  47.     }
  48.   dial_count[0] = __HAL_TIM_GET_COUNTER(&htim9) >> 2;
  49.   dial_count[1] = __HAL_TIM_GET_COUNTER(&htim3) >> 2;
  50. }
  51.  
  52. // this is the interrupt handler , dealling with the switches
  53. void
  54. HandleSwitches (void)
  55. {
  56.  
  57.   int i;
  58.  
  59.   for (i = 0; i < MAX_PUSHBUTTONS; i++)
  60.     {
  61.  
  62.       int sw = HAL_GPIO_ReadPin (sw_arr[i].GPIO, sw_arr[i].Pin)
  63.           == GPIO_PIN_RESET;
  64.  
  65.       // bouncing, reset counter
  66.       if (sw == sw_newstate[i])
  67.         {
  68.           sw_count[i] = 0;
  69.         }
  70.       else
  71.         {
  72.           // count up to debounce time
  73.           if (sw_count[i] < sw_arr[i].Debounce)
  74.             {
  75.               sw_count[i]++;
  76.  
  77.               if (sw_count[i] == sw_arr[i].Debounce)
  78.                 {
  79.                   sw_newstate[i] = sw;
  80.                 }
  81.             }
  82.         }
  83.     }
  84.   push_pos[0] = sw_newstate[0];
  85.   push_pos[1] = sw_newstate[1];
  86.  
  87.   int cnt0 = __HAL_TIM_GET_COUNTER(&htim9) >> 2;  // counts 4 per step
  88.   int cnt1 = __HAL_TIM_GET_COUNTER(&htim3) >> 2;  // counts 4 per step
  89.  
  90.   // always count up, or count down to zero but dont wrap
  91.  
  92.   if ((dial_pos[0] != 0) || (cnt0 > dial_count[0]))
  93.     dial_pos[0] += cnt0 - dial_count[0];
  94.   dial_count[0] = cnt0;
  95.  
  96.   if ((dial_pos[1] != 0) || (cnt1 > dial_count[1]))
  97.     dial_pos[1] += cnt1 - dial_count[1];
  98.   dial_count[1] = cnt1;
  99.  
  100. }
  101.  
  102.