From 2e5adb569802b3e8f3ad8852073b47c90b10d2dd Mon Sep 17 00:00:00 2001 From: Kevin Harrington Date: Tue, 23 Jun 2020 14:00:29 -0400 Subject: [PATCH] close #22 adding an overflow test --- examples/Encoder/Encoder.ino | 8 ++++---- src/ESP32Encoder.cpp | 12 ++++++------ src/ESP32Encoder.h | 4 +++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/examples/Encoder/Encoder.ino b/examples/Encoder/Encoder.ino index 6bdb1f1..a0ed06f 100644 --- a/examples/Encoder/Encoder.ino +++ b/examples/Encoder/Encoder.ino @@ -13,14 +13,14 @@ void setup(){ Serial.begin(115200); // Enable the weak pull down resistors - ESP32Encoder::useInternalWeakPullResistors=DOWN; + //ESP32Encoder::useInternalWeakPullResistors=DOWN; // Enable the weak pull up resistors - //ESP32Encoder::useInternalWeakPullResistors=UP; + ESP32Encoder::useInternalWeakPullResistors=UP; // Attache pins for use as encoder pins - encoder.attachHalfQuad(12, 13); + encoder.attachHalfQuad(19, 18); // Attache pins for use as encoder pins - encoder2.attachHalfQuad(2, 15); + encoder2.attachHalfQuad(17, 16); // set starting count value after attaching encoder.setCount(37); diff --git a/src/ESP32Encoder.cpp b/src/ESP32Encoder.cpp index 2f6a4d0..1a56828 100644 --- a/src/ESP32Encoder.cpp +++ b/src/ESP32Encoder.cpp @@ -110,8 +110,8 @@ void ESP32Encoder::attach(int a, int b, enum encType et) { r_enc_config.lctrl_mode = PCNT_MODE_KEEP; // Rising A on HIGH B = CW Step r_enc_config.hctrl_mode = PCNT_MODE_REVERSE; // Rising A on LOW B = CCW Step - r_enc_config .counter_h_lim = INT16_MAX; - r_enc_config .counter_l_lim = INT16_MIN ; + r_enc_config .counter_h_lim = _INT16_MAX; + r_enc_config .counter_l_lim = _INT16_MIN ; pcnt_unit_config(&r_enc_config); @@ -129,8 +129,8 @@ void ESP32Encoder::attach(int a, int b, enum encType et) { r_enc_config.lctrl_mode = PCNT_MODE_REVERSE; // prior high mode is now low r_enc_config.hctrl_mode = PCNT_MODE_KEEP; // prior low mode is now high - r_enc_config .counter_h_lim = INT16_MAX; - r_enc_config .counter_l_lim = INT16_MIN ; + r_enc_config .counter_h_lim = _INT16_MAX; + r_enc_config .counter_l_lim = _INT16_MIN ; pcnt_unit_config(&r_enc_config); } else { // make sure channel 1 is not set when not full quad @@ -146,8 +146,8 @@ void ESP32Encoder::attach(int a, int b, enum encType et) { r_enc_config.lctrl_mode = PCNT_MODE_DISABLE; // disabling channel 1 r_enc_config.hctrl_mode = PCNT_MODE_DISABLE; // disabling channel 1 - r_enc_config .counter_h_lim = INT16_MAX; - r_enc_config .counter_l_lim = INT16_MIN ; + r_enc_config .counter_h_lim = _INT16_MAX; + r_enc_config .counter_l_lim = _INT16_MIN ; pcnt_unit_config(&r_enc_config); } diff --git a/src/ESP32Encoder.h b/src/ESP32Encoder.h index 4383446..5ee62e9 100644 --- a/src/ESP32Encoder.h +++ b/src/ESP32Encoder.h @@ -3,6 +3,8 @@ #include #include "driver/pcnt.h" #define MAX_ESP32_ENCODERS PCNT_UNIT_MAX +#define _INT16_MAX 32766 +#define _INT16_MIN -32766 enum encType { single, @@ -45,7 +47,7 @@ class ESP32Encoder { pcnt_unit_t unit; bool fullQuad=false; int countsMode = 2; - volatile int32_t count=0; + volatile int64_t count=0; pcnt_config_t r_enc_config; static enum puType useInternalWeakPullResistors; };