def test_init(mock_controller): instance = Mock() instance.get_port = Mock() mock_controller.side_effect = [instance] ftdi_i2c(device='ftdi://dummy', address='0xFF') mock_controller.assert_called_with() instance.configure.assert_called_with('ftdi://dummy') instance.get_port.assert_called_with(0xFF)
def test_cleanup(mock_controller): port = Mock() instance = Mock() instance.get_port = Mock(return_value=port) mock_controller.side_effect = [instance] serial = ftdi_i2c(device='ftdi://dummy', address=0x3C) serial.cleanup() instance.terminate.assert_called_once_with()
def test_data(mock_controller): data = list(fib(100)) port = Mock() instance = Mock() instance.get_port = Mock(return_value=port) mock_controller.side_effect = [instance] serial = ftdi_i2c(device='ftdi://dummy', address=0x3C) serial.data(data) port.write_to.assert_called_once_with(0x40, data)
def test_command(mock_controller): cmds = [3, 1, 4, 2] port = Mock() instance = Mock() instance.get_port = Mock(return_value=port) mock_controller.side_effect = [instance] serial = ftdi_i2c(device='ftdi://dummy', address=0x3C) serial.command(*cmds) port.write_to.assert_called_once_with(0x00, cmds)
def ftdi_i2c(self): from luma.core.interface.serial import ftdi_i2c return ftdi_i2c(address=self.opts.i2c_address)
def test_init_device_address_error(mock_controller): address = 'foo' with pytest.raises(luma.core.error.DeviceAddressError) as ex: ftdi_i2c(device='ftdi://dummy', address=address) assert str(ex.value) == 'I2C device address invalid: {}'.format(address)
#!/usr/bin/env python # moving_cube.py # Nathan Mack 2020 from luma.core.interface.serial import ftdi_i2c from luma.core.render import canvas from luma.oled.device import ssd1306 from time import sleep # Device String ft232h_device = 'ftdi://ftdi:232h/1' # Luma Core I2C Bus Controller oled_i2c = ftdi_i2c(ft232h_device, 0x3C) oled_dev = ssd1306(oled_i2c) try: # Horizontal and vertical coordinates of the cube horz = 0 vert = 0 while True: # Draws the cube through each loop iteration with canvas(oled_dev) as draw: draw.rectangle((horz, vert, horz + 8, vert + 8), outline='white', fill='white') # Calculates the horizontal coordinates of the cube