2727#include " Brewpi.h"
2828#include " TempControl.h"
2929
30+ #if ESP32
31+ #include " rom/gpio.h"
32+ #endif
33+
3034#if ButtonViaPCF8574
3135#include < pcf8574_esp.h>
3236#endif
@@ -573,8 +577,9 @@ int16_t RotaryEncoder::read(void){
573577#define HS_R_START_M 0x3
574578#define HS_R_CW_BEGIN_M 0x4
575579#define HS_R_CCW_BEGIN_M 0x5
576- #if ESP32
577- const uint8_t hs_ttable[7 ][4 ] = {
580+ #if ESP32
581+ // this table is accessed in ISR, needed to stay in RAM
582+ uint8_t hs_ttable[7 ][4 ] = {
578583#else
579584const uint8_t PROGMEM hs_ttable[7 ][4 ] = {
580585#endif
@@ -600,8 +605,9 @@ const uint8_t PROGMEM hs_ttable[7][4] = {
600605#define R_CCW_BEGIN 0x4
601606#define R_CCW_FINAL 0x5
602607#define R_CCW_NEXT 0x6
603- #if ESP32
604- const uint8_t ttable[7 ][4 ] = {
608+ #if ESP32
609+ // this table is accessed in ISR, needed to stay in RAM
610+ uint8_t ttable[7 ][4 ] = {
605611#else
606612const uint8_t PROGMEM ttable[7 ][4 ] = {
607613#endif
@@ -629,14 +635,22 @@ const uint8_t PROGMEM ttable[7][4] = {
629635
630636#ifdef ESP32
631637
632- ICACHE_RAM_ATTR static void isr_rotary (void ) {
638+ #define GPIO_READ (pin ) (((pin)>31 )? (gpio_input_get_high() & (1 <<(pin-32 ))):(gpio_input_get() & (1 <<pin)))
639+
640+ static void IRAM_ATTR isr_rotary (void ) {
633641 rotaryEncoder.process ();
634642}
635643
636- ICACHE_RAM_ATTR static void isr_push ( void ) {
644+ # define MINIMUM_PUSH_GAP 200000
637645
638- if (! digitalRead (rotarySwitchPin))
646+ static void IRAM_ATTR isr_push (void ) {
647+ // using software debouncing.
648+ static uint32_t pushed_time=0 ;
649+ uint32_t now= micros ();
650+ if (! GPIO_READ (rotarySwitchPin) && (now - pushed_time) > MINIMUM_PUSH_GAP ){
651+ pushed_time = now;
639652 rotaryEncoder.setPushed ();
653+ }
640654}
641655
642656#else // #ifdef ESP32
@@ -677,14 +691,19 @@ ISR(PCINT0_vect){
677691#endif // #ifdef ESP8266
678692
679693
680- ICACHE_RAM_ATTR void RotaryEncoder::process (void ){
694+ void IRAM_ATTR RotaryEncoder::process (void ){
695+
681696 static uint8_t state=R_START;
682697 // Grab state of input pins.
683698
684699 #if ESP32
700+ /* uint8_t currPinA = !digitalRead(rotaryAPin);
701+ uint8_t currPinB = !digitalRead(rotaryBPin); */
702+
703+ // uint32_t input=gpio_input_get();
685704
686- uint8_t currPinA = !digitalRead (rotaryAPin);
687- uint8_t currPinB = !digitalRead (rotaryBPin);
705+ uint8_t currPinA = ! GPIO_READ (rotaryAPin);
706+ uint8_t currPinB = ! GPIO_READ (rotaryBPin);
688707
689708 #else // #ifdef ESP32
690709 #if BREWPI_STATIC_CONFIG == BREWPI_SHIELD_DIY
@@ -734,7 +753,7 @@ ICACHE_RAM_ATTR void RotaryEncoder::process(void){
734753}
735754#endif // BREWPI_ROTARY_ENCODER
736755
737- ICACHE_RAM_ATTR void RotaryEncoder::setPushed (void ){
756+ void IRAM_ATTR RotaryEncoder::setPushed (void ){
738757 pushFlag = true ;
739758
740759 // this goes too deep, and needed to put in ICACHE, also it is processed in outer loop
@@ -752,7 +771,7 @@ void RotaryEncoder::init(void){
752771 fastPinMode (rotarySwitchPin, BREWPI_INPUT_PULLUP);
753772 attachInterrupt (rotaryAPin, isr_rotary, CHANGE);
754773 attachInterrupt (rotaryBPin, isr_rotary, CHANGE);
755- attachInterrupt (rotarySwitchPin, isr_push, CHANGE );
774+ attachInterrupt (rotarySwitchPin, isr_push, FALLING );
756775#else // #ifdef ESP32
757776 #define BREWPI_INPUT_PULLUP (USE_INTERNAL_PULL_UP_RESISTORS ? INPUT_PULLUP : INPUT)
758777 fastPinMode (rotaryAPin, BREWPI_INPUT_PULLUP);
0 commit comments