User:Wordfa/oldphonehack/PTT Tafeltjes Telefoon
< User:Wordfa | oldphonehack
Ralph Crutzen's PTT Tafeltjes Telefoon Eng translation
#!/usr/bin/env python
import sys, os, time, random, pygame
import RPi.GPIO as GPIO
# translated to english by DeepL and wordfa
# Speaker Function - Play Sound
# Takes a file Path
def say(file):
pygame.mixer.music.load(file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
# Ask Question for Telefone Letter
def askQuestion(numOne, numTwo):
print("Wat is", numOne, "x", numTwo,"?")
say("audio/" + str(numOne) + ".mp3")
say("audio/keer.mp3")
say("audio/" + str(numTwo) + ".mp3")
say("audio/is.mp3")
def getNummer():
# number of Pulses
nPulsen = 0
# Watch Dail or Button
DialContact = GPIO.input(DialPIN)
NumberContact = GPIO.input(NumberPIN) # Low if pressed
while DialContact == False and NumberContact == True:
DialContact = GPIO.input(DialPIN)
NumberContact = GPIO.input(NumberPIN)
# Number Pressed (Selected)
if NumberContact == False:
return -1
# Handle Pulses
done = False
while done == False and DialContact == True:
nPulsen = nPulsen + 1
startTime = time.time()
time.sleep(0.1)
DialContact = GPIO.input(DialPIN)
# Check time between Pulses
while done == False and DialContact == False:
if time.time() - startTime >= 0.2:
done = True
DialContact = GPIO.input(DialPIN)
# Return number
return nPulsen % 10
# Tell you when handset is picked up
def handsetCallback(channel):
print("Handset picked up!", channel)
# Restart Script
# Set Up all the IO Stuff
GPIO.cleanup()
python = sys.executable
os.execl(python, python, * sys.argv)
# Name pins
DialPIN = 25
NumberPIN = 23
handsetPIN = 24
# Set Up Pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(DialPIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(handsetPIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(NumberPIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)
# Use an interrupt for the handset, because it can be put down at any time
GPIO.add_event_detect(handsetPIN, GPIO.BOTH, callback = handsetCallback)
# Start audio mixer
pygame.mixer.init()
while True:
try:
# Wait for pick up
print("Wait for pick up...")
handsetContact = GPIO.input(handsetPIN)
while handsetContact == True:
handsetContact = GPIO.input(handsetPIN)
time.sleep (1)
# Which times table to practise
print("Which times table to practise?")
say("audio/welk.mp3")
table = getNummer()
# List to keep track of answered questions
sums = []
for som in range(10):
sums.append(0) # 0 is bad, 1 is goed
numOfCorrectAns = 0
while numOfCorrectAns < 10:
# Set Question?
numOne = random.randint(1,10)
while sums[numOne - 1] == 1:
numOne = random.randint(1,10)
if table == -1:
numTwo = random.randint(1,10)
elif table == 0:
numTwo = 10
else:
numTwo = table
result = numOne * numTwo
numOfDigits = len(str(result))
askQuestion(numOne, numTwo)
thisDigit = 0
answer = ""
# Watch for Answer
while thisDigit < numOfDigits:
nummer = getNummer()
if nummer > -1: # Dial Turned
answer = answer + str(nummer)
thisDigit = thisDigit + 1
else: # Number Key Pressed
askQuestion(numOne, numTwo)
thisDigit = 0
answer = ""
print(answer)
# Check Answer
# string to Int Answer
if int(answer) == result:
numOfCorrectAns = numOfCorrectAns + 1
sums[numOne - 1] = 1
print("Good Job!")
say("audio/goed.mp3")
else:
print("You suck, the correct answer is ", result)
say("audio/fout.mp3")
say("audio/" + str(result) + ".mp3")
print()
time.sleep(1)
say("audio/einde.mp3")
except KeyboardInterrupt: # Ctrl+C
GPIO.cleanup()