Download the file
  1. #include "rotary.h"
  2.  
  3. void RotaryEncoder::do_run() {
  4. /*
  5.   if (Serial.available()) {
  6.   char c = Serial.read();
  7.   switch (c) {
  8.   case '+':
  9.   Serial.println("+");
  10.   //menu->clockwise();
  11.   break;
  12.   case '-':
  13.   Serial.println("-");
  14.   //menu->counter_clockwise();
  15.   break;
  16.   case ' ':
  17.   Serial.println("S");
  18.   //menu->click();
  19.   break;
  20.   default:
  21.   break;
  22.   }
  23.   }
  24. */
  25. int8_t direction = read_direction();
  26. switch (direction) {
  27. case 1:
  28. handler->clockwise();
  29. //Serial.println("+");
  30. break;
  31. case -1:
  32. handler->counter_clockwise();
  33. //Serial.println("-");
  34. break;
  35. }
  36.  
  37. if (read_click() == 1) {
  38. handler->click();
  39. //Serial.println("S");
  40. }
  41. }
  42.  
  43. uint8_t RotaryEncoder::read_direction() {
  44. old_pins <<= 2;
  45. old_pins |= (( ENC_PORT & ENC_BITS ) >> ENC_OFFS);
  46. dir += enc_states[( old_pins & 0x0f )];
  47. if (dir/2 > old_dir/2) {
  48. dir = old_dir = 0;
  49. return 1;
  50. }
  51. else if (dir/2 < old_dir/2) {
  52. dir = old_dir = 0;
  53. return -1;
  54. }
  55. return 0;
  56. }
  57.  
  58. uint8_t RotaryEncoder::read_click() {
  59. old_state <<= 1;
  60. old_state |= !digitalRead(but_pin) & 0x01;
  61. return but_states[old_state & 0x03];
  62. }
  63.  
  64. int8_t RotaryEncoder::enc_states[] = {0,1,-1,0,-1,0,0,1,1,0,0,-1,0,-1,1,0};
  65. int8_t RotaryEncoder::but_states[] = {0, 1, -1 ,0};
  66. // vim:ft=arduino
  67.