kbdLED 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # reads and writes RGB and brightness data for led background color of Clevo NL51 Laptops
  4. # ensure to compile drivers before ! read: https://wiki.siningsoft.de/doku.php?id=terra:1500p
  5. # Kernelmodul: tuxedo_keyboard
  6. # ToDo: add argument and function for switching color on commandline
  7. # ToDo: add argument and functions for switching brightness on commandline
  8. # 2026-02-02 - devnull - initial
  9. # 2026-02-03 - devnull - added Brightness restore
  10. # 2026-02-04 - devnull - JSON statefile
  11. # 2026-02-11 - devnull - created class
  12. # 2026-02-18 - devnull - added logging
  13. ver="1.1"
  14. import sys
  15. import argparse
  16. import json
  17. import logging
  18. class kbdLED_class:
  19. # 2026-02-11 - devnull - Einführung einer Klassendefinition
  20. # diese Versionsnummer wird getrennt vom Programmcode geführt, da es sich getrennt entwickeln wird
  21. __version__ = 1.1
  22. config = dict()
  23. # Objekt erzeugen
  24. def __init__(self, cmd, log, kbd_state_fp='/var/lib/kbdLED.state.json',
  25. sysfs_color_fp='/sys/devices/platform/tuxedo_keyboard/leds/rgb:kbd_backlight/multi_intensity',
  26. sysfs_bri_fp='/sys/devices/platform/tuxedo_keyboard/leds/rgb:kbd_backlight/brightness'):
  27. self.log = log
  28. self.config = {"sysfs" :
  29. {
  30. "color" : sysfs_color_fp ,
  31. "bri" : sysfs_bri_fp
  32. },
  33. "state" : kbd_state_fp}
  34. self.log.debug(str(self.config))
  35. if cmd=="start":
  36. self.readStateFile(self.config['state'])
  37. self.log.debug(str(self.config))
  38. self.writeColor()
  39. self.writeBrightness()
  40. elif cmd=="stop":
  41. self.readColor()
  42. self.readBrightness()
  43. self.writeStateFile(self.config['state'])
  44. def readStateFile(self,kbd_state_fp):
  45. config = {"color":
  46. {"R": 255,
  47. "G": 255,
  48. "B": 255},
  49. "Brightness": 255
  50. }
  51. try:
  52. with open(kbd_state_fp, 'r') as kbd_state_fh:
  53. self.config = json.load(kbd_state_fh)
  54. except FileNotFoundError:
  55. self.log.error("keine Statusdatei gefunden, nutze Standardwerte")
  56. except PermissionError:
  57. self.log.error("Fehler beim lesen")
  58. def writeStateFile(self,kbd_state_fp):
  59. try:
  60. with open(kbd_state_fp, 'w') as kbd_state_fh:
  61. json.dump(self.config,kbd_state_fh)
  62. except FileNotFoundError:
  63. self.log.error("Verzeichnis oder Datei nicht gefunden")
  64. # color String nach Config Dict
  65. def color2dict(self,color_string):
  66. ca=color_string.split(" ")
  67. self.config['color'] = {"R": int(ca[0]),
  68. "G": int(ca[1]),
  69. "B": int(ca[2])}
  70. # color Confi Dict nach String
  71. def dict2color(self):
  72. return str(self.config['color']['R']) + " " + str(self.config['color']['G']) + " " + str(self.config['color']['B'])
  73. # reads the color Values from sysfs
  74. def readColor(self):
  75. try:
  76. with open(self.config['sysfs']['color']) as fh:
  77. self.color2dict(fh.read())
  78. except FileNotFoundError:
  79. self.log.error("fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['color'])
  80. sys.exit(1)
  81. except PermissionError:
  82. self.log.error("keine Rechte die Datei zu lesen " + self.config['sysfs']['color'])
  83. sys.exit(1)
  84. # writes the color values to sysfs
  85. def writeColor(self):
  86. try:
  87. #with open(self.config['sysfs']['color']) as fh:
  88. # fh.write(self.dict2color())
  89. fh = open(self.config['sysfs']['color'], 'w')
  90. fh.write(self.dict2color())
  91. fh.close()
  92. except FileNotFoundError:
  93. self.log.error("fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['color'])
  94. sys.exit(1)
  95. except PermissionError:
  96. self.log.error("keine Rechte die Datei zu schreiben " + self.config['sysfs']['color'])
  97. sys.exit(1)
  98. # reads Brightness from sysfs
  99. def readBrightness(self):
  100. try:
  101. with open(self.config['sysfs']['bri']) as fh:
  102. self.config['Brightness'] = int(fh.read())
  103. except FileNotFoundError:
  104. self.log.error("fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['bri'])
  105. sys.exit(1)
  106. except PermissionError:
  107. self.log.error("keine Rechte die Datei zu lesen " + self.config['sysfs']['bri'])
  108. sys.exit(1)
  109. # writes Brightness to sysfs
  110. def writeBrightness(self):
  111. try:
  112. #with open(self.config['sysfs']['bri']) as fh:
  113. # fh.write(self.config['Brightness'])
  114. fh = open(self.config['sysfs']['bri'], 'w')
  115. fh.write(str(self.config['Brightness']))
  116. fh.close()
  117. except FileNotFoundError:
  118. self.log.error("- fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['bri'])
  119. sys.exit(1)
  120. except PermissionError:
  121. self.log.error("- keine Rechte die Datei zu schreiben " + self.config['sysfs']['bri'])
  122. sys.exit(1)
  123. # Press the green button in the gutter to run the script.
  124. # noinspection aK
  125. if __name__ == '__main__':
  126. #https: // argparse.readthedocs.io
  127. parser = argparse.ArgumentParser(
  128. prog='T1500_KeyboardLED',
  129. description='saves and restore LED Colors',
  130. epilog='GPL')
  131. output = parser.add_mutually_exclusive_group(required=False)
  132. output.add_argument('--verbose', action='store_true', help='extended logging', required=False)
  133. output.add_argument('--debug', action='store_true', help='debug output', required=False)
  134. startstop = parser.add_mutually_exclusive_group()
  135. startstop.add_argument('--start', action='store_true', help='will be used for system starts')
  136. startstop.add_argument('--stop', action='store_true', help='will be used for system shutdowns')
  137. startstop.add_argument('--version', action='version', version='%(prog)s ' + ver, default='d')
  138. args = parser.parse_args()
  139. if args.verbose:
  140. logging.basicConfig(level=logging.INFO)
  141. elif args.debug:
  142. logging.basicConfig(level=logging.DEBUG)
  143. else:
  144. logging.basicConfig(level=logging.ERROR)
  145. logging.basicConfig(format='%(asctime)s %(message)s')
  146. log = logging.getLogger(__name__)
  147. if args.start:
  148. print("Keyboard: read and set Color and Brightness")
  149. log.debug("System start")
  150. kL = kbdLED_class("start", log)
  151. elif args.stop:
  152. print("Keyboard: read and save Color and Brightness")
  153. log.debug("System stop")
  154. kL = kbdLED_class("stop", log)
  155. else:
  156. parser.print_help()
  157. sys.exit((10))
  158. sys.exit(0)