def test_change_duty_cycle(self): chan = PWMOutput(self.bus, 0, 0, 0, self.PWMController) chan._write_state = Mock() self.assertRaises(ValueError, chan.change_duty_cycle, 0) self.assertRaises(ValueError, chan.change_duty_cycle, -1) self.assertRaises(ValueError, chan.change_duty_cycle, 1.1) self.PWMController.sleeping = True self.assertRaises(RuntimeError, chan.change_duty_cycle, 0.5) self.PWMController.sleeping = False chan.change_duty_cycle(0.5) self.assertInternalState(chan, _on_count=0, _off_count=0x800, _full_on=False) chan._write_state.assert_called_once() chan._write_state.reset_mock() chan.change_duty_cycle(1) self.assertInternalState(chan, _full_on=True) chan._write_state.assert_called_once() chan._write_state.reset_mock() chan.change_duty_cycle(0.2) self.assertInternalState(chan, _on_count=0, _off_count=0x333, _full_on=False) chan._write_state.assert_called_once()
def test_prop_enabled(self): chan = PWMOutput(self.bus, 0, 0, 0, self.PWMController) self.assertInternalState(chan, _full_off=True) self.assertFalse(chan.enabled) chan._full_off = False self.assertTrue(chan.enabled) chan._first_reg = Channels.CHAN_ALL self.assertFalse(chan.enabled) chan._full_off = True self.assertFalse(chan.enabled)
def test_creation(self, write_state): chan = PWMOutput(self.bus, 0, 0, 0, self.PWMController) self.assertInternalState(chan, _on_count=0, _full_on=False, _off_count=0, _full_off=True) write_state.assert_called_once() write_state.reset_mock() chan = PWMOutput(self.bus, 0, 0, (0xFF / 4095), self.PWMController) self.assertInternalState(chan, _on_count=0xFF, _full_on=False, _off_count=0, _full_off=True) write_state.assert_called_once()
def test_change_pulse_width(self, width_limits_from_freq): width_limits_from_freq.return_value = (1, 1000) chan = PWMOutput(self.bus, 0, 0, 0, self.PWMController) chan._write_state = Mock() self.assertRaises(ValueError, chan.change_pulse_width, 0) self.assertRaises(ValueError, chan.change_pulse_width, 1001) self.PWMController.sleeping = True self.assertRaises(RuntimeError, chan.change_duty_cycle, 0.5) self.PWMController.sleeping = False chan.change_pulse_width(500) self.assertInternalState(chan, _on_count=0, _off_count=500, _full_on=False) chan._write_state.assert_called_once()
def test_prop_pulse_width(self, width_limits_from_freq): self.PWMController.frequency = 200 chan = PWMOutput(self.bus, 0, 0, 0, self.PWMController) width_limits_from_freq.return_value = (1, 1000) chan._full_off = True self.assertEqual(chan.pulse_width, 0) chan._first_reg = Channels.CHAN_ALL chan._full_off = False self.assertEqual(chan.pulse_width, 0) chan._off_count = 500 chan._on_count = 0 chan._first_reg = 0 self.assertEqual(chan.pulse_width, 500) chan._full_on = True self.assertEqual(chan.pulse_width, 1e6 / 200)
def test__write_state(self): states = ( { "_on_count": 0, "_full_on": False, "_off_count": 0, "_full_off": True }, { "_on_count": 0x123, "_full_on": False, "_off_count": 0x456, "_full_off": True, }, { "_on_count": 0, "_full_on": True, "_off_count": 0, "_full_off": False }, ) expected_results = ( b"\x00\x00\x00\x00\x10", b"\x00\x23\x01\x56\x14", b"\x00\x00\x10\x00\x00", ) chan = PWMOutput(self.bus, 0, 0, 0, self.PWMController) for state, expected_result in zip(states, expected_results): self.bus.i2c_rdwr.reset_mock() # Set the object internal state chan.__dict__.update(state) chan._write_state() self.bus.i2c_rdwr.assert_called_once() # Take the i2c_msg passed to i2c_rdwr, take it's buf pointer, and # return the 5 bytes it points too: result = string_at(self.bus.i2c_rdwr.call_args[0][0].buf, size=5) self.assertEqual(result, expected_result)
def test_prop_duty_cycle(self): chan = PWMOutput(self.bus, 0, 0, 0, self.PWMController) chan._full_off = True self.assertEqual(chan.duty_cycle, 0) chan._first_reg = Channels.CHAN_ALL chan._full_off = False self.assertEqual(chan.duty_cycle, 0) chan._first_reg = 0 chan._off_count = 4096 * 0.5 self.assertEqual(chan.duty_cycle, 0.5) chan._full_on = True self.assertEqual(chan.duty_cycle, 1.0)
def test_set_enable(self): chan = PWMOutput(self.bus, 0, 0, 0, self.PWMController) chan._write_state = Mock() self.PWMController.sleeping = True self.assertRaises(RuntimeError, chan.set_enable, True) self.PWMController.sleeping = False chan.set_enable(True) self.assertInternalState(chan, _full_off=False) chan._write_state.assert_called_once() chan._write_state.reset_mock() chan.set_enable(False) self.assertInternalState(chan, _full_off=True) chan._write_state.assert_called_once()