\ ********************************************************************* 
\ using the KY-040 Rotary Encoder 
\    Filename:      KY040encoder.txt 
\    Date:          05 jan 2022 
\    Updated:       07 jan 2022 
\    File Version:  1.0 
\    MCU:           ESP32-WROOM-32 
\    Forth:         ESP32forth v 7.05 
\    Copyright:     Marc PETREMANN 
\    Author:        Marc PETREMANN 
\    GNU General Public License 
\ ********************************************************************* 
 
\ alternative solution for "marker", (marker not defined in ESP32forth) 
DEFINED? --ky040 [if] forget --ky040  [then] 
create --ky040 
 
 
interrupts 
 
\ enable interrupt on GPIO G15 
: intG15enable ( -- ) 
    \ Falling Edge interrupt 
    15 GPIO_INTR_NEGEDGE gpio_set_intr_type drop 
  ; 
 
\ disable interrupt on GPIO G15 
: intG15disable ( -- ) 
    15 GPIO_INTR_DISABLE gpio_set_intr_type drop 
  ; 
 
: pinsInit ( -- ) 
    04 input pinmode            \ GO4 as an input 
    04 gpio_pulldown_en drop    \ Enable pull-down on GPIO 04 
    15 input pinmode            \ G15 as an input 
    15 gpio_pulldown_en drop    \ Enable pull-down on GPIO 15 
    intG15enable 
  ; 
 
\ *** use test only for testing encoder *** 
: test 
    cr ." PIN: " 
    cr ."  - G15: " 15 digitalRead . 
    cr ."  - G04: " 04 digitalRead . 
  ; 
 
pinsInit 
' test 15 pinchange 
\ *** end test **************************** 
 
 
0 value KYvar   \ content is incremented or decremented 
 
\ increment content of KYvar 
: incKYvar ( n -- ) 
    1 +to KYvar 
  ; 
 
\ decrement content of KYvar 
: decKYvar ( n -- ) 
    -1 +to KYvar 
  ; 
 
\ used by interruption when G15 activated 
: testIncDec ( -- ) 
    intG15disable 
    15 digitalRead 0= if 
        04 digitalRead  if  
            incKYvar 
        else    
            decKYvar 
        then 
        cr ." KYvar: " KYvar .  
    then 
    1000 0 do loop  \ small wait loop 
    intG15enable 
  ; 
 
 
pinsInit 
' testIncDec 15 pinchange 
 

Legal: site web personnel sans commerce / personal site without seling