Skip to content

Commit

Permalink
Merge pull request #118 from ladyada/main
Browse files Browse the repository at this point in the history
add support for different chip detection + quad rotary demo
  • Loading branch information
ladyada authored Jun 28, 2023
2 parents d99b564 + 8b20eea commit b317568
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 15 deletions.
47 changes: 32 additions & 15 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,18 @@ 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
)
f"Seesaw hardware ID returned 0x{self.chip_id} is not "
"correct! Please check your wiring."
)

pid = self.get_version() >> 16
Expand All @@ -164,15 +174,22 @@ 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 (_ATTINY816_HW_ID_CODE, _ATTINY806_HW_ID_CODE, _ATTINY1616_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 Expand Up @@ -255,10 +272,10 @@ def analog_read(self, pin, delay=0.008):
if pin not in self.pin_mapping.analog_pins:
raise ValueError("Invalid ADC pin")

if self.chip_id == _ATTINY8X7_HW_ID_CODE:
offset = pin
elif self.chip_id == _SAMD09_HW_ID_CODE:
if self.chip_id == _SAMD09_HW_ID_CODE:
offset = self.pin_mapping.analog_pins.index(pin)
else:
offset = pin

self.read(_ADC_BASE, _ADC_CHANNEL_OFFSET + offset, buf, delay)
ret = struct.unpack(">H", buf)[0]
Expand Down Expand Up @@ -351,10 +368,10 @@ def analog_write(self, pin, value):
if pin not in self.pin_mapping.pwm_pins:
raise ValueError("Invalid PWM pin")

if self.chip_id == _ATTINY8X7_HW_ID_CODE:
offset = pin
elif self.chip_id == _SAMD09_HW_ID_CODE:
if self.chip_id == _SAMD09_HW_ID_CODE:
offset = self.pin_mapping.pwm_pins.index(pin)
else:
offset = pin

if self.pin_mapping.pwm_width == 16:
cmd = bytearray([offset, (value >> 8), value & 0xFF])
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

"""Quad I2C rotary encoder NeoPixel color picker 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 boards/chips that don't handle clock-stretching well, try running I2C at 50KHz
# import busio
# i2c = busio.I2C(board.SCL, board.SDA, frequency=50000)
# For using the built-in STEMMA QT connector on a microcontroller
i2c = board.STEMMA_I2C()
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 PCB
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]
print(positions)
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])

0 comments on commit b317568

Please sign in to comment.