Пример #1
0
class DeviceManagerBase():
    def __init__(self, name=None, shouldSetup=False):
        self.connection_status = 'not connected'
        self.ser = serial.Serial(timeout=1, write_timeout=1)
        self.sent_command_event = Event()
        self.emulator_mode = False
        if not name:
            self.name = 'Mysterial Serial Device'
        else:
            self.name = name
        if shouldSetup:
            self.Setup()

    def ConnectToDevice(self,
                        defPort=None,
                        baud=9600,
                        qrymsg=b'ping',
                        retmsg='pong',
                        trycount=1,
                        ports=sc.serial_ports(),
                        readsequence='\n'):
        if self.emulator_mode:
            print('starting in emulator mode')
        elif self.ser.is_open:
            print('already connected')
        else:
            print('attempting to connect to Device: %s' % self.name)
            s = sc.ping_controller(defPort, ports, baud, qrymsg, retmsg,
                                   trycount, readsequence)
            self.ser.port = s
            self.ser.baudrate = baud
            self.ser.open()
            self.connection_status = 'connected'
            return s

    def WaitForResponse(self, response='ok'):
        DeviceNotBusy = False
        while (DeviceNotBusy != True):
            ret = self.ser.readline().decode().rstrip()
            print('WaitForResponse: ', ret)
            if (response in ret):
                DeviceNotBusy = True
                print("got the response: " + response)

    def SendCommand(self, com, waitMsg=None, term="\n"):
        if not self.emulator_mode:
            print('Device %s sending command: %s' % (self.name, com))
            self.sent_command_event.notify(com)
            command = str(com) + term
            self.ser.write(command.encode())
            if waitMsg:
                print('waitMsg: ', waitMsg)
                self.WaitForResponse()
                #workaround to trigger busy:processing response from Marlin
                if waitMsg == 'M84\n':
                    self.ser.write(waitMsg.encode())
                    self.WaitForResponse()
        else:
            print('Emulator %s sending command: %s' % (self.name, com))
            self.sent_command_event.notify(com)

    def SetInitialState(self):
        raise NotImplementedError()

    def Setup(self):
        raise NotImplementedError()
class Xbox_Interface:
    def __init__(self,delay=1.1):
        self.current_pos = SystemPosition(0,0,0,0,0,0,0,0)
        self.msg = None
        self.isActive = False
        self.axis_threshold = 0.1
        #self.HandleInput()
        self.delay = delay
        self.axis_moved_event = Event()
        self.button_press_event = Event()
        self.button_release_event = Event()
        th = threading.Thread(target=self.HandleInput)
        th.start()

    def get_pos(self):
        return self.current_pos

    def get_msg(self):
        return self.msg

    def HandleInput(self):
        try:
            self.isActive = True
            with Xbox360Controller(0, axis_threshold=0.0) as controller:
                # Button A events
                controller.button_a.when_pressed = self.on_button_pressed
                controller.button_a.when_released = self.on_button_released
                # Button B events
                controller.button_b.when_pressed = self.on_button_pressed
                controller.button_b.when_released = self.on_button_released
                # Button Y events
                controller.button_y.when_pressed = self.on_button_pressed
                controller.button_y.when_released = self.on_button_released
                # Button X events
                controller.button_x.when_pressed = self.on_button_pressed
                controller.button_x.when_released = self.on_button_released
                # Button Trigger L events
                controller.button_trigger_l.when_pressed = self.on_button_pressed
                # Button Trigger R events
                controller.button_trigger_r.when_pressed = self.on_button_pressed
                # Button Thumb L events
                controller.button_thumb_l.when_pressed = self.on_button_pressed
                # Button Thumb R events
                controller.button_thumb_r.when_pressed = self.on_button_pressed
                # Start Button events
                controller.button_start.when_pressed = self.on_button_pressed

                # Left and right axis move event
                controller.axis_l.when_moved = self.on_axis_moved
                controller.axis_r.when_moved = self.on_axis_moved
                
                signal.pause()
        except KeyboardInterrupt:
            self.isActive = False
            pass

    #button Y:pump0, B:pump1, A:pump2
    def on_button_pressed(self, button):
        print('Button {0} was pressed'.format(button.name))
        self.button_press_event.notify(button)

    def on_button_released(self, button):
        print('Button {0} was released'.format(button.name))
        self.button_release_event.notify(button)

    def on_axis_moved(self, axis):
        #print('Axis {0} moved to {1} {2}'.format(axis.name, axis.x, axis.y))
        if(abs(axis.x)>self.axis_threshold):
            self.axis_moved_event.notify(axis)
        elif(abs(axis.y)>self.axis_threshold):
            self.axis_moved_event.notify(axis)