Skip to content

Commit 6467edb

Browse files
authored
Merge pull request #64 from jepler/incrementalencoder
Add a convenience class that mimics rotaryio.IncrementalEncoder in the core
2 parents a3da645 + 1294a38 commit 6467edb

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

adafruit_seesaw/rotaryio.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: 2017 Dean Miller for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# pylint: disable=missing-docstring,invalid-name,too-many-public-methods
6+
7+
8+
"""
9+
`adafruit_seesaw.digitalio`
10+
====================================================
11+
"""
12+
13+
__version__ = "0.0.0-auto.0"
14+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_seesaw.git"
15+
16+
17+
class IncrementalEncoder:
18+
"""IncrementalEncoder determines the relative rotational position based
19+
on two series of pulses."""
20+
21+
def __init__(self, seesaw, encoder=0):
22+
"""Create an IncrementalEncoder object associated with the given
23+
eesaw device."""
24+
self._seesaw = seesaw
25+
self._encoder = encoder
26+
27+
@property
28+
def position(self):
29+
"""The current position in terms of pulses. The number of pulses per
30+
rotation is defined by the specific hardware."""
31+
return self._seesaw.encoder_position(self._encoder)
32+
33+
@position.setter
34+
def position(self, value):
35+
self._seesaw.set_encoder_position(value, self._encoder)

examples/seesaw_rotary_simpletest.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@
44
import board
55
from adafruit_seesaw.seesaw import Seesaw
66
from adafruit_seesaw.digitalio import DigitalIO
7+
from adafruit_seesaw.rotaryio import IncrementalEncoder
78

89
i2c_bus = board.I2C()
910

1011
seesaw = Seesaw(i2c_bus, addr=0x36)
1112

13+
seesaw_product = (seesaw.get_version() >> 16) & 0xFFFF
14+
print("Found product {}".format(seesaw_product))
15+
if seesaw_product != 4991:
16+
print("Wrong firmware loaded? Expected 4991")
17+
1218
button = DigitalIO(seesaw, 24)
1319
button_held = False
1420

15-
last_position = seesaw.encoder_position()
21+
encoder = IncrementalEncoder(seesaw)
22+
last_position = None
1623

1724
while True:
1825

1926
# read position of the rotary encoder
20-
position = seesaw.encoder_position()
27+
position = encoder.position
2128
if position != last_position:
2229
last_position = position
2330
print("Position: {}".format(position))

0 commit comments

Comments
 (0)