Skip to content

Commit

Permalink
Merge pull request #125 from caternuson/iss166_set_pwm_freq
Browse files Browse the repository at this point in the history
Fix `set_pwm_freq()` for ATtinys
  • Loading branch information
caternuson authored Nov 9, 2023
2 parents 7b7c220 + 4da237e commit 4bc09f8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
3 changes: 2 additions & 1 deletion adafruit_seesaw/attiny8x7.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class ATtiny8x7_Pinmap:
pwm_width = 16 # we dont actually use all 16 bits but whatever

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

"""No pins on this board are capable of touch input"""
touch_pins = ()
3 changes: 2 additions & 1 deletion adafruit_seesaw/attinyx16.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class ATtinyx16_Pinmap:
pwm_width = 16 # we dont actually use all 16 bits but whatever

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

"""No pins on this board are capable of touch input"""
touch_pins = ()
15 changes: 9 additions & 6 deletions adafruit_seesaw/seesaw.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,17 @@ def get_temp(self):

def set_pwm_freq(self, pin, freq):
"""Set the PWM frequency of a pin by number"""
if pin in self.pin_mapping.pwm_pins:
cmd = bytearray(
[self.pin_mapping.pwm_pins.index(pin), (freq >> 8), freq & 0xFF]
)
self.write(_TIMER_BASE, _TIMER_FREQ, cmd)
else:
if pin not in self.pin_mapping.pwm_pins:
raise ValueError("Invalid PWM pin")

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

cmd = bytearray([offset, (freq >> 8), freq & 0xFF])
self.write(_TIMER_BASE, _TIMER_FREQ, cmd)

def encoder_position(self, encoder=0):
"""The current position of the encoder"""
buf = bytearray(4)
Expand Down

0 comments on commit 4bc09f8

Please sign in to comment.