Simple RGB LED controller for Mac OS X
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

caselights.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # CaseLights Linux Qt System Tray client
  2. # depends on:
  3. # - python-pyqt5
  4. # - python-pyserial
  5. import sys
  6. import serial, serial.tools, serial.tools.list_ports
  7. from PyQt5 import QtWidgets, QtGui, QtCore
  8. from PyQt5.QtWidgets import QSystemTrayIcon, QAction, QMenu
  9. from PyQt5.QtGui import QIcon, QPixmap
  10. from PyQt5.QtCore import QCoreApplication, QSettings
  11. class CaseLights():
  12. name = "CaseLights"
  13. vendor = "xythobuz"
  14. version = "0.1"
  15. staticColors = [
  16. [ "Off", "0", "0", "0", None ],
  17. [ "Red", "255", "0", "0", None ],
  18. [ "Green", "0", "255", "0", None ],
  19. [ "Blue", "0", "0", "255", None ],
  20. [ "White", "255", "255", "255", None ],
  21. ]
  22. usedPort = None
  23. serial = None
  24. menu = None
  25. portMenu = None
  26. portActions = None
  27. refreshAction = None
  28. quitAction = None
  29. def __init__(self):
  30. app = QtWidgets.QApplication(sys.argv)
  31. QCoreApplication.setApplicationName(self.name)
  32. if not QSystemTrayIcon.isSystemTrayAvailable():
  33. print("System Tray is not available on this platform!")
  34. sys.exit(0)
  35. self.readSettings()
  36. if self.usedPort is not None:
  37. self.connect()
  38. pic = QPixmap(32, 32)
  39. pic.load("icon.png")
  40. icon = QIcon(pic)
  41. self.menu = QMenu()
  42. colorMenu = QMenu("&Colors")
  43. for color in self.staticColors:
  44. color[4] = QAction(color[0])
  45. colorMenu.addAction(color[4])
  46. colorMenu.triggered.connect(self.setStaticColor)
  47. self.menu.addMenu(colorMenu)
  48. lightMenu = QMenu("&UV-Light")
  49. lightOnAction = QAction("O&n")
  50. lightOnAction.triggered.connect(self.lightsOn)
  51. lightMenu.addAction(lightOnAction)
  52. lightOffAction = QAction("O&ff")
  53. lightOffAction.triggered.connect(self.lightsOff)
  54. lightMenu.addAction(lightOffAction)
  55. self.menu.addMenu(lightMenu)
  56. self.refreshSerialPorts()
  57. self.quitAction = QAction("&Quit")
  58. self.quitAction.triggered.connect(self.exit)
  59. self.menu.addAction(self.quitAction)
  60. trayIcon = QSystemTrayIcon(icon)
  61. trayIcon.setToolTip(self.name + " " + self.version)
  62. trayIcon.setContextMenu(self.menu)
  63. trayIcon.setVisible(True)
  64. sys.exit(app.exec_())
  65. def exit(self):
  66. if self.serial is not None:
  67. if self.serial.is_open:
  68. print("turning off lights")
  69. self.serial.write(b'RGB 0 0 0\n')
  70. self.serial.write(b'UV 0\n')
  71. print("closing connection")
  72. self.serial.close()
  73. QCoreApplication.quit()
  74. def readSettings(self):
  75. settings = QSettings(self.vendor, self.name)
  76. self.usedPort = settings.value("serial_port")
  77. if self.usedPort is not None:
  78. print("serial port stored: " + self.usedPort)
  79. else:
  80. print("no serial port stored")
  81. def writeSettings(self):
  82. settings = QSettings(self.vendor, self.name)
  83. settings.setValue("serial_port", self.usedPort)
  84. if self.usedPort is not None:
  85. print("storing serial port: " + self.usedPort)
  86. else:
  87. print("not storing any serial port")
  88. del settings
  89. def refreshSerialPorts(self):
  90. self.portMenu = QMenu("Port")
  91. ports = serial.tools.list_ports.comports()
  92. self.portActions = []
  93. for port in ports:
  94. action = QAction(port.device)
  95. self.portActions.append(action)
  96. self.portMenu.addAction(action)
  97. self.portMenu.triggered.connect(self.selectSerialPort)
  98. if self.refreshAction == None:
  99. self.refreshAction = QAction("&Refresh")
  100. self.refreshAction.triggered.connect(self.refreshSerialPorts)
  101. self.portMenu.addAction(self.refreshAction)
  102. self.menu.insertMenu(self.quitAction, self.portMenu)
  103. def selectSerialPort(self, action):
  104. self.usedPort = action.text()
  105. self.writeSettings()
  106. if self.connect():
  107. self.portMenu.setActiveAction(action)
  108. def connect(self):
  109. if self.usedPort is None:
  110. print("not connecting to any serial port")
  111. return False
  112. if self.serial is not None:
  113. print("closing previous port")
  114. self.serial.close()
  115. self.serial = serial.Serial()
  116. self.serial.port = self.usedPort
  117. self.serial.baudrate = 115200
  118. self.serial.open()
  119. if self.serial.is_open:
  120. print("connected to: " + self.usedPort)
  121. else:
  122. print("error connecting to: " + self.usedPort)
  123. return self.serial.is_open
  124. def setStaticColor(self, action):
  125. for color in self.staticColors:
  126. if color[4] is action:
  127. if self.serial.is_open:
  128. r = str.encode(color[1])
  129. g = str.encode(color[2])
  130. b = str.encode(color[3])
  131. self.serial.write(b'RGB ' + r + b' ' + g + b' ' + b + b'\n')
  132. else:
  133. print("not connected")
  134. return True
  135. print("color not found")
  136. return False
  137. def lightsOn(self):
  138. if self.serial.is_open:
  139. self.serial.write(b'UV 1\n')
  140. else:
  141. print("not connected")
  142. def lightsOff(self):
  143. if self.serial.is_open:
  144. self.serial.write(b'UV 0\n')
  145. else:
  146. print("not connected")
  147. if __name__ == "__main__":
  148. tray = CaseLights()