Skip to content

Commit 87ea110

Browse files
authored
Merge pull request #85 from ladyada/master
Add ATtiny8x7 and examples
2 parents b9259da + cd50bd0 commit 87ea110

File tree

7 files changed

+210
-26
lines changed

7 files changed

+210
-26
lines changed

adafruit_seesaw/attiny8x7.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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,too-few-public-methods
6+
7+
"""
8+
`adafruit_seesaw.attiny8x7` - Pin definition for Adafruit ATtiny8x7 Breakout with seesaw
9+
==================================================================================
10+
"""
11+
12+
__version__ = "0.0.0-auto.0"
13+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_seesaw.git"
14+
15+
16+
class ATtiny8x7_Pinmap:
17+
"""This class is automatically used by `adafruit_seesaw.seesaw.Seesaw` when
18+
a ATtiny8x7 Breakout is detected.
19+
20+
It is also a reference for the capabilities of each pin."""
21+
22+
#: The pins capable of analog output
23+
analog_pins = (0, 1, 2, 3, 6, 7, 18, 19, 20)
24+
25+
"""The effective bit resolution of the PWM pins"""
26+
pwm_width = 16 # we dont actually use all 16 bits but whatever
27+
28+
"""The pins capable of PWM output"""
29+
pwm_pins = (0, 1, 9, 12, 13)
30+
31+
"""No pins on this board are capable of touch input"""
32+
touch_pins = ()

adafruit_seesaw/seesaw.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ def const(x):
107107

108108
_TOUCH_CHANNEL_OFFSET = const(0x10)
109109

110-
_HW_ID_CODE = const(0x55)
110+
_SAMD09_HW_ID_CODE = const(0x55)
111+
_ATTINY8X7_HW_ID_CODE = const(0x87)
111112
_EEPROM_I2C_ADDR = const(0x3F)
112113

113114
_ENCODER_STATUS = const(0x00)
@@ -126,33 +127,30 @@ class Seesaw:
126127
127128
:param ~busio.I2C i2c_bus: Bus the SeeSaw is connected to
128129
:param int addr: I2C address of the SeeSaw device
129-
:param ~digitalio.DigitalInOut drdy: Pin connected to SeeSaw's 'ready' output"""
130+
:param ~digitalio.DigitalInOut drdy: Pin connected to SeeSaw's 'ready' output
131+
:param bool reset: Whether to do a software reset on init"""
130132

131133
INPUT = const(0x00)
132134
OUTPUT = const(0x01)
133135
INPUT_PULLUP = const(0x02)
134136
INPUT_PULLDOWN = const(0x03)
135137

136-
def __init__(self, i2c_bus, addr=0x49, drdy=None):
138+
def __init__(self, i2c_bus, addr=0x49, drdy=None, reset=True):
137139
self._drdy = drdy
138140
if drdy is not None:
139141
drdy.switch_to_input()
140142

141143
self.i2c_device = I2CDevice(i2c_bus, addr)
142-
self.sw_reset()
143-
144-
def sw_reset(self):
145-
"""Trigger a software reset of the SeeSaw chip"""
146-
self.write8(_STATUS_BASE, _STATUS_SWRST, 0xFF)
147-
time.sleep(0.500)
144+
if reset:
145+
self.sw_reset()
148146

149-
chip_id = self.read8(_STATUS_BASE, _STATUS_HW_ID)
147+
self.chip_id = self.read8(_STATUS_BASE, _STATUS_HW_ID)
150148

151-
if chip_id != _HW_ID_CODE:
149+
if self.chip_id not in (_ATTINY8X7_HW_ID_CODE, _SAMD09_HW_ID_CODE):
152150
raise RuntimeError(
153151
"Seesaw hardware ID returned (0x{:x}) is not "
154-
"correct! Expected 0x{:x}. Please check your wiring.".format(
155-
chip_id, _HW_ID_CODE
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
156154
)
157155
)
158156

@@ -166,12 +164,21 @@ def sw_reset(self):
166164
from adafruit_seesaw.robohat import MM1_Pinmap
167165

168166
self.pin_mapping = MM1_Pinmap
169-
else:
167+
elif self.chip_id == _SAMD09_HW_ID_CODE:
170168
from adafruit_seesaw.samd09 import SAMD09_Pinmap
171169

172170
self.pin_mapping = SAMD09_Pinmap
171+
elif self.chip_id == _ATTINY8X7_HW_ID_CODE:
172+
from adafruit_seesaw.attiny8x7 import ATtiny8x7_Pinmap
173+
174+
self.pin_mapping = ATtiny8x7_Pinmap
173175
# pylint: enable=import-outside-toplevel
174176

177+
def sw_reset(self):
178+
"""Trigger a software reset of the SeeSaw chip"""
179+
self.write8(_STATUS_BASE, _STATUS_SWRST, 0xFF)
180+
time.sleep(0.500)
181+
175182
def get_options(self):
176183
"""Retrieve the 'options' word from the SeeSaw board"""
177184
buf = bytearray(4)
@@ -241,9 +248,14 @@ def analog_read(self, pin):
241248
if pin not in self.pin_mapping.analog_pins:
242249
raise ValueError("Invalid ADC pin")
243250

251+
if self.chip_id == _ATTINY8X7_HW_ID_CODE:
252+
offset = pin
253+
elif self.chip_id == _SAMD09_HW_ID_CODE:
254+
offset = self.pin_mapping.analog_pins.index(pin)
255+
244256
self.read(
245257
_ADC_BASE,
246-
_ADC_CHANNEL_OFFSET + self.pin_mapping.analog_pins.index(pin),
258+
_ADC_CHANNEL_OFFSET + offset,
247259
buf,
248260
)
249261
ret = struct.unpack(">H", buf)[0]
@@ -334,20 +346,19 @@ def digital_write_bulk_b(self, pins, value):
334346

335347
def analog_write(self, pin, value):
336348
"""Set the value of an analog output by number"""
337-
pin_found = False
349+
if pin not in self.pin_mapping.pwm_pins:
350+
raise ValueError("Invalid PWM pin")
351+
352+
if self.chip_id == _ATTINY8X7_HW_ID_CODE:
353+
offset = pin
354+
elif self.chip_id == _SAMD09_HW_ID_CODE:
355+
offset = self.pin_mapping.pwm_pins.index(pin)
356+
338357
if self.pin_mapping.pwm_width == 16:
339-
if pin in self.pin_mapping.pwm_pins:
340-
pin_found = True
341-
cmd = bytearray(
342-
[self.pin_mapping.pwm_pins.index(pin), (value >> 8), value & 0xFF]
343-
)
358+
cmd = bytearray([offset, (value >> 8), value & 0xFF])
344359
else:
345-
if pin in self.pin_mapping.pwm_pins:
346-
pin_found = True
347-
cmd = bytearray([self.pin_mapping.pwm_pins.index(pin), value])
360+
cmd = bytearray([offset, value])
348361

349-
if pin_found is False:
350-
raise ValueError("Invalid PWM pin")
351362
self.write(_TIMER_BASE, _TIMER_PWM, cmd)
352363
time.sleep(0.001)
353364

examples/seesaw_analogin_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# Simple seesaw test reading analog value
5+
# on SAMD09, analog in can be pins 2, 3, or 4
6+
# on Attiny8x7, analog in can be pins 0, 1, 2, 3, 6, 7, 18, 19, 20
7+
#
8+
# See the seesaw Learn Guide for wiring details:
9+
# https://learn.adafruit.com/adafruit-seesaw-atsamd09-breakout?view=all#circuitpython-wiring-and-test
10+
11+
import time
12+
import board
13+
from adafruit_seesaw.seesaw import Seesaw
14+
from adafruit_seesaw.analoginput import AnalogInput
15+
16+
i2c_bus = board.I2C()
17+
ss = Seesaw(i2c_bus)
18+
19+
analogin_pin = 2
20+
analog_in = AnalogInput(ss, analogin_pin)
21+
22+
while True:
23+
print(analog_in.value)
24+
time.sleep(0.1)

examples/seesaw_digitalio_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# Simple seesaw test using an LED attached to Pin 5 and a button on pin 2
5+
#
6+
# See the seesaw Learn Guide for wiring details:
7+
# https://learn.adafruit.com/adafruit-seesaw-atsamd09-breakout?view=all#circuitpython-wiring-and-test
8+
9+
import time
10+
import board
11+
import digitalio
12+
from adafruit_seesaw.seesaw import Seesaw
13+
from adafruit_seesaw.digitalio import DigitalIO
14+
15+
i2c_bus = board.I2C()
16+
ss = Seesaw(i2c_bus)
17+
18+
button_pin = 2
19+
led_pin = 5
20+
21+
button = DigitalIO(ss, button_pin)
22+
button.direction = digitalio.Direction.INPUT
23+
button.pull = digitalio.Pull.UP
24+
25+
led = DigitalIO(ss, led_pin)
26+
led.direction = digitalio.Direction.OUTPUT
27+
28+
while True:
29+
# simply set the LED to the same 'value' as the button pin
30+
led.value = button.value
31+
time.sleep(0.1)

examples/seesaw_eeprom_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# Simple seesaw test reading and writing the internal EEPROM
5+
# The ATtiny8xx series has a true 128 byte EEPROM, the SAMD09 mimics it in flash with 64 bytes
6+
# THE LAST BYTE IS USED FOR I2C ADDRESS CHANGE!
7+
#
8+
# See the seesaw Learn Guide for wiring details:
9+
# https://learn.adafruit.com/adafruit-seesaw-atsamd09-breakout?view=all#circuitpython-wiring-and-test
10+
11+
import time
12+
import board
13+
from adafruit_seesaw import seesaw
14+
15+
i2c_bus = board.I2C()
16+
ss = seesaw.Seesaw(i2c_bus)
17+
18+
val = ss.eeprom_read8(0x02) # read from address 2
19+
print("Read 0x%02x from EEPROM address 0x02" % val)
20+
21+
print("Incremening value")
22+
ss.eeprom_write8(0x02, val + 1)
23+
24+
val = ss.eeprom_read8(0x02) # read from address 2
25+
print("Second read 0x%02x from EEPROM address 0x02" % val)
26+
27+
while True:
28+
# Do not write EEPROM in a loop, it has 100k cycle life
29+
time.sleep(1)

examples/seesaw_neopixel_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# Simple seesaw test writing NeoPixels
5+
# Can use any valid GPIO pin, up to 60 pixels!
6+
#
7+
# See the seesaw Learn Guide for wiring details:
8+
# https://learn.adafruit.com/adafruit-seesaw-atsamd09-breakout?view=all#circuitpython-wiring-and-test
9+
10+
import time
11+
import board
12+
from rainbowio import colorwheel
13+
from adafruit_seesaw import seesaw, neopixel
14+
15+
i2c_bus = board.I2C()
16+
ss = seesaw.Seesaw(i2c_bus)
17+
18+
NEOPIXEL_PIN = 6 # change to any pin
19+
NEOPIXEL_NUM = 30 # no more than 60!
20+
pixels = neopixel.NeoPixel(ss, NEOPIXEL_PIN, NEOPIXEL_NUM)
21+
pixels.brightness = 0.3 # not so brite!
22+
23+
color_offset = 0 # start at red
24+
25+
# cycle through all colors along the strip
26+
while True:
27+
for i in range(NEOPIXEL_NUM):
28+
rc_index = (i * 256 // NEOPIXEL_NUM) + color_offset
29+
pixels[i] = colorwheel(rc_index & 255)
30+
pixels.show()
31+
color_offset += 1
32+
time.sleep(0.01)

examples/seesaw_pwmout_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# Simple seesaw test for writing PWM outputs
5+
# On the SAMD09 breakout these are pins 5, 6, and 7
6+
# On the ATtiny8x7 breakout these are pins 0, 1, 9, 12, 13
7+
#
8+
# See the seesaw Learn Guide for wiring details:
9+
# https://learn.adafruit.com/adafruit-seesaw-atsamd09-breakout?view=all#circuitpython-wiring-and-test
10+
11+
import time
12+
import board
13+
from adafruit_seesaw import seesaw, pwmout
14+
15+
i2c_bus = board.I2C()
16+
ss = seesaw.Seesaw(i2c_bus)
17+
18+
PWM_PIN = 9 # change to a valid PWM output!
19+
pwm = pwmout.PWMOut(ss, PWM_PIN)
20+
21+
while True:
22+
# the API PWM range is 0 to 65535, but we increment by 256 since our
23+
# resolution is often only 8 bits underneath
24+
pwm.duty_cycle = (pwm.duty_cycle + 256) % 65536
25+
time.sleep(0.01)

0 commit comments

Comments
 (0)