Modul ändern

change_neopixel_modul
Hofei 5 years ago
parent cf82fddbcb
commit 701b5ad4f4

141
uhr.py

@ -1,33 +1,25 @@
#!/usr/bin/python3 #!/usr/bin/python3
from datetime import datetime import datetime
from datetime import timedelta import os
import sys
import time
import shlex import shlex
import subprocess import subprocess
import toml import sys
import os
import threading import threading
import time
import gpiozero import gpiozero
import signal import toml
sys.path.append("/home/pi/rpi_ws281x") import neopixel
from neopixel import * import board
# # # # # # # # # # # LED strip configuration
# LED strip configuration:
# # # # # # # # # #
LED_COUNT = 60 # Number of LED pixels. LED_COUNT = 60 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!). LED_PIN = board.D18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz) LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5) LED_PIXEL_ORDER = neopixel.GRB # Strip type and colour ordering
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift) LED_BRIGHTNESS = 0.2
LED_BRIGHTNESS = 4 # Set to 0 for darkest and 255 for brightest
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP = ws.WS2811_STRIP_GRB # Strip type and colour ordering
standard_helligkeit = LED_BRIGHTNESS
# Zahl stellt den Wert in Minuten dar, wie lange kein Gerät erreichbar sein darf dass die Uhr "abgeschalten" wird # Zahl stellt den Wert in Minuten dar, wie lange kein Gerät erreichbar sein darf dass die Uhr "abgeschalten" wird
ABSCHALTWERT = 4 ABSCHALTWERT = 4
@ -51,9 +43,9 @@ stdliste = [std0, std1, std2, std3, std4, std5, std6, std7, std8, std9, std10, s
# # # # # # # # # # # # # # # # # # # #
# GPIO: # GPIO:
# # # # # # # # # # # # # # # # # # # #
I_MODE_TASTER = 3 I_MODE_TASTER = gpiozero.Button(3)
I_PLUS_TASTER = 4 I_PLUS_TASTER = gpiozero.Button(4)
I_MINUS_TASTER = 14 I_MINUS_TASTER = gpiozero.Button(14)
# # # # # # # # # # # # # # # # # # # #
@ -69,13 +61,12 @@ def rgb_standard():
return rgbconf return rgbconf
def alle_led(r, g, b, strip): def alle_led(r, g, b, pixels):
for pos in range(strip.numPixels()): pixels.fill(r, g, b)
strip.setPixelColorRGB(pos, r, g, b) pixels.show()
strip.show()
def led_calc(zeit, stdliste, rgbdict, led_gesetzt, strip): def led_calc(zeit, stdliste, rgbdict, led_gesetzt, pixels):
if zeit.hour > 11: if zeit.hour > 11:
index = zeit.hour - 12 index = zeit.hour - 12
else: else:
@ -98,62 +89,31 @@ def led_calc(zeit, stdliste, rgbdict, led_gesetzt, strip):
if {minute} & {second}: if {minute} & {second}:
for counter in range(0, 3): for counter in range(0, 3):
rgbdict["rgb_s"][counter] = int((rgbdict["rgb_min"][counter] + rgbdict["rgb_s"][counter]) / 2) rgbdict["rgb_s"][counter] = int((rgbdict["rgb_min"][counter] + rgbdict["rgb_s"][counter]) / 2)
led_gesetzt = led_setzen(hour, minute, second, rgbdict, led_gesetzt, strip) led_gesetzt = led_setzen(hour, minute, second, rgbdict, led_gesetzt, pixels)
return zeit.second, led_gesetzt return zeit.second, led_gesetzt
def led_setzen(hour, minute, second, rgbdict, led_gesetzt, strip): def led_setzen(hour, minute, second, rgbdict, led_gesetzt, pixels):
if not isinstance(led_gesetzt, list): if not isinstance(led_gesetzt, list):
led_gesetzt = [led_gesetzt] led_gesetzt = [led_gesetzt]
# Stunden setzen # Stunden setzen
for stunde in hour: for stunde in hour:
strip.setPixelColorRGB(stunde, rgbdict["rgb_std"][0], rgbdict["rgb_std"][1], rgbdict["rgb_std"][2]) pixels[stunde] = rgbdict["rgb_std"][0], rgbdict["rgb_std"][1], rgbdict["rgb_std"][2]
# print("Stunden setzen")
# print(stunde, rgbdict["rgb_std"][0], rgbdict["rgb_std"][1], rgbdict["rgb_std"][2])
# Minute setzen # Minute setzen
strip.setPixelColorRGB(minute, rgbdict["rgb_min"][0], rgbdict["rgb_min"][1], rgbdict["rgb_min"][2]) pixels[minute] = rgbdict["rgb_min"][0], rgbdict["rgb_min"][1], rgbdict["rgb_min"][2]
# print("Minute setzen")
# print(minute, rgbdict["rgb_min"][0], rgbdict["rgb_min"][1], rgbdict["rgb_min"][2])
# Sekunde setzen # Sekunde setzen
strip.setPixelColorRGB(second, rgbdict["rgb_s"][0], rgbdict["rgb_s"][1], rgbdict["rgb_s"][2]) pixels[second] = rgbdict["rgb_s"][0], rgbdict["rgb_s"][1], rgbdict["rgb_s"][2]
# print("Sekunde setzen")
# print(second, rgbdict["rgb_s"][0], rgbdict["rgb_s"][1], rgbdict["rgb_s"][2])
# Neue Leere LED berechnen # Neue Leere LED berechnen
led_gesetzt_neu = set(hour) | {minute} | {second} led_gesetzt_neu = set(hour) | {minute} | {second}
leerliste = list(set(led_gesetzt) - led_gesetzt_neu) leerliste = list(set(led_gesetzt) - led_gesetzt_neu)
for leer in leerliste: for leer in leerliste:
strip.setPixelColorRGB(leer, rgbdict["rgb_leer"][0], rgbdict["rgb_leer"][1], rgbdict["rgb_leer"][2]), pixels[leer] = rgbdict["rgb_leer"][0], rgbdict["rgb_leer"][1], rgbdict["rgb_leer"][2]
# print("Leer setzen")
# print(leer, rgbdict["rgb_leer"][0], rgbdict["rgb_leer"][1], rgbdict["rgb_leer"][2])
strip.show()
return list(led_gesetzt_neu)
def taster_callback(button):
start = datetime.now()
ende = datetime.now()
while button.is_pressed:
ende = datetime.now()
dauer = ende - start
dauer = dauer + timedelta(seconds=0.3)
pin_num = check_taster(button)
if pin_num == I_MODE_TASTER:
handler_mode_taster(dauer)
def check_taster(button):
pin_num = ""
for i in str(button.pin):
if i.isdigit():
pin_num = pin_num + str(i)
pin_num = int(pin_num)
return pin_num
pixels.show()
def handler_mode_taster(dauer): return list(led_gesetzt_neu)
if dauer > timedelta(seconds=3):
shutdown()
def shutdown(): def shutdown():
@ -162,10 +122,8 @@ def shutdown():
subprocess.call(cmd) subprocess.call(cmd)
# # # # # # # # # #
# Threads # Threads
# # # # # # # # # # def check_anwesenheit(pixels):
def check_anwesenheit(strip):
"""Funktion, welche als eigener Thread laeuft, um selbststaendig in einem gewissenen Intervall """Funktion, welche als eigener Thread laeuft, um selbststaendig in einem gewissenen Intervall
alle Geraete in der Toml Liste zu pingen alle Geraete in der Toml Liste zu pingen
arg: Objekt des neopixel LED Ringes arg: Objekt des neopixel LED Ringes
@ -191,12 +149,11 @@ def check_anwesenheit(strip):
with open(configfile) as conffile: with open(configfile) as conffile:
wlanliste = toml.loads(conffile.read()) wlanliste = toml.loads(conffile.read())
status_anwesend_liste = [] status_anwesend_liste = []
delta = timedelta(seconds=301) delta = datetime.timedelta(seconds=301)
last = datetime.now() last = datetime.datetime.now()
status = {} status = {}
global standard_helligkeit
while True: while True:
now = datetime.now() now = datetime.datetime.now()
# Status der IP Adressen ermitteln # Status der IP Adressen ermitteln
if delta.seconds > 300: if delta.seconds > 300:
for key_status in wlanliste.keys(): for key_status in wlanliste.keys():
@ -215,7 +172,7 @@ def check_anwesenheit(strip):
status[key_status] = False status[key_status] = False
if status["anwesend"]: if status["anwesend"]:
status_anwesend_liste = [] # Geraet von anwesend erreichbar status_anwesend_liste = [] # Geraet von anwesend erreichbar
helligkeit = standard_helligkeit helligkeit = LED_BRIGHTNESS
elif not status["anwesend"] and not status["dimmen"]: # Wenn kein Geraet erreichbar ist elif not status["anwesend"] and not status["dimmen"]: # Wenn kein Geraet erreichbar ist
if len(status_anwesend_liste) < ABSCHALTWERT + 1: if len(status_anwesend_liste) < ABSCHALTWERT + 1:
status_anwesend_liste.append(status["anwesend"]) status_anwesend_liste.append(status["anwesend"])
@ -223,40 +180,36 @@ def check_anwesenheit(strip):
if len(status_anwesend_liste) > ABSCHALTWERT: if len(status_anwesend_liste) > ABSCHALTWERT:
helligkeit = 0 helligkeit = 0
if status["dimmen"]: if status["dimmen"]:
helligkeit = standard_helligkeit * 0.5 helligkeit = LED_BRIGHTNESS * 0.5
if now.hour < 5 and not status["dimmen"]: if now.hour < 5 and not status["dimmen"]:
helligkeit = 0 helligkeit = 0
strip.setBrightness(int(helligkeit)) pixels.brightness = helligkeit
last = datetime.now() last = datetime.datetime.now()
delta = now - last delta = now - last
time.sleep(30) time.sleep(30)
def main(): def main():
mode_taster = gpiozero.Button(I_MODE_TASTER, hold_time=0.3)
mode_taster.when_pressed = taster_callback
lastsecond = None lastsecond = None
led_gesetzt = 0 led_gesetzt = 0
rgbconf = rgb_standard() rgbconf = rgb_standard()
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, pixels = neopixel.NeoPixel(LED_PIN, LED_COUNT, brightness=LED_BRIGHTNESS,
LED_CHANNEL, LED_STRIP) auto_write=False, pixel_order=LED_PIXEL_ORDER)
strip.begin()
strip.setBrightness(255) thread_check_wlan = threading.Thread(target=check_anwesenheit, args=(pixels,))
thread_check_wlan = threading.Thread(target=check_anwesenheit, args=(strip,))
thread_check_wlan.start() thread_check_wlan.start()
alle_led(rgbconf["rgb_leer"][0], rgbconf["rgb_leer"][1], rgbconf["rgb_leer"][2], strip) alle_led(rgbconf["rgb_leer"][0], rgbconf["rgb_leer"][1], rgbconf["rgb_leer"][2], pixels)
while True: while True:
try: try:
rgbconf = rgb_standard() rgbconf = rgb_standard()
now = datetime.now() now = datetime.datetime.now()
if lastsecond != now.second: if lastsecond != now.second:
lastsecond, led_gesetzt = led_calc(now, stdliste, rgbconf, led_gesetzt, strip) lastsecond, led_gesetzt = led_calc(now, stdliste, rgbconf, led_gesetzt, pixels)
time.sleep(0.2) time.sleep(0.2)
except KeyboardInterrupt: except KeyboardInterrupt:
print("KeyboardInterrupt") print("KeyboardInterrupt")
strip.setBrightness(0) pixels.brightness = 0
alle_led(0, 0, 0, strip) alle_led(0, 0, 0, pixels)
sys.exit() sys.exit()

Loading…
Cancel
Save