Skip to content

add support for different chip detection + quad rotary demo #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions adafruit_seesaw/seesaw.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ def const(x):
_TOUCH_CHANNEL_OFFSET = const(0x10)

_SAMD09_HW_ID_CODE = const(0x55)
_ATTINY8X7_HW_ID_CODE = const(0x87)
_ATTINY806_HW_ID_CODE = const(0x84)
_ATTINY807_HW_ID_CODE = const(0x85)
_ATTINY816_HW_ID_CODE = const(0x86)
_ATTINY817_HW_ID_CODE = const(0x87)
_ATTINY1616_HW_ID_CODE = const(0x88)
_ATTINY1617_HW_ID_CODE = const(0x89)
_EEPROM_I2C_ADDR = const(0x3F)

_ENCODER_STATUS = const(0x00)
Expand Down Expand Up @@ -145,13 +150,13 @@ def __init__(self, i2c_bus, addr=0x49, drdy=None, reset=True):
self.sw_reset()

self.chip_id = self.read8(_STATUS_BASE, _STATUS_HW_ID)

if self.chip_id not in (_ATTINY8X7_HW_ID_CODE, _SAMD09_HW_ID_CODE):
if self.chip_id not in (_ATTINY806_HW_ID_CODE,_ATTINY807_HW_ID_CODE,
_ATTINY816_HW_ID_CODE,_ATTINY817_HW_ID_CODE,
_ATTINY1616_HW_ID_CODE,_ATTINY1617_HW_ID_CODE,
_SAMD09_HW_ID_CODE):
raise RuntimeError(
"Seesaw hardware ID returned (0x{:x}) is not "
"correct! Expected 0x{:x} or 0x{:x}. Please check your wiring.".format(
self.chip_id, _SAMD09_HW_ID_CODE, _ATTINY8X7_HW_ID_CODE
)
"correct! Please check your wiring.".format(self.chip_id,)
)

pid = self.get_version() >> 16
Expand All @@ -164,15 +169,20 @@ def __init__(self, i2c_bus, addr=0x49, drdy=None, reset=True):
from adafruit_seesaw.robohat import MM1_Pinmap

self.pin_mapping = MM1_Pinmap
elif pid in (_5690_PID, _5681_PID, _5743_PID):
elif (pid in (_5690_PID, _5681_PID, _5743_PID)) or (self.chip_id in
(_ATTINY817_HW_ID_CODE,
_ATTINY807_HW_ID_CODE,
_ATTINY1617_HW_ID_CODE)):
from adafruit_seesaw.attinyx16 import ATtinyx16_Pinmap

self.pin_mapping = ATtinyx16_Pinmap
elif self.chip_id == _SAMD09_HW_ID_CODE:
from adafruit_seesaw.samd09 import SAMD09_Pinmap

self.pin_mapping = SAMD09_Pinmap
elif self.chip_id == _ATTINY8X7_HW_ID_CODE:
elif self.chip_id in (_ATTINY817_HW_ID_CODE,
_ATTINY807_HW_ID_CODE,
_ATTINY1617_HW_ID_CODE):
from adafruit_seesaw.attiny8x7 import ATtiny8x7_Pinmap

self.pin_mapping = ATtiny8x7_Pinmap
Expand Down
54 changes: 54 additions & 0 deletions examples/seesaw_quadrotary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# SPDX-FileCopyrightText: 2023 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""I2C rotary encoder NeoPixel color picker and brightness setting example."""
import board
from rainbowio import colorwheel
import digitalio
import adafruit_seesaw.seesaw
import adafruit_seesaw.neopixel
import adafruit_seesaw.rotaryio
import adafruit_seesaw.digitalio

# For use with the STEMMA connector on QT Py RP2040
# import busio
# i2c = busio.I2C(board.SCL1, board.SDA1)
# seesaw = seesaw.Seesaw(i2c, 0x49)

i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
seesaw = adafruit_seesaw.seesaw.Seesaw(i2c, 0x49)

encoders = [adafruit_seesaw.rotaryio.IncrementalEncoder(seesaw, n) for n in range(4)]
switches = [adafruit_seesaw.digitalio.DigitalIO(seesaw, pin) for pin in (12, 14, 17, 9)]
for switch in switches:
switch.switch_to_input(digitalio.Pull.UP) # input & pullup!

# four neopixels per 'stick'
pixels = adafruit_seesaw.neopixel.NeoPixel(seesaw, 18, 4)
pixels.brightness = 0.5

last_positions = [-1, -1, -1, -1]
colors = [0, 0, 0, 0] # start at red

while True:
# negate the position to make clockwise rotation positive
positions = [encoder.position for encoder in encoders]

for n, rotary_pos in enumerate(positions):
if rotary_pos != last_positions[n]:
print(f"Rotary #{n}: {rotary_pos}")
last_positions[n] = rotary_pos

if switches[n].value: # Change the LED color if switch is not pressed
if rotary_pos > last_positions[n]: # Advance forward through the colorwheel.
colors[n] += 8
else:
colors[n] -= 8 # Advance backward through the colorwheel.
colors[n] = (colors[n] + 256) % 256 # wrap around to 0-256

# if switch is pressed, light up white, otherwise use the stored color
if not switches[n].value:
pixels[n] = 0xFFFFFF
else:
pixels[n] = colorwheel(colors[n])