Skip to content

Commit

Permalink
Merge pull request #44 from LennartHennigs/gaaf-fixes
Browse files Browse the repository at this point in the history
2.1.0 updates
  • Loading branch information
LennartHennigs authored Apr 13, 2023
2 parents 4044263 + 0807891 commit 70364ff
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 81 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@

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

## [2.1.0] - 2024-04-13

- Added `triggerOnBounds(bool triggerEvents = true)` method to be able to suppress rotate events when the boundaries are reached
- Made `now` and time related attributes to `unsigned long`
- Replaced cases with lookup table to simplify `loop()`
- Simplified state change in the `loop()`
- Positions are only set within the defined boundaries
- some refactoring suggested by GPT-4

Most changes were suggested in [#43](https://github.com/LennartHennigs/ESPRotary/pull/43) by [Alex Herman](https://github.com/gaaf). Thank you.

## [2.0.0] - 2022-12-27

- added speedup functionality as suggested in [#37](https://github.com/LennartHennigs/ESPRotary/discussions/37)
Expand Down Expand Up @@ -78,7 +89,7 @@
- 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
- removed ```#pragma``` message for ```moves_per_click``` constructor parameter
- renamed variable ```moves_per_click``` to ```steps_per_click```
## [1.2.1] - 2020-04-20
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017-2022 LennartHennigs
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
Expand Down
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Arduino/ESP library to simplify reading rotary encoder data.

- Author: Lennart Hennigs (<https://www.lennarthennigs.de>)
- Copyright (C) 2017-2022 Lennart Hennigs.
- Copyright (C) 2017-2023 Lennart Hennigs.
- Released under the MIT license.

## Description
Expand Down Expand Up @@ -37,11 +37,11 @@ Some of the code based of this library is based on code from [PJRC](https://www.
- `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).
- 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.

### Ranges

- In the constructor you can define an upper and a lower treshhold. The encoder value will not be bypass these values.
- 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)`
Expand All @@ -53,7 +53,9 @@ Some of the code based of this library is based on code from [PJRC](https://www.

- 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 retrigger the vent.
- 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

Expand All @@ -75,15 +77,15 @@ Some of the code based of this library is based on code from [PJRC](https://www.
- 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`.
- 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 handers that let you track if the speedup mode was entered or exited:
- 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:
Expand All @@ -103,15 +105,15 @@ Some of the code based of this library is based on code from [PJRC](https://www.

### IDs for Encoder Instances

- Each enocder instance gets a unique (auto incremented) ID upon creation.
- 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](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!
- And if you find this library helpful, please consider giving it a star at [GitHub](https://github.com/LennartHennigs/ESPRotary). Thanks!

## Examples

Expand All @@ -128,9 +130,9 @@ 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);
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 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 initial_pos = 0, int increment = 1);

int getPosition() const;
void resetPosition(int p = 0, bool fireCallback = true);
Expand All @@ -141,6 +143,8 @@ These are the constructor and the member functions the library provides:
void setIncrement(int inc);
int getIncrement() const;

void triggerOnBounds(bool triggerEvents = true);

void enableSpeedup(bool enable);
void setSpeedupInterval(int time);
void setSpeedupIncrement(int inc);
Expand Down Expand Up @@ -186,7 +190,7 @@ Or download the ZIP archive (<https://github.com/lennarthennigs/ESPRotary/zipbal
MIT License
Copyright (c) 2017-2022 Lennart Hennigs
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
Expand Down
3 changes: 3 additions & 0 deletions examples/RangedCounter/RangedCounter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ void setup() {
Serial.print("Current position: ");
Serial.println(r.getPosition());
Serial.println();

// enable this to be also notified about rotation events when the bounds are hit
// r.triggerOnBounds(true); // off per default
}

void loop() {
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ setSpeedupStartedHandler KEYWORD2
setSpeedupEndedHandler KEYWORD2
isInSpeedup KEYWORD2
retriggerEvent KEYWORD2
triggerOnBounds KEYWORD2
getID KEYWORD2
setID KEYWORD2
loop KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ESP Rotary",
"version": "2.0.0",
"version": "2.1.0",
"keywords": "rotary encoder, esp8266",
"description": "ESP8266/Arduino Library for reading rotary encoder values.",
"homepage": "https://github.com/LennartHennigs/ESPRotary",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP Rotary
version=2.0.0
version=2.1.0
author=Lennart Hennigs
maintainer=Lennart Hennigs <mail@lennarthennigs.de>
sentence=ESP8266/Arduino Library for reading rotary encoder values.
Expand Down
111 changes: 54 additions & 57 deletions src/ESPRotary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ void ESPRotary::resetPosition(int p /* = 0 */, bool fireCallback /* = true */) {
// yes...
steps = p * steps_per_click;
_isWithinBounds();
if (fireCallback && change_cb != NULL) change_cb(*this);
if (fireCallback) _callCallback(change_cb);
last_event = rotary_event::none;
dir = rotary_direction::undefined;
in_speedup = false;
}

/////////////////////////////////////////////////////////////////

void ESPRotary::setIncrement(int inc) {
increment = inc;
void ESPRotary::setIncrement(int increment) {
increment = increment;
}

/////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -194,7 +194,7 @@ bool ESPRotary::operator==(ESPRotary &rhs) {
/////////////////////////////////////////////////////////////////

void ESPRotary::loop() {
long now = millis();
unsigned long now = millis();
// did it change (enough)?
if (!_wasRotated()) return;
dir = (steps > last_steps) ? rotary_direction::right : rotary_direction::left;
Expand All @@ -212,47 +212,22 @@ void ESPRotary::loop() {
/////////////////////////////////////////////////////////////////

bool ESPRotary::_wasRotated() {
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:
steps += increment;
break;
case 2:
case 4:
case 11:
case 13:
steps -= increment;
break;
case 3:
case 12:
steps += 2 * increment;
break;
default:
steps -= 2 * increment;
break;
}
state = (s >> 2);
return (abs(steps - last_steps) >= steps_per_click * increment);
static const int8_t factors[] = {0, 1, -1, 2, -1, 0, -2, 1, 1, -2, 0, -1, 2, -1, 1, 0};
int encoderState = (state & 3) | digitalRead(pin1) << 2 | digitalRead(pin2) << 3 ;
steps += factors[encoderState] * increment;
state = (encoderState >> 2);
int stepDifference = abs(steps - last_steps);
return stepDifference >= (steps_per_click * increment);
}

/////////////////////////////////////////////////////////////////

void ESPRotary::_checkForSpeedup(long now) {
void ESPRotary::_checkForSpeedup(unsigned long now) {
if (now - last_turn > speedup_interval) {
if (in_speedup) _setEvent(rotary_event::speedup_ended);
return;
}
steps += (dir == rotary_direction::right ? 1 : -1) * (speedup_increment - increment) * steps_per_click;
steps += ((dir == rotary_direction::right ? 1 : -1) * (speedup_increment - increment) * steps_per_click);
int pos = getPosition();
// only trigger speedup when you are not "on a wall"
if (pos > lower_bound && pos < upper_bound) {
Expand All @@ -262,65 +237,87 @@ void ESPRotary::_checkForSpeedup(long now) {

/////////////////////////////////////////////////////////////////

bool ESPRotary::_isWithinBounds(bool alert /* = false */) {
void ESPRotary::triggerOnBounds(bool triggerEvents /* = true */) {
boundsTrigger = triggerEvents;
}

/////////////////////////////////////////////////////////////////

void ESPRotary::_callCallback(CallbackFunction callback) {
if (callback != NULL) callback(*this);
}

/////////////////////////////////////////////////////////////////

bool ESPRotary::_isWithinBounds(bool triggerAlerts /* = false */) {
int pos = getPosition();
if (pos > lower_bound && pos < upper_bound) return true;

if (pos >= upper_bound) {
if (in_speedup) _setEvent(rotary_event::speedup_ended);
if (alert) _setEvent(rotary_event::upper_bound_hit);
steps = upper_bound * steps_per_click;
if (in_speedup) _setEvent(rotary_event::speedup_ended);
if (triggerAlerts) _setEvent(rotary_event::upper_bound_hit);

} else if (pos <= lower_bound) {
if (in_speedup) _setEvent(rotary_event::speedup_ended);
if (alert) _setEvent(rotary_event::lower_bound_hit);
steps = lower_bound * steps_per_click;
if (in_speedup) _setEvent(rotary_event::speedup_ended);
if (triggerAlerts) _setEvent(rotary_event::lower_bound_hit);
}

return false;
}
/////////////////////////////////////////////////////////////////

void ESPRotary::_setEvent(rotary_event e) {
switch (e) {
void ESPRotary::_setEvent(rotary_event event) {
switch (event) {
case rotary_event::left_rotation:
if (left_cb != NULL) left_cb(*this);
if (change_cb != NULL) change_cb(*this);
_callCallback(left_cb);
_callCallback(change_cb);
break;

case rotary_event::right_rotation:
if (right_cb != NULL) right_cb(*this);
if (change_cb != NULL) change_cb(*this);
_callCallback(right_cb);
_callCallback(change_cb);
break;

case rotary_event::speedup_started:
if (speedup_start_cb != NULL) speedup_start_cb(*this);
_callCallback(speedup_start_cb);
in_speedup = true;
break;

case rotary_event::speedup_ended:
if (speedup_end_cb != NULL) speedup_end_cb(*this);
_callCallback(speedup_end_cb);
in_speedup = false;
break;

case rotary_event::upper_bound_hit:
if (last_event == rotary_event::upper_bound_hit && !retrigger_event) return;
if (upper_cb != NULL) upper_cb(*this);
if (boundsTrigger) {
_callCallback(right_cb);
_callCallback(change_cb);
}
_callCallback(upper_cb);
break;

case rotary_event::lower_bound_hit:
if (last_event == rotary_event::lower_bound_hit && !retrigger_event) return;
if (lower_cb != NULL) lower_cb(*this);
if (boundsTrigger) {
_callCallback(left_cb);
_callCallback(change_cb);
}
_callCallback(lower_cb);
break;

case rotary_event::none:
break;
}
last_event = e;
last_event = event;
}

/////////////////////////////////////////////////////////////////

void ESPRotary::setSpeedupInterval(int time) {
speedup_interval = time;
void ESPRotary::setSpeedupInterval(int interval) {
speedup_interval = interval;
}

/////////////////////////////////////////////////////////////////
Expand All @@ -331,8 +328,8 @@ int ESPRotary::getSpeedupInterval() const {

/////////////////////////////////////////////////////////////////

void ESPRotary::setSpeedupIncrement(int inc) {
speedup_increment = inc;
void ESPRotary::setSpeedupIncrement(int increment) {
speedup_increment = increment;
}

/////////////////////////////////////////////////////////////////
Expand Down
Loading

0 comments on commit 70364ff

Please sign in to comment.