Erriez Full step Rotary Encoder  1.1.0
Three speed Full Step Rotary Encoder Library for Arduino
ErriezRotaryFullStep.cpp
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2018-2020 Erriez
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 #if (defined(__AVR__) || defined(ARDUINO_ARCH_SAM))
34 #include <avr/pgmspace.h>
35 #else
36 #include <pgmspace.h>
37 #endif
38 
39 #include "ErriezRotaryFullStep.h"
40 
41 #define DIR_NONE 0x00
42 #define DIR_CW 0x10
43 #define DIR_CCW 0x20
44 
45 // Use the full-step state table, clockwise and counter clockwise
46 // Emits a code at '00' only
47 #define RFS_START 0x00
48 #define RFS_CW_FINAL 0x01
49 #define RFS_CW_BEGIN 0x02
50 #define RFS_CW_NEXT 0x03
51 #define RFS_CCW_BEGIN 0x04
52 #define RFS_CCW_FINAL 0x05
53 #define RFS_CCW_NEXT 0x06
54 
55 static const PROGMEM uint8_t fullStepTable[7][4] = {
56  // RFS_START
58  // RFS_CW_FINAL
59  {RFS_CW_NEXT, RFS_START, RFS_CW_FINAL, RFS_START | DIR_CW},
60  // RFS_CW_BEGIN
61  {RFS_CW_NEXT, RFS_CW_BEGIN, RFS_START, RFS_START},
62  // RFS_CW_NEXT
63  {RFS_CW_NEXT, RFS_CW_BEGIN, RFS_CW_FINAL, RFS_START},
64  // RFS_CCW_BEGIN
65  {RFS_CCW_NEXT, RFS_START, RFS_CCW_BEGIN, RFS_START},
66  // RFS_CCW_FINAL
68  // RFS_CCW_NEXT
70 };
71 
88 RotaryFullStep::RotaryFullStep(uint8_t pin1, uint8_t pin2, bool pullUp, uint8_t sensitivity) :
89  _pin1(pin1), _pin2(pin2), _state(0), _sensitivity(sensitivity)
90 {
91  if (pullUp) {
92  // Enable internal pull-up
93  pinMode(_pin1, INPUT_PULLUP);
94  pinMode(_pin2, INPUT_PULLUP);
95  }
96 }
97 
120 {
121  int pinState;
122  int rotaryState;
123  unsigned long timeStamp;
124  unsigned long changeTime;
125 
126  // Sample rotary digital pins
127  pinState = (digitalRead(_pin1) << 1) | digitalRead(_pin2);
128 
129  // Determine new state from the pins and state table.
130  _state = pgm_read_byte(&fullStepTable[_state & 0x0f][pinState]);
131 
132  // Check rotary state
133  switch (_state & 0x30) {
134  case DIR_CW:
135  rotaryState = 1;
136  break;
137  case DIR_CCW:
138  rotaryState = -1;
139  break;
140  case DIR_NONE:
141  default:
142  rotaryState = 0;
143  }
144 
145  // Check if rotary changed
146  if (rotaryState != 0) {
147  timeStamp = millis();
148  changeTime = timeStamp - _lastChange;
149  _lastChange = timeStamp;
150 
151  // Check speed change
152  if (changeTime < (_sensitivity / 2)) {
153  rotaryState *= 3;
154  } else if (changeTime < _sensitivity) {
155  rotaryState *= 2;
156  }
157  }
158 
159  return rotaryState;
160 }
161 
168 void RotaryFullStep::setSensitivity(uint8_t sensitivity)
169 {
170  _sensitivity = sensitivity;
171 }
172 
180 {
181  return _sensitivity;
182 }
#define RFS_CW_FINAL
Rotary full step clock wise final.
#define RFS_CW_BEGIN
Rotary full step clock begin.
void setSensitivity(uint8_t sensitivity)
Set sensitivity value.
int read()
Read Rotary Encoder state.
#define RFS_START
Rotary full step start.
#define RFS_CCW_NEXT
Rotary full step counter clockwise next.
#define RFS_CCW_FINAL
Rotary full step counter clockwise final.
#define DIR_CCW
Counter-clockwise step.
#define RFS_CW_NEXT
Rotary full step clock next.
RotaryFullStep(uint8_t pin1, uint8_t pin2, bool pullUp=true, uint8_t sensitivity=100)
Constructor full step Rotary Encoder.
#define DIR_CW
Clockwise step.
#define DIR_NONE
No complete step yet.
#define RFS_CCW_BEGIN
Rotary full step counter clockwise begin.
uint8_t getSensitivity()
Get sensitivity value.
Three speed full step Rotary Encoder library for Arduino.