#!/usr/bin/python3 from gpiozero import Button, LED from signal import pause from threading import Thread import serial import time from sys import argv import subprocess from math import ceil if len(argv) > 1: serial_port = argv[1] else: serial_port = "/dev/ttyUSB0" with open("zenders.csv") as file: ZENDERLIJST = [("Uitschakelen", None), ("AirPlay", None)] + [tuple(line.strip().split("\t")) for line in file.readlines()] class NoritakeDisplay: def __init__(self, port, baudrate=19200, parity=serial.PARITY_EVEN, **kwargs): self._serial = serial.Serial(port, baudrate=baudrate, parity=parity, **kwargs) def reset(self): self._serial.write(bytes([0x1B])) time.sleep(0.01) self._serial.write(bytes([0x49])) time.sleep(0.01) def clear(self): self._serial.write(bytes([0x0E])) time.sleep(0.01) def goto_begin(self): self._serial.write(bytes([0x0C])) time.sleep(0.01) def block_cursor(self): self._serial.write(bytes([0x15])) time.sleep(0.01) def line_cursor(self): self._serial.write(bytes([0x14])) time.sleep(0.01) def cursor_off(self): self._serial.write(bytes([0x16])) time.sleep(0.01) def print_text(self, text): # self.clear() self.goto_begin() self._serial.write(text.encode('ascii')) def center_text(self, text): # self.clear() self.goto_begin() start = ceil((20 - len(text))/2) self._serial.write((" "*start).encode('ascii')) self._serial.write(text.encode('ascii')) self._serial.write((" "*(20 - start - len(text))).encode('ascii')) def type_text(self, text): self.clear() self.goto_begin() for letter in text: self._serial.write(letter.encode('ascii')) time.sleep(.02) class WebRadio: knop = Button(14) links = Button(15) rechts = Button(18) zenderfile = "laatste_zender.txt" laatst_gedraaid = None def __init__(self, display): # Voeg display toe aan onze radio self.display = display # Stel interrupts in voor de knoppen self.knop.when_pressed = self.button_pressed self.links.when_pressed = self.dial_turned # Start een thread om de staat van het display te controleren self.thread = Thread(target=self.check_display) self.thread.start() self.mplayer = None with open(WebRadio.zenderfile, "r") as file: self.zender = int(file.read().strip()) self.gekozen_zender = self.zender print(ZENDERLIJST[self.zender][0]) self.display.center_text(ZENDERLIJST[self.zender][0]) self.switch_zender() def button_pressed(self): self.gekozen_zender = self.zender if self.zender == 0: self.afsluiten() else: self.switch_zender() with open(self.zenderfile, "w") as file: file.write(str(self.gekozen_zender)) def afsluiten(self): self.mplayer.terminate() self.display.center_text("Doei!!") self.thread.join() time.sleep(2) self.display.clear() subprocess.call(["sudo", "shutdown", "-h", "now"]) quit(0) def switch_zender(self): if self.mplayer: self.mplayer.terminate() if self.zender == 1: self.mplayer = subprocess.Popen(["/home/pi/projects/airplay-audio-project/shairport/shairport", "-a", "RaspberryPi"], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL) else: self.mplayer = subprocess.Popen(["mplayer", *ZENDERLIJST[self.zender][1:]], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL) def dial_turned(self): if self.rechts.is_pressed: self.zender = self.zender + 1 if self.zender < len(ZENDERLIJST) - 1 else 0 else: self.zender = self.zender - 1 if self.zender > 0 else len(ZENDERLIJST) - 1 print(ZENDERLIJST[self.zender][0]) self.display.center_text(ZENDERLIJST[self.zender][0]) self.laatst_gedraaid = time.time() def check_display(self): while True: time.sleep(0.5) if self.gekozen_zender == 0: break if self.laatst_gedraaid and time.time() - self.laatst_gedraaid > 5 and self.zender != self.gekozen_zender: self.zender = self.gekozen_zender print(ZENDERLIJST[self.zender][0]) self.display.center_text(ZENDERLIJST[self.zender][0]) self.laatst_gedraaid = None display = NoritakeDisplay(serial_port) webradio = WebRadio(display=display) pause()