lib Modulove
Library for building custom scripts for Modulove modules.
encoder.h
Go to the documentation of this file.
1 
11 #ifndef ENCODER_H
12 #define ENCODER_H
13 
14 #include <Arduino.h>
15 
16 // Encoder & button
17 #include <SimpleRotary.h>
18 
20 
21 namespace modulove {
22 
23 class Encoder {
24  public:
26  enum Direction {
27  DIRECTION_UNCHANGED,
28  DIRECTION_INCREMENT,
29  DIRECTION_DECREMENT,
30  };
31 
33  enum PressType {
34  PRESS_NONE,
35  PRESS_SHORT,
36  PRESS_LONG,
37  };
38 
39  Encoder() : encoder_(ENCODER_PIN1, ENCODER_PIN2, ENCODER_SW_PIN) {}
40  ~Encoder() {}
41 
43  void setDirection(byte direction) {
44  reversed_ = direction == 1;
45  }
46 
50  return (reversed_)
51  ? rotate_reversed()
52  : rotate();
53  }
54 
55  Direction rotate() {
56  switch (encoder_.rotate()) {
57  case 1:
58  return DIRECTION_INCREMENT;
59  case 2:
60  return DIRECTION_DECREMENT;
61  default:
62  return DIRECTION_UNCHANGED;
63  }
64  }
65 
66  Direction rotate_reversed() {
67  switch (encoder_.rotate()) {
68  case 1:
69  return DIRECTION_DECREMENT;
70  case 2:
71  return DIRECTION_INCREMENT;
72  default:
73  return DIRECTION_UNCHANGED;
74  }
75  }
76 
79  switch (_press()) {
80  case 1:
81  return PRESS_SHORT;
82  case 2:
83  return PRESS_LONG;
84  default:
85  return PRESS_NONE;
86  }
87  }
88 
90  bool ShortPressed() {
91  return Pressed() == PRESS_SHORT;
92  }
93 
95  bool LongPressed() {
96  return Pressed() == PRESS_LONG;
97  }
98 
99  private:
100  SimpleRotary encoder_;
101  static const int LONG_PRESS_DURATION_MS = 1000;
102  byte reversed_ = 0;
103 
104  byte _press() {
105  // Check for long press to endable editing seed.
106  // press and release for < 1 second to return 1 for short press
107  // press and release for > 1 second to return 2 for long press.
108  return encoder_.pushType(LONG_PRESS_DURATION_MS);
109  }
110 };
111 
112 } // namespace modulove
113 
114 #endif
Arduino pin definitions for the Modulove A-RYTH-MATIC module.
Definition: encoder.h:23
Direction Rotate()
Get the rotary direction if it has turned.
Definition: encoder.h:49
PressType Pressed()
Definition: encoder.h:78
PressType
Enum for type of switch press.
Definition: encoder.h:33
bool ShortPressed()
Definition: encoder.h:90
Direction
Enum constants for encoder rotation increment/decrement state.
Definition: encoder.h:26
bool LongPressed()
Definition: encoder.h:95
void setDirection(byte direction)
Set the encoder direction by passing 0 for cw increment or 1 for ccw increment.
Definition: encoder.h:43