Skip to content

Commit 4bc09f8

Browse files
authored
Merge pull request #125 from caternuson/iss166_set_pwm_freq
Fix `set_pwm_freq()` for ATtinys
2 parents 7b7c220 + 4da237e commit 4bc09f8

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

adafruit_seesaw/attiny8x7.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class ATtiny8x7_Pinmap:
2626
pwm_width = 16 # we dont actually use all 16 bits but whatever
2727

2828
"""The pins capable of PWM output"""
29-
pwm_pins = (0, 1, 9, 12, 13)
29+
pwm_pins = (0, 1, 9, 12, 13) # 8 bit PWM mode
30+
pwm_pins += (6, 7, 8) # 16 bit PWM mode
3031

3132
"""No pins on this board are capable of touch input"""
3233
touch_pins = ()

adafruit_seesaw/attinyx16.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class ATtinyx16_Pinmap:
2525
pwm_width = 16 # we dont actually use all 16 bits but whatever
2626

2727
"""The pins capable of PWM output"""
28-
pwm_pins = (0, 1, 7, 11, 16)
28+
pwm_pins = (0, 1, 7, 11, 16) # 8 bit PWM mode
29+
pwm_pins += (4, 5, 6) # 16 bit PWM mode
2930

3031
"""No pins on this board are capable of touch input"""
3132
touch_pins = ()

adafruit_seesaw/seesaw.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,17 @@ def get_temp(self):
390390

391391
def set_pwm_freq(self, pin, freq):
392392
"""Set the PWM frequency of a pin by number"""
393-
if pin in self.pin_mapping.pwm_pins:
394-
cmd = bytearray(
395-
[self.pin_mapping.pwm_pins.index(pin), (freq >> 8), freq & 0xFF]
396-
)
397-
self.write(_TIMER_BASE, _TIMER_FREQ, cmd)
398-
else:
393+
if pin not in self.pin_mapping.pwm_pins:
399394
raise ValueError("Invalid PWM pin")
400395

396+
if self.chip_id == _SAMD09_HW_ID_CODE:
397+
offset = self.pin_mapping.pwm_pins.index(pin)
398+
else:
399+
offset = pin
400+
401+
cmd = bytearray([offset, (freq >> 8), freq & 0xFF])
402+
self.write(_TIMER_BASE, _TIMER_FREQ, cmd)
403+
401404
def encoder_position(self, encoder=0):
402405
"""The current position of the encoder"""
403406
buf = bytearray(4)

0 commit comments

Comments
 (0)