From 1ab422118cfacee567725e0f1244a66f30350e6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergiusz=20Baza=C5=84ski?= Date: Sun, 11 Nov 2012 13:35:07 +0100 Subject: [PATCH] Basic web control. --- Lasers_und_stuff/Cannon.py | 9 ++++++++ Lasers_und_stuff/main.py | 43 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/Lasers_und_stuff/Cannon.py b/Lasers_und_stuff/Cannon.py index b997284..f124ce6 100644 --- a/Lasers_und_stuff/Cannon.py +++ b/Lasers_und_stuff/Cannon.py @@ -5,10 +5,19 @@ Created on Nov 11, 2012 ''' import serial import glob +import threading from math import ceil +def mutex(m): + def wrapper(self, *args, **kwargs): + self.s.acquire() + m() + self.s.release() + return wrapper + class CannonController : def __init__(self): + self.s = threading.Semaphore() print("cannon controller started") self.position = [0, 0] self.fired = False diff --git a/Lasers_und_stuff/main.py b/Lasers_und_stuff/main.py index 4869d96..15ab4e3 100644 --- a/Lasers_und_stuff/main.py +++ b/Lasers_und_stuff/main.py @@ -10,6 +10,7 @@ import flask import threading import os import signal +import json from pygame.locals import * from Cannon import CannonController from Cannon import Gunpoint @@ -18,9 +19,47 @@ app = flask.Flask(__name__) app.debug = True pygame.init() +cannon = None + +@app.route("/left/") +def move_left(amount): + cannon.move_left(amount) + return "OK" + +@app.route("/right/") +def move_right(amount): + cannon.move_right(amount) + return "OK" + +@app.route("/up/") +def move_up(amount): + cannon.move_up(amount) + return "OK" + +@app.route("/down/") +def move_down(amount): + cannon.move_down(amount) + return "OK" + +@app.route("/status") +def status(): + return json.dumps(cannon.get_data()) + @app.route("/") def root(): - return "COCKS" + return """ + + Turret! + + +
+ Up + Down + Left + Right +
+ +""" def main(): #create the screen @@ -35,7 +74,7 @@ def main(): colors = [GRAY, VIOLET, RED, GREEN, BLUE, VERYLIGHT] - + global cannon cannon = CannonController()