| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # reads and writes RGB and brightness data for led background color of Clevo NL51 Laptops
- # ensure to compile drivers before ! read: https://wiki.siningsoft.de/doku.php?id=terra:1500p
- # Kernelmodul: tuxedo_keyboard
- # ToDo: add argument and function for switching color on commandline
- # ToDo: add argument and functions for switching brightness on commandline
- # 2026-02-02 - devnull - initial
- # 2026-02-03 - devnull - added Brightness restore
- # 2026-02-04 - devnull - JSON statefile
- # 2026-02-11 - devnull - created class
- # 2026-02-18 - devnull - added logging
- ver="1.1"
- import sys
- import argparse
- import json
- import logging
- 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, log, 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.log = log
- self.config = {"sysfs" :
- {
- "color" : sysfs_color_fp ,
- "bri" : sysfs_bri_fp
- },
- "state" : kbd_state_fp}
- self.log.debug(str(self.config))
- if cmd=="start":
- self.readStateFile(self.config['state'])
- self.log.debug(str(self.config))
- self.writeColor()
- self.writeBrightness()
- elif cmd=="stop":
- self.readColor()
- self.readBrightness()
- self.writeStateFile(self.config['state'])
- def readStateFile(self,kbd_state_fp):
- config = {"color":
- {"R": 255,
- "G": 255,
- "B": 255},
- "Brightness": 255
- }
- try:
- with open(kbd_state_fp, 'r') as kbd_state_fh:
- self.config = json.load(kbd_state_fh)
- except FileNotFoundError:
- self.log.error("keine Statusdatei gefunden, nutze Standardwerte")
- except PermissionError:
- self.log.error("Fehler beim lesen")
- def writeStateFile(self,kbd_state_fp):
- try:
- with open(kbd_state_fp, 'w') as kbd_state_fh:
- json.dump(self.config,kbd_state_fh)
- except FileNotFoundError:
- self.log.error("Verzeichnis oder Datei nicht gefunden")
- # color String nach Config Dict
- def color2dict(self,color_string):
- ca=color_string.split(" ")
- self.config['color'] = {"R": int(ca[0]),
- "G": int(ca[1]),
- "B": int(ca[2])}
- # color Confi Dict nach String
- def dict2color(self):
- return str(self.config['color']['R']) + " " + str(self.config['color']['G']) + " " + str(self.config['color']['B'])
- # reads the color Values from sysfs
- def readColor(self):
- try:
- with open(self.config['sysfs']['color']) as fh:
- self.color2dict(fh.read())
- except FileNotFoundError:
- self.log.error("fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['color'])
- sys.exit(1)
- except PermissionError:
- self.log.error("keine Rechte die Datei zu lesen " + self.config['sysfs']['color'])
- 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())
- fh = open(self.config['sysfs']['color'], 'w')
- fh.write(self.dict2color())
- fh.close()
- except FileNotFoundError:
- self.log.error("fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['color'])
- sys.exit(1)
- except PermissionError:
- self.log.error("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:
- self.log.error("fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['bri'])
- sys.exit(1)
- except PermissionError:
- self.log.error("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'])
- fh = open(self.config['sysfs']['bri'], 'w')
- fh.write(str(self.config['Brightness']))
- fh.close()
- except FileNotFoundError:
- self.log.error("- fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['bri'])
- sys.exit(1)
- except PermissionError:
- self.log.error("- keine Rechte die Datei zu schreiben " + self.config['sysfs']['bri'])
- sys.exit(1)
- # Press the green button in the gutter to run the script.
- # noinspection aK
- if __name__ == '__main__':
- #https: // argparse.readthedocs.io
- parser = argparse.ArgumentParser(
- prog='T1500_KeyboardLED',
- description='saves and restore LED Colors',
- epilog='GPL')
- output = parser.add_mutually_exclusive_group(required=False)
- output.add_argument('--verbose', action='store_true', help='extended logging', required=False)
- output.add_argument('--debug', action='store_true', help='debug output', required=False)
- 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.verbose:
- logging.basicConfig(level=logging.INFO)
- elif args.debug:
- logging.basicConfig(level=logging.DEBUG)
- else:
- logging.basicConfig(level=logging.ERROR)
- logging.basicConfig(format='%(asctime)s %(message)s')
- log = logging.getLogger(__name__)
- if args.start:
- print("Keyboard: read and set Color and Brightness")
- log.debug("System start")
- kL = kbdLED_class("start", log)
- elif args.stop:
- print("Keyboard: read and save Color and Brightness")
- log.debug("System stop")
- kL = kbdLED_class("stop", log)
- else:
- parser.print_help()
- sys.exit((10))
- sys.exit(0)
|