-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
205 lines (155 loc) · 4.2 KB
/
test.py
File metadata and controls
205 lines (155 loc) · 4.2 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from flask import Flask, render_template, request, json
#import serial
#import serial.tools.list_ports
import sys
import time
import socket
# 自身の名称を app という名前でインスタンス化しま
app = Flask(__name__)
arduino = None
currLevel = 0.4
currColor = ""
currAnim = ""
currPal = ""
# ルーティング
# indexアクセス
@app.route("/")
def home():
return render_template('main.html')
# /brightnessアクセス
@app.route("/brightness/", methods=['POST'])
def brightness():
app.logger.debug("aaa")
btn_name = get_btn_name(request)
print("Brightness:", btn_name)
# arduino_send_cmd("B="+str(btn_name))
return ""
# /durationアクセス
@app.route("/duration/", methods=['POST'])
def duration():
btn_name = get_btn_name(request)
print("Duration:", btn_name)
# arduino_send_cmd("D="+str(btn_name))
return ""
# /colorアクセス
@app.route("/color/", methods=['POST'])
def color():
btn_name = get_btn_name(request)
print("Color:", btn_name)
# arduino_send_cmd("C="+str(btn_name))
return ""
# /paletteアクセス
@app.route("/palette/", methods=['POST'])
def palette():
global currLevel
global currColor
global currAnim
global currPal
btn_name = get_btn_name(request)
print("Palette:", btn_name)
# arduino_send_cmd("P="+str(btn_name))
return ""
# /animアクセス
@app.route("/anim/", methods=['POST'])
def anim():
btn_name = get_btn_name(request)
print("Animation code:", btn_name)
# arduino_send_cmd("A="+str(btn_name))
return ""
"""
@app.route("/pal/", methods=['POST'])
def pal():
global currLevel
global currColor
global currAnim
global currPal
if 'btnRgb' in request.form:
# arduino_send_cmd("a")
currPal = "RGB"
elif 'btnRainbow' in request.form:
# arduino_send_cmd("b")
currPal = "Rainbow"
elif 'btnParty' in request.form:
# arduino_send_cmd("d")
currPal = "Party"
elif 'btnFire' in request.form:
# arduino_send_cmd("f")
currPal = "Fire"
templateData = {
'currAnim': currAnim,
'currPal': currPal,
'currLevel': currLevel,
'currColor': currColor
}
return ""
"""
def get_btn_name(request):
btn_name = ""
for key in request.form.keys():
#print ("Button pressed:", key)
btn_name = key
return btn_name
def arduino_get_resp(s):
time.sleep(.1)
while (s.in_waiting > 0):
print(s.readline().decode(), end="")
"""
def arduino_send_cmd(s):
arduino.flush()
s = s+'\n'
arduino.write(s.encode())
arduino_get_resp(arduino)
time.sleep(.1)
arduino.flush()
"""
# try to detect the USB port where Arduino is connected
"""
def arduino_get_port():
print("Listing ports")
port = None
ports = serial.tools.list_ports.comports()
for p in ports:
print(p)
if "Arduino" in p[1]:
port = p[0]
print("Arduino detected on port", port)
return port
"""
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
ip = s.getsockname()[0]
except:
ip = '127.0.0.1'
finally:
s.close()
return ip
if __name__ == "__main__":
port = None
"""
# use the USB port name if passed
if len(sys.argv) > 1:
port = sys.argv[1]
print("Arduino port: " + port)
# otherwise tries to detect the port
# this seems to work only on Windows if Arduino USB driver is installed
while(port == None):
port = arduino_get_port()
if port == None:
print("Arduino not found. Retrying...")
time.sleep(5)
# open the serial interface
arduino = serial.Serial(port, 9600, timeout=1)
time.sleep(.5)
"""
#print("Port", arduino)
print("Current IP is", get_ip())
print("Point your browser to http://", get_ip(), sep="")
print()
#app.run(host='0.0.0.0', port=80, debug=True)
# どこからでもアクセスできるよ
# threaded=true にすると同時アクセスができる
app.debug = True
app.run(host='0.0.0.0', port=8000, threaded=True)