From 8fd5dcb6eb5a8f10787c0bf766d70dde619dd7d0 Mon Sep 17 00:00:00 2001 From: Hofei Date: Mon, 30 Dec 2019 00:03:16 +0100 Subject: [PATCH] verschiedene modien --- uhr.py | 64 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/uhr.py b/uhr.py index b0db224..5a4420c 100644 --- a/uhr.py +++ b/uhr.py @@ -29,7 +29,6 @@ CONFIG = load_config() LED_COUNT = 60 # Number of LED pixels. LED_PIN = board.D18 # GPIO pin connected to the pixels (must support PWM!). LED_PIXEL_ORDER = neopixel.GRB # Strip type and colour ordering -LED_BRIGHTNESS = 0.05 # Zahl stellt den Wert in Minuten dar, wie lange kein Gerät erreichbar sein darf dass die Uhr "abgeschalten" wird ABSCHALTWERT = 4 @@ -51,13 +50,20 @@ stdliste = [std0, std1, std2, std3, std4, std5, std6, std7, std8, std9, std10, s class Uhr: - def __init__(self, pixels): + def __init__(self, pixels, mode): self.helligkeit = pixels.brightness self.pixels = pixels + self.helligkeit_geaendert = None + self.mode = mode + self._rgb_leer = None + self._rgb_sekunde = None + self._rgb_minute = None + self._rgb_stunde = None def set_helligkeit(self, helligkeit): self.helligkeit = helligkeit self.pixels.brightness = self.helligkeit + self.helligkeit_geaendert = datetime.datetime.now() def helligkeit_erhoehen(self): self.helligkeit += 0.05 @@ -71,6 +77,18 @@ class Uhr: self.helligkeit = 1 self.set_helligkeit(self.helligkeit) + def rgb_farben_lesen(self): + self._rgb_leer = CONFIG["mode"][self.mode]["leer"] + self._rgb_sekunde = CONFIG["mode"][self.mode]["sekunde"] + self._rgb_minute = CONFIG["mode"][self.mode]["minute"] + self._rgb_stunde = CONFIG["mode"][self.mode]["stunde"] + + rgbconf = {"rgb_leer": self._rgb_leer, "rgb_s": self._rgb_sekunde, "rgb_min": self._rgb_minute, + "rgb_std": self._rgb_stunde} + print("In der Klasse:") + print(rgbconf) + return rgbconf + # GPIO I_MODE_TASTER = gpiozero.Button(3) @@ -78,17 +96,10 @@ I_PLUS_TASTER = gpiozero.Button(4) I_MINUS_TASTER = gpiozero.Button(17) -# # # # # # # # # # -# Funktionen -# # # # # # # # # # -# Farbkonfiguration -def rgb_standard(): - rgb_leer = [0, 0, 0] - rgb_s = [0, 180, 0] - rgb_min = [32, 178, 170] - rgb_std = [255, 0, 255] - rgbconf = {"rgb_leer": rgb_leer, "rgb_s": rgb_s, "rgb_min": rgb_min, "rgb_std": rgb_std} - return rgbconf +def config_schreiben(pfad=SKRIPTPFAD): + configfile = os.path.join(pfad, "uhr_cfg.toml") + with open(configfile, "w") as conffile: + conffile.write(toml.dumps(CONFIG)) def alle_led(r, g, b, pixels): @@ -153,7 +164,7 @@ def shutdown(): # Threads -def check_anwesenheit(uhr): +def check_anwesenheit(uhr, pixels): """Funktion, welche als eigener Thread laeuft, um selbststaendig in einem gewissenen Intervall alle Geraete in der Toml Liste zu pingen arg: Objekt des neopixel LED Ringes @@ -178,7 +189,7 @@ def check_anwesenheit(uhr): """pingt die IP 3x an return (0 | !0) 0 wenn erreichbar """ - befehl = "/usr/bin/arping -c 3 -w 10 -b -f -I {interface} {ip}".format(interface=interface, ip=ip) + befehl = "/usr/bin/arping -q -c 3 -w 10 -b -f -I {interface} {ip}".format(interface=interface, ip=ip) cmd = shlex.split(befehl) return subprocess.call(cmd) @@ -219,10 +230,10 @@ def check_anwesenheit(uhr): if len(status_anwesend_liste) > ABSCHALTWERT: helligkeit = 0 if status["dimmen"]: - helligkeit = uhr.helligkeit * 0.5 + helligkeit = 0.03 if now.hour < 5 and not status["dimmen"]: helligkeit = 0 - uhr.set_helligkeit(helligkeit) + pixels.brightness = helligkeit last = datetime.datetime.now() delta = now - last time.sleep(30) @@ -231,12 +242,15 @@ def check_anwesenheit(uhr): def main(): lastsecond = None led_gesetzt = 0 - rgbconf = rgb_standard() - pixels = neopixel.NeoPixel(LED_PIN, LED_COUNT, brightness=LED_BRIGHTNESS, + + pixels = neopixel.NeoPixel(LED_PIN, LED_COUNT, brightness=CONFIG["led_helligkeit"], auto_write=False, pixel_order=LED_PIXEL_ORDER) - uhr = Uhr(pixels) - thread_check_wlan = threading.Thread(target=check_anwesenheit, args=(uhr,)) + uhr = Uhr(pixels, CONFIG["mode_nummer"]) + thread_check_wlan = threading.Thread(target=check_anwesenheit, args=(uhr, pixels)) thread_check_wlan.start() + + rgbconf = uhr.rgb_farben_lesen() + print(rgbconf) alle_led(rgbconf["rgb_leer"][0], rgbconf["rgb_leer"][1], rgbconf["rgb_leer"][2], pixels) I_PLUS_TASTER.when_pressed = uhr.helligkeit_erhoehen @@ -244,10 +258,16 @@ def main(): try: while True: - rgbconf = rgb_standard() + rgbconf = uhr.rgb_farben_lesen() + print(rgbconf) now = datetime.datetime.now() if lastsecond != now.second: lastsecond, led_gesetzt = led_calc(now, stdliste, rgbconf, led_gesetzt, pixels) + if uhr.helligkeit_geaendert is not None: + if (datetime.datetime.now() - uhr.helligkeit_geaendert) > datetime.timedelta(seconds=30): + CONFIG["led_helligkeit"] = uhr.helligkeit + config_schreiben() + uhr.helligkeit_geaendert = None time.sleep(0.2) finally: pixels.brightness = 0