-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
81 lines (59 loc) · 2.13 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
import threading
import time
from flask import Flask, jsonify, render_template, send_file
from flask_restful import Resource, Api, reqparse
from flask_cors import CORS
import RPi.GPIO as GPIO
from A4988.A4988 import A4988
app = Flask(__name__,
static_folder='./dist',
template_folder='./dist')
api = Api(app)
CORS(app, supports_credentials=True)
# get_parse = reqparse.RequestParser()
# get_parse.add_argument('rotation_dir',type=int)
# get_parse.add_argument('stepper_num',type=int)
# get_parse.add_argument('speed',type = int)
# get_parse.add_argument('speed',type = float)
# GPIO 20:STEP; GPIO 21:DIR
class CommonResult:
@staticmethod
def result_ok(code=200, message="OK", result=None):
return jsonify({"code": code, "message": message, "result": result})
@staticmethod
def result_fail(code=500, message="Fail", result=None):
return jsonify({"code": code, "message": message, "result": result})
stepper = A4988(20, 21)
# 继电器电源控制
GPIO.setup(19, GPIO.OUT)
# 避免驱动烧坏 开始关闭19 GPIO
GPIO.setup(19, GPIO.HIGH)
@app.route('/')
def index():
return render_template('index.html')
class A4988Control(Resource):
def get(self, rotation_dir, stepper_num, speed):
# rotation_dir
# clockwise = 1
# anticlockwise = 0
try:
# print("方向(1开,顺时针):" + rotation_dir +"步进步数(1X1.8°):" + stepper_num+ "步进延迟速度:" + speed)
stepper.RotationStep(stepper_num, rotation_dir, speed)
return CommonResult.result_ok()
except Exception as e:
print(e)
return CommonResult.result_fail(result=e)
class PowerSwitch(Resource):
def get(self, switch):
if (switch == 1):
GPIO.setup(19, GPIO.LOW)
else:
GPIO.setup(19, GPIO.HIGH)
api.add_resource(PowerSwitch, '/A4988/power/<int:switch>')
api.add_resource(A4988Control, '/A4988/stepper/<int:rotation_dir>/<int:stepper_num>/<float:speed>')
if __name__ == '__main__':
app.run(
host='0.0.0.0',
port=88,
debug=True)