Download the file
  1. #include "RotaryEncoder.h"
  2.  
  3. #define ENC_PORT PINB
  4.  
  5. RotaryEncoder::RotaryEncoder(uint8_t a, uint8_t b) {
  6. pina = a;
  7. pinb = b;
  8. old_pins = 0;
  9. old_dir = dir = 0;
  10. pinMode(pina, INPUT);
  11. pinMode(pinb, INPUT);
  12. digitalWrite(pina, HIGH);
  13. digitalWrite(pinb, HIGH);
  14. }
  15.  
  16. int8_t RotaryEncoder::enc_states[] = {0,1,-1,0,-1,0,0,1,1,0,0,-1,0,-1,1,0};
  17.  
  18. uint8_t RotaryEncoder::read() {
  19. old_pins <<= 2;
  20. old_pins |= (( ENC_PORT & 0x30 ) >> 4);
  21. dir += RotaryEncoder::enc_states[( old_pins & 0x0f )];
  22. if (dir/2 > old_dir/2) {
  23. dir = old_dir = 0;
  24. return 1;
  25. }
  26. else if (dir/2 < old_dir/2) {
  27. dir = old_dir = 0;
  28. return -1;
  29. }
  30. return 0;
  31. }
  32. // vim:ft=arduino
  33.