Files
dragonpilot/tools/joystick/web.py

79 lines
2.0 KiB
Python
Raw Normal View History

2022-04-13 14:29:17 -07:00
#!/usr/bin/env python3
2022-04-14 16:10:42 -07:00
import time
import threading
from flask import Flask
2022-04-13 14:29:17 -07:00
import cereal.messaging as messaging
app = Flask(__name__)
pm = messaging.PubMaster(['testJoystick'])
index = """
<html>
<head>
<script src="https://github.com/bobboteck/JoyStick/releases/download/v1.1.6/joy.min.js"></script>
</head>
<body>
<div id="joyDiv" style="width:100%;height:100%"></div>
<script type="text/javascript">
// Set up gamepad handlers
let gamepad = null;
window.addEventListener("gamepadconnected", function(e) {
gamepad = e.gamepad;
});
window.addEventListener("gamepaddisconnected", function(e) {
gamepad = null;
});
2022-04-13 14:29:17 -07:00
// Create JoyStick object into the DIV 'joyDiv'
var joy = new JoyStick('joyDiv');
setInterval(function(){
var x = -joy.GetX()/100;
var y = joy.GetY()/100;
if (x === 0 && y === 0 && gamepad !== null) {
let gamepadstate = navigator.getGamepads()[gamepad.index];
x = -gamepadstate.axes[0];
y = -gamepadstate.axes[1];
}
2022-04-13 14:29:17 -07:00
let xhr = new XMLHttpRequest();
xhr.open("GET", "/control/"+x+"/"+y);
xhr.send();
}, 50);
</script>
"""
@app.route("/")
def hello_world():
return index
last_send_time = time.monotonic()
2022-04-13 14:29:17 -07:00
@app.route("/control/<x>/<y>")
def control(x, y):
2022-04-14 16:10:42 -07:00
global last_send_time
2022-04-13 14:29:17 -07:00
x,y = float(x), float(y)
x = max(-1, min(1, x))
y = max(-1, min(1, y))
dat = messaging.new_message('testJoystick')
dat.testJoystick.axes = [y,x]
dat.testJoystick.buttons = [False]
pm.send('testJoystick', dat)
last_send_time = time.monotonic()
2022-04-13 14:29:17 -07:00
return ""
2022-04-14 16:10:42 -07:00
def handle_timeout():
while 1:
this_time = time.monotonic()
2022-04-14 16:10:42 -07:00
if (last_send_time+0.5) < this_time:
2022-04-25 20:07:12 -07:00
#print("timeout, no web in %.2f s" % (this_time-last_send_time))
2022-04-14 16:10:42 -07:00
dat = messaging.new_message('testJoystick')
dat.testJoystick.axes = [0,0]
dat.testJoystick.buttons = [False]
pm.send('testJoystick', dat)
time.sleep(0.1)
def main():
2022-04-14 16:10:42 -07:00
threading.Thread(target=handle_timeout, daemon=True).start()
2022-04-13 14:29:17 -07:00
app.run(host="0.0.0.0")
if __name__ == '__main__':
main()