Subversion Repositories DashDisplay

Rev

Rev 71 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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