-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
version 1.2: AiEsp32RotaryEncoderNumberSelector
- Loading branch information
1 parent
68e0deb
commit 6993a63
Showing
6 changed files
with
176 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "AiEsp32RotaryEncoder.h" | ||
#include "AiEsp32RotaryEncoderNumberSelector.h" | ||
#define ROTARY_ENCODER_A_PIN 32 | ||
#define ROTARY_ENCODER_B_PIN 21 | ||
#define ROTARY_ENCODER_BUTTON_PIN 25 | ||
#define ROTARY_ENCODER_STEPS 4 | ||
AiEsp32RotaryEncoder *rotaryEncoder = new AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, -1, ROTARY_ENCODER_STEPS); | ||
AiEsp32RotaryEncoderNumberSelector numberSelector = AiEsp32RotaryEncoderNumberSelector(); | ||
/* | ||
In this example an additional functionality is used. | ||
AiEsp32RotaryEncoderNumberSelector is that additional helper which will hide calculation for a rotary encoder. | ||
In example 1 - you can select some temperature between -12 and +31.5 degrees in steps by 0.5 degrees; using precision of 1 decimal | ||
In example 2 - you can select some frequency between 6999000 and 7000000 Hz in steps by 10 Hz; using precision of 0 decimal (integer) | ||
Internally AiEsp32RotaryEncoderNumberSelector will do the math and set the most apropriate acceleration, min and max values for you | ||
use setRange to set parameters | ||
use setValue for a default/initial value | ||
and finally read the value with getValue | ||
So, this value is actually value you need | ||
In code bellow comment / uncomment example 1 or 2 | ||
*/ | ||
|
||
void rotary_onButtonClick() | ||
{ | ||
static unsigned long lastTimePressed = 0; | ||
if (millis() - lastTimePressed < 200) | ||
return; | ||
lastTimePressed = millis(); | ||
|
||
Serial.print("Selected value is "); | ||
Serial.print(numberSelector.getValue(), 1); | ||
Serial.println(" ***********************"); | ||
} | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
rotaryEncoder->begin(); | ||
rotaryEncoder->setup( | ||
[] { rotaryEncoder->readEncoder_ISR(); }, | ||
[] { rotary_onButtonClick(); }); | ||
|
||
numberSelector.attachEncoder(rotaryEncoder); | ||
//example 1 | ||
//numberSelector.setRange(-12.0, 31.5, 0.5, false, 1); | ||
//numberSelector.setValue(24.3); | ||
//example 2 | ||
numberSelector.setRange(6999000.0, 7000000.0, 10, false, 0); | ||
numberSelector.setValue(6999500.0); | ||
} | ||
|
||
void loop() | ||
{ | ||
if (rotaryEncoder->encoderChanged()) | ||
{ | ||
Serial.print(numberSelector.getValue()); | ||
Serial.println(" "); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
name=Ai Esp32 Rotary Encoder | ||
version=1.1 | ||
version=1.2 | ||
author=Igor Antolic (adapted code from github.com/marcmerlin/IoTuz) | ||
maintainer=Igor Antolic <iantolic@gmail.com> | ||
sentence=Easy implement rotary encoder to your application | ||
paragraph=Supports acceleration, setting boundaries. Works with ESP32. | ||
paragraph=Supports acceleration, setting boundaries. Works with ESP32. New in version 1.2: AiEsp32RotaryEncoderNumberSelector introduced to help selecting for example range -12 do 31.5 in steps of 0.5. | ||
category=Device Control | ||
url=https://github.com/iantolic/ai-esp32-rotary-encoder.git | ||
architectures=esp32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#pragma once | ||
|
||
#include <Arduino.h> | ||
#include "AiEsp32RotaryEncoder.h" | ||
/* | ||
setRange (float min, max, step) | ||
100,500,25 | ||
88 , 104 , 0.05 | ||
coef = 1/step | ||
range min/step, max/step -> 4 - 20 | ||
realValue = value*step -> 6*25 =150 | ||
*/ | ||
//#define DEBUG_NUM_SEL | ||
|
||
class AiEsp32RotaryEncoderNumberSelector | ||
{ | ||
private: | ||
float minValue = 0; | ||
float maxValue = 100; | ||
float step = 2; | ||
float coeficient = 1; | ||
AiEsp32RotaryEncoder *encoder; | ||
|
||
public: | ||
AiEsp32RotaryEncoderNumberSelector(AiEsp32RotaryEncoder *encoderInstance = NULL) | ||
{ | ||
encoder = encoderInstance; | ||
} | ||
|
||
void attachEncoder(AiEsp32RotaryEncoder *encoderInstance = NULL) | ||
{ | ||
encoder = encoderInstance; | ||
} | ||
|
||
void setRange(float minValue, float maxValue, float step, bool cycleValues, unsigned int decimals = 0) | ||
{ | ||
this->minValue = minValue; | ||
this->maxValue = maxValue; | ||
this->coeficient = pow(10.0, decimals); | ||
if (maxValue < minValue) | ||
coeficient *= -1.0; | ||
this->step = step * this->coeficient; | ||
|
||
long minEncoderValue = (long)((this->coeficient * this->minValue) / this->step); | ||
long maxEncoderValue = (long)((this->coeficient * this->maxValue) / this->step); | ||
long range = maxEncoderValue - minEncoderValue; | ||
|
||
encoder->setBoundaries(minEncoderValue, maxEncoderValue, cycleValues); | ||
|
||
if (range < 20) | ||
encoder->setAcceleration(0); | ||
else if (range < 60) | ||
encoder->setAcceleration(20); | ||
else if (range < 200) | ||
encoder->setAcceleration(100); | ||
else if (range < 1000) | ||
encoder->setAcceleration(300); | ||
else | ||
encoder->setAcceleration(500); | ||
|
||
#ifdef DEBUG_NUM_SEL | ||
Serial.println(minEncoderValue); | ||
Serial.println(maxEncoderValue); | ||
Serial.println(range); | ||
Serial.println(step); | ||
Serial.println(coeficient); | ||
|
||
#endif | ||
} | ||
|
||
void setValue(float value) | ||
{ | ||
long encoderValue = (long)((coeficient * value) / step); | ||
encoder->setEncoderValue(encoderValue); | ||
} | ||
|
||
float getValue() | ||
{ | ||
float encoderValue = 1.0 * encoder->readEncoder(); | ||
float value = encoderValue * step / coeficient; | ||
|
||
#ifdef DEBUG_NUM_SEL | ||
Serial.print(encoderValue); | ||
Serial.print(" -> "); | ||
Serial.println(value); | ||
#endif | ||
return value; | ||
} | ||
}; |