Commanders
Arduino buttons/bus library
ButtonsCommanderEncoder.cpp
1 /*************************************************************
2 project: <Commanders>
3 author: <Thierry PARIS>
4 description: <Encoder returning a current value, or a moving direction.>
5 *************************************************************/
6 
7 #include <Commanders.h>
8 #ifndef NO_BUTTONSCOMMANDER
9 #ifndef NO_BUTTONSCOMMANDERENCODER
10 
12 {
13 }
14 
15 void ButtonsCommanderEncoder::begin(unsigned long inId, int inPin1, int inPin2, byte inMoveIncrement, int inStartingValue, int inMinimum, int inMaximum)
16 {
17  this->Id = inId;
18 
19  this->pin1 = Arduino_to_GPIO_pin(inPin1);
20  this->pin2 = Arduino_to_GPIO_pin(inPin2);
21  pinMode2f(this->pin1, INPUT);
22  pinMode2f(this->pin2, INPUT);
23  digitalWrite2f(this->pin1, HIGH); //turn pullup resistor on
24  digitalWrite2f(this->pin2, HIGH); //turn pullup resistor on
25 
26  this->moveIncrement = inMoveIncrement;
27  this->incrementPosition = 0;
28  this->lastEncoded = 0;
29  this->mini = inMinimum;
30  this->maxi = inMaximum;
31  this->currentValue = inStartingValue;
32  this->startingCurrentValue = inStartingValue;
33 }
34 
36 {
37 #ifdef COMMANDERS_DEBUG_MODE
38  if (this->Id == UNDEFINED_ID)
39  Serial.println(F("This encoder have no ID defined : call begin() !"));
40 #endif
41 
42  int MSB = digitalRead2f(this->pin1); //MSB = most significant bit
43  int LSB = digitalRead2f(this->pin2); //LSB = least significant bit
44 
45  int encoded = (MSB << 1) | LSB; //converting the 2 pin value to single number
46  if (encoded == 0)
47  return UNDEFINED_ID;
48  int sum = (this->lastEncoded << 2) | encoded; //adding it to the previous encoded value
49 
50  char inc = 0;
51  if (sum == 13 || sum == 4 || sum == 2 || sum == 11) inc = 1;
52  if (sum == 14 || sum == 7 || sum == 1 || sum == 8) inc = -1;
53 
54  this->lastEncoded = encoded; //store this value for next time
55 
56  if (inc == 0)
57  return UNDEFINED_ID;
58 
59  if (this->moveIncrement > 1)
60  {
61  this->incrementPosition += inc;
62  if (abs(this->incrementPosition) < this->moveIncrement)
63  {
64  return UNDEFINED_ID; // needs to move more to obtain a position change...
65  }
66 
67  this->incrementPosition = 0; // moves have been made enough to change position !
68  }
69 
70  if (this->mini != this->maxi)
71  {
72  // If the encoder has been defined with a mini/maxi interval,
73  // move the value and return it !
74  this->currentValue += inc;
75 
76  if (this->currentValue > this->maxi)
77  this->currentValue = this->maxi;
78 
79  if (this->currentValue < this->mini)
80  this->currentValue = this->mini;
81 
82  return Commanders::RaiseEvent(this->GetId(), COMMANDERS_EVENT_MOVEPOSITION, this->currentValue);
83  }
84 
85  // if no interval defined, just return the move direction.
86  return Commanders::RaiseEvent(this->GetId(),
89 }
90 
91 #ifdef COMMANDERS_PRINT_COMMANDERS
92 void ButtonsCommanderEncoder::printCommander()
93 {
94  Serial.println(F(" Encoder - Pin1:"));
95  Serial.print(GPIO_to_Arduino_pin(this->pin1));
96  Serial.print(F(" / Pin2: "));
97  Serial.print(GPIO_to_Arduino_pin(this->pin2));
98  Serial.print(F(" / Mini: "));
99  Serial.print(this->maxi);
100  Serial.print(F(" / Maxi: "));
101  Serial.println(this->maxi);
102 }
103 #endif
104 #endif
105 #endif
unsigned long GetId() const
static unsigned long RaiseEvent(unsigned long inId, COMMANDERS_EVENT_TYPE inEvent = COMMANDERS_EVENT_MOVEPOSITIONID, int inData = 0)
Definition: Commanders.cpp:27
void begin(unsigned long inId, int inPin1, int inPin2, byte inMoveIncrement = 3, int inStartingCurrentValue = 0, int inMinimum = 0, int inMaximum = 0)
#define UNDEFINED_ID
Definition: Events.h:38