def connect(self): config.UID_LED_STRIP_BRICKLET = None self.setup.label_led_strip_found.setText('No') self.setup.label_led_strip_uid.setText('None') config.UID_MULTI_TOUCH_BRICKLET = None self.setup.label_multi_touch_found.setText('No') self.setup.label_multi_touch_uid.setText('None') config.UID_DUAL_BUTTON_BRICKLET = (None, None) self.setup.label_dual_button1_found.setText('No') self.setup.label_dual_button1_uid.setText('None') self.setup.label_dual_button2_found.setText('No') self.setup.label_dual_button2_uid.setText('None') config.UID_PIEZO_SPEAKER_BRICKLET = None self.setup.label_piezo_speaker_found.setText('No') self.setup.label_piezo_speaker_uid.setText('None') config.UID_SEGMENT_DISPLAY_4X7_BRICKLET = None self.setup.label_segment_display_found.setText('No') self.setup.label_segment_display_uid.setText('None') if self.ipcon != None: try: self.ipcon.disconnect() except: pass self.ipcon = IPConnection() host = self.setup.edit_host.text() port = self.setup.spinbox_port.value() try: self.ipcon.connect(host, port) except Error as e: self.error_msg.showMessage('Connection Error: ' + str(e.description) + "<br><br>Brickd installed and running?") return except socket.error as e: self.error_msg.showMessage('Socket error: ' + str(e) + "<br><br>Brickd installed and running?") return self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, self.cb_enumerate) self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, self.cb_connected) # Wait for a second to give user visual feedback timer = QTimer(self) timer.setSingleShot(True) timer.timeout.connect(self.ipcon.enumerate) timer.start(250)
rgb = hsv_to_rgb(self.hues[y][x]*config.FIRE_HUE_FACTOR, 255, max(0, interpolate2(x, y) - self.values[y][x])) self.leds[x][config.LED_COLS-1-y] = rgb for x in range(config.LED_ROWS): rgb = hsv_to_rgb(self.hues[0][x]*config.FIRE_HUE_FACTOR, 255, max(0, interpolate1(x))) self.leds[x][config.LED_COLS-1] = rgb self.percent += 20 if self.percent >= 100: shift_up() generate_line() self.percent = 0 make_frame() if __name__ == "__main__": # Create IP Connection and connect it ipcon = IPConnection() ipcon.connect(config.HOST, config.PORT) # Create Fire object and start rendering fire = Fire(ipcon) fire.frame_rendered(0) input('Press enter to exit\n') ipcon.disconnect()
class Blinkenlights(QApplication): HOST = "localhost" PORT = 4223 ipcon = None projects = [] active_project = None error_msg = None def __init__(self, args): super(QApplication, self).__init__(args) self.error_msg = QErrorMessage() self.error_msg.setWindowTitle("Starter Kit: Blinkenlights Demo " + config.DEMO_VERSION) signal.signal(signal.SIGINT, self.exit_demo) signal.signal(signal.SIGTERM, self.exit_demo) self.make_gui() self.connect() def exit_demo(self, signl=None, frme=None): try: self.ipcon.disconnect() self.timer.stop() self.tabs.destroy() except: pass sys.exit() def make_gui(self): self.main = MainWindow(self) self.main.setWindowIcon(QIcon(load_pixmap('starter_kit_blinkenlights_demo-icon.png'))) self.tabs = QTabWidget() widget = QWidget() layout = QVBoxLayout() layout.addWidget(self.tabs) widget.setLayout(layout) self.main.setCentralWidget(widget) self.setup = SetupWidget(self.tabs, self) self.tetris = TetrisWidget(self.tabs, self) self.pong = PongWidget(self.tabs, self) self.fire = FireWidget(self.tabs, self) self.text = TextWidget(self.tabs, self) self.images = ImagesWidget(self.tabs, self) self.rainbow = RainbowWidget(self.tabs, self) self.projects.append(self.setup) self.projects.append(self.tetris) self.projects.append(self.pong) self.projects.append(self.fire) self.projects.append(self.text) self.projects.append(self.images) self.projects.append(self.rainbow) self.tabs.addTab(self.setup, "Setup") self.tabs.addTab(self.tetris, "Tetris") self.tabs.addTab(self.pong, "Pong") self.tabs.addTab(self.fire, "Fire") self.tabs.addTab(self.text, "Text") self.tabs.addTab(self.images, "Images") self.tabs.addTab(self.rainbow, "Rainbow") self.active_project = self.projects[0] self.tabs.currentChanged.connect(self.tab_changed_slot) self.main.setWindowTitle("Starter Kit: Blinkenlights Demo " + config.DEMO_VERSION) self.main.show() def connect(self): config.UID_LED_STRIP_BRICKLET = None self.setup.label_led_strip_found.setText('No') self.setup.label_led_strip_uid.setText('None') config.UID_MULTI_TOUCH_BRICKLET = None self.setup.label_multi_touch_found.setText('No') self.setup.label_multi_touch_uid.setText('None') config.UID_DUAL_BUTTON_BRICKLET = (None, None) self.setup.label_dual_button1_found.setText('No') self.setup.label_dual_button1_uid.setText('None') self.setup.label_dual_button2_found.setText('No') self.setup.label_dual_button2_uid.setText('None') config.UID_PIEZO_SPEAKER_BRICKLET = None self.setup.label_piezo_speaker_found.setText('No') self.setup.label_piezo_speaker_uid.setText('None') config.UID_SEGMENT_DISPLAY_4X7_BRICKLET = None self.setup.label_segment_display_found.setText('No') self.setup.label_segment_display_uid.setText('None') if self.ipcon != None: try: self.ipcon.disconnect() except: pass self.ipcon = IPConnection() host = self.setup.edit_host.text() port = self.setup.spinbox_port.value() try: self.ipcon.connect(host, port) except Error as e: self.error_msg.showMessage('Connection Error: ' + str(e.description) + "<br><br>Brickd installed and running?") return except socket.error as e: self.error_msg.showMessage('Socket error: ' + str(e) + "<br><br>Brickd installed and running?") return self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, self.cb_enumerate) self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, self.cb_connected) # Wait for a second to give user visual feedback timer = QTimer(self) timer.setSingleShot(True) timer.timeout.connect(self.ipcon.enumerate) timer.start(250) def tab_changed_slot(self, tabIndex): self.active_project.stop() self.active_project = self.projects[tabIndex] self.active_project.start() def cb_enumerate(self, uid, connected_uid, position, hardware_version, firmware_version, device_identifier, enumeration_type): if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \ enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE: if device_identifier == LEDStrip.DEVICE_IDENTIFIER: config.UID_LED_STRIP_BRICKLET = uid self.setup.label_led_strip_found.setText('Yes') self.setup.label_led_strip_uid.setText(uid) elif device_identifier == MultiTouch.DEVICE_IDENTIFIER: config.UID_MULTI_TOUCH_BRICKLET = uid self.setup.label_multi_touch_found.setText('Yes') self.setup.label_multi_touch_uid.setText(uid) elif device_identifier == DualButton.DEVICE_IDENTIFIER: if config.UID_DUAL_BUTTON_BRICKLET[0] == None: config.UID_DUAL_BUTTON_BRICKLET = (uid, None) self.setup.label_dual_button1_found.setText('Yes') self.setup.label_dual_button1_uid.setText(uid) else: config.UID_DUAL_BUTTON_BRICKLET = (config.UID_DUAL_BUTTON_BRICKLET[0], uid) self.setup.label_dual_button2_found.setText('Yes') self.setup.label_dual_button2_uid.setText(uid) elif device_identifier == PiezoSpeaker.DEVICE_IDENTIFIER: config.UID_PIEZO_SPEAKER_BRICKLET = uid self.setup.label_piezo_speaker_found.setText('Yes') self.setup.label_piezo_speaker_uid.setText(uid) elif device_identifier == SegmentDisplay4x7.DEVICE_IDENTIFIER: config.UID_SEGMENT_DISPLAY_4X7_BRICKLET = uid self.setup.label_segment_display_found.setText('Yes') self.setup.label_segment_display_uid.setText(uid) def cb_connected(self, connected_reason): if connected_reason == IPConnection.CONNECT_REASON_AUTO_RECONNECT: while True: try: self.ipcon.enumerate() break except Error as e: self.error_msg.showMessage('Enumerate Error: ' + str(e.description)) time.sleep(1)