Guest User

Untitled

a guest
Feb 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. /* Rotary encoder handler for arduino.
  2. *
  3. * Copyright 2011 Ben Buxton. Licenced under the GNU GPL Version 3.
  4. * Contact: bb@cactii.net
  5. *
  6. */
  7.  
  8. #include "Arduino.h"
  9. #include "Mrot.h"
  10.  
  11. /*
  12. * The below state table has, for each state (row), the new state
  13. * to set based on the next encoder output. From left to right in,
  14. * the table, the encoder outputs are 00, 01, 10, 11, and the value
  15. * in that position is the new state to set.
  16. */
  17.  
  18. #define R_START 0x0
  19.  
  20. #ifdef HALF_STEP
  21. // Use the half-step state table (emits a code at 00 and 11)
  22. #define R_CCW_BEGIN 0x1
  23. #define R_CW_BEGIN 0x2
  24. #define R_START_M 0x3
  25. #define R_CW_BEGIN_M 0x4
  26. #define R_CCW_BEGIN_M 0x5
  27. const unsigned char ttable[6][4] = {
  28. // R_START (00)
  29. {R_START_M, R_CW_BEGIN, R_CCW_BEGIN, R_START},
  30. // R_CCW_BEGIN
  31. {R_START_M | DIR_CCW, R_START, R_CCW_BEGIN, R_START},
  32. // R_CW_BEGIN
  33. {R_START_M | DIR_CW, R_CW_BEGIN, R_START, R_START},
  34. // R_START_M (11)
  35. {R_START_M, R_CCW_BEGIN_M, R_CW_BEGIN_M, R_START},
  36. // R_CW_BEGIN_M
  37. {R_START_M, R_START_M, R_CW_BEGIN_M, R_START | DIR_CW},
  38. // R_CCW_BEGIN_M
  39. {R_START_M, R_CCW_BEGIN_M, R_START_M, R_START | DIR_CCW},
  40. };
  41. #else
  42. // Use the full-step state table (emits a code at 00 only)
  43. #define R_CW_FINAL 0x1
  44. #define R_CW_BEGIN 0x2
  45. #define R_CW_NEXT 0x3
  46. #define R_CCW_BEGIN 0x4
  47. #define R_CCW_FINAL 0x5
  48. #define R_CCW_NEXT 0x6
  49.  
  50. const unsigned char ttable[7][4] = {
  51. // R_START
  52. {R_START, R_CW_BEGIN, R_CCW_BEGIN, R_START},
  53. // R_CW_FINAL
  54. {R_CW_NEXT, R_START, R_CW_FINAL, R_START | DIR_CW},
  55. // R_CW_BEGIN
  56. {R_CW_NEXT, R_CW_BEGIN, R_START, R_START},
  57. // R_CW_NEXT
  58. {R_CW_NEXT, R_CW_BEGIN, R_CW_FINAL, R_START},
  59. // R_CCW_BEGIN
  60. {R_CCW_NEXT, R_START, R_CCW_BEGIN, R_START},
  61. // R_CCW_FINAL
  62. {R_CCW_NEXT, R_CCW_FINAL, R_START, R_START | DIR_CCW},
  63. // R_CCW_NEXT
  64. {R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START},
  65. };
  66. #endif
  67.  
  68. /*
  69. * Constructor. Each arg is the pin number for each encoder contact.
  70. */
  71. Mrot::Mrot(char pin_) {
  72. pin = pin_;
  73. state = R_START;
  74. }
  75.  
  76. void Mrot::begin(bool pullup) {
  77. if (pullup){
  78. // Enable weak pullups
  79. pinMode(pin,INPUT_PULLUP);
  80. }else{
  81. // Set pins to input.
  82. pinMode(pin, INPUT);
  83. }
  84. }
  85.  
  86. void Mrot::preprocess() {
  87. firstPinState = digitalRead(pin);
  88. }
  89.  
  90. unsigned char Mrot::process() {
  91. // Determine new state from the pins and state table.
  92. state = ttable[state & 0xf][(digitalRead(pin) << 1) | firstPinState];
  93. // Return emit bits, ie the generated event.
  94. return state & 0x30;
  95. }
Add Comment
Please, Sign In to add comment