Skip to content

Commit b317568

Browse files
authored
Merge pull request #118 from ladyada/main
add support for different chip detection + quad rotary demo
2 parents d99b564 + 8b20eea commit b317568

File tree

2 files changed

+86
-15
lines changed

2 files changed

+86
-15
lines changed

adafruit_seesaw/seesaw.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ def const(x):
105105
_TOUCH_CHANNEL_OFFSET = const(0x10)
106106

107107
_SAMD09_HW_ID_CODE = const(0x55)
108-
_ATTINY8X7_HW_ID_CODE = const(0x87)
108+
_ATTINY806_HW_ID_CODE = const(0x84)
109+
_ATTINY807_HW_ID_CODE = const(0x85)
110+
_ATTINY816_HW_ID_CODE = const(0x86)
111+
_ATTINY817_HW_ID_CODE = const(0x87)
112+
_ATTINY1616_HW_ID_CODE = const(0x88)
113+
_ATTINY1617_HW_ID_CODE = const(0x89)
109114
_EEPROM_I2C_ADDR = const(0x3F)
110115

111116
_ENCODER_STATUS = const(0x00)
@@ -145,13 +150,18 @@ def __init__(self, i2c_bus, addr=0x49, drdy=None, reset=True):
145150
self.sw_reset()
146151

147152
self.chip_id = self.read8(_STATUS_BASE, _STATUS_HW_ID)
148-
149-
if self.chip_id not in (_ATTINY8X7_HW_ID_CODE, _SAMD09_HW_ID_CODE):
153+
if self.chip_id not in (
154+
_ATTINY806_HW_ID_CODE,
155+
_ATTINY807_HW_ID_CODE,
156+
_ATTINY816_HW_ID_CODE,
157+
_ATTINY817_HW_ID_CODE,
158+
_ATTINY1616_HW_ID_CODE,
159+
_ATTINY1617_HW_ID_CODE,
160+
_SAMD09_HW_ID_CODE,
161+
):
150162
raise RuntimeError(
151-
"Seesaw hardware ID returned (0x{:x}) is not "
152-
"correct! Expected 0x{:x} or 0x{:x}. Please check your wiring.".format(
153-
self.chip_id, _SAMD09_HW_ID_CODE, _ATTINY8X7_HW_ID_CODE
154-
)
163+
f"Seesaw hardware ID returned 0x{self.chip_id} is not "
164+
"correct! Please check your wiring."
155165
)
156166

157167
pid = self.get_version() >> 16
@@ -164,15 +174,22 @@ def __init__(self, i2c_bus, addr=0x49, drdy=None, reset=True):
164174
from adafruit_seesaw.robohat import MM1_Pinmap
165175

166176
self.pin_mapping = MM1_Pinmap
167-
elif pid in (_5690_PID, _5681_PID, _5743_PID):
177+
elif (pid in (_5690_PID, _5681_PID, _5743_PID)) or (
178+
self.chip_id
179+
in (_ATTINY816_HW_ID_CODE, _ATTINY806_HW_ID_CODE, _ATTINY1616_HW_ID_CODE)
180+
):
168181
from adafruit_seesaw.attinyx16 import ATtinyx16_Pinmap
169182

170183
self.pin_mapping = ATtinyx16_Pinmap
171184
elif self.chip_id == _SAMD09_HW_ID_CODE:
172185
from adafruit_seesaw.samd09 import SAMD09_Pinmap
173186

174187
self.pin_mapping = SAMD09_Pinmap
175-
elif self.chip_id == _ATTINY8X7_HW_ID_CODE:
188+
elif self.chip_id in (
189+
_ATTINY817_HW_ID_CODE,
190+
_ATTINY807_HW_ID_CODE,
191+
_ATTINY1617_HW_ID_CODE,
192+
):
176193
from adafruit_seesaw.attiny8x7 import ATtiny8x7_Pinmap
177194

178195
self.pin_mapping = ATtiny8x7_Pinmap
@@ -255,10 +272,10 @@ def analog_read(self, pin, delay=0.008):
255272
if pin not in self.pin_mapping.analog_pins:
256273
raise ValueError("Invalid ADC pin")
257274

258-
if self.chip_id == _ATTINY8X7_HW_ID_CODE:
259-
offset = pin
260-
elif self.chip_id == _SAMD09_HW_ID_CODE:
275+
if self.chip_id == _SAMD09_HW_ID_CODE:
261276
offset = self.pin_mapping.analog_pins.index(pin)
277+
else:
278+
offset = pin
262279

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

354-
if self.chip_id == _ATTINY8X7_HW_ID_CODE:
355-
offset = pin
356-
elif self.chip_id == _SAMD09_HW_ID_CODE:
371+
if self.chip_id == _SAMD09_HW_ID_CODE:
357372
offset = self.pin_mapping.pwm_pins.index(pin)
373+
else:
374+
offset = pin
358375

359376
if self.pin_mapping.pwm_width == 16:
360377
cmd = bytearray([offset, (value >> 8), value & 0xFF])

examples/seesaw_quadrotary.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# SPDX-FileCopyrightText: 2023 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""Quad I2C rotary encoder NeoPixel color picker example."""
5+
import board
6+
from rainbowio import colorwheel
7+
import digitalio
8+
import adafruit_seesaw.seesaw
9+
import adafruit_seesaw.neopixel
10+
import adafruit_seesaw.rotaryio
11+
import adafruit_seesaw.digitalio
12+
13+
# For boards/chips that don't handle clock-stretching well, try running I2C at 50KHz
14+
# import busio
15+
# i2c = busio.I2C(board.SCL, board.SDA, frequency=50000)
16+
# For using the built-in STEMMA QT connector on a microcontroller
17+
i2c = board.STEMMA_I2C()
18+
seesaw = adafruit_seesaw.seesaw.Seesaw(i2c, 0x49)
19+
20+
encoders = [adafruit_seesaw.rotaryio.IncrementalEncoder(seesaw, n) for n in range(4)]
21+
switches = [adafruit_seesaw.digitalio.DigitalIO(seesaw, pin) for pin in (12, 14, 17, 9)]
22+
for switch in switches:
23+
switch.switch_to_input(digitalio.Pull.UP) # input & pullup!
24+
25+
# four neopixels per PCB
26+
pixels = adafruit_seesaw.neopixel.NeoPixel(seesaw, 18, 4)
27+
pixels.brightness = 0.5
28+
29+
last_positions = [-1, -1, -1, -1]
30+
colors = [0, 0, 0, 0] # start at red
31+
32+
while True:
33+
# negate the position to make clockwise rotation positive
34+
positions = [encoder.position for encoder in encoders]
35+
print(positions)
36+
for n, rotary_pos in enumerate(positions):
37+
if rotary_pos != last_positions[n]:
38+
print(f"Rotary #{n}: {rotary_pos}")
39+
last_positions[n] = rotary_pos
40+
41+
if switches[n].value: # Change the LED color if switch is not pressed
42+
if (
43+
rotary_pos > last_positions[n]
44+
): # Advance forward through the colorwheel.
45+
colors[n] += 8
46+
else:
47+
colors[n] -= 8 # Advance backward through the colorwheel.
48+
colors[n] = (colors[n] + 256) % 256 # wrap around to 0-256
49+
50+
# if switch is pressed, light up white, otherwise use the stored color
51+
if not switches[n].value:
52+
pixels[n] = 0xFFFFFF
53+
else:
54+
pixels[n] = colorwheel(colors[n])

0 commit comments

Comments
 (0)