#!/usr/bin/python3 # -*- coding: utf-8 -*- import serial from serial import SerialException import _thread import sys import time from PyQt5.QtWidgets import (QApplication, QMainWindow) from ui import Ui_MainWindow class AppWindow(QMainWindow): selectedLED = 14 ledBuf = [0] * 64 ser = serial.Serial() animations = [] def __init__(self): super().__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ser.baudrate = 115200 _thread.start_new_thread( self.read, () ) self.connect() for i in range(1,65): id = "radioButton_" + str(i) getattr(self.ui, id).toggled.connect(self.selectLED) self.ui.horizontalSlider.valueChanged.connect(self.changeLED) self.ui.comboBox.currentIndexChanged.connect(self.selectAnim) self.ui.comboBox.addItems(str(i) for i in range(64)) self.ui.spinBox.valueChanged.connect(self.setFrame) self.ui.spinBox_2.valueChanged.connect(self.setStart) self.ui.spinBox_3.valueChanged.connect(self.setLength) self.ui.pushButton.clicked.connect(self.connect) self.ui.pushButton_2.clicked.connect(self.startAnim) self.ui.pushButton_3.clicked.connect(self.pauseAnim) self.ui.pushButton_4.clicked.connect(self.stopAnim) self.ui.pushButton_5.clicked.connect(self.saveFrame) self.ui.pushButton_6.clicked.connect(self.clearFrame) self.ui.pushButton_7.clicked.connect(self.getFrame) self.ui.pushButton_8.clicked.connect(self.saveAnim) self.show() def setFrame(self): if self.ser.is_open: self.ser.write(b'~h') self.writeLng(self.ui.spinBox.value()) def setStart(self): id = self.ui.comboBox.currentIndex() self.animations[id][1] = self.sender().value() def setLength(self): id = self.ui.comboBox.currentIndex() self.animations[id][2] = self.sender().value() def startAnim(self): if self.ser.is_open: self.ser.write(b'~s') self.write(self.ui.comboBox.currentIndex()) def pauseAnim(self): if self.ser.is_open: self.ser.write(b'~t') def stopAnim(self): if self.ser.is_open: self.ser.write(b'~u~c ') oldFrame = 0; def saveFrame(self): if self.ser.is_open: newFrame = self.ui.spinBox.value() if int(self.oldFrame/16) != int(newFrame/16): self.ser.write(b'~x')# load page self.writeInt(newFrame/16) self.ser.write(b'~y')# write to buffer self.write(newFrame%16) if int(self.oldFrame/16) != int(newFrame/16): self.ser.write(b'~z')# write page self.writeInt(newFrame/16) self.status("saved") self.oldFrame = newFrame self.ui.spinBox.setValue(newFrame+1) def clearFrame(self): for i in range(64): btn = getattr(self.ui, "radioButton_" + str(i+1)) btn.setStyleSheet("background-color:rgba(255,0,0,0)") self.ledBuf[i] = 0 self.ui.horizontalSlider.setValue(0) def getFrame(self): if self.ser.is_open: self.ser.write(b'~g') self.ui.spinBox.setValue(int(self.ser.readline())) def saveAnim(self): id = self.ui.comboBox.currentIndex() if self.ser.is_open: self.ser.write(b'~b') self.write(id) self.writeLng(int(self.animations[id][1])) self.writeLng(int(self.animations[id][2])) def selectAnim(self): id = self.ui.comboBox.currentIndex() if len(self.animations) > id: self.ui.spinBox_2.setValue(int(self.animations[id][1])) self.ui.spinBox_3.setValue(int(self.animations[id][2])) def loadAnim(self): self.selectAnim(); print("loaded animations") def changeLED(self): val = self.sender().value() self.ledBuf[self.selectedLED] = val if self.ser.is_open: self.ser.write(b'~`') self.write(self.selectedLED+32) self.write(val+32) self.ui.label_7.setText("Value: " + str(val)) for i in range(1,65): btn = getattr(self.ui, "radioButton_" + str(i)) if btn.isChecked(): btn.setStyleSheet("background-color:rgba(255,0,0," + str(val*17) + ")") break def selectLED(self): id = self.sender().text() if id != "": self.selectedLED = int(id) self.ui.horizontalSlider.setValue(self.ledBuf[self.selectedLED]) def connect(self): port = self.ui.lineEdit.text() if self.ser.port == port and self.ser.is_open: return try: self.ser.port = port self.ser.open() self.animations = [] self.ser.write(b'~B~c ') self.status("connected") except serial.SerialException: self.status(self.ser.port + " not available") def status(self,txt): self.ui.label_6.setText(txt) def read(self): while 1: if self.ser.is_open: while self.ser.inWaiting()>0: line = self.ser.readline().decode("utf-8") print(line) if len(self.animations) < 64: self.animations.append(line.split(",")) if len(self.animations) == 64: self.loadAnim() time.sleep(.1) def write(self,val): self.ser.write((int(val)&0xFF).to_bytes(1, byteorder='big', signed=False)) def writeInt(self,val): self.ser.write((int(val)&0xFFFF).to_bytes(2, byteorder='big', signed=False)) def writeLng(self,val): self.ser.write((int(val)&0xFFFFFFFF).to_bytes(4, byteorder='big', signed=False)) app = QApplication(sys.argv) w = AppWindow() w.show() sys.exit(app.exec_())