Ver Fonte

Version 1.1 Code Ausgelagert in Klasse

devnull há 1 mês atrás
pai
commit
e8b6653d1b
2 ficheiros alterados com 172 adições e 157 exclusões
  1. 172 0
      kbdLED
  2. 0 157
      main.py

+ 172 - 0
kbdLED

@@ -0,0 +1,172 @@
+#!/usr/bin/python3
+# -*- coding: utf-8 -*-
+from typing import Dict
+
+# ließ und schreibt RGB Daten für die farbige Tastaturbeleuchtung auf Clevo NL51 Laptops
+# Notwendigerweise müssen die Treiber von NovaCustom oder Tuxedo kompiliert und geladen sein
+# siehe: https://wiki.siningsoft.de/doku.php?id=terra:1500p
+# Kernelmodul: tuxedo_keyboard
+
+# 2026-02-02 - devnull - initial
+# 2026-02-03 - devnull - added Brightness restore
+# 2026-02-04 - devnull - JSON statefile
+# 2026-02-11 - devnull - Einführung einer Klasse
+
+ver="1.1"
+
+import os
+import sys
+import argparse
+import json
+
+
+
+class kbdLED_class:
+
+    # 2026-02-11 - devnull - Einführung einer Klassendefinition
+
+    # diese Versionsnummer wird getrennt vom Programmcode geführt, da es sich getrennt entwickeln wird
+    __version__ = 1.1
+    config = Dict()
+
+    # Objekt erzeugen
+    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'):
+
+        self.config['sysfs']['color']=sysfs_color_fp
+        self.config['sysfs']['bri']=sysfs_bri_fp
+        self.config['state']=kbd_state_fp
+
+        if cmd=="start":
+            self.readStateFile(self.config['state'])
+            self.writeColor()
+            self.writeBrightness()
+
+        elif cmd=="stop":
+            self.readColor()
+            self.readBrightness()
+            self.writeStateFile(self.config['state'])
+
+
+
+    def readStateFile(kbd_state_fp):
+
+        config = {"color":
+                      {"R": 255,
+                       "G": 255,
+                       "B": 255},
+                  "Brightness": 255
+                  }
+
+        try:
+            with open(kbd_state_fp, 'r') as kbd_state_fh:
+                config = json.load(kbd_state_fh)
+
+        except FileNotFoundError:
+            print("keine Statusdatei gefunden, nutze Standardwerte")
+        except PermissionError:
+            print("Fehler beim lesen")
+
+        return config
+
+    def writeStateFile(kbd_state_fp):
+        try:
+            with open(kbd_state_fp, 'w') as kbd_state_fh:
+                json.dump(self.config, kbd_state_fh)
+
+        except FileNotFoundError:
+            print("Verzeichnis nicht gefunden")
+
+        return True
+
+    # color String nach Config Dict
+    def color2dict(self,color_string):
+        ca=color_string.split(" ")
+        self.config['color']['R'] = int(ca[0])
+        self.config['color']['G'] = int(ca[1])
+        self.config['color']['B'] = int(ca[2])
+
+    # color Confi Dict nach String
+    def dict2color(self):
+        return self.config['color']['R'] + " " + self.config['color']['G'] + " " + self.config['color']['B']
+
+    # reads the color Values from sysfs
+    def readColor(self):
+        try:
+            with open(self.config['sysfs']['sysfs_color_fp']) as fh:
+                self.color2dict(fh.read())
+
+        except FileNotFoundError:
+            print("- fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['sysfs_color_fp'])
+            sys.exit(1)
+        except PermissionError:
+            print("- keine Rechte die Datei zu lesen " + self.config['sysfs']['sysfs_color_fp'])
+            sys.exit(1)
+
+
+    # writes the color values to sysfs
+    def writeColor(self):
+        try:
+            with open(self.config['sysfs']['color']) as fh:
+                fh.write(self.dict2color())
+
+        except FileNotFoundError:
+            print("- fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['color'])
+            sys.exit(1)
+        except PermissionError:
+            print("- keine Rechte die Datei zu schreiben " + self.config['sysfs']['color'])
+            sys.exit(1)
+
+    # reads Brightness from sysfs
+    def readBrightness(self):
+        try:
+            with open(self.config['sysfs']['bri']) as fh:
+                self.config['Brightness'] = int(fh.read())
+
+        except FileNotFoundError:
+            print("- fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['bri'])
+            sys.exit(1)
+        except PermissionError:
+            print("- keine Rechte die Datei zu lesen " + self.config['sysfs']['bri'])
+            sys.exit(1)
+
+    # writes Brightness to sysfs
+    def writeBrightness(self):
+        try:
+            with open(self.config['sysfs']['bri']) as fh:
+                fh.write(self.config['Brightness'])
+
+        except FileNotFoundError:
+            print("- fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['bri'])
+            sys.exit(1)
+        except PermissionError:
+            print("- keine Rechte die Datei zu schreiben " + self.config['sysfs']['bri'])
+            sys.exit(1)
+
+
+# Press the green button in the gutter to run the script.
+if __name__ == '__main__':
+
+    parser = argparse.ArgumentParser(
+                    prog='T1500_KeyboardLED',
+                    description='saves and restore LED Colors',
+                    epilog='GPL')
+    startstop = parser.add_mutually_exclusive_group()
+    startstop.add_argument('--start', action='store_true', help='will be used for system starts')
+    startstop.add_argument('--stop', action='store_true', help='will be used for system shutdowns')
+    startstop.add_argument('--version', action='version', version='%(prog)s ' + ver, default='d')
+
+    args = parser.parse_args()
+
+    if args.start:
+        print("Keyboard: read and set Color and Brightness")
+        kL = kbdLED_class("start")
+
+    elif args.stop:
+        print("Keyboard: read and save Color and Brightness")
+        kL = kbdLED_class("stop")
+
+    else:
+        parser.print_help()
+        sys.exit((1))
+
+sys.exit(0)

+ 0 - 157
main.py

@@ -1,157 +0,0 @@
-# -*- coding: utf-8 -*-
-
-# ließ und schreibt RGB Daten für die farbige Tastaturbeleuchtung auf Clevo NL51 Laptops
-# Notwendigerweise müssen die Treiber von NovaCustom oder Tuxedo kompiliert und geladen sein
-# siehe: https://wiki.siningsoft.de/doku.php?id=terra:1500p
-# Kernelmodul: tuxedo_keyboard
-
-# 2026-02-02 - devnull - initial
-# 2026-02-03 - devnull - added Brightness restore
-
-ver="0.1"
-
-import os
-import sys
-import argparse
-
-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'
-keep_color_fp = '/var/lib/kbdLED.state'
-keep_bri_fp = '/var/lib/kbdBRI.state'
-led_color = []
-
-# ließt die zwischengespeicherte Werte vom Dateisystem
-def get_Color_Keep(kfp):
-    if os.access(kfp, os.R_OK):
-        try:
-            with open(kfp) as fh:
-                return fh.read()
-
-        except Exception as ex:
-            print("- fehler beim Öffnen der Status Datei")
-
-    else:
-        print("- kann Datei " + kfp + " nicht zum lesen öffnen, wird später geschrieben")
-        return "255 255 255"
-
-#schreibe die Haltewerte
-def set_Color_Keep(color, kfp):
-    try:
-        kfh = open(kfp, 'w')
-        kfh.write(color)
-        kfh.close()
-
-    except:
-        print("- Fehler beim Schreiben der Persistenzwerte")
-
-
-# ließt die aktuellen Werte der Tastaturbeleuchtung
-def get_Color_LED(afp):
-    if os.access(afp, os.R_OK):
-        try:
-            with open(afp) as fh:
-                return fh.read()
-
-        except Exception as ex:
-            print("- fehler beim Öffnen der Sysfs Datei " + afp)
-            print("- Kernel Modul tuxedo_keybaord geladen ?")
-            print(ex)
-            sys.exit(1)
-
-    else:
-        print("- kann Datei " + afp + " nicht zum lesen öffnen, falsche Rechte ?")
-        sys.exit(1)
-
-# schreibe die Haltewerte
-def set_Color_LED(color, afp):
-    try:
-        afh = open(afp, 'w')
-        afh.write(color)
-        afh.close()
-
-    except:
-        print("- Fehler beim Schreiben der Ist-Werte")
-
-# ließt die zwischengespeicherte Werte vom Dateisystem
-def get_Brig_Keep(kfp):
-    if os.access(kfp, os.R_OK):
-        try:
-            with open(kfp) as fh:
-                return fh.read()
-
-        except Exception as ex:
-            print("- fehler beim Öffnen der Status Datei")
-
-    else:
-        print("- kann Datei " + kfp + " nicht zum lesen öffnen, wird später geschrieben")
-        return "255"
-
-#schreibe die Haltewerte
-def set_Brig_Keep(color, kfp):
-    try:
-        kfh = open(kfp, 'w')
-        kfh.write(color)
-        kfh.close()
-
-    except:
-        print("- Fehler beim Schreiben der Persistenzwerte")
-
-
-# ließt die aktuellen Werte der Tastaturbeleuchtung
-def get_Brig_LED(afp):
-    if os.access(afp, os.R_OK):
-        try:
-            with open(afp) as fh:
-                return fh.read()
-
-        except Exception as ex:
-            print("- fehler beim Öffnen der Sysfs Datei " + afp)
-            print("- Kernel Modul tuxedo_keybaord geladen ?")
-            print(ex)
-            sys.exit(1)
-
-    else:
-        print("- kann Datei " + afp + " nicht zum lesen öffnen, falsche Rechte ?")
-        sys.exit(1)
-
-# schreibe die Haltewerte
-def set_Brig_LED(color, afp):
-    try:
-        afh = open(afp, 'w')
-        afh.write(color)
-        afh.close()
-
-    except:
-        print("- Fehler beim Schreiben der Ist-Werte")
-# teilt den Eingabestring in die drei Farben
-def splitColor(color:str):
-    color_array = color.split(' ')
-    led_R = color_array[0]
-    led_G = color_array[1]
-    led_B = color_array[2]
-
-# Press the green button in the gutter to run the script.
-if __name__ == '__main__':
-
-    parser = argparse.ArgumentParser(
-                    prog='T1500_KeyboardLED',
-                    description='saves and restore LED Colors',
-                    epilog='GPL')
-    startstop = parser.add_mutually_exclusive_group()
-    startstop.add_argument('--start', action='store_true', help='will be used for system starts')
-    startstop.add_argument('--stop', action='store_true', help='will be used for system shutdowns')
-    startstop.add_argument('--version', action='version', version='%(prog)s ' + ver, default='d')
-
-    args = parser.parse_args()
-
-    if args.start:
-        print("Startup set Keyboard Color and Brightness")
-        set_Color_LED(get_Brig_Keep(keep_color_fp), sysfs_color_fp)
-        set_Brig_LED(get_Brig_Keep(keep_bri_fp), sysfs_bri_fp)
-
-    elif args.stop:
-        print("Shutdown save Keyboard Color Brightness")
-        set_Color_Keep(get_Brig_LED(sysfs_color_fp), keep_color_fp)
-        set_Brig_Keep(get_Brig_LED(sysfs_bri_fp), keep_bri_fp)
-
-sys.exit(0)