Hi all. I am porting the REBL_UI (DELTA_G) project from Arduino (with LCD) to ESP32 (OLED OLED). The hardware consists of the ESP32, a rotary encoder and an SSD1306 OLED. I don't mind saying that the code is more complex than I am used to.
I have managed (after a LOT of work) to compile and uploaded the code for the REBL_UI project to an ESP32 DevkitC Ver4. It all seems to work correctly, except that the encoder count results are always positive and always 1. I would normally expect results of 1,2,3,4, after 4 right turns, and then 3, 2, 1, -1 after 4 left turns.
I've stared at, and played with, the code for over a week now - but, no joy. I would much appreciate any help given. Here is the relevant portions of the code.
BTW: I tried contacting the author at github, a little over a week now. No reply. I assume, at this time of year, a vacation is at fault.
// The library .H file in it's entirety
#ifndef REBLInterface_h
#define REBLInterface_h
#include "Arduino.h"
#include "pins_arduino.h"
#include "Defines.h"
void initInterface(uint8_t, uint8_t, uint8_t); //, uint8_t);
void initButton(uint8_t);
void pollButton();
void clearButton();
boolean checkButton();
boolean checkButtonLongPress();
void readButton();
void buttonOn();
void buttonOff();
boolean isButtonOn();
void initEncoder(uint8_t, uint8_t); //, uint8_t);
void encoderOn();
void encoderOff();
char checkRotaryEncoder();
void useRotaryEncoderASCII(char&);
boolean peekRotaryEncoder();
boolean isEncoderOn();
// The 2 functions, below, concern the using the results of the rotary encoder.
// comment from author
// call with mult == 0 allows only one step per call no matter how fast the encoder is turning
template<class T>
void useRotaryEncoder(T& var, int mult = 0)
{
if (mult){
var += (checkRotaryEncoder() * mult);
}
else {
int chek = checkRotaryEncoder();
if (chek > 0) var += 1;
if (chek < 0) var -= 1;
}
}
template<class T>
void useRotaryEncoder(T& var, T mini, T maxi, int mult = 0)
{
if (mult){
var += (checkRotaryEncoder() * mult);
}
else {
int chek = checkRotaryEncoder();
if (chek > 0) var += 1;
if (chek < 0) var -= 1;
}
if (var > maxi){
var = mini;
} else if (var < mini) {
var = maxi;
}
}
#endif
Portions of the .cpp file
// These are the portions of the .cpp file having to do with the rotary counter.
// Please note the following:
volatile char encoderCounter; // the variable for the positive or negative count
// on the decoder is declared as a char
volatile byte *bReg; // this and the next variable are used in the
// ISR_encoder_handler() and initEncoder functions
byte bMask; // I DO NOT understand their functionality and have a gut
// feeling that they (when used on the ESP32) acre causing
// or contributing to the problem
void initEncoder(uint8_t pinA, uint8_t pinB) { //, uint8_t pinB) {
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
bMask = digitalPinToBitMask(pinB);
// I ADDED (unsigned char*) for ESP32
*bReg = (unsigned char*) portInputRegister(digitalPinToPort(pinB));
encoderInterrupt = digitalPinToInterrupt(pinA);
}
void encoderOn() {
attachInterrupt(encoderInterrupt, ISR_encoder_handler, FALLING);
encoderCounter = 0;
}
void ISR_encoder_handler() { // Here only a plus result is returned
// and it is always 1
if (*bReg & bMask) {
encoderCounter--;
} else {
encoderCounter++;
}
}
char checkRotaryEncoder() {
if (encoderIsOn) {
cli();
char retval = encoderCounter;
encoderCounter = 0;
sei();
return retval;
} else {
return 0;
}
}