piano-appp/backend/app.py

46 lines
815 B
Python
Raw Normal View History

2022-10-03 03:32:41 +00:00
from flask import Flask
from threading import Thread
import os
app = Flask(__name__)
threadFlag = True
def playFunction(freq):
global threadFlag
2022-10-03 04:03:36 +00:00
os.system("beep -f"+ freq+" -l 100000 &")
2022-10-03 03:32:41 +00:00
while not threadFlag:
2022-10-03 04:03:36 +00:00
pass
2022-10-03 03:32:41 +00:00
#print("beep -f %f" % (freq))
2022-10-03 04:01:29 +00:00
thread2 = 0
2022-10-03 03:32:41 +00:00
tc = 0
@app.before_first_request
def activate_job():
global tc
tc = 0
@app.route('/startfreq/<freq>')
def freq(freq):
2022-10-03 03:56:47 +00:00
global threadFlag
2022-10-03 04:01:29 +00:00
global thread2
2022-10-03 03:56:47 +00:00
threadFlag = False
2022-10-03 03:32:41 +00:00
print(freq)
2022-10-03 03:54:22 +00:00
thread2 = Thread(target=playFunction, args=(freq,))
2022-10-03 03:56:47 +00:00
thread2.start()
2022-10-03 03:32:41 +00:00
return("200")
@app.route('/stopfreq/<freq2>')
def freq2(freq2):
global threadFlag
2022-10-03 04:01:29 +00:00
global thread2
2022-10-03 03:32:41 +00:00
threadFlag = True
2022-10-03 04:01:29 +00:00
thread2.stop()
2022-10-03 03:32:41 +00:00
return("200")
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')