163 lines
4.8 KiB
Python
163 lines
4.8 KiB
Python
import os
|
|
from flask import Flask, redirect, render_template, request, session
|
|
import csv # CSV handling
|
|
import random
|
|
import RPi.GPIO as GPIO
|
|
import time
|
|
|
|
app = Flask(__name__)
|
|
|
|
def load_database():
|
|
file = os.path.join(os.path.expanduser("~"), "Robotik-Ablage", "quiz.csv")
|
|
|
|
#with open(file, newline="", encoding="utf-8") as csvfile: #
|
|
with open(file, newline="", encoding="cp1252") as csvfile: # Code wird als Excel Windows-1252 Input CSV gelesen
|
|
database = csv.reader(csvfile, delimiter=";")
|
|
q_a = [] # Empty list for all questions and answers
|
|
for row in database: # alle Zeilen in der Datenbank durchlaufen
|
|
index = int(row[0]) # Laufende Nummer
|
|
question = str(row[1]) # Frage (ist der erste Teil)
|
|
answer = str(row[2]) # Antwort (3. Spalte in der CSV Datenbank)
|
|
q_a.append({"index": index,"question": question, "answer": answer}) # Eine Liste bauen
|
|
return q_a
|
|
|
|
###
|
|
# Volle Drehung sind etwa 16400
|
|
# 1/4 Drehung = 1 Loch weiter = 4100
|
|
ViertelRundeSteps=4100
|
|
|
|
# ----------------- MOTOR KRAM
|
|
GPIO.setmode(GPIO.BCM)
|
|
GPIO.setwarnings(False)
|
|
coil_A_1_pin = 24 # pink
|
|
coil_A_2_pin = 4 # orange
|
|
coil_B_1_pin = 23 # blau
|
|
coil_B_2_pin = 25 # gelb
|
|
coil2_A_1_pin = 18 # pink
|
|
coil2_A_2_pin = 22 # orange
|
|
coil2_B_1_pin = 17 # blau
|
|
coil2_B_2_pin = 27 # gelb
|
|
|
|
StepCount = 8
|
|
Seq = list(range(0, StepCount))
|
|
Seq[0] = [0,1,0,0]
|
|
Seq[1] = [0,1,0,1]
|
|
Seq[2] = [0,0,0,1]
|
|
Seq[3] = [1,0,0,1]
|
|
Seq[4] = [1,0,0,0]
|
|
Seq[5] = [1,0,1,0]
|
|
Seq[6] = [0,0,1,0]
|
|
Seq[7] = [0,1,1,0]
|
|
|
|
GPIO.setup(coil_A_1_pin, GPIO.OUT)
|
|
GPIO.setup(coil_A_2_pin, GPIO.OUT)
|
|
GPIO.setup(coil_B_1_pin, GPIO.OUT)
|
|
GPIO.setup(coil_B_2_pin, GPIO.OUT)
|
|
GPIO.setup(coil2_A_1_pin, GPIO.OUT)
|
|
GPIO.setup(coil2_A_2_pin, GPIO.OUT)
|
|
GPIO.setup(coil2_B_1_pin, GPIO.OUT)
|
|
GPIO.setup(coil2_B_2_pin, GPIO.OUT)
|
|
|
|
def setStep(w1, w2, w3, w4):
|
|
GPIO.output(coil_A_1_pin, w1)
|
|
GPIO.output(coil2_A_1_pin, w1)
|
|
GPIO.output(coil_A_2_pin, w2)
|
|
GPIO.output(coil2_A_2_pin, w2)
|
|
GPIO.output(coil_B_1_pin, w3)
|
|
GPIO.output(coil2_B_1_pin, w3)
|
|
GPIO.output(coil_B_2_pin, w4)
|
|
GPIO.output(coil2_B_2_pin, w4)
|
|
|
|
def forward(delay, steps):
|
|
for i in range(steps):
|
|
for j in range(StepCount):
|
|
setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
|
|
time.sleep(delay)
|
|
|
|
def backwards(delay, steps):
|
|
for i in range(steps):
|
|
for j in reversed(range(StepCount)):
|
|
setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
|
|
time.sleep(delay)
|
|
|
|
# ----------------- MOTOR KRAM ENDE
|
|
|
|
def FahreMotor():
|
|
forward(1.0/1000.0, ViertelRundeSteps)
|
|
# falls auf der anderen Seite montiert:
|
|
#backwards(1.0/1000.0, ViertelRundeSteps)
|
|
return True
|
|
|
|
fragen_liste = load_database()
|
|
random.shuffle(fragen_liste)
|
|
|
|
current_index = 0
|
|
correct_sum = 0
|
|
last_correct = None
|
|
max_correct_sum = 5
|
|
debugtxt=""
|
|
alerttxt=""
|
|
successtxt=""
|
|
won=False
|
|
|
|
@app.route('/')
|
|
|
|
def quiz():
|
|
return render_template('quiz.html', frage=fragen_liste[current_index],
|
|
last_correct=last_correct,debugtxt=debugtxt, correct_sum=correct_sum,
|
|
max_correct_sum = max_correct_sum,
|
|
alerttxt=alerttxt, successtxt=successtxt,
|
|
won=won)
|
|
|
|
@app.route('/submit_quiz', methods=['POST'])
|
|
def submit_quiz():
|
|
global current_index
|
|
global fragen_liste
|
|
global debugtxt, alerttxt, successtxt
|
|
global correct_sum
|
|
global won
|
|
|
|
won=False
|
|
|
|
# Überprüfen Sie die Antwort und aktualisieren Sie current_index
|
|
user_answer = request.form.get('answer')
|
|
correct_answer = fragen_liste[current_index]['answer']
|
|
|
|
if user_answer.strip() == correct_answer.strip():
|
|
last_correct = True
|
|
correct_sum += 1
|
|
# debugtxt = correct_answer + " ist richtig! passt zu deiner Angabe: " + user_answer
|
|
successtxt = correct_answer + " ist richtig! passt zu deiner Angabe: " + user_answer
|
|
alerttxt = ""
|
|
elif user_answer == "42":
|
|
# EASTEREGG!!! #
|
|
last_correct = True
|
|
correct_sum = 5
|
|
successtxt = "Korrekt! Die Antwort auf alle Fragen ist 42."
|
|
alerttxt = ""
|
|
else:
|
|
last_correct = False
|
|
alerttxt = "falsch: Richtig wäre " +correct_answer + " / und nicht: " + user_answer
|
|
successtxt = ""
|
|
correct_sum = 0
|
|
|
|
current_index += 1
|
|
|
|
# Überprüfen, ob wir alle Fragen beantwortet haben
|
|
if current_index >= len(fragen_liste):
|
|
fragen_liste = load_database()
|
|
random.shuffle(fragen_liste)
|
|
current_index=0
|
|
|
|
if correct_sum >= 5:
|
|
fragen_liste = load_database()
|
|
random.shuffle(fragen_liste)
|
|
current_index=0
|
|
successtxt += "<br /> <br /> <br /> Gewonnen! - Gut gemacht! <br /> Guten Appetit!"
|
|
# print ("gewonnen! - Fahre 10 Sek!")
|
|
FahreMotor()
|
|
correct_sum =0
|
|
won = True
|
|
|
|
return redirect('/')
|