예제 #1
0
파일: lcd_16x2.py 프로젝트: fk0815/brickv
class LCD16x2(PluginBase):
    MAX_LINE = 2
    MAX_POSITION = 16
    qtcb_pressed = pyqtSignal(int)
    qtcb_released = pyqtSignal(int)

    def __init__(self, *args):
        super().__init__(BrickletLCD16x2, *args)

        self.lcd = self.device

        # the firmware version of a EEPROM Bricklet can (under common circumstances)
        # not change during the lifetime of an EEPROM Bricklet plugin. therefore,
        # it's okay to make final decisions based on it here
        self.has_custom_character = self.firmware_version >= (2, 0, 1)

        self.qtcb_pressed.connect(self.cb_pressed)
        self.lcd.register_callback(self.lcd.CALLBACK_BUTTON_PRESSED,
                                   self.qtcb_pressed.emit)
        self.qtcb_released.connect(self.cb_released)
        self.lcd.register_callback(self.lcd.CALLBACK_BUTTON_RELEASED,
                                   self.qtcb_released.emit)

        self.line_label = QLabel('Line:')
        self.line_combo = QComboBox()

        for i in range(LCD16x2.MAX_LINE):
            self.line_combo.addItem(str(i))

        self.pos_label = QLabel('Position:')
        self.pos_combo = QComboBox()

        for i in range(LCD16x2.MAX_POSITION):
            self.pos_combo.addItem(str(i))

        self.line_pos_layout = QHBoxLayout()
        self.line_pos_layout.addWidget(self.line_label)
        self.line_pos_layout.addWidget(self.line_combo)
        self.line_pos_layout.addWidget(self.pos_label)
        self.line_pos_layout.addWidget(self.pos_combo)
        self.line_pos_layout.addStretch()

        self.text_label = QLabel('Text:')
        self.text_edit = QLineEdit()
        self.text_edit.setMaxLength(LCD16x2.MAX_POSITION)
        self.text_button = QPushButton('Send Text')
        self.text_layout = QHBoxLayout()
        self.text_layout.addWidget(self.text_label)
        self.text_layout.addWidget(self.text_edit)
        self.text_layout.addWidget(self.text_button)

        self.clear_button = QPushButton("Clear Display")

        self.bl_button = QPushButton()
        self.cursor_button = QPushButton()
        self.blink_button = QPushButton()

        self.onofflayout = QHBoxLayout()
        self.onofflayout.addWidget(self.bl_button)
        self.onofflayout.addWidget(self.cursor_button)
        self.onofflayout.addWidget(self.blink_button)

        self.b0_label = QLabel('Button 0: Released,')
        self.b1_label = QLabel('Button 1: Released,')
        self.b2_label = QLabel('Button 2: Released')

        self.buttonlayout = QHBoxLayout()
        self.buttonlayout.addWidget(self.b0_label)
        self.buttonlayout.addWidget(self.b1_label)
        self.buttonlayout.addWidget(self.b2_label)

        self.cursor_button.clicked.connect(self.cursor_clicked)
        self.blink_button.clicked.connect(self.blink_clicked)
        self.clear_button.clicked.connect(self.clear_clicked)
        self.bl_button.clicked.connect(self.bl_clicked)
        self.text_button.clicked.connect(self.text_clicked)

        if self.has_custom_character:
            line = QFrame()
            line.setObjectName("line")
            line.setFrameShape(QFrame.HLine)
            line.setFrameShadow(QFrame.Sunken)

            self.scribble_widget = ScribbleWidget(5, 8, 25, QColor(Qt.white),
                                                  QColor(Qt.blue))

            self.char_index_label = QLabel('Index:')
            self.char_index_combo = QComboBox()
            self.char_index_combo.currentIndexChanged.connect(
                self.char_index_changed)
            for i in range(8):
                self.char_index_combo.addItem(str(i))

            self.char_index_layout = QHBoxLayout()
            self.char_index_layout.addStretch()
            self.char_index_layout.addWidget(self.char_index_label)
            self.char_index_layout.addWidget(self.char_index_combo)
            self.char_index_layout.addStretch()

            self.char_index_save = QPushButton('Save Character')
            self.char_index_save.clicked.connect(self.char_index_save_clicked)
            self.char_show = QPushButton('Show all Custom Characters on LCD')
            self.char_show.clicked.connect(self.show_clicked)
            self.char_save_layout = QVBoxLayout()
            self.char_save_layout.addStretch()
            self.char_save_layout.addLayout(self.char_index_layout)
            self.char_save_layout.addWidget(self.char_index_save)
            self.char_save_layout.addWidget(self.char_show)

            help_label = QLabel(
                'Use "\\0, \\1, ..., \\7" in text field to show custom characters.'
            )
            help_label.setWordWrap(True)

            self.char_save_layout.addWidget(help_label)
            self.char_save_layout.addStretch()

            grid_stretch_layout = QHBoxLayout()
            grid_stretch_layout.addWidget(QLabel('Custom Character:'))
            grid_stretch_layout.addWidget(self.scribble_widget)
            grid_stretch_layout.addLayout(self.char_save_layout)
            grid_stretch_layout.addStretch()

            self.char_main_layout = QHBoxLayout()
            self.char_main_layout.addStretch()
            self.char_main_layout.addLayout(grid_stretch_layout)
            self.char_main_layout.addStretch()

        layout = QVBoxLayout(self)
        layout.addLayout(self.line_pos_layout)
        layout.addLayout(self.text_layout)
        layout.addWidget(self.clear_button)
        layout.addLayout(self.onofflayout)
        layout.addLayout(self.buttonlayout)

        if self.has_custom_character:
            layout.addWidget(line)
            layout.addLayout(self.char_main_layout)

        layout.addStretch(1)

    def is_backlight_on_async(self, on):
        if on:
            self.bl_button.setText('Backlight Off')
        else:
            self.bl_button.setText('Backlight On')

    def get_config_async(self, config):
        cursor, blink = config
        if cursor:
            self.cursor_button.setText('Cursor Off')
        else:
            self.cursor_button.setText('Cursor On')

        if blink:
            self.blink_button.setText('Blink Off')
        else:
            self.blink_button.setText('Blink On')

    def start(self):
        async_call(self.lcd.is_backlight_on, None, self.is_backlight_on_async,
                   self.increase_error_count)
        async_call(self.lcd.get_config, None, self.get_config_async,
                   self.increase_error_count)

    def stop(self):
        pass

    def destroy(self):
        pass

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

    def cb_pressed(self, button):
        if button == 0:
            self.b0_label.setText('Button 0: Pressed,')
        elif button == 1:
            self.b1_label.setText('Button 1: Pressed,')
        elif button == 2:
            self.b2_label.setText('Button 2: Pressed')

    def cb_released(self, button):
        if button == 0:
            self.b0_label.setText('Button 0: Released,')
        elif button == 1:
            self.b1_label.setText('Button 1: Released,')
        elif button == 2:
            self.b2_label.setText('Button 2: Released')

    def bl_clicked(self):
        if self.bl_button.text().replace('&', '') == 'Backlight On':
            async_call(self.lcd.backlight_on, None, None,
                       self.increase_error_count)
            self.bl_button.setText('Backlight Off')
        else:
            async_call(self.lcd.backlight_off, None, None,
                       self.increase_error_count)
            self.bl_button.setText('Backlight On')

    def get_config(self):
        cursor = self.cursor_button.text().replace('&', '') == 'Cursor Off'
        blink = self.blink_button.text().replace('&', '') == 'Blink Off'

        return (cursor, blink)

    def cursor_clicked(self):
        cursor, blink = self.get_config()

        async_call(self.lcd.set_config, (not cursor, blink), None,
                   self.increase_error_count)

        if cursor:
            self.cursor_button.setText('Cursor On')
        else:
            self.cursor_button.setText('Cursor Off')

    def blink_clicked(self):
        cursor, blink = self.get_config()

        async_call(self.lcd.set_config, (cursor, not blink), None,
                   self.increase_error_count)

        if blink:
            self.blink_button.setText('Blink On')
        else:
            self.blink_button.setText('Blink Off')

    def clear_clicked(self):
        async_call(self.lcd.clear_display, None, None,
                   self.increase_error_count)

    def text_clicked(self):
        line = int(self.line_combo.currentText())
        position = int(self.pos_combo.currentText())
        text = self.text_edit.text()

        if self.has_custom_character:
            for i in range(8):
                text = text.replace('\\' + str(i), chr(i + 8))

        async_call(self.lcd.write_line,
                   (line, position, unicode_to_ks0066u(text)), None,
                   self.increase_error_count)

    def char_index_save_clicked(self):
        char = [0] * 8
        img = self.scribble_widget.image()

        for j in range(img.height()):
            for i in range(img.width() - 1, -1, -1):
                if img.pixel(
                        i, j) == self.scribble_widget.foreground_color().rgb():
                    char[j] |= 1 << (4 - i)

        index = int(self.char_index_combo.currentText())

        async_call(self.lcd.set_custom_character, (index, char), None,
                   self.increase_error_count)

    def show_clicked(self):
        async_call(self.lcd.clear_display, None, None,
                   self.increase_error_count)

        line1 = '0:{0} 1:{1} 2:{2} 3:{3}'.format(chr(8), chr(9), chr(10),
                                                 chr(11))
        line2 = '4:{0} 5:{1} 6:{2} 7:{3}'.format(chr(12), chr(13), chr(14),
                                                 chr(15))

        async_call(self.lcd.write_line, (0, 0, line1), None,
                   self.increase_error_count)
        async_call(self.lcd.write_line, (1, 0, line2), None,
                   self.increase_error_count)

    def custom_character_async(self, characters):
        r = []
        g = []
        b = []

        for j in range(self.scribble_widget.image().height()):
            for i in range(self.scribble_widget.image().width() - 1, -1, -1):
                if characters[j] & (1 << i):
                    r.append(255)
                    g.append(255)
                    b.append(255)
                else:
                    r.append(0)
                    g.append(0)
                    b.append(255)

        self.scribble_widget.array_draw(r, g, b)

    def char_index_changed(self, index):
        async_call(self.lcd.get_custom_character, index,
                   self.custom_character_async, self.increase_error_count)
예제 #2
0
class RGBLEDMatrix(COMCUPluginBase, Ui_RGBLEDMatrix):
    qtcb_frame_started = pyqtSignal(int)

    STATE_IDLE = 0
    STATE_COLOR_GRADIENT = 3
    STATE_COLOR_DOT = 4
    STATE_COLOR_SCRIBBLE = 5

    def __init__(self, *args):
        COMCUPluginBase.__init__(self, BrickletRGBLEDMatrix, *args)

        self.setupUi(self)

        self.rgb_led_matrix = self.device

        self.color_button = QColorButton()
        self.below_scribble_layout.insertWidget(2, self.color_button)

        self.scribble_widget = ScribbleWidget(8, 8, 35,
                                              self.color_button.color(),
                                              QColor(Qt.black))

        def set_state_scribble():
            self.state = self.STATE_COLOR_SCRIBBLE

        self.scribble_widget.scribbling_started.connect(set_state_scribble)
        self.scribble_layout.insertWidget(1, self.scribble_widget)

        self.qtcb_frame_started.connect(self.cb_frame_started)

        self.color_button.colorChanged.connect(self.color_changed)
        self.button_clear_drawing.clicked.connect(
            self.scribble_widget.clear_image)
        self.button_drawing.clicked.connect(self.drawing_clicked)
        self.button_color.clicked.connect(self.color_clicked)
        self.button_gradient.clicked.connect(self.gradient_clicked)
        self.button_dot.clicked.connect(self.dot_clicked)
        self.box_frame_duration.valueChanged.connect(
            self.frame_duration_changed)

        self.state = self.STATE_IDLE

        self.gradient_counter = 0
        self.dot_counter = 0
        self.dot_direction = 1

        self.voltage = 0

        self.cbe_supply_voltage = CallbackEmulator(
            self, self.rgb_led_matrix.get_supply_voltage, None,
            self.cb_supply_voltage, self.increase_error_count)

    def set_rgb(self, r, g, b):
        async_call(self.rgb_led_matrix.set_red, r, None,
                   self.increase_error_count)
        async_call(self.rgb_led_matrix.set_green, g, None,
                   self.increase_error_count)
        async_call(self.rgb_led_matrix.set_blue, b, None,
                   self.increase_error_count)

    def cb_supply_voltage(self, voltage):
        self.label_voltage.setText(str(voltage / 1000.0) + 'V')

    def cb_frame_started(self):
        if self.state == self.STATE_COLOR_GRADIENT:
            self.render_color_gradient()
        elif self.state == self.STATE_COLOR_DOT:
            self.render_color_dot()
        elif self.state == self.STATE_COLOR_SCRIBBLE:
            self.render_color_scribble()

    def color_changed(self):
        self.scribble_widget.set_foreground_color(self.color_button.color())

    def frame_duration_changed(self, duration):
        async_call(self.rgb_led_matrix.set_frame_duration, duration, None,
                   self.increase_error_count)

    def drawing_clicked(self):
        old_state = self.state
        self.state = self.STATE_COLOR_SCRIBBLE
        if old_state == self.STATE_IDLE:
            self.render_color_scribble()

    def color_clicked(self):
        old_state = self.state
        self.state = self.STATE_COLOR_SCRIBBLE
        self.scribble_widget.fill_image(self.color_button.color())
        if old_state == self.STATE_IDLE:
            self.render_color_scribble()

    def gradient_clicked(self):
        old_state = self.state
        self.state = self.STATE_COLOR_GRADIENT
        if old_state == self.STATE_IDLE:
            self.render_color_gradient()

    def dot_clicked(self):
        self.dot_counter = 0
        self.dot_direction = 1
        old_state = self.state
        self.state = self.STATE_COLOR_DOT
        if old_state == self.STATE_IDLE:
            self.render_color_dot()

    def render_color_scribble(self):
        r = []
        g = []
        b = []
        for i in range(8):
            for j in range(8):
                color = QColor(self.scribble_widget.image().pixel(j, i))
                r.append(color.red())
                g.append(color.green())
                b.append(color.blue())

        self.set_rgb(r, g, b)

    def render_color_gradient(self):
        self.gradient_counter += NUM_LEDS * self.box_speed.value(
        ) / 100.0 / 4.0
        ra = []
        ga = []
        ba = []

        range_leds = list(range(NUM_LEDS))
        range_leds = range_leds[
            int(self.gradient_counter) %
            NUM_LEDS:] + range_leds[:int(self.gradient_counter) % NUM_LEDS]
        range_leds = reversed(range_leds)

        for i in range_leds:
            r, g, b = colorsys.hsv_to_rgb(1.0 * i / NUM_LEDS, 1, 0.2)
            ra.append(int(r * 255))
            ga.append(int(g * 255))
            ba.append(int(b * 255))

        self.scribble_widget.array_draw(ra, ga, ba, 4)
        self.set_rgb(ra, ga, ba)

    def render_color_dot(self):
        color = self.color_button.color()
        r = color.red()
        g = color.green()
        b = color.blue()

        self.dot_counter = self.dot_counter % NUM_LEDS

        index = self.dot_counter
        line = self.dot_counter // 8
        if line % 2:
            index = line * 8 + (7 - (self.dot_counter % 8))

        r_val = [0] * NUM_LEDS
        g_val = [0] * NUM_LEDS
        b_val = [0] * NUM_LEDS

        r_val[index] = r
        g_val[index] = g
        b_val[index] = b

        self.scribble_widget.array_draw(r_val, g_val, b_val)
        self.set_rgb(r_val, g_val, b_val)

        self.dot_counter += self.dot_direction * self.box_speed.value()

        if self.dot_counter >= NUM_LEDS:
            self.dot_direction = -1
            self.dot_counter = NUM_LEDS - 1
        elif self.dot_counter < 0:
            self.dot_direction = 1
            self.dot_counter = 0

    def start(self):
        self.rgb_led_matrix.register_callback(
            self.rgb_led_matrix.CALLBACK_FRAME_STARTED,
            self.qtcb_frame_started.emit)
        self.cbe_supply_voltage.set_period(250)
        async_call(self.rgb_led_matrix.set_frame_duration,
                   self.box_frame_duration.value(), None,
                   self.increase_error_count)

    def stop(self):
        self.rgb_led_matrix.register_callback(
            self.rgb_led_matrix.CALLBACK_FRAME_STARTED, None)
        self.cbe_supply_voltage.set_period(0)
        async_call(self.rgb_led_matrix.set_frame_duration, 0, None,
                   self.increase_error_count)

    def destroy(self):
        pass

    @staticmethod
    def has_device_identifier(device_identifier):
        return device_identifier == BrickletRGBLEDMatrix.DEVICE_IDENTIFIER
예제 #3
0
class LCD16x2(PluginBase):
    MAX_LINE = 2
    MAX_POSITION = 16
    qtcb_pressed = pyqtSignal(int)
    qtcb_released = pyqtSignal(int)

    def __init__(self, *args):
        super().__init__(BrickletLCD16x2, *args)

        self.lcd = self.device

        # the firmware version of a EEPROM Bricklet can (under common circumstances)
        # not change during the lifetime of an EEPROM Bricklet plugin. therefore,
        # it's okay to make final decisions based on it here
        self.has_custom_character = self.firmware_version >= (2, 0, 1)

        self.qtcb_pressed.connect(self.cb_pressed)
        self.lcd.register_callback(self.lcd.CALLBACK_BUTTON_PRESSED,
                                   self.qtcb_pressed.emit)
        self.qtcb_released.connect(self.cb_released)
        self.lcd.register_callback(self.lcd.CALLBACK_BUTTON_RELEASED,
                                   self.qtcb_released.emit)

        self.line_label = QLabel('Line:')
        self.line_combo = QComboBox()

        for i  in range(LCD16x2.MAX_LINE):
            self.line_combo.addItem(str(i))

        self.pos_label = QLabel('Position:')
        self.pos_combo = QComboBox()

        for i  in range(LCD16x2.MAX_POSITION):
            self.pos_combo.addItem(str(i))

        self.line_pos_layout = QHBoxLayout()
        self.line_pos_layout.addWidget(self.line_label)
        self.line_pos_layout.addWidget(self.line_combo)
        self.line_pos_layout.addWidget(self.pos_label)
        self.line_pos_layout.addWidget(self.pos_combo)
        self.line_pos_layout.addStretch()

        self.text_label = QLabel('Text:')
        self.text_edit = QLineEdit()
        self.text_edit.setMaxLength(LCD16x2.MAX_POSITION)
        self.text_button = QPushButton('Send Text')
        self.text_layout = QHBoxLayout()
        self.text_layout.addWidget(self.text_label)
        self.text_layout.addWidget(self.text_edit)
        self.text_layout.addWidget(self.text_button)

        self.clear_button = QPushButton("Clear Display")

        self.bl_button = QPushButton()
        self.cursor_button = QPushButton()
        self.blink_button = QPushButton()

        self.onofflayout = QHBoxLayout()
        self.onofflayout.addWidget(self.bl_button)
        self.onofflayout.addWidget(self.cursor_button)
        self.onofflayout.addWidget(self.blink_button)

        self.b0_label = QLabel('Button 0: Released,')
        self.b1_label = QLabel('Button 1: Released,')
        self.b2_label = QLabel('Button 2: Released')

        self.buttonlayout = QHBoxLayout()
        self.buttonlayout.addWidget(self.b0_label)
        self.buttonlayout.addWidget(self.b1_label)
        self.buttonlayout.addWidget(self.b2_label)

        self.cursor_button.clicked.connect(self.cursor_clicked)
        self.blink_button.clicked.connect(self.blink_clicked)
        self.clear_button.clicked.connect(self.clear_clicked)
        self.bl_button.clicked.connect(self.bl_clicked)
        self.text_button.clicked.connect(self.text_clicked)

        if self.has_custom_character:
            line = QFrame()
            line.setObjectName("line")
            line.setFrameShape(QFrame.HLine)
            line.setFrameShadow(QFrame.Sunken)

            self.scribble_widget = ScribbleWidget(5, 8, 25, QColor(Qt.white), QColor(Qt.blue))

            self.char_index_label = QLabel('Index:')
            self.char_index_combo = QComboBox()
            self.char_index_combo.currentIndexChanged.connect(self.char_index_changed)
            for i in range(8):
                self.char_index_combo.addItem(str(i))

            self.char_index_layout = QHBoxLayout()
            self.char_index_layout.addStretch()
            self.char_index_layout.addWidget(self.char_index_label)
            self.char_index_layout.addWidget(self.char_index_combo)
            self.char_index_layout.addStretch()

            self.char_index_save = QPushButton('Save Character')
            self.char_index_save.clicked.connect(self.char_index_save_clicked)
            self.char_show = QPushButton('Show all Custom Characters on LCD')
            self.char_show.clicked.connect(self.show_clicked)
            self.char_save_layout = QVBoxLayout()
            self.char_save_layout.addStretch()
            self.char_save_layout.addLayout(self.char_index_layout)
            self.char_save_layout.addWidget(self.char_index_save)
            self.char_save_layout.addWidget(self.char_show)

            help_label = QLabel('Use "\\0, \\1, ..., \\7" in text field to show custom characters.')
            help_label.setWordWrap(True)

            self.char_save_layout.addWidget(help_label)
            self.char_save_layout.addStretch()

            grid_stretch_layout = QHBoxLayout()
            grid_stretch_layout.addWidget(QLabel('Custom Character:'))
            grid_stretch_layout.addWidget(self.scribble_widget)
            grid_stretch_layout.addLayout(self.char_save_layout)
            grid_stretch_layout.addStretch()

            self.char_main_layout = QHBoxLayout()
            self.char_main_layout.addStretch()
            self.char_main_layout.addLayout(grid_stretch_layout)
            self.char_main_layout.addStretch()

        layout = QVBoxLayout(self)
        layout.addLayout(self.line_pos_layout)
        layout.addLayout(self.text_layout)
        layout.addWidget(self.clear_button)
        layout.addLayout(self.onofflayout)
        layout.addLayout(self.buttonlayout)

        if self.has_custom_character:
            layout.addWidget(line)
            layout.addLayout(self.char_main_layout)

        layout.addStretch(1)

    def is_backlight_on_async(self, on):
        if on:
            self.bl_button.setText('Backlight Off')
        else:
            self.bl_button.setText('Backlight On')

    def get_config_async(self, config):
        cursor, blink = config
        if cursor:
            self.cursor_button.setText('Cursor Off')
        else:
            self.cursor_button.setText('Cursor On')

        if blink:
            self.blink_button.setText('Blink Off')
        else:
            self.blink_button.setText('Blink On')

    def start(self):
        async_call(self.lcd.is_backlight_on, None, self.is_backlight_on_async, self.increase_error_count)
        async_call(self.lcd.get_config, None, self.get_config_async, self.increase_error_count)

    def stop(self):
        pass

    def destroy(self):
        pass

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

    def cb_pressed(self, button):
        if button == 0:
            self.b0_label.setText('Button 0: Pressed,')
        elif button == 1:
            self.b1_label.setText('Button 1: Pressed,')
        elif button == 2:
            self.b2_label.setText('Button 2: Pressed')

    def cb_released(self, button):
        if button == 0:
            self.b0_label.setText('Button 0: Released,')
        elif button == 1:
            self.b1_label.setText('Button 1: Released,')
        elif button == 2:
            self.b2_label.setText('Button 2: Released')

    def bl_clicked(self):
        if self.bl_button.text().replace('&', '') == 'Backlight On':
            async_call(self.lcd.backlight_on, None, None, self.increase_error_count)
            self.bl_button.setText('Backlight Off')
        else:
            async_call(self.lcd.backlight_off, None, None, self.increase_error_count)
            self.bl_button.setText('Backlight On')

    def get_config(self):
        cursor = self.cursor_button.text().replace('&', '') == 'Cursor Off'
        blink = self.blink_button.text().replace('&', '') == 'Blink Off'

        return (cursor, blink)

    def cursor_clicked(self):
        cursor, blink = self.get_config()

        async_call(self.lcd.set_config, (not cursor, blink), None, self.increase_error_count)

        if cursor:
            self.cursor_button.setText('Cursor On')
        else:
            self.cursor_button.setText('Cursor Off')

    def blink_clicked(self):
        cursor, blink = self.get_config()

        async_call(self.lcd.set_config, (cursor, not blink), None, self.increase_error_count)

        if blink:
            self.blink_button.setText('Blink On')
        else:
            self.blink_button.setText('Blink Off')

    def clear_clicked(self):
        async_call(self.lcd.clear_display, None, None, self.increase_error_count)

    def text_clicked(self):
        line = int(self.line_combo.currentText())
        position = int(self.pos_combo.currentText())
        text = self.text_edit.text()

        if self.has_custom_character:
            for i in range(8):
                text = text.replace('\\' + str(i), chr(i + 8))

        async_call(self.lcd.write_line, (line, position, unicode_to_ks0066u(text)), None, self.increase_error_count)

    def char_index_save_clicked(self):
        char = [0] * 8
        img = self.scribble_widget.image()

        for j in range(img.height()):
            for i in range(img.width() - 1, -1, -1):
                if img.pixel(i, j) == self.scribble_widget.foreground_color().rgb():
                    char[j] |= 1 << (4 - i)

        index = int(self.char_index_combo.currentText())

        async_call(self.lcd.set_custom_character, (index, char), None, self.increase_error_count)

    def show_clicked(self):
        async_call(self.lcd.clear_display, None, None, self.increase_error_count)

        line1 = '0:{0} 1:{1} 2:{2} 3:{3}'.format(chr(8), chr(9), chr(10), chr(11))
        line2 = '4:{0} 5:{1} 6:{2} 7:{3}'.format(chr(12), chr(13), chr(14), chr(15))

        async_call(self.lcd.write_line, (0, 0, line1), None, self.increase_error_count)
        async_call(self.lcd.write_line, (1, 0, line2), None, self.increase_error_count)

    def custom_character_async(self, characters):
        r = []
        g = []
        b = []

        for j in range(self.scribble_widget.image().height()):
            for i in range(self.scribble_widget.image().width() - 1, -1, -1):
                if characters[j] & (1 << i):
                    r.append(255)
                    g.append(255)
                    b.append(255)
                else:
                    r.append(0)
                    g.append(0)
                    b.append(255)

        self.scribble_widget.array_draw(r, g, b)

    def char_index_changed(self, index):
        async_call(self.lcd.get_custom_character, index, self.custom_character_async, self.increase_error_count)
예제 #4
0
class RGBLEDMatrix(COMCUPluginBase, Ui_RGBLEDMatrix):
    qtcb_frame_started = pyqtSignal(int)

    STATE_IDLE = 0
    STATE_COLOR_GRADIENT = 3
    STATE_COLOR_DOT = 4
    STATE_COLOR_SCRIBBLE = 5

    def __init__(self, *args):
        COMCUPluginBase.__init__(self, BrickletRGBLEDMatrix, *args)

        self.setupUi(self)

        self.rgb_led_matrix = self.device

        self.color_button = QColorButton()
        self.below_scribble_layout.insertWidget(2, self.color_button)

        self.scribble_widget = ScribbleWidget(8, 8, 35, self.color_button.color(), QColor(Qt.black))
        def set_state_scribble():
            self.state = self.STATE_COLOR_SCRIBBLE
        self.scribble_widget.scribbling_started.connect(set_state_scribble)
        self.scribble_layout.insertWidget(1, self.scribble_widget)

        self.qtcb_frame_started.connect(self.cb_frame_started)

        self.color_button.colorChanged.connect(self.color_changed)
        self.button_clear_drawing.clicked.connect(self.scribble_widget.clear_image)
        self.button_drawing.clicked.connect(self.drawing_clicked)
        self.button_color.clicked.connect(self.color_clicked)
        self.button_gradient.clicked.connect(self.gradient_clicked)
        self.button_dot.clicked.connect(self.dot_clicked)
        self.box_frame_duration.valueChanged.connect(self.frame_duration_changed)

        self.state = self.STATE_IDLE

        self.gradient_counter = 0
        self.dot_counter = 0
        self.dot_direction = 1

        self.voltage = 0

        self.cbe_supply_voltage = CallbackEmulator(self.rgb_led_matrix.get_supply_voltage,
                                                   None,
                                                   self.cb_supply_voltage,
                                                   self.increase_error_count)

    def set_rgb(self, r, g, b):
        async_call(self.rgb_led_matrix.set_red, r, None, self.increase_error_count)
        async_call(self.rgb_led_matrix.set_green, g, None, self.increase_error_count)
        async_call(self.rgb_led_matrix.set_blue, b, None, self.increase_error_count)

    def cb_supply_voltage(self, voltage):
        self.label_voltage.setText(str(voltage / 1000.0) + 'V')

    def cb_frame_started(self):
        if self.state == self.STATE_COLOR_GRADIENT:
            self.render_color_gradient()
        elif self.state == self.STATE_COLOR_DOT:
            self.render_color_dot()
        elif self.state == self.STATE_COLOR_SCRIBBLE:
            self.render_color_scribble()

    def color_changed(self):
        self.scribble_widget.set_foreground_color(self.color_button.color())

    def frame_duration_changed(self, duration):
        async_call(self.rgb_led_matrix.set_frame_duration, duration, None, self.increase_error_count)

    def drawing_clicked(self):
        old_state = self.state
        self.state = self.STATE_COLOR_SCRIBBLE
        if old_state == self.STATE_IDLE:
            self.render_color_scribble()

    def color_clicked(self):
        old_state = self.state
        self.state = self.STATE_COLOR_SCRIBBLE
        self.scribble_widget.fill_image(self.color_button.color())
        if old_state == self.STATE_IDLE:
            self.render_color_scribble()

    def gradient_clicked(self):
        old_state = self.state
        self.state = self.STATE_COLOR_GRADIENT
        if old_state == self.STATE_IDLE:
            self.render_color_gradient()

    def dot_clicked(self):
        self.dot_counter = 0
        self.dot_direction = 1
        old_state = self.state
        self.state = self.STATE_COLOR_DOT
        if old_state == self.STATE_IDLE:
            self.render_color_dot()

    def render_color_scribble(self):
        r = []
        g = []
        b = []
        for i in range(8):
            for j in range(8):
                color = QColor(self.scribble_widget.image().pixel(j, i))
                r.append(color.red())
                g.append(color.green())
                b.append(color.blue())

        self.set_rgb(r, g, b)

    def render_color_gradient(self):
        self.gradient_counter += NUM_LEDS * self.box_speed.value() / 100.0 / 4.0
        ra = []
        ga = []
        ba = []

        range_leds = list(range(NUM_LEDS))
        range_leds = range_leds[int(self.gradient_counter) % NUM_LEDS:] + range_leds[:int(self.gradient_counter) % NUM_LEDS]
        range_leds = reversed(range_leds)

        for i in range_leds:
            r, g, b = colorsys.hsv_to_rgb(1.0*i/NUM_LEDS, 1, 0.2)
            ra.append(int(r*255))
            ga.append(int(g*255))
            ba.append(int(b*255))

        self.scribble_widget.array_draw(ra, ga, ba, 4)
        self.set_rgb(ra, ga, ba)

    def render_color_dot(self):
        color = self.color_button.color()
        r = color.red()
        g = color.green()
        b = color.blue()

        self.dot_counter = self.dot_counter % NUM_LEDS

        index = self.dot_counter
        line = self.dot_counter // 8
        if line % 2:
            index = line*8 + (7 - (self.dot_counter % 8))

        r_val = [0]*NUM_LEDS
        g_val = [0]*NUM_LEDS
        b_val = [0]*NUM_LEDS

        r_val[index] = r
        g_val[index] = g
        b_val[index] = b

        self.scribble_widget.array_draw(r_val, g_val, b_val)
        self.set_rgb(r_val, g_val, b_val)

        self.dot_counter += self.dot_direction * self.box_speed.value()

        if self.dot_counter >= NUM_LEDS:
            self.dot_direction = -1
            self.dot_counter = NUM_LEDS - 1
        elif self.dot_counter < 0:
            self.dot_direction = 1
            self.dot_counter = 0

    def start(self):
        self.rgb_led_matrix.register_callback(self.rgb_led_matrix.CALLBACK_FRAME_STARTED, self.qtcb_frame_started.emit)
        self.cbe_supply_voltage.set_period(250)
        async_call(self.rgb_led_matrix.set_frame_duration, self.box_frame_duration.value(), None, self.increase_error_count)

    def stop(self):
        self.rgb_led_matrix.register_callback(self.rgb_led_matrix.CALLBACK_FRAME_STARTED, None)
        self.cbe_supply_voltage.set_period(0)
        async_call(self.rgb_led_matrix.set_frame_duration, 0, None, self.increase_error_count)

    def destroy(self):
        pass

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