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_PULLUP);
22  pinMode2f(this->pin2, INPUT_PULLUP);
23  //digitalWrite2f(this->pin1, HIGH); //turn pullup resistor on
24  //digitalWrite2f(this->pin2, HIGH); //turn pullup resistor on
25 
26  this->moveIncrement = inMoveIncrement;
27 #ifdef VISUALSTUDIO
28  this->moveIncrement = 1; // easier to test...
29 #endif
30  this->incrementPosition = 0;
31  this->lastEncoded = 0;
32  this->mini = inMinimum;
33  this->maxi = inMaximum;
34  this->currentValue = inStartingValue;
35  this->startingCurrentValue = inStartingValue;
36 }
37 
39 {
40 #ifdef COMMANDERS_DEBUG_MODE
41  if (this->Id == UNDEFINED_ID || this->pin1 == DP_INVALID || this->pin2 == DP_INVALID)
42  {
43  if (this->lastEncoded != 32767) // If the error message has not been yet shown...
44  {
45  Serial.println(F("This encoder have no ID or pins defined : call begin() !"));
46  // use it as a debug flag !
47  this->lastEncoded = 32767; // The error message has been shown...
48  }
49  }
50 #endif
51 
52  if (this->Id == UNDEFINED_ID || this->pin1 == DP_INVALID || this->pin2 == DP_INVALID)
53  {
54  return UNDEFINED_ID;
55  }
56 
57  int MSB = digitalRead2f(this->pin1); //MSB = most significant bit
58  int LSB = digitalRead2f(this->pin2); //LSB = least significant bit
59 
60  int encoded = (MSB << 1) | LSB; //converting the 2 pin value to single number
61  if (encoded == 0)
62  return UNDEFINED_ID;
63  int sum = (this->lastEncoded << 2) | encoded; //adding it to the previous encoded value
64 
65  int8_t inc = 0;
66  if (sum == 13 || sum == 4 || sum == 2 || sum == 11) inc = 1;
67  if (sum == 14 || sum == 7 || sum == 1 || sum == 8) inc = -1;
68 
69 #ifdef COMMANDERS_DEBUG_MODE
70  if (inc != 0)
71  {
72  Serial.print(F("Encoder move of "));
73  Serial.print(inc);
74  Serial.print(F(" : "));
75  Serial.println(this->currentValue+inc);
76  }
77 #endif
78 
79  this->lastEncoded = encoded; //store this value for next time
80 
81  if (inc == 0)
82  return UNDEFINED_ID;
83 
84  if (this->moveIncrement > 1)
85  {
86  this->incrementPosition += inc;
87  if (abs(this->incrementPosition) < this->moveIncrement)
88  {
89  return UNDEFINED_ID; // needs to move more to obtain a position change...
90  }
91 
92  this->incrementPosition = 0; // moves have been made enough to change position !
93  }
94 
95  if (this->mini != this->maxi)
96  {
97  // If the encoder has been defined with a mini/maxi interval,
98  // move the value and return it !
99  this->currentValue += inc;
100 
101  if (this->currentValue > this->maxi)
102  this->currentValue = this->maxi;
103 
104  if (this->currentValue < this->mini)
105  this->currentValue = this->mini;
106 
107  return Commanders::RaiseEvent(this->GetId(), COMMANDERS_EVENT_MOVEPOSITION, this->currentValue);
108  }
109 
110  // if no interval defined, just return the move direction.
111  return Commanders::RaiseEvent(this->GetId(),
114 }
115 
116 #ifdef COMMANDERS_PRINT_COMMANDERS
117 void ButtonsCommanderEncoder::printCommander()
118 {
119  Serial.print(F(" Encoder - Pin1:"));
120  Serial.print(GPIO_to_Arduino_pin(this->pin1));
121  Serial.print(F(" / Pin2: "));
122  Serial.print(GPIO_to_Arduino_pin(this->pin2));
123  Serial.print(F(" / Mini: "));
124  Serial.print(this->maxi);
125  Serial.print(F(" / Maxi: "));
126  Serial.println(this->maxi);
127 }
128 #endif
129 #endif
130 #endif
static unsigned long RaiseEvent(unsigned long inId, COMMANDERS_EVENT_TYPE inEvent = COMMANDERS_EVENT_MOVEPOSITIONID, int inData = 0)
Definition: Commanders.cpp:27
unsigned long GetId() const
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