added test feature and credentials

This commit is contained in:
cpu
2023-12-11 16:40:53 +01:00
parent fe274974c4
commit 8b0fd02e09
4 changed files with 95 additions and 8 deletions

1
credentials.txt Normal file
View File

@@ -0,0 +1 @@
foo:bar

46
dummygpio.py Normal file
View File

@@ -0,0 +1,46 @@
import time
HIGH = True
LOW = False
IN = 0
PUD_UP = None
DUMMY_FALLING_EDGE = "FALLING"
CHANGE_PIN_VALUE_AFTER = 8 # Transition to HIGH after 5 seconds
BCM = 0
FALLING = None
pull_up_down = {PUD_UP: DUMMY_FALLING_EDGE}
state = HIGH
pin_last_change = {}
def setwarnings(value):
pass
def setmode(mode):
pass
def cleanup():
pass
def setup(pin, direction, **kwargs):
pin_last_change[pin] = time.time()
def input(pin):
last_change = pin_last_change.get(pin, 0)
current = time.time()
if current - last_change >= CHANGE_PIN_VALUE_AFTER:
pin_last_change[pin] = current
return HIGH
return LOW
def add_event_detect(pin, edge, callback, **kwargs):
pin_last_change[pin] = time.time()
if edge == FALLING:
callback(pin)

View File

@@ -1,11 +1,18 @@
import RPi.GPIO as GPIO import signal
import sys
import time import time
from datetime import datetime from datetime import datetime
import subprocess import subprocess
import sys
from slovak_datetime_formatter import get_datetime_as_slovak_sentence from slovak_datetime_formatter import get_datetime_as_slovak_sentence
BUTTON_TIMEOUT = 30 # pressing and holding the button longer then e.g. 2 seconds terminates playing if len(sys.argv) > 1 and sys.argv[1] == "test":
import dummygpio as GPIO
else:
import RPi.GPIO as GPIO
BUTTON_TIMEOUT = (
30 # pressing and holding the button longer then e.g. 2 seconds terminates playing
)
PIN = 17 # listen to changes on this GPIO PIN = 17 # listen to changes on this GPIO
GPIO.setmode(GPIO.BCM) # use BCM pin layout GPIO.setmode(GPIO.BCM) # use BCM pin layout
GPIO.setwarnings(False) GPIO.setwarnings(False)
@@ -14,12 +21,14 @@ GPIO.setwarnings(False)
def setup_pin(): def setup_pin():
GPIO.setup(PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def terminate_playing(proc): def terminate_playing(proc):
print("Terminating playing...") print("Terminating playing...")
proc.terminate() proc.terminate()
proc.wait(timeout=5) proc.wait(timeout=5)
print("Terminated") print("Terminated")
def play_presny_cas(): def play_presny_cas():
print("Playing presny_cas.wav") print("Playing presny_cas.wav")
proc = subprocess.Popen(["aplay", "presny_cas.wav"]) proc = subprocess.Popen(["aplay", "presny_cas.wav"])
@@ -27,20 +36,41 @@ def play_presny_cas():
print("GPIO went HIGH - terminating playing") print("GPIO went HIGH - terminating playing")
terminate_playing(proc) terminate_playing(proc)
def play_beep(): def play_beep():
print("Playing beep.wav") print("Playing beep.wav")
subprocess.call(["aplay", "beep.wav"]) subprocess.call(["aplay", "beep.wav"])
def get_credentials():
with open("credentials.txt") as f:
cred_line = f.readline().strip()
username, password = cred_line.split(":")
return username, password
def generate_presny_cas(): def generate_presny_cas():
text = get_datetime_as_slovak_sentence(datetime.now()) text = get_datetime_as_slovak_sentence(datetime.now())
print(f"Generating presny_cas.wav for '{text}'") print(f"Generating presny_cas.wav for '{text}' using a remote API call")
username, password = get_credentials()
subprocess.call( subprocess.call(
[ [
"curl", "--silent", "--get", "--data-urlencode", f"text={text}", "https://tts.virtonline.eu/api/tts", "--output", "presny_cas.wav", "curl",
"--silent",
"-u",
f"{username}:{password}",
"--get",
"--data-urlencode",
f"text={text}",
"https://tts.virtonline.eu/api/tts",
"--output",
"presny_cas.wav",
] ]
) )
print("Generated presny_cas.wav") print("Generated presny_cas.wav")
def wait_for_button_release_or_timeout(pin, timeout): def wait_for_button_release_or_timeout(pin, timeout):
timeout_start = time.time() timeout_start = time.time()
while time.time() < timeout_start + timeout: while time.time() < timeout_start + timeout:
@@ -54,6 +84,15 @@ def handle_pin(pin):
generate_presny_cas() generate_presny_cas()
play_presny_cas() play_presny_cas()
def signal_handler(sig, frame):
print("Interrupt received, cleaning up!")
GPIO.cleanup()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
try: try:
setup_pin() setup_pin()
play_beep() play_beep()
@@ -62,8 +101,9 @@ try:
print(f"Waiting for the GPIO {PIN} to go LOW...") print(f"Waiting for the GPIO {PIN} to go LOW...")
while True: while True:
time.sleep(0.01) time.sleep(0.01)
except Exception:
print("Cought exception...") except Exception as e:
sys.exit() print("Caught exception: ", e)
finally: finally:
GPIO.cleanup() GPIO.cleanup()

Binary file not shown.