def run(window, device, host, port): run_window = window factory = PiGPIOFactory(host=host, port=port) if device in (N_DigitalOutputDevice, N_LED, N_Buzzer): run_window.Layout(led_layout()) switch = False device_open = False while True: event, values = run_window.read() if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: d = DigitalOutputDevice(values['-pin-'], pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False switch = False run_window['-open-'].update(image_data=icon_open) run_window['-switch-'].update(image_data=icon_switch_off) d.close() if event == '-switch-': if device_open: switch = not switch run_window['-switch-'].update(image_data=icon_switch_on if switch else icon_switch_off) d.on() if switch else d.off() else: sg.popup_no_titlebar("Open device first!") if event in (sg.WIN_CLOSED, 'Exit'): break elif device in (N_PWMOutputDevice, N_PWMLED): run_window.Layout(pwmled_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break # if not exit-event, get param cycle = 0 if str(values['-cycle-']).startswith('Select') else values['-cycle-'] frequency = 100 if values['-frequency-'].startswith('Select') else int( values['-frequency-'].replace('Hz', '')) if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-frequency-': if device_open: d.frequency = frequency if event == '-cycle-': if device_open: d.value = cycle if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: d = PWMOutputDevice(values['-pin-'], initial_value=cycle, frequency=frequency, pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open) if event == '-pulse-': if device_open: d.pulse() else: sg.popup_no_titlebar("Open device first!") elif device == N_Servo: run_window.Layout(servo_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break value = 0 if str(values['-value-']).startswith('Select') else values['-value-'] min_pulse_width = (1 if values['-min_pulse_width-'].startswith('Select') else float( values['-min_pulse_width-'].replace('ms', ''))) / 1000 max_pulse_width = (2 if values['-max_pulse_width-'].startswith('Select') else float( values['-max_pulse_width-'].replace('ms', ''))) / 1000 if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-value-': if device_open: d.value = value if event in ('-min_pulse_width-', '-max_pulse_width-'): if device_open: sg.popup_no_titlebar('Pulse-width param only work before open!') if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: d = Servo(values['-pin-'], initial_value=value, min_pulse_width=min_pulse_width, max_pulse_width=max_pulse_width, pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open) elif device == N_AngularServo: run_window.Layout(angularservo_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break angle = 0 if str(values['-angle-']).startswith('Select') else values['-angle-'] min_angle = -90 if str(values['-min_angle-']).startswith('Select') else values['-min_angle-'] max_angle = 90 if str(values['-max_angle-']).startswith('Select') else values['-max_angle-'] min_pulse_width = (1 if values['-min_pulse_width-'].startswith('Select') else float( values['-min_pulse_width-'].replace('ms', ''))) / 1000 max_pulse_width = (2 if values['-max_pulse_width-'].startswith('Select') else float( values['-max_pulse_width-'].replace('ms', ''))) / 1000 if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-angle-': if device_open: d.angle = angle if event in ('-min_pulse_width-', '-max_pulse_width-', '-min_angle-', '-max_angle-'): if device_open: sg.popup_no_titlebar('Pulse-width param only work before open!') if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: d = AngularServo(values['-pin-'], initial_angle=angle, min_angle=min_angle, max_angle=max_angle, min_pulse_width=min_pulse_width, max_pulse_width=max_pulse_width, pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open) elif device == N_PhaseEnableMotor: run_window.Layout(phaseenablemotor_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break # if not exit-event, get param speed = 0 if str(values['-speed-']).startswith('Select') else values['-speed-'] if event == '-direction_pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-speed_pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-speed-': if device_open: d.value = speed if event == '-open-': if not device_open: select = 'Select direction pin' if values['-direction_pin-'] == select or values['-speed_pin-'] == select: sg.popup_no_titlebar("Select your pin!") continue else: d = PhaseEnableMotor(phase=values['-direction_pin-'], enable=values['-speed_pin-'], pin_factory=factory) d.value = 0 device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open) elif device == N_Button: run_window.Layout(button_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break # if not exit-event, get param if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-pull_up-': if device_open: sg.popup_no_titlebar('pull-up param only work before open!') if event == '-test-': if device_open: sg.popup_no_titlebar('Now you can test button!') d.wait_for_press() sg.popup_no_titlebar('Yuu pressed button!') d.wait_for_release() sg.popup_no_titlebar('Yuu released button!') else: sg.popup_no_titlebar("Open device first!") if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: pull_up = True if str(values['-pull_up-']).startswith('Select') else values['-pull_up-'] d = Button(values['-pin-'], pull_up=pull_up, bounce_time=0.1, pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open) elif device in (N_LineSensor, N_MotionSensor, N_LightSensor): run_window.Layout(linesensor_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-test-': if device_open: sg.popup_no_titlebar('Now you can test sensor!') d.wait_for_active() sg.popup_no_titlebar('device now is active!') d.wait_for_inactive() sg.popup_no_titlebar('device now is inactive, Test over!') else: sg.popup_no_titlebar("Open device first!") if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: d = eval(device)(pin=values['-pin-'], pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open)
class iodevice: def __init__(self, pin: int, mode: iostate, pin_factory = None, pull_up_in: bool = False, active_state_in = None, bounce_time_in = None, active_high_out : bool = True, initial_value_out = False): self.pin = pin self.mode = mode self.device = None self.current_value = initial_value_out self.pin_factory = pin_factory self.active_state_in = active_state_in self.bounce_time_in = None self.active_high_out = active_high_out self.pull_up_in = pull_up_in if (mode == iostate.Input): self.device = DigitalInputDevice(self.pin, pull_up = self.pull_up_in, pin_factory = self.pin_factory) elif (mode == iostate.Output): self.device = DigitalOutputDevice(self.pin, active_high = True, initial_value = self.current_value, pin_factory = self.pin_factory) def state(self): value = self.value() mode = 'O' if self.mode == iostate.Output else 'I' if self.mode == iostate.Input else 'F' val = '-' if self.mode == iostate.Float else '?' if value is None else '1' if value else '0' resistor = ' ' if self.mode == iostate.Float or self.mode == iostate.Output \ else '-' if self.device.pull_up is None \ else 'u' if self.device.pull_up else 'd' return f'({self.pin:2})={mode}{val}{resistor}' def value(self): if (self.mode == iostate.Output): return self.current_value if (self.mode == iostate.Input): return self.device.value return None def high(self): if (self.mode == iostate.Output): self.current_value = True self.device.on() else: print("Output failed. Not in Output state") def low(self): if (self.mode == iostate.Output): self.current_value = False self.device.off() else: print("Output failed. Not in Output state") def set(self, value: bool): if (value): self.high() else: self.low() def inputMode(self): if (self.mode == iostate.Input): return if (self.mode == iostate.Output): self.device.close() self.device = None self.device = DigitalInputDevice(self.pin, pull_up = self.pull_up_in, active_state = self.active_state_in, bounce_time = self.bounce_time_in, pin_factory = self.pin_factory) self.current_value = self.device.value self.mode = iostate.Input def outputMode(self, initial_value: bool = None): if (self.mode == iostate.Output): if (initial_value != None): self.current_value = initial_value self.set(self.current_value) return if (self.mode == iostate.Input): self.device.close() self.device = None self.device = DigitalOutputDevice(self.pin, active_high = self.active_high_out, initial_value = initial_value, pin_factory = self.pin_factory) self.current_value = initial_value self.mode = iostate.Output def close(self): if (self.mode == iostate.Input or self.mode == iostate.Output): self.device.close() self.device = None self.mode = iostate.Float self.current_value = None def blink(self, on_time=1, off_time=0, n=None, background=True): self.outputMode() value = self.current_value self.device.blink(on_time,off_time,n,background) set(value) def wait_for_active(self, timeout = None): self.inputMode() self.device.wait_for_active(timeout) def wait_for_inactive(self, timeout = None): self.inputMode() self.device.wait_for_inactive(timeout) @property def active_time(self): self.inputMode() return self.device.active_time @property def inactive_time(self): self.inputMode() return self.device.inactive_time @property def when_activated(self): self.inputMode() return self.device.when_activated @when_activated.setter def when_activated(self, x): self.inputMode() self.device.when_activated = x @property def when_deactivated(self): self.inputMode() return self.device.when_deactivated @when_deactivated.setter def when_deactivated(self, x): self.inputMode() self.device.when_deactivated = x