def setOutput(self): readonly(self.state, False) if self.handler: self.handler.stop() if self.gpio is not None: self.gpio.close() self.gpio = GPIO(self.pin, GPIO.OUT, force_own=True)
def test_open_close(self): gpio = GPIO(pin_output, "in") self.assertTrue(gpio.mode == "in") self.assertTrue(gpio.pin == pin_output) self.assertTrue(gpio.fd > 0) gpio.close()
def setInput(self): readonly(self.state, True) if self.gpio is not None: self.gpio.close() self.gpio = GPIO(self.pin, GPIO.IN, force_own=True) self.state.setChecked(self.gpio.get()) if self.gpio.interrupts_available: self.handler = AsyncHandler(function=partial(self.gpio.poll, timeout=0.5)) self.handler.event.connect(self.onValueChange) self.handler.start()
def test_interactive(self): gpio = GPIO(pin_output, "out") gpio.set(False) self.assertTrue(raw_input("GPIO out is low? y/n ") == "y") gpio.set(True) self.assertTrue(raw_input("GPIO out is high? y/n ") == "y") gpio.set(False) self.assertTrue(raw_input("GPIO out is low? y/n ") == "y") gpio.close()
def test_loopback_async(self): gpio_in = GPIO(pin_input, "in") gpio_out = GPIO(pin_output, "out") # Wrapper for running poll() in a thread def threaded_poll(gpio, timeout): ret = queue.Queue() def f(): ret.put(gpio.poll(timeout)) thread = threading.Thread(target=f) thread.start() return ret
def __init__(self, pin, parent=None): super(GPIOWidget, self).__init__(parent) self.handler = None self.gpio = None self.pin = pin self.label = QLabel(GPIO.pin_to_name(pin)) self.none = QRadioButton() self.none.setChecked(True) self.input = QRadioButton() self.out = QRadioButton() self.state = QCheckBox() readonly(self.state, True) layout = QHBoxLayout() layout.addWidget(self.label) layout.addWidget(self.none) layout.addWidget(self.input) layout.addWidget(self.out) layout.addWidget(self.state) self.setLayout(layout) self.none.toggled.connect(lambda: self.setNone() if self.none.isChecked() else None) self.input.toggled.connect(lambda: self.setInput() if self.input.isChecked() else None) self.out.toggled.connect(lambda: self.setOutput() if self.out.isChecked() else None) self.state.stateChanged.connect(lambda: self.setValue(self.state.isChecked()))
class GPIOWidget(QWidget): def __init__(self, pin, parent=None): super(GPIOWidget, self).__init__(parent) self.handler = None self.gpio = None self.pin = pin self.label = QLabel(GPIO.pin_to_name(pin)) self.none = QRadioButton() self.none.setChecked(True) self.input = QRadioButton() self.out = QRadioButton() self.state = QCheckBox() readonly(self.state, True) layout = QHBoxLayout() layout.addWidget(self.label) layout.addWidget(self.none) layout.addWidget(self.input) layout.addWidget(self.out) layout.addWidget(self.state) self.setLayout(layout) self.none.toggled.connect(lambda: self.setNone() if self.none.isChecked() else None) self.input.toggled.connect(lambda: self.setInput() if self.input.isChecked() else None) self.out.toggled.connect(lambda: self.setOutput() if self.out.isChecked() else None) self.state.stateChanged.connect(lambda: self.setValue(self.state.isChecked())) def setNone(self): readonly(self.state, False) self.state.setChecked(False) if self.handler: self.handler.stop() if self.gpio: self.gpio.close() self.gpio = None def setOutput(self): readonly(self.state, False) if self.handler: self.handler.stop() if self.gpio is not None: self.gpio.close() self.gpio = GPIO(self.pin, GPIO.OUT, force_own=True) def setInput(self): readonly(self.state, True) if self.gpio is not None: self.gpio.close() self.gpio = GPIO(self.pin, GPIO.IN, force_own=True) self.state.setChecked(self.gpio.get()) if self.gpio.interrupts_available: self.handler = AsyncHandler(function=partial(self.gpio.poll, timeout=0.5)) self.handler.event.connect(self.onValueChange) self.handler.start() def setValue(self, value): if self.gpio: if self.gpio.mode == GPIO.OUT: self.gpio.set(value) def onValueChange(self, value): if self.gpio: self.state.setChecked(value == "rising") def __del__(self): if self.handler: self.handler.stop()
def test_loopback(self): gpio_in = GPIO(pin_input, GPIO.IN) gpio_out = GPIO(pin_output, GPIO.OUT) self.assertTrue(gpio_in.mode == "in") self.assertTrue(gpio_out.mode == "out") gpio_out.set(False) self.assertTrue(gpio_in.get() == False) gpio_out.set(True) self.assertTrue(gpio_in.get() == True) gpio_out.set(False) self.assertTrue(gpio_in.get() == False) gpio_in.close() gpio_out.close()