Subversion Repositories DashDisplay

Rev

Rev 44 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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