初始化提交
This commit is contained in:
3
arduino-cli/libraries/ESPRotary/.gitignore
vendored
Normal file
3
arduino-cli/libraries/ESPRotary/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.DS_Store
|
||||
.vscode
|
||||
*.code-workspace
|
||||
82
arduino-cli/libraries/ESPRotary/CHANGELOG.md
Normal file
82
arduino-cli/libraries/ESPRotary/CHANGELOG.md
Normal file
@@ -0,0 +1,82 @@
|
||||
# Changelog
|
||||
|
||||
## Unreleased
|
||||
|
||||
## [1.6.0] - 2022-05-26
|
||||
|
||||
- added ID feature to the class as suggested by [Jon](https://github.com/mscreations) in [#34](https://github.com/LennartHennigs/ESPRotary/pull/34)
|
||||
- added == operator as suggested by [Jon](https://github.com/mscreations) in [#34](<https://github.com/LennartHennigs/>
|
||||
- Defined getter methods as ```const```
|
||||
|
||||
## [1.5.0] - 2021-06-22
|
||||
|
||||
- Added option to define increment as suggested by [sblaisot](https://github.com/sblaisot) in pull request [#32](https://github.com/LennartHennigs/ESPRotary/pull/32)
|
||||
- Added a `begin()` function and a default constructor the be able to instantiate an object within a function
|
||||
- Updated the `RangedCounter` and `SimpleCounterWithButton` examples. They now use `begin()`
|
||||
- Added getter and setter functions for the upper and lower bounds
|
||||
- ```void setUpperBound(int upper_bound);```
|
||||
- ```void setLowerBound(int lower_bound);```
|
||||
- ```int getUpperBound();```
|
||||
- ```int getLowerBound();```
|
||||
|
||||
## [1.4.2] - 2020-11-29
|
||||
|
||||
- remove unused variable `last_read_ms` as pointed out by [ornea](https://github.com/ornea) in [#26](https://github.com/LennartHennigs/ESPRotary/issues/26)
|
||||
- cleaned up the protected vars as suggested by [Jim-2249](https://github.com/Jim-2249) in [#30](https://github.com/LennartHennigs/ESPRotary/issues/30)
|
||||
- simplified the `loop()` function
|
||||
|
||||
## [1.4.1] - 2020-11-29
|
||||
|
||||
- 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)
|
||||
- made ```resetPosition()``` callback trigger optional as suggested by [ab-it](https://github.com/ab-it)
|
||||
- fixed bug in ```directionToString()``` introduced in the 1.4 version, found by [ab-it](https://github.com/ab-it)
|
||||
|
||||
## [1.4] - 2020-11-26
|
||||
|
||||
- added callback to ```resetPosition()``` as suggested by [ab-it](https://github.com/ab-it) in [#17](https://github.com/LennartHennigs/ESPRotary/issues/17)
|
||||
- Fixed warning in ```directionToString()``` as suggested by [ab-it](https://github.com/ab-it) in [#18](https://github.com/LennartHennigs/ESPRotary/issues/18)
|
||||
- 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)
|
||||
- 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)
|
||||
- Avoid triggering change_cb after resetPosition to other position than 0
|
||||
|
||||
## [1.3] - 2020-06-05
|
||||
|
||||
- Fixed typo in example
|
||||
- updated ```README.md```
|
||||
- added function ```setStepsPerClick()```, as requested by [asalillo](https://github.com/lasalillo) in [#12](https://github.com/LennartHennigs/ESPRotary/issues/12)
|
||||
- Fixed bug [#13](https://github.com/LennartHennigs/ESPRotary/issues/13) in ```resetPosition()```, as pointed out by [jjarp](https://github.com/jjarp)
|
||||
- removed ```#pragma``` message for ```moves_per_click``` contructor parameter
|
||||
- renamed variable ```moves_per_click``` to ```steps_per_click```
|
||||
|
||||
## [1.2.1] - 2020-04-20
|
||||
|
||||
- Fixed bug related to minimum and maximum bounds
|
||||
- Added ```RangedCounter.ino``` example
|
||||
- Added optional parameter to ```resetPosition()```
|
||||
|
||||
## [1.2.0] - 2020-04-20
|
||||
|
||||
- Created and added CHANGELOG.md
|
||||
- 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))
|
||||
|
||||
## [1.1.2] - 2020-03-31
|
||||
|
||||
- Simplified example
|
||||
|
||||
## [1.1.1] - 2020-03-30
|
||||
|
||||
- Removed ```yield()``` in main ```loop()``` since it caused some problems
|
||||
|
||||
## [1.1.0] - 2020-03-27
|
||||
|
||||
- Added ```yield()``` to the main ```loop()```
|
||||
- Updated the example (as suggested by [per1234](https://github.com/per1234))
|
||||
|
||||
## [1.0.0] - 2017-11-14
|
||||
|
||||
- Added option to set number of clicks to constructor (as suggested by [svititom](https://github.com/LennartHennigs/ESPRotary/commit/95e86b171e8d1489cd603d493898c5ea516935db))
|
||||
|
||||
## Note
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
21
arduino-cli/libraries/ESPRotary/LICENSE
Normal file
21
arduino-cli/libraries/ESPRotary/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017-2022 LennartHennigs
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
143
arduino-cli/libraries/ESPRotary/README.md
Normal file
143
arduino-cli/libraries/ESPRotary/README.md
Normal file
@@ -0,0 +1,143 @@
|
||||
# ESPRotary
|
||||
|
||||
Arduino/ESP library to simplify reading rotary encoder data.
|
||||
|
||||
- Author: Lennart Hennigs (<https://www.lennarthennigs.de>)
|
||||
- Copyright (C) 2017-2022 Lennart Hennigs.
|
||||
- Released under the MIT license.
|
||||
|
||||
## Description
|
||||
|
||||
This library allows you read out interactions with a rotary encoder and act on them.
|
||||
It uses callback functions to be notified when the rotary encoder changes.
|
||||
|
||||
It has been tested with Arduino, ESP8266 and ESP32 devices.
|
||||
|
||||
To see the latest changes to the library please take a look at the [Changelog](https://github.com/LennartHennigs/ESPRotary/blob/master/CHANGELOG.md).
|
||||
|
||||
Some of the code based of this library is based on code from [PJRC](https://www.pjrc.com/teensy/td_libs_Encoder.html).
|
||||
|
||||
## How to Use
|
||||
|
||||
### Definition
|
||||
|
||||
- Use the `begin()` or the parameterized constructor to create a new instance of the class
|
||||
- Encoder produce different numbers of "click" on a single turn.
|
||||
- You can specify the number of clicks in the constructor, or via a setter function
|
||||
- ```void setStepsPerClick(int steps)```
|
||||
- ```int getStepsPerClick() const```
|
||||
|
||||
### Callback Handlers
|
||||
|
||||
- The library provides several callback handlers to detect events
|
||||
- ```void setChangedHandler(CallbackFunction f)```
|
||||
- ```void setRightRotationHandler(CallbackFunction f)```
|
||||
- ```void setLeftRotationHandler(CallbackFunction f)```
|
||||
- ```void setUpperOverflowHandler(CallbackFunction f)```
|
||||
- ```void setLowerOverflowHandler(CallbackFunction f)```
|
||||
- The library does not detect button clicks. You have to use a seperate library for this, e.g. [Button2](https://github.com/LennartHennigs/Button2).
|
||||
|
||||
### Ranges
|
||||
|
||||
- In the constructor you can define an upper and a lower treshhold. The encoder value will not be bypass these values.
|
||||
- There are also getter and setter functions the these values
|
||||
- ```void setUpperBound(int upper_bound)```
|
||||
- ```void setLowerBound(int lower_bound)```
|
||||
- ```int getUpperBound() const```
|
||||
- ```int getLowerBound() const```
|
||||
|
||||
### Reading out information
|
||||
|
||||
- The class allows you the get the position and the direction after a click using these function:
|
||||
- ```int getPosition() const```
|
||||
- ```byte getDirection() const```
|
||||
- ```String directionToString(byte direction) const```
|
||||
|
||||
### Speed
|
||||
|
||||
- You can define the speed, i.e. the increment the a single click in the constructor
|
||||
- There is also a getter and setter function for this
|
||||
- ```void setIncrement(int inc);```
|
||||
- ```int getIncrement() const```
|
||||
|
||||
### The Loop
|
||||
|
||||
- For the class to work, you need to call the `loop()` member function in your sketch's `loop()` function.
|
||||
- See the examples for more details.
|
||||
|
||||
### IDs for Encoder Instances
|
||||
|
||||
- Each enocder instance gets a unique (auto incremented) ID upon creation.
|
||||
- You can get a encoders' ID via `getID()`.
|
||||
- Alternatively, you can use `setID(int newID)` to set a new one. But then you need to make sure that they are unique.
|
||||
- You can also use the `==` operator to compare encoders
|
||||
|
||||
## Notes
|
||||
|
||||
- To see the latest changes to the library please take a look at the [Changelog](https://github.com/LennartHennigs/ESPRotary/blob/master/CHANGELOG.md).
|
||||
- And if you find this library helpful, please consider giving it a star at [GitHub](<https://github.com/LennartHennigs/ESPRotary>. Thanks!
|
||||
|
||||
## Class Definition
|
||||
|
||||
These are the constructor and the member functions the library provides:
|
||||
|
||||
``` c++
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
int getPosition() const;
|
||||
void resetPosition(int p = 0, bool fireCallback = true);
|
||||
|
||||
byte getDirection() const;
|
||||
String directionToString(byte direction) const;
|
||||
|
||||
void setIncrement(int inc);
|
||||
int getIncrement() const;
|
||||
|
||||
void setUpperBound(int upper_bound);
|
||||
void setLowerBound(int lower_bound);
|
||||
int getUpperBound() const;
|
||||
int getLowerBound() const;
|
||||
|
||||
void setStepsPerClick(int steps);
|
||||
int getStepsPerClick() const;
|
||||
|
||||
void setChangedHandler(CallbackFunction f);
|
||||
void setRightRotationHandler(CallbackFunction f);
|
||||
void setLeftRotationHandler(CallbackFunction f);
|
||||
void setUpperOverflowHandler(CallbackFunction f);
|
||||
void setLowerOverflowHandler(CallbackFunction f);
|
||||
|
||||
void loop();
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
Open the Arduino IDE choose "Sketch > Include Library" and search for "ESP Rotary".
|
||||
Or download the ZIP archive (<https://github.com/lennarthennigs/ESPRotary/zipball/master>), and choose "Sketch > Include Library > Add .ZIP Library..." and select the downloaded file.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017-2022 Lennart Hennigs
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,75 @@
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "Button2.h" // https://github.com/LennartHennigs/Button2
|
||||
#include "ESPRotary.h"
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
#define ROTARY_PIN1 D1
|
||||
#define ROTARY_PIN2 D2
|
||||
#define BUTTON_PIN D4
|
||||
|
||||
#define CLICKS_PER_STEP 4 // this number depends on your rotary encoder
|
||||
#define MIN_POS 4
|
||||
#define MAX_POS 20
|
||||
#define START_POS 10
|
||||
#define INCREMENT 2 // this number is the counter increment on each step
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
ESPRotary r;
|
||||
Button2 b; // https://github.com/LennartHennigs/Button2
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
delay(50);
|
||||
|
||||
b.begin(BUTTON_PIN);
|
||||
b.setTapHandler(click);
|
||||
b.setLongClickHandler(resetPosition);
|
||||
|
||||
r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP, MIN_POS, MAX_POS, START_POS, INCREMENT);
|
||||
r.setChangedHandler(rotate);
|
||||
r.setLeftRotationHandler(showDirection);
|
||||
r.setRightRotationHandler(showDirection);
|
||||
|
||||
Serial.println("\n\nRanged Counter");
|
||||
Serial.println("You can only set values between " + String(MIN_POS) + " and " + String(MAX_POS) +".");
|
||||
Serial.print("Increment: ");
|
||||
Serial.println(r.getIncrement());
|
||||
Serial.print("Current position: ");
|
||||
Serial.println(r.getPosition());
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
r.loop();
|
||||
b.loop();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
// on change
|
||||
void rotate(ESPRotary& r) {
|
||||
Serial.println(r.getPosition());
|
||||
}
|
||||
|
||||
// on left or right rotation
|
||||
void showDirection(ESPRotary& r) {
|
||||
Serial.println(r.directionToString(r.getDirection()));
|
||||
}
|
||||
|
||||
// single click
|
||||
void click(Button2& btn) {
|
||||
Serial.println("Click!");
|
||||
}
|
||||
|
||||
// long click
|
||||
void resetPosition(Button2& btn) {
|
||||
r.resetPosition();
|
||||
Serial.println("Reset!");
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,43 @@
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "ESPRotary.h"
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
#define ROTARY_PIN1 14
|
||||
#define ROTARY_PIN2 12
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
ESPRotary r;
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
delay(50);
|
||||
Serial.println("\n\nSimple Counter");
|
||||
|
||||
r.begin(ROTARY_PIN1, ROTARY_PIN2);
|
||||
r.setChangedHandler(rotate);
|
||||
r.setLeftRotationHandler(showDirection);
|
||||
r.setRightRotationHandler(showDirection);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
r.loop();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
// on change
|
||||
void rotate(ESPRotary& r) {
|
||||
Serial.println(r.getPosition());
|
||||
}
|
||||
|
||||
// on left or right rotattion
|
||||
void showDirection(ESPRotary& r) {
|
||||
Serial.println(r.directionToString(r.getDirection()));
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,65 @@
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "Button2.h" // https://github.com/LennartHennigs/Button2
|
||||
#include "ESPRotary.h"
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
#define ROTARY_PIN1 D1
|
||||
#define ROTARY_PIN2 D2
|
||||
#define BUTTON_PIN D4
|
||||
|
||||
#define CLICKS_PER_STEP 4 // this number depends on your rotary encoder
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
ESPRotary r;
|
||||
Button2 b;
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
delay(50);
|
||||
Serial.println("\n\nSimple Counter");
|
||||
|
||||
r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP);
|
||||
r.setChangedHandler(rotate);
|
||||
r.setLeftRotationHandler(showDirection);
|
||||
r.setRightRotationHandler(showDirection);
|
||||
|
||||
b.begin(BUTTON_PIN);
|
||||
b.setTapHandler(click);
|
||||
b.setLongClickHandler(resetPosition);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
r.loop();
|
||||
b.loop();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
// on change
|
||||
void rotate(ESPRotary& r) {
|
||||
Serial.println(r.getPosition());
|
||||
}
|
||||
|
||||
// on left or right rotation
|
||||
void showDirection(ESPRotary& r) {
|
||||
Serial.println(r.directionToString(r.getDirection()));
|
||||
}
|
||||
|
||||
// single click
|
||||
void click(Button2& btn) {
|
||||
Serial.println("Click!");
|
||||
}
|
||||
|
||||
// long click
|
||||
void resetPosition(Button2& btn) {
|
||||
r.resetPosition();
|
||||
Serial.println("Reset!");
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
24
arduino-cli/libraries/ESPRotary/keywords.txt
Normal file
24
arduino-cli/libraries/ESPRotary/keywords.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
ESPRotary KEYWORD1
|
||||
getPosition KEYWORD2
|
||||
begin KEYWORD2
|
||||
setUpperBound KEYWORD2
|
||||
setLowerBound KEYWORD2
|
||||
getUpperBound KEYWORD2
|
||||
getLowerBound KEYWORD2
|
||||
resetPosition KEYWORD2
|
||||
getDirection KEYWORD2
|
||||
getIncrement KEYWORD2
|
||||
setIncrement KEYWORD2
|
||||
getStepsPerClick KEYWORD2
|
||||
setStepsPerClick KEYWORD2
|
||||
directionToString KEYWORD2
|
||||
setChangedHandler KEYWORD2
|
||||
setRightRotationHandler KEYWORD2
|
||||
setLeftRotationHandler KEYWORD2
|
||||
setUpperOverflowHandler KEYWORD2
|
||||
setLowerOverflowHandler KEYWORD2
|
||||
getID KEYWORD2
|
||||
setID KEYWORD2
|
||||
loop KEYWORD2
|
||||
RE_RIGHT LITERAL1
|
||||
RE_LEFT LITERAL1
|
||||
17
arduino-cli/libraries/ESPRotary/library.json
Normal file
17
arduino-cli/libraries/ESPRotary/library.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "ESP Rotary",
|
||||
"version": "1.6.0",
|
||||
"keywords": "rotary encoder, esp8266",
|
||||
"description": "ESP8266/Arduino Library for reading rotary encoder values.",
|
||||
"homepage": "https://github.com/LennartHennigs/ESPRotary",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/LennartHennigs/ESPRotary"
|
||||
},
|
||||
"authors": {
|
||||
"name": "Lennart Hennigs",
|
||||
"url": "https://lennarthennigs.de"
|
||||
},
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
}
|
||||
9
arduino-cli/libraries/ESPRotary/library.properties
Normal file
9
arduino-cli/libraries/ESPRotary/library.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
name=ESP Rotary
|
||||
version=1.6.0
|
||||
author=Lennart Hennigs
|
||||
maintainer=Lennart Hennigs <mail@lennarthennigs.de>
|
||||
sentence=ESP8266/Arduino Library for reading rotary encoder values.
|
||||
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.
|
||||
category=Other
|
||||
url=https://github.com/LennartHennigs/ESPRotary
|
||||
architectures=*
|
||||
220
arduino-cli/libraries/ESPRotary/src/ESPRotary.cpp
Normal file
220
arduino-cli/libraries/ESPRotary/src/ESPRotary.cpp
Normal file
@@ -0,0 +1,220 @@
|
||||
/////////////////////////////////////////////////////////////////
|
||||
/*
|
||||
ESP8266/Arduino Library for reading rotary encoder values.
|
||||
Copyright 2017-2021 Lennart Hennigs.
|
||||
*/
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "ESPRotary.h"
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
// initialize static counter
|
||||
|
||||
int ESPRotary::_nextID = 0;
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
ESPRotary::ESPRotary() {
|
||||
id = _nextID++;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
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 */) {
|
||||
id = _nextID++;
|
||||
begin(pin1, pin2, steps_per_click, lower_bound, upper_bound, inital_pos, increment);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
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 */) {
|
||||
this->pin1 = pin1;
|
||||
this->pin2 = pin2;
|
||||
pinMode(pin1, INPUT_PULLUP);
|
||||
pinMode(pin2, INPUT_PULLUP);
|
||||
|
||||
setUpperBound(upper_bound);
|
||||
setLowerBound(lower_bound);
|
||||
setIncrement(increment);
|
||||
setStepsPerClick(steps_per_click);
|
||||
|
||||
loop();
|
||||
resetPosition(inital_pos, false);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::setUpperBound(int upper_bound) {
|
||||
this->upper_bound = (lower_bound < upper_bound) ? upper_bound: lower_bound;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::setLowerBound(int lower_bound) {
|
||||
this->lower_bound = (lower_bound < upper_bound) ? lower_bound : upper_bound;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
int ESPRotary::getUpperBound() const {
|
||||
return this->upper_bound;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
int ESPRotary::getLowerBound() const {
|
||||
return this->lower_bound;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::setChangedHandler(CallbackFunction f) {
|
||||
change_cb = f;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::setRightRotationHandler(CallbackFunction f) {
|
||||
right_cb = f;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::setLeftRotationHandler(CallbackFunction f) {
|
||||
left_cb = f;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::setUpperOverflowHandler(CallbackFunction f) {
|
||||
upper_cb = f;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::setLowerOverflowHandler(CallbackFunction f) {
|
||||
lower_cb = f;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::resetPosition(int p /* = 0 */, bool fireCallback /* = true */) {
|
||||
if (p > upper_bound) {
|
||||
last_position = upper_bound * steps_per_click;
|
||||
} else {
|
||||
last_position = (lower_bound > p) ? lower_bound * steps_per_click : p * steps_per_click;
|
||||
}
|
||||
|
||||
if (position != last_position) {
|
||||
position = last_position;
|
||||
if (fireCallback && change_cb != NULL) change_cb (*this);
|
||||
}
|
||||
direction = 0;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::setIncrement(int inc) {
|
||||
increment = inc;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
int ESPRotary::getIncrement() const {
|
||||
return increment;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::setStepsPerClick(int steps) {
|
||||
steps_per_click = (steps < 1) ? 1 : steps;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
int ESPRotary::getStepsPerClick() const {
|
||||
return steps_per_click;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
byte ESPRotary::getDirection() const {
|
||||
return direction;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
String ESPRotary::directionToString(byte direction) const {
|
||||
if (direction == RE_LEFT) {
|
||||
return "LEFT";
|
||||
} else {
|
||||
return "RIGHT";
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
int ESPRotary::getPosition() const {
|
||||
return position / steps_per_click;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
int ESPRotary::getID() const {
|
||||
return id;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::setID(int newID) {
|
||||
id = newID;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
bool ESPRotary::operator == (ESPRotary &rhs) {
|
||||
return (this == &rhs);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
void ESPRotary::loop() {
|
||||
int s = state & 3;
|
||||
if (digitalRead(pin1)) s |= 4;
|
||||
if (digitalRead(pin2)) s |= 8;
|
||||
|
||||
switch (s) {
|
||||
case 0: case 5: case 10: case 15:
|
||||
break;
|
||||
case 1: case 7: case 8: case 14:
|
||||
position += increment; break;
|
||||
case 2: case 4: case 11: case 13:
|
||||
position -= increment; break;
|
||||
case 3: case 12:
|
||||
position += 2 * increment; break;
|
||||
default:
|
||||
position -= 2 * increment; break;
|
||||
}
|
||||
state = (s >> 2);
|
||||
|
||||
if (position != last_position && (abs(position - last_position) >= steps_per_click * increment)) {
|
||||
int current_position = getPosition();
|
||||
if (current_position >= lower_bound && current_position <= upper_bound) {
|
||||
if (position > last_position) {
|
||||
direction = RE_RIGHT;
|
||||
if (right_cb != NULL) right_cb (*this);
|
||||
} else {
|
||||
direction = RE_LEFT;
|
||||
if (left_cb != NULL) left_cb (*this);
|
||||
}
|
||||
last_position = position;
|
||||
if (change_cb != NULL) change_cb (*this);
|
||||
} else {
|
||||
position = last_position;
|
||||
if (current_position < lower_bound && lower_cb != NULL) lower_cb (*this);
|
||||
if (current_position > upper_bound && upper_cb != NULL) upper_cb (*this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
83
arduino-cli/libraries/ESPRotary/src/ESPRotary.h
Normal file
83
arduino-cli/libraries/ESPRotary/src/ESPRotary.h
Normal file
@@ -0,0 +1,83 @@
|
||||
/////////////////////////////////////////////////////////////////
|
||||
/*
|
||||
ESP8266/Arduino Library for reading rotary encoder values.
|
||||
Copyright 2017-2021 Lennart Hennigs
|
||||
*/
|
||||
/////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
|
||||
#ifndef ESPRotary_h
|
||||
#define ESPRotary_h
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
#define RE_RIGHT 1
|
||||
#define RE_LEFT 255
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
class ESPRotary {
|
||||
protected:
|
||||
int id;
|
||||
byte pin1, pin2;
|
||||
byte steps_per_click;
|
||||
int lower_bound, upper_bound;
|
||||
int position, last_position;
|
||||
byte direction;
|
||||
byte state;
|
||||
int increment;
|
||||
|
||||
typedef void (*CallbackFunction) (ESPRotary&);
|
||||
CallbackFunction change_cb = NULL;
|
||||
CallbackFunction right_cb = NULL;
|
||||
CallbackFunction left_cb = NULL;
|
||||
CallbackFunction lower_cb = NULL;
|
||||
CallbackFunction upper_cb = NULL;
|
||||
|
||||
public:
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
int getPosition() const;
|
||||
void resetPosition(int p = 0, bool fireCallback = true);
|
||||
|
||||
byte getDirection() const;
|
||||
String directionToString(byte direction) const;
|
||||
|
||||
void setIncrement(int inc);
|
||||
int getIncrement() const;
|
||||
|
||||
void setUpperBound(int upper_bound);
|
||||
void setLowerBound(int lower_bound);
|
||||
int getUpperBound() const;
|
||||
int getLowerBound() const;
|
||||
|
||||
void setStepsPerClick(int steps);
|
||||
int getStepsPerClick() const;
|
||||
|
||||
void setChangedHandler(CallbackFunction f);
|
||||
void setRightRotationHandler(CallbackFunction f);
|
||||
void setLeftRotationHandler(CallbackFunction f);
|
||||
void setUpperOverflowHandler(CallbackFunction f);
|
||||
void setLowerOverflowHandler(CallbackFunction f);
|
||||
|
||||
int getID() const;
|
||||
void setID(int newID);
|
||||
|
||||
bool operator == (ESPRotary &rhs);
|
||||
|
||||
void loop();
|
||||
|
||||
private:
|
||||
static int _nextID;
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
#endif
|
||||
/////////////////////////////////////////////////////////////////
|
||||
Reference in New Issue
Block a user