gui.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import serial
  4. from serial import SerialException
  5. import _thread
  6. import sys
  7. import time
  8. from PyQt5.QtWidgets import (QApplication, QMainWindow)
  9. from ui import Ui_MainWindow
  10. class AppWindow(QMainWindow):
  11. selectedLED = 14
  12. ledBuf = [0] * 64
  13. ser = serial.Serial()
  14. animations = []
  15. def __init__(self):
  16. super().__init__()
  17. self.ui = Ui_MainWindow()
  18. self.ui.setupUi(self)
  19. self.ser.baudrate = 115200
  20. _thread.start_new_thread( self.read, () )
  21. self.connect()
  22. for i in range(1,65):
  23. id = "radioButton_" + str(i)
  24. getattr(self.ui, id).toggled.connect(self.selectLED)
  25. self.ui.horizontalSlider.valueChanged.connect(self.changeLED)
  26. self.ui.comboBox.currentIndexChanged.connect(self.selectAnim)
  27. self.ui.comboBox.addItems(str(i) for i in range(64))
  28. self.ui.spinBox.valueChanged.connect(self.setFrame)
  29. self.ui.spinBox_2.valueChanged.connect(self.setStart)
  30. self.ui.spinBox_3.valueChanged.connect(self.setLength)
  31. self.ui.pushButton.clicked.connect(self.connect)
  32. self.ui.pushButton_2.clicked.connect(self.startAnim)
  33. self.ui.pushButton_3.clicked.connect(self.pauseAnim)
  34. self.ui.pushButton_4.clicked.connect(self.stopAnim)
  35. self.ui.pushButton_5.clicked.connect(self.saveFrame)
  36. self.ui.pushButton_6.clicked.connect(self.clearFrame)
  37. self.ui.pushButton_7.clicked.connect(self.getFrame)
  38. self.ui.pushButton_8.clicked.connect(self.saveAnim)
  39. self.show()
  40. def setFrame(self):
  41. if self.ser.is_open:
  42. self.ser.write(b'~h')
  43. self.writeLng(self.ui.spinBox.value())
  44. def setStart(self):
  45. id = self.ui.comboBox.currentIndex()
  46. self.animations[id][1] = self.sender().value()
  47. def setLength(self):
  48. id = self.ui.comboBox.currentIndex()
  49. self.animations[id][2] = self.sender().value()
  50. def startAnim(self):
  51. if self.ser.is_open:
  52. self.ser.write(b'~s')
  53. self.write(self.ui.comboBox.currentIndex())
  54. def pauseAnim(self):
  55. if self.ser.is_open:
  56. self.ser.write(b'~t')
  57. def stopAnim(self):
  58. if self.ser.is_open:
  59. self.ser.write(b'~u~c ')
  60. oldFrame = 0;
  61. def saveFrame(self):
  62. if self.ser.is_open:
  63. newFrame = self.ui.spinBox.value()
  64. if int(self.oldFrame/16) != int(newFrame/16):
  65. self.ser.write(b'~x')# load page
  66. self.writeInt(newFrame/16)
  67. self.ser.write(b'~y')# write to buffer
  68. self.write(newFrame%16)
  69. if int(self.oldFrame/16) != int(newFrame/16):
  70. self.ser.write(b'~z')# write page
  71. self.writeInt(newFrame/16)
  72. self.status("saved")
  73. self.oldFrame = newFrame
  74. self.ui.spinBox.setValue(newFrame+1)
  75. def clearFrame(self):
  76. for i in range(64):
  77. btn = getattr(self.ui, "radioButton_" + str(i+1))
  78. btn.setStyleSheet("background-color:rgba(255,0,0,0)")
  79. self.ledBuf[i] = 0
  80. self.ui.horizontalSlider.setValue(0)
  81. def getFrame(self):
  82. if self.ser.is_open:
  83. self.ser.write(b'~g')
  84. self.ui.spinBox.setValue(int(self.ser.readline()))
  85. def saveAnim(self):
  86. id = self.ui.comboBox.currentIndex()
  87. if self.ser.is_open:
  88. self.ser.write(b'~b')
  89. self.write(id)
  90. self.writeLng(int(self.animations[id][1]))
  91. self.writeLng(int(self.animations[id][2]))
  92. def selectAnim(self):
  93. id = self.ui.comboBox.currentIndex()
  94. if len(self.animations) > id:
  95. self.ui.spinBox_2.setValue(int(self.animations[id][1]))
  96. self.ui.spinBox_3.setValue(int(self.animations[id][2]))
  97. def loadAnim(self):
  98. self.selectAnim();
  99. print("loaded animations")
  100. def changeLED(self):
  101. val = self.sender().value()
  102. self.ledBuf[self.selectedLED] = val
  103. if self.ser.is_open:
  104. self.ser.write(b'~`')
  105. self.write(self.selectedLED+32)
  106. self.write(val+32)
  107. self.ui.label_7.setText("Value: " + str(val))
  108. for i in range(1,65):
  109. btn = getattr(self.ui, "radioButton_" + str(i))
  110. if btn.isChecked():
  111. btn.setStyleSheet("background-color:rgba(255,0,0," + str(val*17) + ")")
  112. break
  113. def selectLED(self):
  114. id = self.sender().text()
  115. if id != "":
  116. self.selectedLED = int(id)
  117. self.ui.horizontalSlider.setValue(self.ledBuf[self.selectedLED])
  118. def connect(self):
  119. port = self.ui.lineEdit.text()
  120. if self.ser.port == port and self.ser.is_open:
  121. return
  122. try:
  123. self.ser.port = port
  124. self.ser.open()
  125. self.animations = []
  126. self.ser.write(b'~B~c ')
  127. self.status("connected")
  128. except serial.SerialException:
  129. self.status(self.ser.port + " not available")
  130. def status(self,txt):
  131. self.ui.label_6.setText(txt)
  132. def read(self):
  133. while 1:
  134. if self.ser.is_open:
  135. while self.ser.inWaiting()>0:
  136. line = self.ser.readline().decode("utf-8")
  137. print(line)
  138. if len(self.animations) < 64:
  139. self.animations.append(line.split(","))
  140. if len(self.animations) == 64:
  141. self.loadAnim()
  142. time.sleep(.1)
  143. def write(self,val):
  144. self.ser.write((int(val)&0xFF).to_bytes(1, byteorder='big', signed=False))
  145. def writeInt(self,val):
  146. self.ser.write((int(val)&0xFFFF).to_bytes(2, byteorder='big', signed=False))
  147. def writeLng(self,val):
  148. self.ser.write((int(val)&0xFFFFFFFF).to_bytes(4, byteorder='big', signed=False))
  149. app = QApplication(sys.argv)
  150. w = AppWindow()
  151. w.show()
  152. sys.exit(app.exec_())