Skip to content

Commit f6e2f6b

Browse files
committed
pwm!
1 parent 928559d commit f6e2f6b

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

adafruit_seesaw/attiny8x7.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ def const(x):
2121
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_seesaw.git"
2222

2323

24-
_PWM_0_PIN = const(0x04)
25-
_PWM_1_PIN = const(0x05)
26-
_PWM_2_PIN = const(0x06)
27-
_PWM_3_PIN = const(0x07)
28-
29-
3024
class ATtiny8x7_Pinmap:
3125
"""This class is automatically used by `adafruit_seesaw.seesaw.Seesaw` when
3226
a ATtiny8x7 Breakout is detected.
@@ -37,10 +31,10 @@ class ATtiny8x7_Pinmap:
3731
analog_pins = (0, 1, 2, 3, 6, 7, 18, 19, 20)
3832

3933
"""The effective bit resolution of the PWM pins"""
40-
pwm_width = 8
34+
pwm_width = 16 # we dont actually use all 16 bits but whatever
4135

4236
"""The pins capable of PWM output"""
43-
pwm_pins = (_PWM_0_PIN, _PWM_1_PIN, _PWM_2_PIN, _PWM_3_PIN)
37+
pwm_pins = (0, 1, 9, 12, 13)
4438

4539
"""No pins on this board are capable of touch input"""
4640
touch_pins = ()

adafruit_seesaw/seesaw.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,19 @@ def digital_write_bulk_b(self, pins, value):
342342

343343
def analog_write(self, pin, value):
344344
"""Set the value of an analog output by number"""
345-
pin_found = False
345+
if pin not in self.pin_mapping.pwm_pins:
346+
raise ValueError("Invalid PWM pin")
347+
348+
if self.chip_id == _ATTINY8X7_HW_ID_CODE:
349+
offset = pin
350+
elif self.chip_id == _SAMD09_HW_ID_CODE:
351+
offset = self.pin_mapping.pwm_pins.index(pin)
352+
346353
if self.pin_mapping.pwm_width == 16:
347-
if pin in self.pin_mapping.pwm_pins:
348-
pin_found = True
349-
cmd = bytearray(
350-
[self.pin_mapping.pwm_pins.index(pin), (value >> 8), value & 0xFF]
351-
)
354+
cmd = bytearray([offset, (value >> 8), value & 0xFF])
352355
else:
353-
if pin in self.pin_mapping.pwm_pins:
354-
pin_found = True
355-
cmd = bytearray([self.pin_mapping.pwm_pins.index(pin), value])
356+
cmd = bytearray([offset, value])
356357

357-
if pin_found is False:
358-
raise ValueError("Invalid PWM pin")
359358
self.write(_TIMER_BASE, _TIMER_PWM, cmd)
360359
time.sleep(0.001)
361360

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)