Modul ändern

change_neopixel_modul
Hofei 5 years ago
parent cf82fddbcb
commit 701b5ad4f4

141
uhr.py

@ -1,33 +1,25 @@
#!/usr/bin/python3
from datetime import datetime
from datetime import timedelta
import sys
import time
import datetime
import os
import shlex
import subprocess
import toml
import os
import sys
import threading
import time
import gpiozero
import signal
sys.path.append("/home/pi/rpi_ws281x")
from neopixel import *
import toml
import neopixel
import board
# # # # # # # # # #
# LED strip configuration:
# # # # # # # # # #
# LED strip configuration
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_DMA = 5 # DMA channel to use for generating signal (try 5)
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
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
LED_PIXEL_ORDER = neopixel.GRB # Strip type and colour ordering
LED_BRIGHTNESS = 0.2
# Zahl stellt den Wert in Minuten dar, wie lange kein Gerät erreichbar sein darf dass die Uhr "abgeschalten" wird
ABSCHALTWERT = 4
@ -51,9 +43,9 @@ stdliste = [std0, std1, std2, std3, std4, std5, std6, std7, std8, std9, std10, s
# # # # # # # # # #
# GPIO:
# # # # # # # # # #
I_MODE_TASTER = 3
I_PLUS_TASTER = 4
I_MINUS_TASTER = 14
I_MODE_TASTER = gpiozero.Button(3)
I_PLUS_TASTER = gpiozero.Button(4)
I_MINUS_TASTER = gpiozero.Button(14)
# # # # # # # # # #
@ -69,13 +61,12 @@ def rgb_standard():
return rgbconf
def alle_led(r, g, b, strip):
for pos in range(strip.numPixels()):
strip.setPixelColorRGB(pos, r, g, b)
strip.show()
def alle_led(r, g, b, pixels):
pixels.fill(r, g, b)
pixels.show()
def led_calc(zeit, stdliste, rgbdict, led_gesetzt, strip):
def led_calc(zeit, stdliste, rgbdict, led_gesetzt, pixels):
if zeit.hour > 11:
index = zeit.hour - 12
else:
@ -98,62 +89,31 @@ def led_calc(zeit, stdliste, rgbdict, led_gesetzt, strip):
if {minute} & {second}:
for counter in range(0, 3):
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
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):
led_gesetzt = [led_gesetzt]
# Stunden setzen
for stunde in hour:
strip.setPixelColorRGB(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])
pixels[stunde] = rgbdict["rgb_std"][0], rgbdict["rgb_std"][1], rgbdict["rgb_std"][2]
# Minute setzen
strip.setPixelColorRGB(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])
pixels[minute] = rgbdict["rgb_min"][0], rgbdict["rgb_min"][1], rgbdict["rgb_min"][2]
# Sekunde setzen
strip.setPixelColorRGB(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])
pixels[second] = rgbdict["rgb_s"][0], rgbdict["rgb_s"][1], rgbdict["rgb_s"][2]
# Neue Leere LED berechnen
led_gesetzt_neu = set(hour) | {minute} | {second}
leerliste = list(set(led_gesetzt) - led_gesetzt_neu)
for leer in leerliste:
strip.setPixelColorRGB(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[leer] = rgbdict["rgb_leer"][0], rgbdict["rgb_leer"][1], rgbdict["rgb_leer"][2]
def handler_mode_taster(dauer):
if dauer > timedelta(seconds=3):
shutdown()
pixels.show()
return list(led_gesetzt_neu)
def shutdown():
@ -162,10 +122,8 @@ def shutdown():
subprocess.call(cmd)
# # # # # # # # # #
# Threads
# # # # # # # # # #
def check_anwesenheit(strip):
def check_anwesenheit(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
@ -191,12 +149,11 @@ def check_anwesenheit(strip):
with open(configfile) as conffile:
wlanliste = toml.loads(conffile.read())
status_anwesend_liste = []
delta = timedelta(seconds=301)
last = datetime.now()
delta = datetime.timedelta(seconds=301)
last = datetime.datetime.now()
status = {}
global standard_helligkeit
while True:
now = datetime.now()
now = datetime.datetime.now()
# Status der IP Adressen ermitteln
if delta.seconds > 300:
for key_status in wlanliste.keys():
@ -215,7 +172,7 @@ def check_anwesenheit(strip):
status[key_status] = False
if status["anwesend"]:
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
if len(status_anwesend_liste) < ABSCHALTWERT + 1:
status_anwesend_liste.append(status["anwesend"])
@ -223,40 +180,36 @@ def check_anwesenheit(strip):
if len(status_anwesend_liste) > ABSCHALTWERT:
helligkeit = 0
if status["dimmen"]:
helligkeit = standard_helligkeit * 0.5
helligkeit = LED_BRIGHTNESS * 0.5
if now.hour < 5 and not status["dimmen"]:
helligkeit = 0
strip.setBrightness(int(helligkeit))
last = datetime.now()
pixels.brightness = helligkeit
last = datetime.datetime.now()
delta = now - last
time.sleep(30)
def main():
mode_taster = gpiozero.Button(I_MODE_TASTER, hold_time=0.3)
mode_taster.when_pressed = taster_callback
lastsecond = None
led_gesetzt = 0
rgbconf = rgb_standard()
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS,
LED_CHANNEL, LED_STRIP)
strip.begin()
strip.setBrightness(255)
thread_check_wlan = threading.Thread(target=check_anwesenheit, args=(strip,))
pixels = neopixel.NeoPixel(LED_PIN, LED_COUNT, brightness=LED_BRIGHTNESS,
auto_write=False, pixel_order=LED_PIXEL_ORDER)
thread_check_wlan = threading.Thread(target=check_anwesenheit, args=(pixels,))
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:
try:
rgbconf = rgb_standard()
now = datetime.now()
now = datetime.datetime.now()
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)
except KeyboardInterrupt:
print("KeyboardInterrupt")
strip.setBrightness(0)
alle_led(0, 0, 0, strip)
pixels.brightness = 0
alle_led(0, 0, 0, pixels)
sys.exit()

Loading…
Cancel
Save