rotary_encoder.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * rotary_encoder.h
  3. *
  4. * Created on: Jan 12, 2021
  5. * Author: David Original work by Jose (PTDreamer), 2017
  6. */
  7. #ifndef GENERALIO_ROTARY_ENCODER_H_
  8. #define GENERALIO_ROTARY_ENCODER_H_
  9. #define BIG_TRESHOLD 200
  10. #include "main.h"
  11. typedef enum {
  12. Rotate_Increment, /*!< Encoder was incremented */
  13. Rotate_Decrement, /*!< Encoder was decremented */
  14. Rotate_Increment_while_click, /*!< Encoder was incremented */
  15. Rotate_Decrement_while_click, /*!< Encoder was decremented */
  16. Release, /*!< Encoder was decremented */
  17. Click,
  18. LongClick,
  19. Rotate_Nothing, /*!< Encoder stop at it was before */
  20. } RE_Rotation_t;
  21. /**
  22. * @brief Rotary encoder mode selection for rotation
  23. */
  24. typedef enum {
  25. RE_Mode_Zero, /*!< Rotary encoder mode zero. It is used for direction when it will be increment od decrement, default used */
  26. RE_Mode_One /*!< Rotary encoder mode one. It is used for direction when it will be increment od decrement */
  27. } RE_Mode_t;
  28. typedef enum {
  29. RE_BT_PRESSED,
  30. RE_BT_CLICKED,
  31. RE_BT_LONG_CLICK,
  32. RE_BT_DRAG,
  33. RE_BT_UNRELEASED,
  34. RE_BT_HIDLE
  35. } RE_Click_t;
  36. /**
  37. * @brief Rotary main working structure
  38. */
  39. volatile typedef struct {
  40. int32_t Absolute; /*!< Absolute rotation from beginning, for public use */
  41. int32_t Diff; /*!< Rotary difference from last check, for public use */
  42. RE_Rotation_t Rotation; /*!< Increment, Decrement or nothing, for public use */
  43. RE_Mode_t Mode; /*!< Rotary encoder mode selected */
  44. bool LastA; /*!< Last status of A pin when checking. Meant for private use */
  45. int32_t RE_Count; /*!< Temporary variable to store data between rotation and user check */
  46. GPIO_TypeDef* GPIO_A; /*!< Pointer to GPIOx for Rotary encode A pin. Meant for private use */
  47. GPIO_TypeDef* GPIO_B; /*!< Pointer to GPIOx for Rotary encode B pin. Meant for private use */
  48. GPIO_TypeDef* GPIO_BUTTON; /*!< Pointer to GPIOx for Rotary encode B pin. Meant for private use */
  49. uint16_t GPIO_PIN_A; /*!< GPIO pin for rotary encoder A pin. This pin is also set for interrupt */
  50. uint16_t GPIO_PIN_B; /*!< GPIO pin for rotary encoder B pin. */
  51. uint16_t GPIO_PIN_BUTTON; /*!< GPIO pin for rotary encoder B pin. */
  52. RE_Click_t pv_click;
  53. bool direction;
  54. bool halfPointReached;
  55. } RE_State_t;
  56. void RE_Init(RE_State_t* data, GPIO_TypeDef* GPIO_A_Port, uint16_t GPIO_A_Pin, GPIO_TypeDef* GPIO_B_Port, uint16_t GPIO_B_Pin, GPIO_TypeDef* GPIO_BUTTON_Port, uint16_t GPIO_BUTTON_Pin);
  57. void RE_SetMode(RE_State_t* data, RE_Mode_t mode);
  58. RE_Rotation_t RE_Get(RE_State_t* data);
  59. void RE_Process(RE_State_t* data);
  60. extern volatile RE_State_t RE1_Data;
  61. #endif /* GENERALIO_ROTARY_ENCODER_H_ */