Skip to content

Commit

Permalink
Added some logging for easier debugging
Browse files Browse the repository at this point in the history
You might need to add this to build flags to see debug output: `-DCORE_DEBUG_LEVEL=4`.  Set to level 2 for warnings, or 1 for errors.  Level 5 is verbose, but that level isn’t currently used in this library.

See: https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-log.h
  • Loading branch information
MaffooClock committed Oct 31, 2023
1 parent 509eb34 commit f793f73
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ There are other options and methods you can call, but this is just the most basi
> Keep the `onTurned()` and `onPressed()` callbacks lightweight, and definitely _do not_ use any calls to `delay()` here. If you need to do some heavy lifting or use delays, it's better to set a flag here, then check for that flag in your `loop()` and run the appropriate functions from there.
## Debugging
This library makes use of the ESP32-IDF native logging to output some helpful debugging messages to the serial console. To see it, you may have to add a build flag to set the logging level. For PlatformIO, add `-DCORE_DEBUG_LEVEL=4` to the [`build_flags`](https://docs.platformio.org/en/stable/projectconf/sections/env/options/build/build_flags.html) option in [`platformio.ini`](https://docs.platformio.org/en/stable/projectconf/index.html).
After debugging, you can either remove the build flag (if you had to add it), or just reduce the level from debug (4) to info (3), warning (2), or error (1). You can also use verbose (5), but this library does not currently log at that high of a level.
See [`esp32-hal-log.h`](https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-log.h) for more details.
## Compatibility
So far, this has only been tested on an [Arduino Nano ESP32](https://docs.arduino.cc/hardware/nano-esp32). This _should_ work on any ESP32 in Arduino IDE and PlatformIO as long as your framework packages are current.
Expand Down
46 changes: 46 additions & 0 deletions src/ESP32RotaryEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ RotaryEncoder::RotaryEncoder( uint8_t encoderPinA, uint8_t encoderPinB, int8_t e
this->encoderPinButton = encoderPinButton;
this->encoderPinVcc = encoderPinVcc;
this->encoderTripPoint = encoderSteps - 1;

ESP_LOGD( LOG_TAG, "Initialized: A = %i, B = %i, Button = %i, VCC = %i, Steps = %u", encoderPinA, encoderPinB, encoderPinButton, encoderPinVcc, encoderSteps );
}

RotaryEncoder::~RotaryEncoder()
Expand Down Expand Up @@ -35,33 +37,53 @@ void RotaryEncoder::setEncoderType( EncoderType type )
encoderPinMode = INPUT;
buttonPinMode = INPUT_PULLUP;
break;

default:
ESP_LOGE( LOG_TAG, "Invalid encoder type %i", type );
return;
}

ESP_LOGD( LOG_TAG, "Encoder type set to %i", type );
}

void RotaryEncoder::setBoundaries( long minValue, long maxValue, bool circleValues )
{
if( minValue > maxValue )
ESP_LOGW( LOG_TAG, "Minimum value (%i) is greater than maximum value (%i); behavior is undefined.", minValue, maxValue );

setMinValue( minValue );
setMaxValue( maxValue );
setCircular( circleValues );
}

void RotaryEncoder::setMinValue( long minValue )
{
ESP_LOGD( LOG_TAG, "minValue = %i", minValue );

this->minEncoderValue = minValue;
}

void RotaryEncoder::setMaxValue( long maxValue )
{
ESP_LOGD( LOG_TAG, "maxValue = %i", maxValue );

this->maxEncoderValue = maxValue;
}

void RotaryEncoder::setCircular( bool circleValues )
{
ESP_LOGD( LOG_TAG, "Boundaries %s circular", ( circleValues ? "are" : "are not" ) );

this->circleValues = circleValues;
}

void RotaryEncoder::setStepValue( long stepValue )
{
ESP_LOGD( LOG_TAG, "stepValue = %i", stepValue );

if( stepValue > maxEncoderValue || stepValue < minEncoderValue )
ESP_LOGW( LOG_TAG, "Step value (%i) is outside the bounds (%i...%i); behavior is undefined.", stepValue, minEncoderValue, maxEncoderValue );

this->stepValue = stepValue;
}

Expand Down Expand Up @@ -120,13 +142,17 @@ void RotaryEncoder::attachInterrupts()

if( encoderPinButton >= 0 )
attachInterrupt( encoderPinButton, std::bind( &RotaryEncoder::_button_ISR, this ), RISING );

ESP_LOGD( LOG_TAG, "Interrupts attached" );
}

void RotaryEncoder::detachInterrupts()
{
detachInterrupt( encoderPinA );
detachInterrupt( encoderPinB );
detachInterrupt( encoderPinButton );

ESP_LOGD( LOG_TAG, "Interrupts detached" );
}

void RotaryEncoder::begin( bool useTimer )
Expand All @@ -153,6 +179,8 @@ void RotaryEncoder::begin( bool useTimer )

if( useTimer )
beginLoopTimer();

ESP_LOGD( LOG_TAG, "RotaryEncoder active" );
}

bool RotaryEncoder::isEnabled()
Expand All @@ -168,6 +196,8 @@ void RotaryEncoder::enable()
attachInterrupts();

_isEnabled = true;

ESP_LOGD( LOG_TAG, "Input enabled" );
}

void RotaryEncoder::disable()
Expand All @@ -178,13 +208,18 @@ void RotaryEncoder::disable()
detachInterrupts();

_isEnabled = false;

ESP_LOGD( LOG_TAG, "Input disabled" );
}

bool RotaryEncoder::buttonPressed()
{
if( !_isEnabled )
return false;

if( buttonPressedFlag )
ESP_LOGD( LOG_TAG, "Button pressed" );

bool wasPressed = buttonPressedFlag;

buttonPressedFlag = false;
Expand All @@ -197,6 +232,9 @@ bool RotaryEncoder::encoderChanged()
if( !_isEnabled )
return false;

if( encoderChangedFlag )
ESP_LOGD( LOG_TAG, "Knob turned; value: %i", getEncoderValue() );

bool hasChanged = encoderChangedFlag;

encoderChangedFlag = false;
Expand All @@ -213,15 +251,23 @@ long RotaryEncoder::getEncoderValue()

void RotaryEncoder::constrainValue()
{
long unconstrainedValue = currentValue;

if( currentValue < minEncoderValue )
currentValue = circleValues ? maxEncoderValue : minEncoderValue;

else if( currentValue > maxEncoderValue )
currentValue = circleValues ? minEncoderValue : maxEncoderValue;

if( unconstrainedValue != currentValue )
ESP_LOGD( LOG_TAG, "Encoder value '%i' constrained to '%i'", unconstrainedValue, currentValue );
}

void RotaryEncoder::setEncoderValue( long newValue )
{
if( newValue != currentValue )
ESP_LOGD( LOG_TAG, "Overriding encoder value from '%i' to '%i'", currentValue, newValue );

currentValue = newValue;

constrainValue();
Expand Down
2 changes: 2 additions & 0 deletions src/ESP32RotaryEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ class RotaryEncoder {

private:

const char *LOG_TAG = "ESP32RotaryEncoder";

EncoderCallback callbackEncoderChanged = NULL;
ButtonCallback callbackButtonPressed = NULL;

Expand Down

0 comments on commit f793f73

Please sign in to comment.