Skip to content

Commit

Permalink
Merge pull request #37 from cpu20/master
Browse files Browse the repository at this point in the history
Adding callback functionality to the rotary encoder
  • Loading branch information
guyc authored Jul 17, 2018
2 parents 6589ad6 + 61f075e commit 14732a7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
13 changes: 12 additions & 1 deletion gaugette/rotary_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RotaryEncoder:
# Pass the wiring pin numbers here. See:
# https://projects.drogon.net/raspberry-pi/wiringpi2/pins/
#----------------------------------------------------------------------
def __init__(self, gpio, a_pin, b_pin):
def __init__(self, gpio, a_pin, b_pin, callback=None):
self.gpio = gpio
self.a_pin = a_pin
self.b_pin = b_pin
Expand All @@ -46,6 +46,12 @@ def __init__(self, gpio, a_pin, b_pin):
self.last_delta = 0
self.r_seq = self.rotation_sequence()

# Callback function gets called when a rotation is detected
# Function format should be:
# FuncName(x) where x is 1 or -1 depending on the detected rotation
# direction
self.callback = callback

# steps_per_cycle and self.remainder are only used in get_cycles which
# returns a coarse-granularity step count. By default
# steps_per_cycle is 4 as there are 4 steps per
Expand Down Expand Up @@ -94,6 +100,11 @@ def update(self):
self.last_delta = delta
self.r_seq = r_seq
self.steps += delta
if(self.callback is not None):
cycles = self.get_cycles()
if(cycles != 0):
self.callback(cycles)


def get_steps(self):
steps = self.steps
Expand Down
5 changes: 5 additions & 0 deletions gaugette/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ def __init__(self, gpio, pin, pull_up=True):
pull_up_mode = gpio.PUD_UP if pull_up else gpio.PUD_DOWN
self.gpio.setup(self.pin, self.gpio.IN, pull_up_mode)

# edge = gpio.EDGE_FALLING or gpio.EDGE_RISING
# isr = interrupt service routine
def enable_isr(self, edge, isr):
self.gpio.trigger(self.pin, edge, isr)

def get_state(self):
state = self.gpio.input(self.pin)
if self.pull_up:
Expand Down
21 changes: 21 additions & 0 deletions samples/rotary_callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Sample code for both the RotaryEncoder class and the Switch class.
# The common pin for the encoder should be wired to ground.
# The sw_pin should be shorted to ground by the switch.

import time
import gaugette.gpio
import gaugette.rotary_encoder

# Rotary encoder pins
A_PIN = 23
B_PIN = 22

def rotated(direction):
print("Rotated ", direction)

gpio = gaugette.gpio.GPIO()
encoder = gaugette.rotary_encoder.RotaryEncoder(gpio, A_PIN, B_PIN, rotated)
encoder.start()

while True:
time.sleep(100)
20 changes: 20 additions & 0 deletions samples/switch_isr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Sample code for both the RotaryEncoder class and the Switch class.
# The common pin for the encoder should be wired to ground.
# The sw_pin should be shorted to ground by the switch.

import time
import gaugette.gpio
import gaugette.switch

SW_PIN = 21

def pushed():
print("Switch pushed!")

gpio = gaugette.gpio.GPIO()
sw = gaugette.switch.Switch(gpio, SW_PIN)
# Calls pushed() on a falling edge
sw.enable_isr(gpio.EDGE_FALLING, pushed)

while True:
time.sleep(100)

0 comments on commit 14732a7

Please sign in to comment.