Skip to content
Snippets Groups Projects
Verified Commit a2c6d90d authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Reformat led_cheese.py

parent 2ce3e9ff
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@ from credentials import *
from wifi import Wifi
from mytrix import Matrix
def set_colour(matches, event):
"""Set the RGB LED colour depending on the colour name in a message."""
colour = matches.group(1).lower().strip()
......@@ -27,32 +28,35 @@ def set_colour(matches, event):
# Mapping colour names to RGB values
colour_map = {
"schwarz": (0,0,0),
"blau": (0,0,255),
"rot": (255,0,0),
"gruen": (0,255,0),
"tuerkis": (0,255,255),
"lila": (139,0,139),
"orange": (255,140,0),
"pink": (255,105,180),
"rosa": (255,0,255),
"weiss": (255,255,255),
"schwarz": (0, 0, 0),
"blau": (0, 0, 255),
"rot": (255, 0, 0),
"gruen": (0, 255, 0),
"tuerkis": (0, 255, 255),
"lila": (139, 0, 139),
"orange": (255, 140, 0),
"pink": (255, 105, 180),
"rosa": (255, 0, 255),
"weiss": (255, 255, 255),
}
if is_allowed(sender):
if not colour in colour_map:
# The colour name is not in the mapping
matrix.send_room_message(ROOM_ID, "Diese Farbe kenne ich leider nicht, %s :(." % (sender,))
matrix.send_room_message(
ROOM_ID, "Diese Farbe kenne ich leider nicht, %s :(." % (sender,)
)
return
# Map to red, blue and green values, and set PWM duty (scaled from 256 to 1024)
r, g, b = colour_map[colour]
led_red.duty(r*4)
led_green.duty(g*4)
led_blue.duty(b*4)
led_red.duty(r * 4)
led_green.duty(g * 4)
led_blue.duty(b * 4)
matrix.send_room_message(ROOM_ID, "Ich leuchte jetzt %s, %s!" % (colour, sender))
def is_allowed(sender):
"""Check whether a sender is allowed to control the cheese."""
with open("users.txt", "rt") as f:
......@@ -64,6 +68,7 @@ def is_allowed(sender):
matrix.send_room_message(ROOM_ID, "Du hast mir gar nichts zu sagen, %s!" % (sender,))
return False
def allow_user(matches, event):
"""Add a user to the allow list."""
user = matches.group(1)
......@@ -74,6 +79,7 @@ def allow_user(matches, event):
f.write(user + "\n")
matrix.send_room_message(ROOM_ID, "%s darf mich jetzt leuchten lassen!" % (user,))
# Define LED colour pins and PWM objects
pin_red = Pin(13, Pin.OUT)
pin_green = Pin(12, Pin.OUT)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment