Skip to content

Commit

Permalink
Function for gets the pwm frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
voidpp committed Feb 6, 2016
1 parent 6e31007 commit 9081a01
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pca9685_driver/device.py
Expand Up @@ -188,3 +188,15 @@ def set_pwm_frequency(self, value):
self.sleep()
self.write(Registers.PRE_SCALE, reg_val)
self.wake()

def calc_frequency(self, prescale):
"""Calculate the frequency by the controller's prescale, specified by the PCA9685 datasheet
:param prescale: the prescale value of the controller
"""
return int(round(self.__oscillator_clock / ((prescale + 1) * 4096.0)))

def get_pwm_frequency(self):
"""Gets the frequency for all PWM output"""

return self.calc_frequency(self.read(Registers.PRE_SCALE))
18 changes: 18 additions & 0 deletions test/test_device.py
Expand Up @@ -74,3 +74,21 @@ def test_set_pwm_value(self):
# Assert
self.assertEqual(dev.bus.values[24], 18)
self.assertEqual(dev.bus.values[25], 4)

def test_calc_frequency(self):
# Arrange
dev = Device(0, 0, FakeSMBus)

# Act & Assert
self.assertEqual(dev.calc_frequency(30), 197)
self.assertEqual(dev.calc_frequency(5), 1017)

def test_get_pwm_frequency(self):
# Arrange
dev = Device(0, 0, FakeSMBus)

# Act
dev.set_pwm_frequency(197)

# Assert
self.assertEqual(dev.get_pwm_frequency(), 197)

0 comments on commit 9081a01

Please sign in to comment.