def __init__(self, name, pin, action, reset_state=True): self.name = name self.pin = pin if action not in (Device.ACTION_PULSE, Device.ACTION_SWITCH): raise DeviceWrongAction("Wrong action selected") self.action = action if reset_state: gpio_commands.mode(pin=self.pin, set_mode="out") gpio_commands.write(pin=self.pin, value=0) # We assume that the initial state is "off". It is impossible to read # actual state as well as the state can be interfered by the physical # buttons (in case of "PULSE" switches). I will deal with that later. (FIXME) self.write_state(state=0)
def switch(self): if self.action == Device.ACTION_SWITCH: status = int(not self.read_state()) gpio_commands.write(pin=self.pin, value=status) self.write_state(status) elif self.action == Device.ACTION_PULSE: gpio_commands.write(pin=self.pin, value=1) sleep(0.1) gpio_commands.write(pin=self.pin, value=0) self.write_state(int(not self.read_state()))
def test_write_wrong_value(self): with self.assertRaises(WrongWriteValue): write(pin=1, value=2)
def test_write_wrong_pin(self): with self.assertRaises(WrongPinNumber): write(pin=26, value=1)
def test_write_0_low(self, mock_system): write(pin=0, value=0) mock_system.assert_called_with('gpio -g write 0 0')
def test_write_1_high(self, mock_system): write(pin=1, value=1) mock_system.assert_called_with('gpio -g write 1 1')