kbdLED 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. from typing import Dict
  4. # ließ und schreibt RGB Daten für die farbige Tastaturbeleuchtung auf Clevo NL51 Laptops
  5. # Notwendigerweise müssen die Treiber von NovaCustom oder Tuxedo kompiliert und geladen sein
  6. # siehe: https://wiki.siningsoft.de/doku.php?id=terra:1500p
  7. # Kernelmodul: tuxedo_keyboard
  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 - Einführung einer Klasse
  12. ver="1.1"
  13. import os
  14. import sys
  15. import argparse
  16. import json
  17. class kbdLED_class:
  18. # 2026-02-11 - devnull - Einführung einer Klassendefinition
  19. # diese Versionsnummer wird getrennt vom Programmcode geführt, da es sich getrennt entwickeln wird
  20. __version__ = 1.1
  21. config = Dict()
  22. # Objekt erzeugen
  23. def __init__(self, cmd, kbd_state_fp='/var/lib/kbdLED.state.json', sysfs_color_fp='/sys/devices/platform/tuxedo_keyboard/leds/rgb:kbd_backlight/multi_intensity', sysfs_bri_fp='/sys/devices/platform/tuxedo_keyboard/leds/rgb:kbd_backlight/brightness'):
  24. self.config['sysfs']['color']=sysfs_color_fp
  25. self.config['sysfs']['bri']=sysfs_bri_fp
  26. self.config['state']=kbd_state_fp
  27. if cmd=="start":
  28. self.readStateFile(self.config['state'])
  29. self.writeColor()
  30. self.writeBrightness()
  31. elif cmd=="stop":
  32. self.readColor()
  33. self.readBrightness()
  34. self.writeStateFile(self.config['state'])
  35. def readStateFile(kbd_state_fp):
  36. config = {"color":
  37. {"R": 255,
  38. "G": 255,
  39. "B": 255},
  40. "Brightness": 255
  41. }
  42. try:
  43. with open(kbd_state_fp, 'r') as kbd_state_fh:
  44. config = json.load(kbd_state_fh)
  45. except FileNotFoundError:
  46. print("keine Statusdatei gefunden, nutze Standardwerte")
  47. except PermissionError:
  48. print("Fehler beim lesen")
  49. return config
  50. def writeStateFile(kbd_state_fp):
  51. try:
  52. with open(kbd_state_fp, 'w') as kbd_state_fh:
  53. json.dump(self.config, kbd_state_fh)
  54. except FileNotFoundError:
  55. print("Verzeichnis nicht gefunden")
  56. return True
  57. # color String nach Config Dict
  58. def color2dict(self,color_string):
  59. ca=color_string.split(" ")
  60. self.config['color']['R'] = int(ca[0])
  61. self.config['color']['G'] = int(ca[1])
  62. self.config['color']['B'] = int(ca[2])
  63. # color Confi Dict nach String
  64. def dict2color(self):
  65. return self.config['color']['R'] + " " + self.config['color']['G'] + " " + self.config['color']['B']
  66. # reads the color Values from sysfs
  67. def readColor(self):
  68. try:
  69. with open(self.config['sysfs']['sysfs_color_fp']) as fh:
  70. self.color2dict(fh.read())
  71. except FileNotFoundError:
  72. print("- fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['sysfs_color_fp'])
  73. sys.exit(1)
  74. except PermissionError:
  75. print("- keine Rechte die Datei zu lesen " + self.config['sysfs']['sysfs_color_fp'])
  76. sys.exit(1)
  77. # writes the color values to sysfs
  78. def writeColor(self):
  79. try:
  80. with open(self.config['sysfs']['color']) as fh:
  81. fh.write(self.dict2color())
  82. except FileNotFoundError:
  83. print("- fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['color'])
  84. sys.exit(1)
  85. except PermissionError:
  86. print("- keine Rechte die Datei zu schreiben " + self.config['sysfs']['color'])
  87. sys.exit(1)
  88. # reads Brightness from sysfs
  89. def readBrightness(self):
  90. try:
  91. with open(self.config['sysfs']['bri']) as fh:
  92. self.config['Brightness'] = int(fh.read())
  93. except FileNotFoundError:
  94. print("- fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['bri'])
  95. sys.exit(1)
  96. except PermissionError:
  97. print("- keine Rechte die Datei zu lesen " + self.config['sysfs']['bri'])
  98. sys.exit(1)
  99. # writes Brightness to sysfs
  100. def writeBrightness(self):
  101. try:
  102. with open(self.config['sysfs']['bri']) as fh:
  103. fh.write(self.config['Brightness'])
  104. except FileNotFoundError:
  105. print("- fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['bri'])
  106. sys.exit(1)
  107. except PermissionError:
  108. print("- keine Rechte die Datei zu schreiben " + self.config['sysfs']['bri'])
  109. sys.exit(1)
  110. # Press the green button in the gutter to run the script.
  111. if __name__ == '__main__':
  112. parser = argparse.ArgumentParser(
  113. prog='T1500_KeyboardLED',
  114. description='saves and restore LED Colors',
  115. epilog='GPL')
  116. startstop = parser.add_mutually_exclusive_group()
  117. startstop.add_argument('--start', action='store_true', help='will be used for system starts')
  118. startstop.add_argument('--stop', action='store_true', help='will be used for system shutdowns')
  119. startstop.add_argument('--version', action='version', version='%(prog)s ' + ver, default='d')
  120. args = parser.parse_args()
  121. if args.start:
  122. print("Keyboard: read and set Color and Brightness")
  123. kL = kbdLED_class("start")
  124. elif args.stop:
  125. print("Keyboard: read and save Color and Brightness")
  126. kL = kbdLED_class("stop")
  127. else:
  128. parser.print_help()
  129. sys.exit((1))
  130. sys.exit(0)