• Stars
    star
    175
  • Rank 217,417 (Top 5 %)
  • Language
    C++
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Arduino/ESP library to simplify reading rotary encoder data.

ESPRotary

Arduino/ESP library to simplify reading rotary encoder data.

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.

If you find this library helpful please consider giving it a ⭐️ at GitHub and/or buy me a β˜•οΈ. Thanks!

Some of the code based of this library is based on code from PJRC.

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 separate library for this, e.g. my Button2 library.

Ranges

  • In the constructor you can define an upper and a lower threshold. 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
  • See the RangedCounter example for details.

Retrigger (out of bound) events?

  • If you want that out of bound events are only triggered once a bound is reached, you can set:
    • retriggerEvent(false)
  • Otherwise each (out of bounds) turn will re-trigger the event.
  • If you want to disable rotate events that would go beyond your defined boundary you can use:
    • void triggerOnBounds(false)

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

Speedup Mode

  • You can also define, that the increment shall change when the encoder is turned fast.
  • Use these functions to define the interval (between clicks) and the increment:
    • void setSpeedupInterval(int time)
    • void setSpeedupIncrement(int inc)
  • Per default the interval is set to 75ms and the increment is set to 5.
  • There are also getter functions for both parameters:
    • int getSpeedupInterval() const
    • int getSpeedupIncrement() const
  • To enable the speedup mode use this member function:
    • void enableSpeedup(bool enable)
  • ...and to check:
    • bool isSpeedupEnabled() const
  • There are also event handlers that let you track if the speedup mode was entered or exited:
    • void setSpeedupStartedHandler(CallbackFunction f)
    • void setSpeedupEndedHandler(CallbackFunction f)
  • Alternatively, you can use this function to determine its state:
    • bool isInSpeedup() const
  • If you have bound defined, the speedup mode will be automatically ended when you hit it.

The Loop

  • For the class to work, you need to call the loop() member function in your sketch's loop() function.
  • Alternatively, you can use a timer interrupt to call the member loop()function.
  • See the examples for more details.

Using it with a timer interrupt

IDs for Encoder Instances

  • Each encoder 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.
  • And if you find this library helpful, please consider giving it a star at GitHub. Thanks!

Examples

Class Definition

These are the constructor and the member functions the library provides:

  ESPRotary();
  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);

  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);

  int getPosition() const;
  void resetPosition(int p = 0, bool fireCallback = true);

  rotary_direction getDirection() const;
  String directionToString(rotary_direction dir) const;

  void setIncrement(int inc);
  int getIncrement() const;

  void triggerOnBounds(bool triggerEvents = true);

  void enableSpeedup(bool enable);
  void setSpeedupInterval(int time);
  void setSpeedupIncrement(int inc);

  bool isSpeedupEnabled() const;
  int getSpeedupInterval() const;
  int getSpeedupIncrement() const;
  bool isInSpeedup() const;

  rotary_event getLastEvent() const;
  void retriggerEvent(bool retrigger);

  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 setSpeedupStartedHandler(CallbackFunction f);
  void setSpeedupEndedHandler(CallbackFunction f);

  int getID() const;
  void setID(int newID);

  bool operator == (ESPRotary &rhs);

  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-2023 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.

More Repositories

1

Button2

Arduino/ESP button library that provides callback functions to track single, double, triple and long clicks. It also takes care of debouncing.
C++
466
star
2

ESPTelnet

ESP library that allows you to setup a telnet server for debugging.
C++
203
star
3

SimpleFSM

Arduino/ESP library to simplify setting up and running a state machine.
C++
62
star
4

DIYStreamDeck

DIY Stream Deck with the Pi Pico and the Pimoroni RGB Keypad
Python
28
star
5

ESPBattery

Arduino Library to calculate the ESP8266 (Feather Huzzah) LiPo battery level.
C++
24
star
6

RG35XX-H-Notes

How to install and set up Batocera v40 on the Anbernic RG35XX-H.
15
star
7

OTAWrapper

Arduino/ESP library to simplify OTA setup.
C++
9
star
8

Atari-Portfolio-Notes

my notes on the Atari Portfolio
Batchfile
6
star
9

M5FacesEncoder

Arduino Library for reading encoder values from the M5Stack Faces Encoder Module.
C++
5
star
10

casio2ftdi

Schematics for a Casio FX-880P port to FDI Adapter (for data transfer).
4
star
11

PiPortfolioDaemon

NodeJS server to upload and download files from a Pi to a Atari Portfolio
JavaScript
3
star
12

PrototypingWS

Workshop material of a physical prototyping workshop
3
star
13

esp8266-oled-ssd1306

Driver for the SSD1306 and SH1106 based 128x64 and 64x48 pixel OLED display running on the Arduino/ESP8266 platform
C
3
star
14

PortfolioWiFiBridge

C++
2
star
15

VSCode-DosboxX

my setup for compiling and running Turbo Pascal, TASM & A86 programs in DosBoxX
Batchfile
2
star
16

wemos-boilerplate

my commonly used Arduino code for Wemos D1 Mini
C++
2
star
17

node-red-contrib-rc522

a node-red node for using a RC522 NFC card reader
HTML
2
star
18

js_iot_bridge

JavaScript
1
star
19

casio-fx880p-manual

1
star