Skip to content

Instantly share code, notes, and snippets.

@hbradio
Created October 6, 2016 23:21
Show Gist options
  • Save hbradio/87edab43afe581eb14ba847aec0a75e9 to your computer and use it in GitHub Desktop.
Save hbradio/87edab43afe581eb14ba847aec0a75e9 to your computer and use it in GitHub Desktop.
diff --git a/www/app.py b/www/app.py
index f1aba9d..94198b2 100644
--- a/www/app.py
+++ b/www/app.py
@@ -3,12 +3,15 @@ from os import listdir
from os.path import isfile, join
from flask_cors import CORS, cross_origin
import xml.etree.ElementTree
+import Adafruit_GPIO.PWM as pwmLib
app = Flask(__name__)
CORS(app)
+pwm = pwmLib.get_platform_pwm(pwmtype="softpwm")
+
@app.route('/api/v1/blockdiagrams', methods=['GET'])
-def getblockdiagrams():
+def get_block_diagrams():
names = []
for f in listdir('saved-bds'):
if isfile(join('saved-bds', f)) and f.endswith('.xml'):
@@ -16,7 +19,7 @@ def getblockdiagrams():
return jsonify(result=names)
@app.route('/api/v1/blockdiagrams', methods=['POST'])
-def saveblockdiagram():
+def save_block_diagram():
designName = request.form['designName'].replace(' ', '_').replace('.', '_')
bdString = request.form['bdString']
root = xml.etree.ElementTree.Element("root")
@@ -29,12 +32,43 @@ def saveblockdiagram():
return ('', 200)
@app.route('/api/v1/blockdiagrams/<string:id>', methods=['GET'])
-def getblockdiagram(id):
+def get_block_diagram(id):
id = id.replace(' ', '_').replace('.', '_')
bd = [f for f in listdir('saved-bds') if isfile(join('saved-bds', f)) and id in f]
with open(join('saved-bds',bd[0]), 'r') as content_file:
content = content_file.read()
return Response(content, mimetype='text/xml')
[email protected]('/api/v1/sendcommand', methods = ['POST'])
+def send_command():
+ run_command(request.json)
+ return jsonify(request.json)
+
+def init_rover_service():
+ # set up motor pwm
+ pwm.start("XIO-P0", 0);
+ pwm.start("XIO-P1", 0);
+ pwm.start("XIO-P6", 0);
+ pwm.start("XIO-P7", 0);
+
+ # test adapter
+ if pwm.__class__.__name__ == 'DUMMY_PWM_Adapter':
+ def mock_set_duty_cycle(pin, speed):
+ print "Setting pin " + pin + " to speed " + str(speed)
+ pwm.set_duty_cycle = mock_set_duty_cycle
+
+def run_command(decoded):
+ print decoded['command']
+ if decoded['command'] == 'START_MOTOR':
+ print decoded['pin']
+ print decoded['speed']
+ print "Starting motor"
+ pwm.set_duty_cycle(decoded['pin'], float(decoded['speed']))
+ elif decoded['command'] == 'STOP_MOTOR':
+ print decoded['pin']
+ print "Stopping motor"
+ pwm.set_duty_cycle(decoded['pin'], 0)
+
if __name__ == '__main__':
+ init_rover_service()
app.run(host='0.0.0.0', debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment