From 8e6c9840327869f9f57710b6d4b8dbeec103a461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20Gy=C3=B6rffy?= Date: Tue, 12 Sep 2017 09:26:53 +0200 Subject: [PATCH] Port to Ruby --- rotary_encoder.rb | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 rotary_encoder.rb diff --git a/rotary_encoder.rb b/rotary_encoder.rb new file mode 100644 index 0000000..60ef57b --- /dev/null +++ b/rotary_encoder.rb @@ -0,0 +1,39 @@ +require 'rpi_gpio' + +RPi::GPIO.set_numbering :bcm + +CLOCK = 17 +DATA = 18 + +RPi::GPIO.setup(CLOCK, as: :input) +RPi::GPIO.setup(DATA, as: :input) + +def dtState + RPi::GPIO.high?(DATA) ? 1 : 0 +end + +def clock_state + RPi::GPIO.high?(CLOCK) ? 1 : 0 +end + +counter = 0 +clock_last_state = clock_state + +loop do + if clock_state != clock_last_state + if dtState != clock_state + counter += 1 + else + counter -= 1 + end + + puts counter + end + + clock_last_state = clock_state + sleep 0.01 +end + +at_exit do + RPi::GPIO.clean_up +end