Permalink
Browse files

Added switch and rotary encoder classes, samples and docs.

  • Loading branch information...
1 parent d94f8f8 commit 8815674a9e64af56b64e257f5d7bf8363165d019 @guyc committed Dec 11, 2012
Showing with 155 additions and 0 deletions.
  1. +29 −0 README.md
  2. +83 −0 gaugette/rotary_encoder.py
  3. +18 −0 gaugette/switch.py
  4. +25 −0 samples/rotary_test.py
View
@@ -74,6 +74,35 @@ Rgb Led Usage
led.fade(100,0,0)
```
+Rotary Encoder Usage
+====================
+
+```python
+ import gaugette.rotary_encoder
+ A_PIN = 7
+ B_PIN = 9
+ encoder = gaugette.rotary_encoder.RotaryEncoder(A_PIN, B_PIN)
+ while True:
+ delta = encoder.delta()
+ if delta!=0:
+ print "rotate %d" % delta
+```
+
+Switch Usage
+====================
+
+```python
+ import gaugette.switch
+ SW_PIN = 8
+ sw = gaugette.switch.Switch(SW_PIN)
+ last_state = sw.state()
+ while True:
+ state = sw.state()
+ if state != last_state:
+ print "switch %d" % state
+ last_state = state
+```
+
Discussion At
=============
View
@@ -0,0 +1,83 @@
+#----------------------------------------------------------------------
+# rotary_encoder.py from https://github.com/guyc/py-gaugette
+# Guy Carpenter, Clearwater Software
+#
+# This is a class for reading quadrature rotary encoders
+# like the PEC11 Series available from Adafruit:
+# http://www.adafruit.com/products/377
+# The datasheet for this encoder is here:
+# http://www.adafruit.com/datasheets/pec11.pdf
+#
+# This library expects the common pin C to be connected
+# to ground. Pins A and B will have their pull-up resistor
+# pulled high.
+#
+# Usage:
+#
+# import gaugette.rotary_encoder
+# A_PIN = 7 # use wiring pin numbers here
+# B_PIN = 9
+# encoder = gaugette.rotary_encoder.RotaryEncoder(A_PIN, B_PIN)
+# while 1:
+# delta = encoder.delta() # returns 0,1,or -1
+# if delta!=0:
+# print delta
+
+import wiringpi
+import math
+
+class RotaryEncoder:
+
+ # Turning the encoder clockwise generates these
+ # r_state values (binary): 00 01 11 10
+ # STATE_TABLE maps these values back to sequence values [0,1,2,3]
+ # Coincidentally the transformation table contains exactly the
+ # r_state values, but it is just a coincidence that the transform
+ # is symmatrical - ie
+ # sequence = STATE_TABLE[r_state]
+ # and
+ # r_state = STATE_TABLE[sequence].
+ # lookup table below, where sequence = STATE_TABLE[r_state]
+ STATE_TABLE = [ 0, 1, 3, 2 ]
+
+ #----------------------------------------------------------------------
+ # Pass the wiring pin numbers here. See:
+ # https://projects.drogon.net/raspberry-pi/wiringpi/pins/
+ #----------------------------------------------------------------------
+ def __init__(self, a_pin, b_pin):
+ self.a_pin = a_pin
+ self.b_pin = b_pin
+
+ self.gpio = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_PINS)
+
+ self.gpio.pinMode(self.a_pin, self.gpio.INPUT)
+ self.gpio.pullUpDnControl(self.a_pin, self.gpio.PUD_UP)
+
+ self.gpio.pinMode(self.b_pin, self.gpio.INPUT)
+ self.gpio.pullUpDnControl(self.b_pin, self.gpio.PUD_UP)
+
+ self.last_delta = 0
+ self.r_state = self.rotation_state()
+
+ # Gets the 2-bit rotation state of the current position
+ def rotation_state(self):
+ a_state = self.gpio.digitalRead(self.a_pin)
+ b_state = self.gpio.digitalRead(self.b_pin)
+ r_state = a_state | b_state << 1
+ return r_state
+
+ # Returns offset values of -2,-1,0,1,2
+ def delta(self):
+ delta = 0
+ r_state = self.rotation_state()
+ if r_state != self.r_state:
+ delta = (self.STATE_TABLE[r_state] - self.STATE_TABLE[self.r_state] + 4) % 4
+ if delta==3:
+ delta = -1
+ elif delta==2:
+ delta = math.copysign(delta, self.last_delta) # same direction as previous, 2 steps
+
+ self.last_delta = delta
+ self.r_state = r_state
+
+ return delta
View
@@ -0,0 +1,18 @@
+import wiringpi
+
+class Switch:
+
+ def __init__(self, pin):
+ self.pin = pin
+
+ self.gpio = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_PINS)
+ self.gpio.pinMode(self.pin, self.gpio.INPUT)
+ self.gpio.pullUpDnControl(self.pin, self.gpio.PUD_UP)
+
+ def state(self):
+ state = self.gpio.digitalRead(self.pin)
+ # We are pulling-high and switching
+ # to ground, so state will be 1 when the switch is open, and 0
+ # when it is closed. We invert the value here to a more
+ # conventional representation of 0:open, 1:closed.
+ return 1-state;
View
@@ -0,0 +1,25 @@
+# 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 gaugette.rotary_encoder
+import gaugette.switch
+
+A_PIN = 7
+B_PIN = 9
+SW_PIN = 8
+
+encoder = gaugette.rotary_encoder.RotaryEncoder(A_PIN, B_PIN)
+switch = gaugette.switch.Switch(SW_PIN)
+last_state = None
+
+while 1:
+ delta = encoder.delta()
+ if delta!=0:
+ print "rotate %d" % delta
+
+ sw_state = switch.state()
+ if sw_state != last_state:
+ print "switch %d" % sw_state
+ last_state = sw_state
+

0 comments on commit 8815674

Please sign in to comment.