Example #1
0
    def __init__(self, ipcon, uid, version):
        PluginBase.__init__(self, ipcon, uid, 'Joystick Bricklet', version)
        
        self.js = BrickletJoystick(uid, ipcon)
        
        self.qtcb_position.connect(self.cb_position)
        self.js.register_callback(self.js.CALLBACK_POSITION,
                                  self.qtcb_position.emit)
        
        self.qtcb_pressed.connect(self.cb_pressed)
        self.js.register_callback(self.js.CALLBACK_PRESSED,
                                  self.qtcb_pressed.emit)
        
        self.qtcb_released.connect(self.cb_released)
        self.js.register_callback(self.js.CALLBACK_RELEASED,
                                  self.qtcb_released.emit)
        
        self.joystick_frame = JoystickFrame(self)
        self.joystick_frame.setMinimumSize(220, 220)
        self.joystick_frame.setMaximumSize(220, 220)
        self.joystick_frame.set_position(0, 0)
       
        self.calibration_button = QPushButton('Calibrate (0, 0)')
        self.position_label = PositionLabel('Position: (0, 0)')
        
        self.calibration_button.pressed.connect(self.calibration_pressed)
        
        self.current_x = None
        self.current_y = None
        
        plot_list = [['X', Qt.darkGreen, self.get_current_x],
                     ['Y', Qt.blue, self.get_current_y]]
        self.plot_widget = PlotWidget('Position', plot_list)
        
        layout_h1 = QHBoxLayout()
        layout_h1.addStretch()
        layout_h1.addWidget(self.position_label)
        layout_h1.addStretch()

        layout_h2 = QHBoxLayout()
        layout_h2.addStretch()
        layout_h2.addWidget(self.joystick_frame)
        layout_h2.addStretch()

        layout = QVBoxLayout(self)
        layout.addLayout(layout_h1)
        layout.addLayout(layout_h2)
        layout.addWidget(self.plot_widget)
        layout.addWidget(self.calibration_button)
Example #2
0
class Joystick(PluginBase):
    qtcb_position = pyqtSignal(int, int)
    qtcb_pressed = pyqtSignal()
    qtcb_released = pyqtSignal()
    
    def __init__(self, ipcon, uid, version):
        PluginBase.__init__(self, ipcon, uid, 'Joystick Bricklet', version)
        
        self.js = BrickletJoystick(uid, ipcon)
        
        self.qtcb_position.connect(self.cb_position)
        self.js.register_callback(self.js.CALLBACK_POSITION,
                                  self.qtcb_position.emit)
        
        self.qtcb_pressed.connect(self.cb_pressed)
        self.js.register_callback(self.js.CALLBACK_PRESSED,
                                  self.qtcb_pressed.emit)
        
        self.qtcb_released.connect(self.cb_released)
        self.js.register_callback(self.js.CALLBACK_RELEASED,
                                  self.qtcb_released.emit)
        
        self.joystick_frame = JoystickFrame(self)
        self.joystick_frame.setMinimumSize(220, 220)
        self.joystick_frame.setMaximumSize(220, 220)
        self.joystick_frame.set_position(0, 0)
       
        self.calibration_button = QPushButton('Calibrate (0, 0)')
        self.position_label = PositionLabel('Position: (0, 0)')
        
        self.calibration_button.pressed.connect(self.calibration_pressed)
        
        self.current_x = None
        self.current_y = None
        
        plot_list = [['X', Qt.darkGreen, self.get_current_x],
                     ['Y', Qt.blue, self.get_current_y]]
        self.plot_widget = PlotWidget('Position', plot_list)
        
        layout_h1 = QHBoxLayout()
        layout_h1.addStretch()
        layout_h1.addWidget(self.position_label)
        layout_h1.addStretch()

        layout_h2 = QHBoxLayout()
        layout_h2.addStretch()
        layout_h2.addWidget(self.joystick_frame)
        layout_h2.addStretch()

        layout = QVBoxLayout(self)
        layout.addLayout(layout_h1)
        layout.addLayout(layout_h2)
        layout.addWidget(self.plot_widget)
        layout.addWidget(self.calibration_button)

    def start(self):
        async_call(self.js.set_position_callback_period, 20, None, self.increase_error_count)
        
        self.plot_widget.stop = False
        
    def stop(self):
        async_call(self.js.set_position_callback_period, 0, None, self.increase_error_count)
        
        self.plot_widget.stop = True

    def get_url_part(self):
        return 'joystick'

    @staticmethod
    def has_device_identifier(device_identifier):
        return device_identifier == BrickletJoystick.DEVICE_IDENTIFIER

    def calibration_pressed(self):
        try:
            self.js.calibrate()
        except ip_connection.Error:
            return
        
    def get_current_x(self):
        return self.current_x
    
    def get_current_y(self):
        return self.current_y

    def cb_pressed(self):
        self.joystick_frame.set_pressed(True)
        
    def cb_released(self):
        self.joystick_frame.set_pressed(False)

    def cb_position(self, x, y):
        self.current_x = x
        self.current_y = y
        self.position_label.setText(str((x, y)))
        self.joystick_frame.set_position(x, y)