Skip to content

Commit 4ab54d5

Browse files
committed
Move constants and types into class scope
Move constants and types into class scope. Also move mux into private scope. This is an API breaking change. Resolves #20.
1 parent bb82120 commit 4ab54d5

6 files changed

Lines changed: 47 additions & 65 deletions

File tree

examples/BasicRotaryEncoder/BasicRotaryEncoder.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* ESP32RotaryEncoder: BasicRotaryEncoder.ino
3-
*
3+
*
44
* This is a basic example of how to instantiate a single Rotary Encoder.
5-
*
5+
*
66
* Turning the knob will increment/decrement a value between 1 and 10 and
77
* print it to the serial console.
8-
*
8+
*
99
* Pressing the button will output "boop!" to the serial console.
10-
*
10+
*
1111
* Created 3 October 2023
1212
* Updated 1 November 2023
1313
* By Matthew Clark
@@ -42,7 +42,7 @@ void setup()
4242
Serial.begin( 115200 );
4343

4444
// This tells the library that the encoder has its own pull-up resistors
45-
rotaryEncoder.setEncoderType( EncoderType::HAS_PULLUP );
45+
rotaryEncoder.setEncoderType( RotaryEncoder::Type::HAS_PULLUP );
4646

4747
// Range of values to be returned by the encoder: minimum is 1, maximum is 10
4848
// The third argument specifies whether turning past the minimum/maximum will

examples/ButtonPressDuration/ButtonPressDuration.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/**
22
* ESP32RotaryEncoder: ButtonPressDuration.ino
3-
*
3+
*
44
* This example shows how to handle long button-presses differently
55
* from long button-presses
6-
*
6+
*
77
* Turning the knob will increment/decrement a value between 1 and 10 and
88
* print it to the serial console.
9-
*
9+
*
1010
* Pressing the button will output "boop!" to the serial console.
11-
*
11+
*
1212
* Created 1 November 2023
1313
* By Matthew Clark
1414
*/
@@ -63,7 +63,7 @@ void setup()
6363
Serial.begin( 115200 );
6464

6565
// This tells the library that the encoder has its own pull-up resistors
66-
rotaryEncoder.setEncoderType( EncoderType::HAS_PULLUP );
66+
rotaryEncoder.setEncoderType( RotaryEncoder::Type::HAS_PULLUP );
6767

6868
// Range of values to be returned by the encoder: minimum is 1, maximum is 10
6969
// The third argument specifies whether turning past the minimum/maximum will

examples/LeftOrRight/LeftOrRight.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* ESP32RotaryEncoder: LeftOrRight.ino
3-
*
3+
*
44
* This is a simple example of how to track whether the knob was
55
* turned left or right instead of tracking a numeric value
6-
*
6+
*
77
* Created 1 November 2023
88
* By Matthew Clark
99
*/
@@ -83,7 +83,7 @@ void setup()
8383
Serial.begin( 115200 );
8484

8585
// This tells the library that the encoder has its own pull-up resistors
86-
rotaryEncoder.setEncoderType( EncoderType::HAS_PULLUP );
86+
rotaryEncoder.setEncoderType( RotaryEncoder::Type::HAS_PULLUP );
8787

8888
// The encoder will only return -1, 0, or 1, and will not wrap around.
8989
rotaryEncoder.setBoundaries( -1, 1, false );

examples/TwoRotaryEncoders/TwoRotaryEncoders.ino

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
/**
22
* ESP32RotaryEncoder: TwoRotaryEncoders.ino
3-
*
3+
*
44
* This is a basic example of how to instantiate two distinct Rotary Encoders.
5-
*
5+
*
66
* Rotary Encoder #1:
77
* - Turning the knob will increment/decrement a value between 1 and 10,
88
* and print it to the serial console.
9-
*
9+
*
1010
* - Pressing the button will enable/disable Rotary Encoder #2.
11-
*
11+
*
1212
* Rotary Encoder #2:
1313
* - Turning the knob will increment/decrement a value between -100 and 100,
1414
* and print it to the serial console.
15-
*
15+
*
1616
* - Pressing the button will enable/disable Rotary Encoder #1.
17-
*
17+
*
1818
* While a rotary encoder is disabled, turning the knob or pressing the button
1919
* will have no effect.
20-
*
20+
*
2121
* Created 3 October 2023
2222
* Updated 1 November 2023
2323
* By Matthew Clark
@@ -82,7 +82,7 @@ void button2ToggleRE1( unsigned long duration )
8282
void setup_RE1()
8383
{
8484
// This tells the library that the encoder has its own pull-up resistors
85-
rotaryEncoder1.setEncoderType( EncoderType::HAS_PULLUP );
85+
rotaryEncoder1.setEncoderType( RotaryEncoder::Type::HAS_PULLUP );
8686

8787
// Range of values to be returned by the encoder: minimum is 1, maximum is 10
8888
// The third argument specifies whether turning past the minimum/maximum will
@@ -106,7 +106,7 @@ void setup_RE2()
106106
{
107107
// This tells the library that the encoder does not have its own pull-up
108108
// resistors, so the internal pull-up resistors will be enabled
109-
rotaryEncoder2.setEncoderType( EncoderType::FLOATING );
109+
rotaryEncoder2.setEncoderType( RotaryEncoder::Type::FLOATING );
110110

111111
// Range of values to be returned by the encoder: minimum is -100, maximum is 100
112112
// The third argument specifies whether turning past the minimum/maximum will wrap

src/ESP32RotaryEncoder.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ RotaryEncoder::~RotaryEncoder()
3939
esp_timer_delete( loopTimer );
4040
}
4141

42-
void RotaryEncoder::setEncoderType( EncoderType type )
42+
void RotaryEncoder::setEncoderType( Type type )
4343
{
4444
switch( type )
4545
{
@@ -127,12 +127,6 @@ void RotaryEncoder::onPressed( ButtonCallback f )
127127
callbackButtonPressed = f;
128128
}
129129

130-
static void timerCallback( void *arg )
131-
{
132-
RotaryEncoder *instance = (RotaryEncoder *)arg;
133-
instance->loop();
134-
}
135-
136130
void RotaryEncoder::beginLoopTimer()
137131
{
138132
/**
@@ -159,6 +153,12 @@ void RotaryEncoder::beginLoopTimer()
159153
esp_timer_start_periodic( loopTimer, RE_LOOP_INTERVAL );
160154
}
161155

156+
void RotaryEncoder::timerCallback( void *self )
157+
{
158+
RotaryEncoder *instance = (RotaryEncoder *)self;
159+
instance->loop();
160+
}
161+
162162
void RotaryEncoder::attachInterrupts()
163163
{
164164
#if defined( BOARD_HAS_PIN_REMAP ) && ( ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(3,0,0) )
@@ -332,10 +332,10 @@ void RotaryEncoder::setEncoderValue( long newValue )
332332

333333
void ARDUINO_ISR_ATTR RotaryEncoder::loop()
334334
{
335-
if( callbackEncoderChanged != NULL && encoderChanged() )
335+
if( callbackEncoderChanged != nullptr && encoderChanged() )
336336
callbackEncoderChanged( getEncoderValue() );
337337

338-
if( callbackButtonPressed != NULL && buttonPressed() )
338+
if( callbackButtonPressed != nullptr && buttonPressed() )
339339
callbackButtonPressed( buttonPressedDuration );
340340
}
341341

src/ESP32RotaryEncoder.h

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@
1515

1616
#include <atomic>
1717

18-
static constexpr int8_t RE_DEFAULT_PIN = -1;
19-
static constexpr uint8_t RE_DEFAULT_STEPS = 4;
20-
static constexpr uint64_t RE_LOOP_INTERVAL = 100000U; // 0.1 seconds
21-
22-
typedef enum {
23-
FLOATING,
24-
HAS_PULLUP,
25-
SW_FLOAT
26-
} EncoderType;
27-
2818
class RotaryEncoder {
19+
public:
20+
static constexpr int8_t RE_DEFAULT_PIN = -1;
21+
static constexpr uint8_t RE_DEFAULT_STEPS = 4;
22+
static constexpr uint64_t RE_LOOP_INTERVAL = 100000U; // 0.1 seconds
2923

30-
protected:
31-
mutable portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
24+
typedef enum {
25+
FLOATING,
26+
HAS_PULLUP,
27+
SW_FLOAT
28+
} Type;
3229

3330
#if defined( ESP32 )
3431
typedef std::function<void(long)> EncoderCallback;
@@ -38,9 +35,6 @@ class RotaryEncoder {
3835
typedef void (*ButtonCallback)(unsigned long);
3936
#endif
4037

41-
42-
public:
43-
4438
/**
4539
* @brief Construct a new Rotary Encoder instance
4640
*
@@ -60,7 +54,6 @@ class RotaryEncoder {
6054

6155
/**
6256
* @brief Responsible for detaching interrupts and clearing the loop timer
63-
*
6457
*/
6558
~RotaryEncoder();
6659

@@ -73,7 +66,7 @@ class RotaryEncoder {
7366
* HAS_PULLUP if your encoder is a module that has pull-up resistors, (internal pull-ups will not be used);
7467
* SW_FLOAT your encoder is a module that has pull-up resistors, but the resistor for the switch is missing (internal pull-up will be used for switch input only)
7568
*/
76-
void setEncoderType( EncoderType type );
69+
void setEncoderType( Type type );
7770

7871
/**
7972
* @brief Set the minimum and maximum values that the encoder will return.
@@ -147,21 +140,18 @@ class RotaryEncoder {
147140
* @brief Sets up the GPIO pins specified in the constructor and attaches the ISR callback for the encoder.
148141
*
149142
* @note Call this in `setup()` after other "set" methods.
150-
*
151143
*/
152144
void begin( bool useTimer = true );
153145

154146
/**
155147
* @brief Enables the encoder knob and pushbutton if `disable()` was previously used.
156-
*
157148
*/
158149
void enable();
159150

160151
/**
161152
* @brief Disables the encoder knob and pushbutton.
162153
*
163154
* Knob rotation and button presses will have no effect until after `enable()` is called
164-
*
165155
*/
166156
void disable();
167157

@@ -214,26 +204,16 @@ class RotaryEncoder {
214204
* @note This will try to set the value to 0, but if the minimum and maximum configured
215205
* by `setBoundaries()` does not include 0, then the minimum or maximum will be
216206
* used instead
217-
*
218207
*/
219208
void resetEncoderValue() { setEncoderValue( 0 ); }
220209

221-
/**
222-
* @brief Synchronizes the encoder value and button state from ISRs.
223-
*
224-
* Runs on a timer and calls `encoderChanged()` and `buttonPressed()` to determine
225-
* if user-specified callbacks should be run.
226-
*
227-
* This would normally be called in userspace `loop()`, but we're using the `loopTimer` instead.
228-
*
229-
*/
230-
void loop();
231-
232210
private:
233211
const char *LOG_TAG = "ESP32RotaryEncoder";
234212

235-
EncoderCallback callbackEncoderChanged = NULL;
236-
ButtonCallback callbackButtonPressed = NULL;
213+
mutable portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
214+
215+
EncoderCallback callbackEncoderChanged = nullptr;
216+
ButtonCallback callbackButtonPressed = nullptr;
237217

238218
typedef enum {
239219
LEFT = -1,
@@ -279,6 +259,8 @@ class RotaryEncoder {
279259

280260
esp_timer_handle_t loopTimer;
281261
void beginLoopTimer();
262+
static void timerCallback( void *self );
263+
void loop();
282264

283265
void attachInterrupts();
284266
void detachInterrupts();

0 commit comments

Comments
 (0)