├── .github
└── FUNDING.yml
├── .gitignore
├── CHANGELOG.md
├── LICENSE
├── README.md
├── examples
├── ESP32Interrupt
│ └── ESP32Interrupt.ino
├── ESP8266Interrupt
│ └── ESP8266Interrupt.ino
├── RangedCounter
│ └── RangedCounter.ino
├── SimpleCounter
│ └── SimpleCounter.ino
├── SimpleCounterWithButton
│ └── SimpleCounterWithButton.ino
└── Speedup
│ └── Speedup.ino
├── keywords.txt
├── library.json
├── library.properties
└── src
├── ESPRotary.cpp
└── ESPRotary.h
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | ko_fi: lennart0815
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | .vscode
3 | *.code-workspace
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | ## Unreleased
4 |
5 | - none
6 |
7 | **Note**: Unreleased changes are checked in but not part of an official release (available through the Arduino IDE or PlatfomIO) yet. This allows you to test WiP features and give feedback to them.
8 |
9 | ## [2.1.1] - 2023-04-27
10 |
11 | - fixed bug in `setIncrement()` as pointed out by WJurasek-KV
12 | - updated `SimpleCounter.ino` example
13 |
14 | ## [2.1.0] - 2023-04-13
15 |
16 | - updated enums with class feature
17 | - Added `triggerOnBounds(bool triggerEvents = true)` method to be able to suppress rotate events when the boundaries are reached
18 | - Made `now` and time related attributes to `unsigned long`
19 | - Replaced cases with lookup table to simplify `loop()`
20 | - Simplified state change in the `loop()`
21 | - Positions are only set within the defined boundaries
22 | - some refactoring suggested by GPT-4
23 |
24 | Most changes were suggested in [#43](https://github.com/LennartHennigs/ESPRotary/pull/43) by [Alex Herman](https://github.com/gaaf). Thank you.
25 |
26 | ## [2.0.0] - 2022-12-27
27 |
28 | - added speedup functionality as suggested in [#37](https://github.com/LennartHennigs/ESPRotary/discussions/37)
29 |
30 | ```c++
31 | void setSpeedupInterval(int time);
32 | void setSpeedupIncrement(int inc);
33 | void enableSpeedup(bool enable);
34 |
35 | bool isSpeedupEnabled() const;
36 | int getSpeedupInterval() const;
37 | int getSpeedupIncrement() const;
38 |
39 | void setSpeedupStartedHandler(CallbackFunction f);
40 | void setSpeedupEndedHandler(CallbackFunction f);
41 | bool isInSpeedup() const;
42 | ```
43 |
44 | - multiple similar events can now be surpressed
45 | - `retriggerEvent(bool retrigger)`
46 | - Added ESP8266 and ESP32 interrupt examples
47 | - refactored loop and event triggering
48 | - fixed bug with ID
49 |
50 | ## [1.6.1] - 2022-12-24
51 |
52 | - replaced direction constants with enum, now use it like this: `rotary_direction::right`
53 | - refactored `loop()`
54 |
55 | ## [1.6.0] - 2022-05-26
56 |
57 | - added ID feature to the class as suggested by [Jon](https://github.com/mscreations) in [#34](https://github.com/LennartHennigs/ESPRotary/pull/34)
58 | - added == operator as suggested by [Jon](https://github.com/mscreations) in [#34](
59 | - Defined getter methods as ```const```
60 |
61 | ## [1.5.0] - 2021-06-22
62 |
63 | - Added option to define increment as suggested by [sblaisot](https://github.com/sblaisot) in pull request [#32](https://github.com/LennartHennigs/ESPRotary/pull/32)
64 | - Added a `begin()` function and a default constructor the be able to instantiate an object within a function
65 | - Updated the `RangedCounter` and `SimpleCounterWithButton` examples. They now use `begin()`
66 | - Added getter and setter functions for the upper and lower bounds
67 | - ```void setUpperBound(int upper_bound);```
68 | - ```void setLowerBound(int lower_bound);```
69 | - ```int getUpperBound();```
70 | - ```int getLowerBound();```
71 |
72 | ## [1.4.2] - 2020-11-29
73 |
74 | - remove unused variable `last_read_ms` as pointed out by [ornea](https://github.com/ornea) in [#26](https://github.com/LennartHennigs/ESPRotary/issues/26)
75 | - cleaned up the protected vars as suggested by [Jim-2249](https://github.com/Jim-2249) in [#30](https://github.com/LennartHennigs/ESPRotary/issues/30)
76 | - simplified the `loop()` function
77 |
78 | ## [1.4.1] - 2020-11-29
79 |
80 | - reordered the constructor parameters to mitigate a code breaking change as suggested by [ab-it](https://github.com/ab-it) in pull request [#25](https://github.com/LennartHennigs/ESPRotary/pull/25)
81 | - made ```resetPosition()``` callback trigger optional as suggested by [ab-it](https://github.com/ab-it)
82 | - fixed bug in ```directionToString()``` introduced in the 1.4 version, found by [ab-it](https://github.com/ab-it)
83 |
84 | ## [1.4] - 2020-11-26
85 |
86 | - added callback to ```resetPosition()``` as suggested by [ab-it](https://github.com/ab-it) in [#17](https://github.com/LennartHennigs/ESPRotary/issues/17)
87 | - Fixed warning in ```directionToString()``` as suggested by [ab-it](https://github.com/ab-it) in [#18](https://github.com/LennartHennigs/ESPRotary/issues/18)
88 | - Added option to define initial value in the constructor as suggested by [ab-it](https://github.com/ab-it) in [#20](https://github.com/LennartHennigs/ESPRotary/issues/20)
89 | - Fixed upper_bound overflow preventing exemple to work on arduino Uno as suggested by [sblaisot](https://github.com/sblaisot) in pull request [#22](https://github.com/LennartHennigs/ESPRotary/pull/22)
90 | - Avoid triggering change_cb after resetPosition to other position than 0
91 |
92 | ## [1.3] - 2020-06-05
93 |
94 | - Fixed typo in example
95 | - updated ```README.md```
96 | - added function ```setStepsPerClick()```, as requested by [asalillo](https://github.com/lasalillo) in [#12](https://github.com/LennartHennigs/ESPRotary/issues/12)
97 | - Fixed bug [#13](https://github.com/LennartHennigs/ESPRotary/issues/13) in ```resetPosition()```, as pointed out by [jjarp](https://github.com/jjarp)
98 | - removed ```#pragma``` message for ```moves_per_click``` constructor parameter
99 | - renamed variable ```moves_per_click``` to ```steps_per_click```
100 |
101 | ## [1.2.1] - 2020-04-20
102 |
103 | - Fixed bug related to minimum and maximum bounds
104 | - Added ```RangedCounter.ino``` example
105 | - Added optional parameter to ```resetPosition()```
106 |
107 | ## [1.2.0] - 2020-04-20
108 |
109 | - Created and added CHANGELOG.md
110 | - Added optional minimum and maximum bounds to constructor (as suggested by [cornfeedhobo](https://github.com/cornfeedhobo) in issue [#9](https://github.com/LennartHennigs/ESPRotary/issues/9))
111 |
112 | ## [1.1.2] - 2020-03-31
113 |
114 | - Simplified example
115 |
116 | ## [1.1.1] - 2020-03-30
117 |
118 | - Removed ```yield()``` in main ```loop()``` since it caused some problems
119 |
120 | ## [1.1.0] - 2020-03-27
121 |
122 | - Added ```yield()``` to the main ```loop()```
123 | - Updated the example (as suggested by [per1234](https://github.com/per1234))
124 |
125 | ## [1.0.0] - 2017-11-14
126 |
127 | - Added option to set number of clicks to constructor (as suggested by [svititom](https://github.com/LennartHennigs/ESPRotary/commit/95e86b171e8d1489cd603d493898c5ea516935db))
128 |
129 | ## Note
130 |
131 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
132 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
133 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017-2023 Lennart Hennigs
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ESPRotary
2 |
3 | Arduino/ESP library to simplify reading rotary encoder data.
4 |
5 | - Author: Lennart Hennigs ()
6 | - Copyright (C) 2017-2023 Lennart Hennigs.
7 | - Released under the MIT license.
8 |
9 | ## Description
10 |
11 | This library allows you read out interactions with a rotary encoder and act on them.
12 | It uses callback functions to be notified when the rotary encoder changes.
13 |
14 | It has been tested with Arduino, ESP8266 and ESP32 devices.
15 |
16 | To see the latest changes to the library please take a look at the [Changelog](https://github.com/LennartHennigs/ESPRotary/blob/master/CHANGELOG.md).
17 |
18 | If you find this library helpful please consider giving it a ⭐️ at [GitHub](https://github.com/LennartHennigs/ESPRotary) and/or [buy me a ☕️](https://ko-fi.com/lennart0815). Thanks!
19 |
20 | Some of the code based of this library is based on code from [PJRC](https://www.pjrc.com/teensy/td_libs_Encoder.html).
21 |
22 | ## How to Use
23 |
24 | ### Definition
25 |
26 | - Use the `begin()` or the parameterized constructor to create a new instance of the class
27 | - Encoder produce different numbers of "click" on a single turn.
28 | - You can specify the number of clicks in the constructor, or via a setter function
29 | - `void setStepsPerClick(int steps)`
30 | - `int getStepsPerClick() const`
31 |
32 | ### Callback Handlers
33 |
34 | - The library provides several callback handlers to detect events
35 | - `void setChangedHandler(CallbackFunction f)`
36 | - `void setRightRotationHandler(CallbackFunction f)`
37 | - `void setLeftRotationHandler(CallbackFunction f)`
38 | - `void setUpperOverflowHandler(CallbackFunction f)`
39 | - `void setLowerOverflowHandler(CallbackFunction f)`
40 | - The library does not detect button clicks. You have to use a separate library for this, e.g. my [Button2](https://github.com/LennartHennigs/Button2) library.
41 |
42 | ### Ranges
43 |
44 | - In the constructor you can define an upper and a lower threshold. The encoder value will not be bypass these values.
45 | - There are also getter and setter functions the these values
46 | - `void setUpperBound(int upper_bound)`
47 | - `void setLowerBound(int lower_bound)`
48 | - `int getUpperBound() const`
49 | - `int getLowerBound() const`
50 | - See the [RangedCounter](https://github.com/LennartHennigs/ESPRotary/blob/master/examples/RangedCounter/RangedCounter.ino) example for details.
51 |
52 | ### Retrigger (out of bound) events?
53 |
54 | - If you want that out of bound events are only triggered once a bound is reached, you can set:
55 | - `retriggerEvent(false)`
56 | - Otherwise each (out of bounds) turn will re-trigger the event.
57 | - If you want to disable rotate events that would go beyond your defined boundary you can use:
58 | - `void triggerOnBounds(false)`
59 |
60 | ### Reading out information
61 |
62 | - The class allows you the get the position and the direction after a click using these function:
63 | - `int getPosition() const`
64 | - `byte getDirection() const`
65 | - `String directionToString(byte direction) const`
66 |
67 | ### Speed
68 |
69 | - You can define the speed, i.e. the increment the a single click in the constructor
70 | - There is also a getter and setter function for this
71 | - `void setIncrement(int inc)`
72 | - `int getIncrement() const`
73 |
74 | ### Speedup Mode
75 |
76 | - You can also define, that the increment shall change when the encoder is turned fast.
77 | - Use these functions to define the interval (between clicks) and the increment:
78 | - `void setSpeedupInterval(int time)`
79 | - `void setSpeedupIncrement(int inc)`
80 | - Per default the interval is set to `75ms` and the increment is set to `5`.
81 | - There are also getter functions for both parameters:
82 | - `int getSpeedupInterval() const`
83 | - `int getSpeedupIncrement() const`
84 | - To enable the speedup mode use this member function:
85 | - `void enableSpeedup(bool enable)`
86 | - ...and to check:
87 | - `bool isSpeedupEnabled() const`
88 | - There are also event handlers that let you track if the speedup mode was entered or exited:
89 | - `void setSpeedupStartedHandler(CallbackFunction f)`
90 | - `void setSpeedupEndedHandler(CallbackFunction f)`
91 | - Alternatively, you can use this function to determine its state:
92 | - `bool isInSpeedup() const`
93 | - If you have bound defined, the speedup mode will be automatically ended when you hit it.
94 |
95 | ### The Loop
96 |
97 | - For the class to work, you need to call the `loop()` member function in your sketch's `loop()` function.
98 | - Alternatively, you can use a timer interrupt to call the member `loop()`function.
99 | - See the examples for more details.
100 |
101 | ### Using it with a timer interrupt
102 |
103 | - Instead of using the main loop() of your sketch you can also use an interrupt timer function.
104 | - See [ESP32Interrupt](https://github.com/LennartHennigs/ESPRotary/blob/master/examples/ESP32Interrupt/ESP32Interrupt.ino) or [ESP8266Interrupt](https://github.com/LennartHennigs/ESPRotary/blob/master/examples/ESP8266Interrupt/ESP8266Interrupt.ino) to see how
105 |
106 | ### IDs for Encoder Instances
107 |
108 | - Each encoder instance gets a unique (auto incremented) ID upon creation.
109 | - You can get a encoders' ID via `getID()`.
110 | - Alternatively, you can use `setID(int newID)` to set a new one. But then you need to make sure that they are unique.
111 | - You can also use the `==` operator to compare encoders
112 |
113 | ## Notes
114 |
115 | - To see the latest changes to the library please take a look at the [Changelog](https://github.com/LennartHennigs/ESPRotary/blob/master/CHANGELOG.md).
116 | - And if you find this library helpful, please consider giving it a star at [GitHub](https://github.com/LennartHennigs/ESPRotary). Thanks!
117 |
118 | ## Examples
119 |
120 | - [SimpleCounter](https://github.com/LennartHennigs/ESPRotary/blob/master/examples/SimpleCounter/SimpleCounter.ino) - basic example
121 | - [SimpleCounterWithButton](https://github.com/LennartHennigs/ESPRotary/blob/master/examples/SimpleCounterWithButton/SimpleCounterWithButton.ino) - basic example with a button handler
122 | - [RangedCounter](https://github.com/LennartHennigs/ESPRotary/blob/master/examples/RangedCounter/RangedCounter.ino) - shows how to define ranges
123 | - [Speedup](https://github.com/LennartHennigs/ESPRotary/blob/master/examples/Speedup/Speedup.ino) - shows how to implement the speedup mode
124 | - [ESP8266Interrupt](https://github.com/LennartHennigs/ESPRotary/blob/master/examples/ESP8266Interrupt/ESP8266Interrupt.ino) - uses an ESP8266 interrupt instead of the main loop
125 | - [ESP32Interrupt](https://github.com/LennartHennigs/ESPRotary/blob/master/examples/ESP32Interrupt/ESP32Interrupt.ino) - uses an ESP32 interrupt instead of the main loop
126 |
127 | ## Class Definition
128 |
129 | These are the constructor and the member functions the library provides:
130 |
131 | ``` c++
132 | ESPRotary();
133 | ESPRotary(byte pin1, byte pin2, byte steps_per_click = 1, int lower_bound = INT16_MIN, int upper_bound = INT16_MAX, int iniital_pos = 0, int increment = 1);
134 |
135 | void begin(byte pin1, byte pin2, byte steps_per_click = 1, int lower_bound = INT16_MIN, int upper_bound = INT16_MAX, int initial_pos = 0, int increment = 1);
136 |
137 | int getPosition() const;
138 | void resetPosition(int p = 0, bool fireCallback = true);
139 |
140 | rotary_direction getDirection() const;
141 | String directionToString(rotary_direction dir) const;
142 |
143 | void setIncrement(int inc);
144 | int getIncrement() const;
145 |
146 | void triggerOnBounds(bool triggerEvents = true);
147 |
148 | void enableSpeedup(bool enable);
149 | void setSpeedupInterval(int time);
150 | void setSpeedupIncrement(int inc);
151 |
152 | bool isSpeedupEnabled() const;
153 | int getSpeedupInterval() const;
154 | int getSpeedupIncrement() const;
155 | bool isInSpeedup() const;
156 |
157 | rotary_event getLastEvent() const;
158 | void retriggerEvent(bool retrigger);
159 |
160 | void setUpperBound(int upper_bound);
161 | void setLowerBound(int lower_bound);
162 | int getUpperBound() const;
163 | int getLowerBound() const;
164 |
165 | void setStepsPerClick(int steps);
166 | int getStepsPerClick() const;
167 |
168 | void setChangedHandler(CallbackFunction f);
169 | void setRightRotationHandler(CallbackFunction f);
170 | void setLeftRotationHandler(CallbackFunction f);
171 | void setUpperOverflowHandler(CallbackFunction f);
172 | void setLowerOverflowHandler(CallbackFunction f);
173 | void setSpeedupStartedHandler(CallbackFunction f);
174 | void setSpeedupEndedHandler(CallbackFunction f);
175 |
176 | int getID() const;
177 | void setID(int newID);
178 |
179 | bool operator == (ESPRotary &rhs);
180 |
181 | void loop();
182 | ```
183 |
184 | ## Installation
185 |
186 | Open the Arduino IDE choose "Sketch > Include Library" and search for "ESP Rotary".
187 | Or download the ZIP archive (), and choose "Sketch > Include Library > Add .ZIP Library..." and select the downloaded file.
188 |
189 | ## License
190 |
191 | MIT License
192 |
193 | Copyright (c) 2017-2023 Lennart Hennigs
194 |
195 | Permission is hereby granted, free of charge, to any person obtaining a copy
196 | of this software and associated documentation files (the "Software"), to deal
197 | in the Software without restriction, including without limitation the rights
198 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
199 | copies of the Software, and to permit persons to whom the Software is
200 | furnished to do so, subject to the following conditions:
201 |
202 | The above copyright notice and this permission notice shall be included in all
203 | copies or substantial portions of the Software.
204 |
205 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
206 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
207 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
208 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
209 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
210 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
211 | SOFTWARE.
212 |
--------------------------------------------------------------------------------
/examples/ESP32Interrupt/ESP32Interrupt.ino:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////
2 |
3 | #if !defined(ESP32)
4 | #error This sketch needs an ESP32
5 | #else
6 |
7 | /////////////////////////////////////////////////////////////////
8 |
9 | #include "ESPRotary.h"
10 |
11 | /////////////////////////////////////////////////////////////////
12 |
13 | #define ROTARY_PIN1 14
14 | #define ROTARY_PIN2 15
15 |
16 | #define CLICKS_PER_STEP 4
17 |
18 | #define SERIAL_SPEED 115200
19 |
20 | /////////////////////////////////////////////////////////////////
21 |
22 | ESPRotary r;
23 | hw_timer_t *timer = NULL;
24 |
25 | /////////////////////////////////////////////////////////////////
26 |
27 |
28 | void IRAM_ATTR handleLoop() {
29 | r.loop();
30 | }
31 |
32 | /////////////////////////////////////////////////////////////////
33 |
34 | void setup() {
35 | Serial.begin(SERIAL_SPEED);
36 | delay(50);
37 |
38 | r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP);
39 | r.setChangedHandler(rotate);
40 | r.setLeftRotationHandler(showDirection);
41 | r.setRightRotationHandler(showDirection);
42 |
43 |
44 | timer = timerBegin(0, 80, true);
45 | timerAttachInterrupt(timer, &handleLoop, true);
46 | timerAlarmWrite(timer, 10000, true); // every 0.1 seconds
47 | timerAlarmEnable(timer);
48 | }
49 |
50 | /////////////////////////////////////////////////////////////////
51 |
52 | void rotate(ESPRotary& r) {
53 | Serial.println(r.getPosition());
54 | }
55 |
56 | /////////////////////////////////////////////////////////////////
57 |
58 | // on left or right rotation
59 | void showDirection(ESPRotary& r) {
60 | Serial.println(r.directionToString(r.getDirection()));
61 | }
62 |
63 | /////////////////////////////////////////////////////////////////
64 |
65 | void loop() {
66 | delay(1000);
67 | Serial.print(".");
68 | }
69 |
70 | /////////////////////////////////////////////////////////////////
71 | #endif
72 | /////////////////////////////////////////////////////////////////
73 |
--------------------------------------------------------------------------------
/examples/ESP8266Interrupt/ESP8266Interrupt.ino:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////
2 |
3 | #if !defined(ESP8266)
4 | #error This sketch needs an ESP8266
5 | #else
6 |
7 | /////////////////////////////////////////////////////////////////
8 |
9 | #include "ESPRotary.h"
10 | #include "Ticker.h" // https://github.com/esp8266/Arduino/tree/master/libraries/Ticker
11 |
12 | /////////////////////////////////////////////////////////////////
13 |
14 | #define ROTARY_PIN1 D1
15 | #define ROTARY_PIN2 D2
16 |
17 | #define CLICKS_PER_STEP 4
18 |
19 | #define SERIAL_SPEED 115200
20 |
21 | /////////////////////////////////////////////////////////////////
22 |
23 | ESPRotary r;
24 | Ticker t;
25 |
26 | /////////////////////////////////////////////////////////////////
27 |
28 | void setup() {
29 | Serial.begin(SERIAL_SPEED);
30 | delay(50);
31 |
32 | r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP);
33 | r.setChangedHandler(rotate);
34 | r.setLeftRotationHandler(showDirection);
35 | r.setRightRotationHandler(showDirection);
36 |
37 | t.attach_ms(10, handleLoop);
38 | }
39 |
40 | /////////////////////////////////////////////////////////////////
41 |
42 | void rotate(ESPRotary& r) {
43 | Serial.println(r.getPosition());
44 | }
45 |
46 | /////////////////////////////////////////////////////////////////
47 |
48 | // on left or right rotation
49 | void showDirection(ESPRotary& r) {
50 | Serial.println(r.directionToString(r.getDirection()));
51 | }
52 |
53 | /////////////////////////////////////////////////////////////////
54 |
55 | void handleLoop() {
56 | r.loop();
57 | }
58 |
59 | /////////////////////////////////////////////////////////////////
60 |
61 | void loop() {
62 | delay(1000);
63 | Serial.print(".");
64 | }
65 |
66 | /////////////////////////////////////////////////////////////////
67 | #endif
68 | /////////////////////////////////////////////////////////////////
69 |
--------------------------------------------------------------------------------
/examples/RangedCounter/RangedCounter.ino:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////
2 |
3 | #include "ESPRotary.h"
4 |
5 | /////////////////////////////////////////////////////////////////
6 |
7 | #define ROTARY_PIN1 D1
8 | #define ROTARY_PIN2 D2
9 |
10 | #define CLICKS_PER_STEP 4 // this number depends on your rotary encoder
11 | #define MIN_POS 0
12 | #define MAX_POS 40
13 | #define START_POS 20
14 | #define INCREMENT 2 // this number is the counter increment on each step
15 |
16 | #define SERIAL_SPEED 115200
17 |
18 | /////////////////////////////////////////////////////////////////
19 |
20 | ESPRotary r;
21 |
22 | /////////////////////////////////////////////////////////////////
23 |
24 | void setup() {
25 | Serial.begin(SERIAL_SPEED);
26 | delay(50);
27 |
28 | r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP, MIN_POS, MAX_POS, START_POS, INCREMENT);
29 | r.setChangedHandler(rotate);
30 | r.setLeftRotationHandler(showDirection);
31 | r.setRightRotationHandler(showDirection);
32 | r.setLowerOverflowHandler(lower);
33 | r.setUpperOverflowHandler(upper);
34 |
35 | // enable to be only notified one about an out-of-bounds hit
36 | // r.retriggerEvent(false);
37 |
38 | Serial.println("\n\nRanged Counter");
39 | Serial.println("You can only set values between " + String(MIN_POS) + " and " + String(MAX_POS) +".");
40 | Serial.print("Current position: ");
41 | Serial.println(r.getPosition());
42 | Serial.println();
43 |
44 | // enable this to be also notified about rotation events when the bounds are hit
45 | // r.triggerOnBounds(true); // off per default
46 | }
47 |
48 | void loop() {
49 | r.loop();
50 | }
51 |
52 | /////////////////////////////////////////////////////////////////
53 |
54 | // on change
55 | void rotate(ESPRotary& r) {
56 | Serial.println(r.getPosition());
57 | }
58 |
59 | // out of bounds event
60 | void upper(ESPRotary& r) {
61 | Serial.println("upper bound hit");
62 | }
63 |
64 | // out of bounds event
65 | void lower(ESPRotary& r) {
66 | Serial.println("lower bound hit");
67 | }
68 |
69 | // on left or right rotation
70 | void showDirection(ESPRotary& r) {
71 | Serial.println(r.directionToString(r.getDirection()));
72 | }
73 |
74 | /////////////////////////////////////////////////////////////////
--------------------------------------------------------------------------------
/examples/SimpleCounter/SimpleCounter.ino:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////
2 |
3 | #include "ESPRotary.h"
4 |
5 | /////////////////////////////////////////////////////////////////
6 |
7 | #define ROTARY_PIN1 D1
8 | #define ROTARY_PIN2 D2
9 |
10 | // this number depends on your rotary encoder
11 | #define CLICKS_PER_STEP 4
12 |
13 | #define SERIAL_SPEED 115200
14 |
15 | /////////////////////////////////////////////////////////////////
16 |
17 | ESPRotary r;
18 |
19 | /////////////////////////////////////////////////////////////////
20 |
21 | void setup() {
22 | Serial.begin(SERIAL_SPEED);
23 | delay(50);
24 | Serial.println("\n\nSimple Counter");
25 |
26 | r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP);
27 | r.setChangedHandler(rotate);
28 | r.setLeftRotationHandler(showDirection);
29 | r.setRightRotationHandler(showDirection);
30 |
31 | r.setIncrement(10);
32 | Serial.println(r.getIncrement());
33 | }
34 |
35 | void loop() {
36 | r.loop();
37 | }
38 |
39 | /////////////////////////////////////////////////////////////////
40 |
41 | // on change
42 | void rotate(ESPRotary& r) {
43 | Serial.println(r.getPosition());
44 | }
45 |
46 | // on left or right rotattion
47 | void showDirection(ESPRotary& r) {
48 | Serial.println(r.directionToString(r.getDirection()));
49 | }
50 |
51 | /////////////////////////////////////////////////////////////////
52 |
--------------------------------------------------------------------------------
/examples/SimpleCounterWithButton/SimpleCounterWithButton.ino:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////
2 |
3 | #include "Button2.h" // https://github.com/LennartHennigs/Button2
4 | #include "ESPRotary.h"
5 |
6 | /////////////////////////////////////////////////////////////////
7 |
8 | #define ROTARY_PIN1 D1
9 | #define ROTARY_PIN2 D2
10 | #define BUTTON_PIN D4
11 |
12 | #define CLICKS_PER_STEP 4 // this number depends on your rotary encoder
13 |
14 | #define SERIAL_SPEED 115200
15 |
16 | /////////////////////////////////////////////////////////////////
17 |
18 | ESPRotary r;
19 | Button2 b;
20 |
21 | /////////////////////////////////////////////////////////////////
22 |
23 | void setup() {
24 | Serial.begin(SERIAL_SPEED);
25 | delay(50);
26 | Serial.println("\n\nSimple Counter");
27 |
28 | r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP);
29 | r.setChangedHandler(rotate);
30 | r.setLeftRotationHandler(showDirection);
31 | r.setRightRotationHandler(showDirection);
32 |
33 | b.begin(BUTTON_PIN);
34 | b.setTapHandler(click);
35 | b.setLongClickHandler(resetPosition);
36 | }
37 |
38 | void loop() {
39 | r.loop();
40 | b.loop();
41 | }
42 |
43 | /////////////////////////////////////////////////////////////////
44 |
45 | // on change
46 | void rotate(ESPRotary& r) {
47 | Serial.println(r.getPosition());
48 | }
49 |
50 | // on left or right rotation
51 | void showDirection(ESPRotary& r) {
52 | Serial.println(r.directionToString(r.getDirection()));
53 | }
54 |
55 | // single click
56 | void click(Button2& btn) {
57 | Serial.println("Click!");
58 | }
59 |
60 | // long click
61 | void resetPosition(Button2& btn) {
62 | r.resetPosition();
63 | Serial.println("Reset!");
64 | }
65 |
66 | /////////////////////////////////////////////////////////////////
67 |
--------------------------------------------------------------------------------
/examples/Speedup/Speedup.ino:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////
2 |
3 | #include "ESPRotary.h"
4 |
5 | /////////////////////////////////////////////////////////////////
6 |
7 | #define ROTARY_PIN1 D1
8 | #define ROTARY_PIN2 D2
9 |
10 | #define CLICKS_PER_STEP 4 // this number depends on your rotary encoder
11 | #define MIN_POS 0
12 | #define MAX_POS 100
13 | #define START_POS 50
14 | #define INCREMENT 1 // this number is the counter increment on each step
15 |
16 | #define SPEEDUP_STEPS 5
17 |
18 | #define SERIAL_SPEED 115200
19 |
20 |
21 | /////////////////////////////////////////////////////////////////
22 |
23 | ESPRotary r;
24 |
25 | /////////////////////////////////////////////////////////////////
26 |
27 | void setup() {
28 | Serial.begin(SERIAL_SPEED);
29 | delay(50);
30 |
31 | r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP, MIN_POS, MAX_POS, START_POS, INCREMENT);
32 |
33 | r.setChangedHandler(rotate);
34 | r.setLeftRotationHandler(showDirection);
35 | r.setRightRotationHandler(showDirection);
36 | r.setLowerOverflowHandler(lower);
37 | r.setUpperOverflowHandler(upper);
38 | r.setSpeedupStartedHandler(speedupStarted);
39 | r.setSpeedupEndedHandler(speedupEnded);
40 |
41 | r.retriggerEvent(false);
42 | r.enableSpeedup(true);
43 | r.setSpeedupIncrement(SPEEDUP_STEPS);
44 |
45 |
46 | Serial.println("\n\Ranged & Speedup");
47 | Serial.println("You can only set values between " + String(MIN_POS) + " and " + String(MAX_POS) +".");
48 | Serial.print("Current position: ");
49 | Serial.println(r.getPosition());
50 | Serial.println();
51 | }
52 |
53 | void loop() {
54 | r.loop();
55 | }
56 |
57 | /////////////////////////////////////////////////////////////////
58 |
59 | void speedupStarted(ESPRotary& r) {
60 | Serial.println("Speedup started");
61 | }
62 |
63 | void speedupEnded(ESPRotary& r) {
64 | Serial.println("Speedup ended");
65 | }
66 |
67 | // on change
68 | void rotate(ESPRotary& r) {
69 | Serial.println(r.getPosition());
70 | }
71 |
72 |
73 | // out of bounds event
74 | void upper(ESPRotary& r) {
75 | Serial.println("upper bound hit");
76 | }
77 |
78 | // out of bounds event
79 | void lower(ESPRotary& r) {
80 | Serial.println("lower bound hit");
81 | }
82 |
83 | // on left or right rotation
84 | void showDirection(ESPRotary& r) {
85 | Serial.println(r.directionToString(r.getDirection()));
86 | }
87 |
88 | /////////////////////////////////////////////////////////////////
--------------------------------------------------------------------------------
/keywords.txt:
--------------------------------------------------------------------------------
1 | ESPRotary KEYWORD1
2 | getPosition KEYWORD2
3 | begin KEYWORD2
4 | setUpperBound KEYWORD2
5 | setLowerBound KEYWORD2
6 | getUpperBound KEYWORD2
7 | getLowerBound KEYWORD2
8 | resetPosition KEYWORD2
9 | getDirection KEYWORD2
10 | getIncrement KEYWORD2
11 | setIncrement KEYWORD2
12 | getStepsPerClick KEYWORD2
13 | setStepsPerClick KEYWORD2
14 | directionToString KEYWORD2
15 | setChangedHandler KEYWORD2
16 | setRightRotationHandler KEYWORD2
17 | setLeftRotationHandler KEYWORD2
18 | setUpperOverflowHandler KEYWORD2
19 | setLowerOverflowHandler KEYWORD2
20 | setSpeedupInterval KEYWORD2
21 | setSpeedupIncrement KEYWORD2
22 | enableSpeedup KEYWORD2
23 | isSpeedupEnabled KEYWORD2
24 | getSpeedupInterval KEYWORD2
25 | getSpeedupIncrement KEYWORD2
26 | setSpeedupStartedHandler KEYWORD2
27 | setSpeedupEndedHandler KEYWORD2
28 | isInSpeedup KEYWORD2
29 | retriggerEvent KEYWORD2
30 | triggerOnBounds KEYWORD2
31 | getID KEYWORD2
32 | setID KEYWORD2
33 | loop KEYWORD2
34 | direction LITERAL1
35 |
--------------------------------------------------------------------------------
/library.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ESP Rotary",
3 | "version": "2.1.1",
4 | "keywords": "rotary encoder, esp8266",
5 | "description": "ESP8266/Arduino Library for reading rotary encoder values.",
6 | "homepage": "https://github.com/LennartHennigs/ESPRotary",
7 | "repository": {
8 | "type": "git",
9 | "url": "https://github.com/LennartHennigs/ESPRotary"
10 | },
11 | "authors": {
12 | "name": "Lennart Hennigs",
13 | "url": "https://lennarthennigs.de"
14 | },
15 | "frameworks": "arduino",
16 | "platforms": "*"
17 | }
18 |
--------------------------------------------------------------------------------
/library.properties:
--------------------------------------------------------------------------------
1 | name=ESP Rotary
2 | version=2.1.1
3 | author=Lennart Hennigs
4 | maintainer=Lennart Hennigs
5 | sentence=ESP8266/Arduino Library for reading rotary encoder values.
6 | paragraph=It allows you to use callback functions to track rotation. This will reduce and simplify your souce code significantly. Tested with Arduino and ESP8266.
7 | category=Other
8 | url=https://github.com/LennartHennigs/ESPRotary
9 | architectures=*
10 |
--------------------------------------------------------------------------------
/src/ESPRotary.cpp:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////
2 | /*
3 | ESP8266/Arduino Library for reading rotary encoder values.
4 | Copyright 2017-2022 Lennart Hennigs.
5 | */
6 | /////////////////////////////////////////////////////////////////
7 |
8 | #include "ESPRotary.h"
9 |
10 | /////////////////////////////////////////////////////////////////
11 | // initialize static counter
12 |
13 | int ESPRotary::_nextID = 0;
14 |
15 | /////////////////////////////////////////////////////////////////
16 |
17 | void ESPRotary::_setID() {
18 | id = _nextID;
19 | _nextID++;
20 | }
21 |
22 | /////////////////////////////////////////////////////////////////
23 |
24 | ESPRotary::ESPRotary() {
25 | _setID();
26 | }
27 |
28 | /////////////////////////////////////////////////////////////////
29 |
30 | ESPRotary::ESPRotary(byte pin1, byte pin2, byte steps_per_click /* = 1 */, int lower_bound /* = INT16_MIN */, int upper_bound /* = INT16_MAX */, int inital_pos /* = 0 */, int increment /* = 1 */) {
31 | ESPRotary();
32 | begin(pin1, pin2, steps_per_click, lower_bound, upper_bound, inital_pos, increment);
33 | }
34 |
35 | /////////////////////////////////////////////////////////////////
36 |
37 | void ESPRotary::begin(byte pin1, byte pin2, byte steps_per_click /* = 1 */, int lower_bound /* = INT16_MIN */, int upper_bound /* = INT16_MAX */, int inital_pos /* = 0 */, int increment /* = 1 */) {
38 | this->pin1 = pin1;
39 | this->pin2 = pin2;
40 | pinMode(pin1, INPUT_PULLUP);
41 | pinMode(pin2, INPUT_PULLUP);
42 |
43 | setUpperBound(upper_bound);
44 | setLowerBound(lower_bound);
45 | setIncrement(increment);
46 | setStepsPerClick(steps_per_click);
47 |
48 | loop();
49 | steps = inital_pos * steps_per_click;
50 | last_event = rotary_event::none;
51 | dir = rotary_direction::undefined;
52 | }
53 |
54 | /////////////////////////////////////////////////////////////////
55 |
56 | void ESPRotary::setUpperBound(int upper) {
57 | upper_bound = (lower_bound < upper) ? upper : lower_bound;
58 | }
59 |
60 | /////////////////////////////////////////////////////////////////
61 |
62 | void ESPRotary::setLowerBound(int lower) {
63 | lower_bound = (lower < upper_bound) ? lower : upper_bound;
64 | }
65 |
66 | /////////////////////////////////////////////////////////////////
67 |
68 | int ESPRotary::getUpperBound() const {
69 | return upper_bound;
70 | }
71 |
72 | /////////////////////////////////////////////////////////////////
73 |
74 | int ESPRotary::getLowerBound() const {
75 | return lower_bound;
76 | }
77 |
78 | /////////////////////////////////////////////////////////////////
79 |
80 | void ESPRotary::setChangedHandler(CallbackFunction f) {
81 | change_cb = f;
82 | }
83 |
84 | /////////////////////////////////////////////////////////////////
85 |
86 | void ESPRotary::setRightRotationHandler(CallbackFunction f) {
87 | right_cb = f;
88 | }
89 |
90 | /////////////////////////////////////////////////////////////////
91 |
92 | void ESPRotary::setLeftRotationHandler(CallbackFunction f) {
93 | left_cb = f;
94 | }
95 |
96 | /////////////////////////////////////////////////////////////////
97 |
98 | void ESPRotary::setUpperOverflowHandler(CallbackFunction f) {
99 | upper_cb = f;
100 | }
101 |
102 | /////////////////////////////////////////////////////////////////
103 |
104 | void ESPRotary::setLowerOverflowHandler(CallbackFunction f) {
105 | lower_cb = f;
106 | }
107 |
108 | /////////////////////////////////////////////////////////////////
109 |
110 | void ESPRotary::setSpeedupStartedHandler(CallbackFunction f) {
111 | speedup_start_cb = f;
112 | }
113 |
114 | /////////////////////////////////////////////////////////////////
115 |
116 | void ESPRotary::setSpeedupEndedHandler(CallbackFunction f) {
117 | speedup_end_cb = f;
118 | }
119 |
120 | /////////////////////////////////////////////////////////////////
121 |
122 | void ESPRotary::resetPosition(int p /* = 0 */, bool fireCallback /* = true */) {
123 | // change position?
124 | if (p == getPosition()) return;
125 | // yes...
126 | steps = p * steps_per_click;
127 | _isWithinBounds();
128 | if (fireCallback) _callCallback(change_cb);
129 | last_event = rotary_event::none;
130 | dir = rotary_direction::undefined;
131 | in_speedup = false;
132 | }
133 |
134 | /////////////////////////////////////////////////////////////////
135 |
136 | void ESPRotary::setIncrement(int increment) {
137 | this->increment = increment;
138 | }
139 |
140 | /////////////////////////////////////////////////////////////////
141 |
142 | int ESPRotary::getIncrement() const {
143 | return increment;
144 | }
145 |
146 | /////////////////////////////////////////////////////////////////
147 |
148 | void ESPRotary::setStepsPerClick(int steps) {
149 | steps_per_click = (steps < 1) ? 1 : steps;
150 | }
151 |
152 | /////////////////////////////////////////////////////////////////
153 |
154 | int ESPRotary::getStepsPerClick() const {
155 | return steps_per_click;
156 | }
157 |
158 | /////////////////////////////////////////////////////////////////
159 |
160 | rotary_direction ESPRotary::getDirection() const {
161 | return dir;
162 | }
163 |
164 | /////////////////////////////////////////////////////////////////
165 |
166 | String ESPRotary::directionToString(rotary_direction dir) const {
167 | return (dir == rotary_direction::right) ? "right" : "left";
168 | }
169 |
170 | /////////////////////////////////////////////////////////////////
171 |
172 | int ESPRotary::getPosition() const {
173 | return steps / steps_per_click;
174 | }
175 |
176 | /////////////////////////////////////////////////////////////////
177 |
178 | int ESPRotary::getID() const {
179 | return id;
180 | }
181 |
182 | /////////////////////////////////////////////////////////////////
183 |
184 | void ESPRotary::setID(int newID) {
185 | id = newID;
186 | }
187 |
188 | /////////////////////////////////////////////////////////////////
189 |
190 | bool ESPRotary::operator==(ESPRotary &rhs) {
191 | return (this == &rhs);
192 | }
193 |
194 | /////////////////////////////////////////////////////////////////
195 |
196 | void ESPRotary::loop() {
197 | unsigned long now = millis();
198 | // did it change (enough)?
199 | if (!_wasRotated()) return;
200 | dir = (steps > last_steps) ? rotary_direction::right : rotary_direction::left;
201 | // shall I speedup things
202 | if (enable_speedup) _checkForSpeedup(now);
203 | // are we out of bounds?
204 | if (_isWithinBounds(true)) {
205 | // trigger rotation event
206 | _setEvent((dir == rotary_direction::right) ? rotary_event::right_rotation : rotary_event::left_rotation);
207 | last_turn = now;
208 | }
209 | last_steps = steps;
210 | }
211 |
212 | /////////////////////////////////////////////////////////////////
213 |
214 | bool ESPRotary::_wasRotated() {
215 | static const int8_t factors[] = {0, 1, -1, 2, -1, 0, -2, 1, 1, -2, 0, -1, 2, -1, 1, 0};
216 | int encoderState = (state & 3) | digitalRead(pin1) << 2 | digitalRead(pin2) << 3 ;
217 | steps += factors[encoderState] * increment;
218 | state = (encoderState >> 2);
219 | int stepDifference = abs(steps - last_steps);
220 | return stepDifference >= (steps_per_click * increment);
221 | }
222 |
223 | /////////////////////////////////////////////////////////////////
224 |
225 | void ESPRotary::_checkForSpeedup(unsigned long now) {
226 | if (now - last_turn > speedup_interval) {
227 | if (in_speedup) _setEvent(rotary_event::speedup_ended);
228 | return;
229 | }
230 | steps += ((dir == rotary_direction::right ? 1 : -1) * (speedup_increment - increment) * steps_per_click);
231 | int pos = getPosition();
232 | // only trigger speedup when you are not "on a wall"
233 | if (pos > lower_bound && pos < upper_bound) {
234 | if (!in_speedup) _setEvent(rotary_event::speedup_started);
235 | }
236 | }
237 |
238 | /////////////////////////////////////////////////////////////////
239 |
240 | void ESPRotary::triggerOnBounds(bool triggerEvents /* = true */) {
241 | boundsTrigger = triggerEvents;
242 | }
243 |
244 | /////////////////////////////////////////////////////////////////
245 |
246 | void ESPRotary::_callCallback(CallbackFunction callback) {
247 | if (callback != NULL) callback(*this);
248 | }
249 |
250 | /////////////////////////////////////////////////////////////////
251 |
252 | bool ESPRotary::_isWithinBounds(bool triggerAlerts /* = false */) {
253 | int pos = getPosition();
254 | if (pos > lower_bound && pos < upper_bound) return true;
255 |
256 | if (pos >= upper_bound) {
257 | steps = upper_bound * steps_per_click;
258 | if (in_speedup) _setEvent(rotary_event::speedup_ended);
259 | if (triggerAlerts) _setEvent(rotary_event::upper_bound_hit);
260 |
261 | } else if (pos <= lower_bound) {
262 | steps = lower_bound * steps_per_click;
263 | if (in_speedup) _setEvent(rotary_event::speedup_ended);
264 | if (triggerAlerts) _setEvent(rotary_event::lower_bound_hit);
265 | }
266 |
267 | return false;
268 | }
269 | /////////////////////////////////////////////////////////////////
270 |
271 | void ESPRotary::_setEvent(rotary_event event) {
272 | switch (event) {
273 | case rotary_event::left_rotation:
274 | _callCallback(left_cb);
275 | _callCallback(change_cb);
276 | break;
277 |
278 | case rotary_event::right_rotation:
279 | _callCallback(right_cb);
280 | _callCallback(change_cb);
281 | break;
282 |
283 | case rotary_event::speedup_started:
284 | _callCallback(speedup_start_cb);
285 | in_speedup = true;
286 | break;
287 |
288 | case rotary_event::speedup_ended:
289 | _callCallback(speedup_end_cb);
290 | in_speedup = false;
291 | break;
292 |
293 | case rotary_event::upper_bound_hit:
294 | if (last_event == rotary_event::upper_bound_hit && !retrigger_event) return;
295 | if (boundsTrigger) {
296 | _callCallback(right_cb);
297 | _callCallback(change_cb);
298 | }
299 | _callCallback(upper_cb);
300 | break;
301 |
302 | case rotary_event::lower_bound_hit:
303 | if (last_event == rotary_event::lower_bound_hit && !retrigger_event) return;
304 | if (boundsTrigger) {
305 | _callCallback(left_cb);
306 | _callCallback(change_cb);
307 | }
308 | _callCallback(lower_cb);
309 | break;
310 |
311 | case rotary_event::none:
312 | break;
313 | }
314 | last_event = event;
315 | }
316 |
317 | /////////////////////////////////////////////////////////////////
318 |
319 | void ESPRotary::setSpeedupInterval(int interval) {
320 | speedup_interval = interval;
321 | }
322 |
323 | /////////////////////////////////////////////////////////////////
324 |
325 | int ESPRotary::getSpeedupInterval() const {
326 | return speedup_interval;
327 | }
328 |
329 | /////////////////////////////////////////////////////////////////
330 |
331 | void ESPRotary::setSpeedupIncrement(int increment) {
332 | speedup_increment = increment;
333 | }
334 |
335 | /////////////////////////////////////////////////////////////////
336 |
337 | int ESPRotary::getSpeedupIncrement() const {
338 | return speedup_increment;
339 | }
340 |
341 | /////////////////////////////////////////////////////////////////
342 |
343 | void ESPRotary::enableSpeedup(bool enable) {
344 | enable_speedup = enable;
345 | }
346 |
347 | /////////////////////////////////////////////////////////////////
348 |
349 | bool ESPRotary::isSpeedupEnabled() const {
350 | return enable_speedup;
351 | }
352 |
353 | /////////////////////////////////////////////////////////////////
354 |
355 | rotary_event ESPRotary::getLastEvent() const {
356 | return last_event;
357 | }
358 |
359 | /////////////////////////////////////////////////////////////////
360 |
361 | void ESPRotary::retriggerEvent(bool retrigger) {
362 | retrigger_event = retrigger;
363 | }
364 |
365 | /////////////////////////////////////////////////////////////////
366 |
367 | bool ESPRotary::isInSpeedup() const {
368 | return in_speedup;
369 | }
370 |
371 | /////////////////////////////////////////////////////////////////
--------------------------------------------------------------------------------
/src/ESPRotary.h:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////
2 | /*
3 | ESP8266/Arduino Library for reading rotary encoder values.
4 | Copyright 2017-2022 Lennart Hennigs
5 | */
6 | /////////////////////////////////////////////////////////////////
7 | #pragma once
8 |
9 | #ifndef ESPRotary_h
10 | #define ESPRotary_h
11 |
12 | /////////////////////////////////////////////////////////////////
13 |
14 | #include "Arduino.h"
15 |
16 | /////////////////////////////////////////////////////////////////
17 |
18 | enum class rotary_direction {
19 | undefined = 0,
20 | right = 1,
21 | left = 255
22 | };
23 |
24 | enum class rotary_event {
25 | left_rotation,
26 | right_rotation,
27 | speedup_started,
28 | speedup_ended,
29 | upper_bound_hit,
30 | lower_bound_hit,
31 | none
32 | };
33 |
34 | /////////////////////////////////////////////////////////////////
35 |
36 | class ESPRotary {
37 | protected:
38 | int id;
39 | byte pin1, pin2;
40 | byte steps_per_click;
41 | int lower_bound;
42 | int upper_bound;
43 | byte state;
44 | int increment;
45 | int steps = 0;
46 | int last_steps = 0;
47 | rotary_event last_event;
48 | rotary_direction dir;
49 |
50 | bool boundsTrigger = true;
51 | bool retrigger_event = true;
52 | bool enable_speedup = false;
53 | unsigned int speedup_increment = 5;
54 | unsigned int speedup_interval = 75;
55 | int in_speedup = false;
56 | unsigned long last_turn = 0;
57 |
58 | using CallbackFunction = void (*)(ESPRotary&);
59 |
60 | CallbackFunction change_cb = NULL;
61 | CallbackFunction right_cb = NULL;
62 | CallbackFunction left_cb = NULL;
63 | CallbackFunction lower_cb = NULL;
64 | CallbackFunction upper_cb = NULL;
65 |
66 | CallbackFunction speedup_start_cb = NULL;
67 | CallbackFunction speedup_end_cb = NULL;
68 |
69 | public:
70 | ESPRotary();
71 | ESPRotary(byte pin1, byte pin2, byte steps_per_click = 1, int lower_bound = INT16_MIN, int upper_bound = INT16_MAX, int inital_pos = 0, int increment = 1);
72 |
73 | void begin(byte pin1, byte pin2, byte steps_per_click = 1, int lower_bound = INT16_MIN, int upper_bound = INT16_MAX, int inital_pos = 0, int increment = 1);
74 |
75 | int getPosition() const;
76 | void resetPosition(int p = 0, bool fireCallback = true);
77 |
78 | void triggerOnBounds(bool triggerEvents = true);
79 | rotary_direction getDirection() const;
80 | String directionToString(rotary_direction dir) const;
81 |
82 | void setIncrement(int increment);
83 | int getIncrement() const;
84 |
85 | void enableSpeedup(bool enable);
86 | void setSpeedupInterval(int interval);
87 | void setSpeedupIncrement(int increment);
88 |
89 | bool isSpeedupEnabled() const;
90 | int getSpeedupInterval() const;
91 | int getSpeedupIncrement() const;
92 | bool isInSpeedup() const;
93 |
94 | rotary_event getLastEvent() const;
95 | void retriggerEvent(bool retrigger);
96 |
97 | void setUpperBound(int upper_bound);
98 | void setLowerBound(int lower_bound);
99 | int getUpperBound() const;
100 | int getLowerBound() const;
101 |
102 | void setStepsPerClick(int steps);
103 | int getStepsPerClick() const;
104 |
105 | void setChangedHandler(CallbackFunction f);
106 | void setRightRotationHandler(CallbackFunction f);
107 | void setLeftRotationHandler(CallbackFunction f);
108 | void setUpperOverflowHandler(CallbackFunction f);
109 | void setLowerOverflowHandler(CallbackFunction f);
110 | void setSpeedupStartedHandler(CallbackFunction f);
111 | void setSpeedupEndedHandler(CallbackFunction f);
112 |
113 | int getID() const;
114 | void setID(int newID);
115 |
116 | bool operator==(ESPRotary &rhs);
117 |
118 | void loop();
119 |
120 | private:
121 | static int _nextID;
122 |
123 | void _callCallback(CallbackFunction callback);
124 | void _setEvent(rotary_event e);
125 | bool _wasRotated();
126 | bool _isWithinBounds(bool triggerAlerts = false);
127 | void _checkForSpeedup(unsigned long now);
128 | void _setID();
129 | };
130 |
131 | /////////////////////////////////////////////////////////////////
132 | #endif
133 | /////////////////////////////////////////////////////////////////
134 |
--------------------------------------------------------------------------------