-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
194 lines (175 loc) · 7.31 KB
/
api.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
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
from flask import Blueprint, render_template, request
import pytronics
public = Blueprint('api', __name__, template_folder='templates')
ANALOG_PINS = ['A0', 'A1', 'A2', 'A3']
DIGITAL_PINS = ['LED', '2', '3', '4', '5', '6', '7']
DIGITAL_PIN_NAME_ERROR = "You seem to be trying to read from digital pin {0}, which does not work. Try 'LED', '2', '3', '4', '5', '6', or '7'."
DIGITAL_PIN_STATE_ERROR = "You seem to be trying to set digital pin {0} to state {1}, which does not work. Try 'LOW' or 'HIGH'."
### Support for pins ###
@public.route('/analog/read/<pin_name>', methods=['GET', 'POST'])
def analog_read(pin_name):
if pin_name not in ANALOG_PINS:
return "You seem to be trying to read from analog pin {0}, which does not exist. Try A0, A1, A2, or A3.".format(pin_name)
else:
return str(pytronics.analogRead(pin_name))
@public.route('/digital/read/<pin_name>', methods=['GET', 'POST'])
def digital_read(pin_name):
if pin_name not in DIGITAL_PINS:
return DIGITAL_PIN_NAME_ERROR.format(pin_name)
else:
return str(pytronics.digitalRead(pin_name))
@public.route('/digital/write/<pin_name>', methods=['GET', 'POST'])
def digital_write(pin_name):
if pin_name in DIGITAL_PINS:
data = request.form['data'].upper()
if data in ['1', 'ON', 'HIGH']:
pytronics.digitalWrite(pin_name, 'HIGH')
return 'Set pin {0} to HIGH'.format(pin_name)
elif data in ['0', 'OFF', 'LOW']:
pytronics.digitalWrite(pin_name, 'LOW')
return 'Set pin {0} to LOW'.format(pin_name)
else:
return DIGITAL_PIN_STATE_ERROR.format(pin_name, data)
else:
return DIGITAL_PIN_NAME_ERROR.format(pin_name)
@public.route('/digital/toggle/<pin_name>', methods=['GET', 'POST'])
def digital_toggle(pin_name):
if pin_name in DIGITAL_PINS:
if pytronics.digitalRead(pin_name) == '1':
pytronics.digitalWrite(pin_name, 'LOW')
return 'Set pin {0} to LOW'.format(pin_name)
else:
pytronics.digitalWrite(pin_name, 'HIGH')
return 'Set pin {0} to HIGH'.format(pin_name)
else:
return DIGITAL_PIN_NAME_ERROR.format(pin_name)
@public.route('/digital/write/<pin_name>/<state>')
def digital_write_shortcut(pin_name, state):
if pin_name in DIGITAL_PINS:
if state.upper() in ['1', 'ON', 'HIGH']:
pytronics.digitalWrite(pin_name, 'HIGH')
return 'Set pin {0} to HIGH'.format(pin_name)
elif state.upper() in ['0', 'OFF', 'LOW']:
pytronics.digitalWrite(pin_name, 'LOW')
return 'Set pin {0} to LOW'.format(pin_name)
else:
return DIGITAL_PIN_STATE_ERROR.format(pin_name, data)
else:
return DIGITAL_PIN_NAME_ERROR.format(pin_name)
@public.route('/read-pins', methods=['GET', 'POST'])
def read_pins():
import json
# return json.dumps(pytronics.readPins(DIGITAL_PINS))
pins = pytronics.readPins(DIGITAL_PINS)
analog = {}
for chan in ['A0', 'A1', 'A2', 'A3']:
analog[chan] = pytronics.analogRead(chan)
return json.dumps({ 'pins': pins, 'analog': analog })
### Support for i2c ###
@public.route('/i2cget/<addr>/<reg>/<mode>')
def i2cget(addr, reg, mode):
iaddr = int(addr, 0)
ireg = int(reg, 0)
try:
res = pytronics.i2cRead(iaddr, ireg, mode)
print('## i2cget ## {0}'.format(res))
return str(res)
except (OSError, IOError) as e:
import errno
print('## i2cget ## Error: [{0}] {1}'.format(errno.errorcode[e.errno], e.strerror))
return str(-1)
except Exception as e:
return 'Internal server error', 500
@public.route('/i2cset/<addr>/<reg>/<val>/<mode>')
def i2cset(addr, reg, val, mode):
iaddr = int(addr, 0)
ireg = int(reg, 0)
ival = int(val, 0)
try:
pytronics.i2cWrite(iaddr, ireg, ival, mode)
print('## i2cset ##')
return str(0)
except (OSError, IOError) as e:
import errno
print('## i2cset ## Error: [{0}] {1}'.format(errno.errorcode[e.errno], e.strerror))
return str(-1)
except Exception as e:
return 'Internal server error', 500
@public.route('/i2c_read', methods=['POST'])
def i2c_read():
import json
try:
params = json.loads(request.form['params'])
print('## i2c_read ## ' + str(params))
value = pytronics.i2cRead(params['addr'], params['reg'], params['size'], params['length'])
result = {
'success': True,
'value': value
}
return json.dumps(result)
except (OSError, IOError) as e:
import errno
print('## i2c_read ## Error: [{0}] {1}'.format(errno.errorcode[e.errno], e.strerror))
result = {
'success': False,
'errorCode': errno.errorcode[e.errno],
'errorMessage': e.strerror
}
return json.dumps(result)
except Exception as e:
return 'Internal server error', 500
@public.route('/i2c_write', methods=['POST'])
def i2c_write():
import json
try:
params = json.loads(request.form['params'])
print('## i2c_write ## ' + str(params))
pytronics.i2cWrite(params['addr'], params['reg'], params['value'], params['size'])
result = {
'success': True
}
return json.dumps(result)
except (OSError, IOError) as e:
import errno
print('## i2c_write ## Error: [{0}] {1}'.format(errno.errorcode[e.errno], e.strerror))
result = {
'success': False,
'errorCode': errno.errorcode[e.errno],
'errorMessage': e.strerror
}
return json.dumps(result)
except Exception as e:
return 'Internal server error', 500
@public.route('/i2cscan', methods=['POST'])
def i2cscan():
from i2c import scanBus
import json
return json.dumps(scanBus())
### Support for serial ###
@public.route('/serial/read/<channel>/<speed>/<num_bytes>', methods=['GET', 'POST'])
def serial_read(channel, speed, num_bytes):
return 'Fake data' # pytronics.serialRead(num_bytes, speed, channel)
@public.route('/serial/write/<channel>/<speed>', methods=['GET', 'POST'])
def serial_write(channel, speed):
data = request.form['data']
pytronics.serialWrite(data, speed, channel)
return 'Sent to serial port {0} at {1} bps: {2}'.format(channel, speed, data)
### Support for SPI bus ###
@public.route('/spi/<channel>/read')
def spi_read(channel):
if str(channel) not in ['0', '1', '2', '3']:
return "You seem to be trying to read from SPI channel {0}, which does not exist. Try 0, 1, 2, or 3.".format(channel)
return pytronics.spiRead(channel)
@public.route('/spi/<channel>/write/<data>')
def spi_write():
if str(channel) not in ['0', '1', '2', '3']:
return "You seem to be trying to write to SPI channel {0}, which does not exist. Try 0, 1, 2, or 3.".format(channel)
return pytronics.spiWrite(data, channel)
@public.route('/spi/<channel>/speed/', defaults={'target': ''})
@public.route('/spi/<channel>/speed/<target>')
def spi_speed(channel, target):
if str(channel) not in ['0', '1', '2', '3']:
return "You seem to be trying to use SPI channel {0}, which does not exist. Try 0, 1, 2, or 3.".format(channel)
if str(target) == '':
return str(pytronics.spiGetSpeed(str(channel)))
return str(pytronics.spiSetSpeed(target, channel))