From 7464361cde5f4891b7fe587d561d25c4d1b849d6 Mon Sep 17 00:00:00 2001 From: Chris Leishman Date: Tue, 14 Oct 2025 08:34:28 -0700 Subject: [PATCH 1/2] Move invariant constants out of class definition Hides the implementation and avoids duplication across instances. --- src/ESP32RotaryEncoder.cpp | 16 ++++++++++++++++ src/ESP32RotaryEncoder.h | 15 --------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/ESP32RotaryEncoder.cpp b/src/ESP32RotaryEncoder.cpp index 1987864..dea6e23 100644 --- a/src/ESP32RotaryEncoder.cpp +++ b/src/ESP32RotaryEncoder.cpp @@ -21,6 +21,22 @@ #endif #endif +static const DRAM_ATTR char *LOG_TAG = "ESP32RotaryEncoder"; + +typedef enum { + LEFT = -1, + STILL = 0, + RIGHT = 1 +} Rotation; + +static const Rotation encoderStates[16] = { + STILL, LEFT, RIGHT, STILL, + RIGHT, STILL, STILL, LEFT, + LEFT, STILL, STILL, RIGHT, + STILL, RIGHT, LEFT, STILL +}; + + RotaryEncoder::RotaryEncoder( uint8_t encoderPinA, uint8_t encoderPinB, int8_t buttonPin, int8_t vccPin, uint8_t encoderSteps ) : encoderPinA{ encoderPinA }, encoderPinB{ encoderPinB }, diff --git a/src/ESP32RotaryEncoder.h b/src/ESP32RotaryEncoder.h index 4ad62dc..8143d89 100644 --- a/src/ESP32RotaryEncoder.h +++ b/src/ESP32RotaryEncoder.h @@ -230,24 +230,9 @@ class RotaryEncoder { void loop(); private: - const char *LOG_TAG = "ESP32RotaryEncoder"; - EncoderCallback callbackEncoderChanged = NULL; ButtonCallback callbackButtonPressed = NULL; - typedef enum { - LEFT = -1, - STILL = 0, - RIGHT = 1 - } Rotation; - - Rotation encoderStates[16] = { - STILL, LEFT, RIGHT, STILL, - RIGHT, STILL, STILL, LEFT, - LEFT, STILL, STILL, RIGHT, - STILL, RIGHT, LEFT, STILL - }; - int encoderPinMode = INPUT; int buttonPinMode = INPUT; From bea5ad43256f0c40497e72b740965f0a86398c0b Mon Sep 17 00:00:00 2001 From: Chris Leishman Date: Tue, 14 Oct 2025 08:46:21 -0700 Subject: [PATCH 2/2] Prefer nullptr over NULL --- src/ESP32RotaryEncoder.cpp | 4 ++-- src/ESP32RotaryEncoder.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ESP32RotaryEncoder.cpp b/src/ESP32RotaryEncoder.cpp index dea6e23..681ac9c 100644 --- a/src/ESP32RotaryEncoder.cpp +++ b/src/ESP32RotaryEncoder.cpp @@ -348,10 +348,10 @@ void RotaryEncoder::setEncoderValue( long newValue ) void ARDUINO_ISR_ATTR RotaryEncoder::loop() { - if( callbackEncoderChanged != NULL && encoderChanged() ) + if( callbackEncoderChanged != nullptr && encoderChanged() ) callbackEncoderChanged( getEncoderValue() ); - if( callbackButtonPressed != NULL && buttonPressed() ) + if( callbackButtonPressed != nullptr && buttonPressed() ) callbackButtonPressed( buttonPressedDuration ); } diff --git a/src/ESP32RotaryEncoder.h b/src/ESP32RotaryEncoder.h index 8143d89..c193426 100644 --- a/src/ESP32RotaryEncoder.h +++ b/src/ESP32RotaryEncoder.h @@ -230,8 +230,8 @@ class RotaryEncoder { void loop(); private: - EncoderCallback callbackEncoderChanged = NULL; - ButtonCallback callbackButtonPressed = NULL; + EncoderCallback callbackEncoderChanged = nullptr; + ButtonCallback callbackButtonPressed = nullptr; int encoderPinMode = INPUT; int buttonPinMode = INPUT;