Skip to content

Commit 5e53ccb

Browse files
authored
Merge pull request #61 from jfurcean/add-basic-rotary
Add rotary encoder support
2 parents 14c0128 + 902665d commit 5e53ccb

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

adafruit_seesaw/seesaw.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def const(x):
6161
_EEPROM_BASE = const(0x0D)
6262
_NEOPIXEL_BASE = const(0x0E)
6363
_TOUCH_BASE = const(0x0F)
64+
_ENCODER_BASE = const(0x11)
6465

6566
_GPIO_DIRSET_BULK = const(0x02)
6667
_GPIO_DIRCLR_BULK = const(0x03)
@@ -109,6 +110,12 @@ def const(x):
109110
_HW_ID_CODE = const(0x55)
110111
_EEPROM_I2C_ADDR = const(0x3F)
111112

113+
_ENCODER_STATUS = const(0x00)
114+
_ENCODER_INTENSET = const(0x10)
115+
_ENCODER_INTENCLR = const(0x20)
116+
_ENCODER_POSITION = const(0x30)
117+
_ENCODER_DELTA = const(0x40)
118+
112119
# TODO: update when we get real PID
113120
_CRICKIT_PID = const(9999)
114121
_ROBOHATMM1_PID = const(9998)
@@ -362,6 +369,31 @@ def set_pwm_freq(self, pin, freq):
362369
else:
363370
raise ValueError("Invalid PWM pin")
364371

372+
def get_encoder_pos(self, encoder=0):
373+
"""Read the current position of the encoder"""
374+
buf = bytearray(4)
375+
self.read(_ENCODER_BASE, _ENCODER_POSITION + encoder, buf)
376+
return struct.unpack(">i", buf)[0]
377+
378+
def set_encoder_pos(self, pos, encoder=0):
379+
"""Set the current position of the encoder"""
380+
cmd = struct.pack(">i", pos)
381+
self.write(_ENCODER_BASE, _ENCODER_POSITION + encoder, cmd)
382+
383+
def get_encoder_delta(self, encoder=0):
384+
"""Read the change in encoder position since it was last read"""
385+
buf = bytearray(4)
386+
self.read(_ENCODER_BASE, _ENCODER_DELTA + encoder, buf)
387+
return struct.unpack(">i", buf)[0]
388+
389+
def enable_encoder_interrupt(self, encoder=0):
390+
"""Enable the interrupt to fire when the encoder changes position"""
391+
self.write8(_ENCODER_BASE, _ENCODER_INTENSET + encoder, 0x01)
392+
393+
def disable_encoder_interrupt(self, encoder=0):
394+
"""Disable the interrupt from firing when the encoder changes"""
395+
self.write8(_ENCODER_BASE, _ENCODER_INTENCLR + encoder, 0x01)
396+
365397
# def enable_sercom_data_rdy_interrupt(self, sercom):
366398
#
367399
# _sercom_inten.DATA_RDY = 1

examples/seesaw_rotary_simpletest.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2021 John Furcean
2+
# SPDX-License-Identifier: MIT
3+
4+
import board
5+
import busio
6+
from adafruit_seesaw.seesaw import Seesaw
7+
from adafruit_seesaw.digitalio import DigitalIO
8+
9+
i2c_bus = busio.I2C(board.SCL, board.SDA)
10+
11+
seesaw = Seesaw(i2c_bus, addr=0x36)
12+
13+
button = DigitalIO(seesaw, 24)
14+
button_held = False
15+
16+
last_pos = seesaw.get_encoder_pos()
17+
18+
while True:
19+
20+
# read position of the rotary encoder
21+
pos = seesaw.get_encoder_pos()
22+
if pos != last_pos:
23+
last_pos = pos
24+
print(f"Position: {pos}")
25+
26+
if not button.value and not button_held:
27+
button_held = True
28+
print("Button pressed")
29+
30+
if button.value and button_held:
31+
button_held = False
32+
print("Button released")

0 commit comments

Comments
 (0)