-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUPYRPC_cli.py
350 lines (272 loc) · 13.1 KB
/
UPYRPC_cli.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
MIT License
Copyright (c) 2019 sistemicorp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This CLI provides a linux CLI interface to the MicroPyBoard "server".
"""
import sys
import time
import logging
import argparse
from UPYRPC import UPYRPC
from target.upyrpc_const import *
VERSION = "0.2.0"
# Command Line Interface...
# FIXME: this is horribly done...
def parse_args():
epilog = """
Usage examples:
python3 UPYRPC_cli.py --port /dev/ttyACM0 adc --100
python3 UPYRPC_cli.py --port /dev/ttyACM0 adc --all
"""
parser = argparse.ArgumentParser(description='UPYRPC_cli',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=epilog)
parser.add_argument("-p", '--port', dest='port', default=None, type=str,
action='store', help='Active serial port')
parser.add_argument("-a", '--all', dest='all_funcs', default=0, action='store_true', help='run all tests')
parser.add_argument("-v", '--verbose', dest='verbose', default=0, action='count', help='Increase verbosity')
parser.add_argument("-d", '--debug', dest='debug', default=False, action='store_true', help='Enable debug prints on pyboard')
parser.add_argument("--version", dest="show_version", action='store_true', help='Show version and exit')
subp = parser.add_subparsers(dest="_cmd", help='commands')
led_toggle_parser = subp.add_parser('led_toggle')
led_toggle_parser.add_argument('-a', "--all", dest="all", action='store_true', help='run all tests sequentially', default=False, required=False)
led_toggle_parser.add_argument('--100', dest="t100", action='store_true', help='toggle led using server.cmd', default=False, required=False)
led_toggle_parser.add_argument('--101', dest="t101", action='store_true', help='toggle led using wrapper API', default=False, required=False)
led_toggle_parser.add_argument('--102', dest="t102", action='store_true', help='toggle led using wrapper API only once', default=False, required=False)
adc_parser = subp.add_parser('adc')
adc_parser.add_argument('-a', "--all", dest="all", action='store_true', help='run all tests sequentially', default=False, required=False)
adc_parser.add_argument('--100', dest="t100", action='store_true', help='adc_read', default=False, required=False)
adc_parser.add_argument('--200', dest="t200", action='store_true', help='adc_read_multi', default=False, required=False)
pwm_parser = subp.add_parser('pwm')
pwm_parser.add_argument('-a', "--all", dest="all", action='store_true', help='run all tests sequentially', default=False, required=False)
pwm_parser.add_argument('--100', dest="t100", action='store_true', help='PWM on Y1', default=False, required=False)
misc_parser = subp.add_parser('misc')
misc_parser.add_argument('-a', "--all", dest="all", action='store_true', help='run all tests sequentially', default=False, required=False)
misc_parser.add_argument('--100', dest="t100", action='store_true', help='unique id', default=False, required=False)
misc_parser.add_argument('--200', dest="t200", action='store_true', help='pyboard server version and uname', default=False, required=False)
misc_parser.add_argument('--300', dest="t300", action='store_true', help='reset', default=False, required=False)
misc_parser.add_argument('--400', dest="t400", action='store_true', help='long running example', default=False, required=False)
misc_parser.add_argument('--500', dest="t500", action='store_true', help='Init GPIO Y1 PP', default=False, required=False)
misc_parser.add_argument('--501', dest="t501", action='store_true', help='Init GPIO X12 Input Pull-UP', default=False, required=False)
args = parser.parse_args()
if args.show_version:
logging.info("Version {}".format(VERSION))
sys.exit(0)
if not args.port:
parser.error("--port is required")
return args
def test_led_toggle(args, pyb):
did_something = False
_all = False
if args._cmd == "led_toggle": _all = args.all
all = args.all_funcs or _all
_success = True
logging.info("test_led_toggle:")
if all or args.t100:
# This is an example of how to execute non-blocking, long running async task
# using the server.cmd({}) interface, NORMALLY we would use the wrapped versions,
# and NOT construct commands ourselves...
did_something = True
logging.info("T100: Toggle Red LED with raw commands...")
cmds = ["upyrpc_main.upyrpc.cmd({{'method': 'led_toggle', 'args': {{ 'led': {} }} }})".format(LED_RED)]
success, result = pyb.server_cmd(cmds, repl_enter=False, repl_exit=False)
logging.info("{} {}".format(success, result))
cmds = ["upyrpc_main.upyrpc.ret(method='led_toggle')"]
retry = 5
succeeded = False
while retry and not succeeded:
time.sleep(0.5)
success, result = pyb.server_cmd(cmds, repl_enter=False, repl_exit=False)
logging.info("{} {}".format(success, result))
if success:
for r in result:
if r.get("method", False) == 'led_toggle' and r.get("value", False) == True:
succeeded = True
retry -= 1
if _success and not success: _success = False
cmds = ["upyrpc_main.upyrpc.cmd({{'method': 'led_toggle', 'args': {{ 'led': {}, 'on_ms': 0 }} }})".format(LED_RED)]
success, result = pyb.server_cmd(cmds, repl_enter=False, repl_exit=False)
logging.info("{} {}".format(success, result))
if all or args.t101:
did_something = True
logging.info("T101: Toggle Red LED with wrapper API...")
success, result = pyb.led_toggle(2, 200)
logging.info("{} {}".format(success, result))
if _success and not success: _success = False
time.sleep(5) # let the led toggle for a bit
success, result = pyb.led_toggle(2, 0)
logging.info("{} {}".format(success, result))
if _success and not success: _success = False
if all or args.t102:
did_something = True
logging.info("T102: Toggle Orange LED with wrapper API for 1.5 sec ON")
success, result = pyb.led_toggle(3, 1500, once=True)
logging.info("{} {}".format(success, result))
if _success and not success: _success = False
if did_something: return _success
else: logging.error("No Tests were specified")
return False
def test_adc(args, pyb):
did_something = False
_all = False
if args._cmd == "adc": _all = args.all
all = args.all_funcs or _all
_success = True
logging.info("test_adc:")
if all or args.t100:
did_something = True
logging.info("T100: Reading ADC...")
success, result = pyb.adc_read("VREF")
logging.info("{} {}".format(success, result))
if _success and not success: _success = False
if all or args.t200:
did_something = True
logging.info("T200: Reading (multi) ADC...")
success, result = pyb.adc_read_multi(pins=["X19", "X20"])
logging.info("{} {}".format(success, result))
success, result = pyb.get_server_method("adc_read_multi_results")
logging.info("{} {}".format(success, result))
if _success and not success: _success = False
if did_something: return _success
else: logging.error("No Tests were specified")
return False
def test_pwm(args, pyb):
did_something = False
_all = False
if args._cmd == "pwm": _all = args.all
all = args.all_funcs or _all
_success = True
logging.info("test_pwm:")
if all or args.t100:
did_something = True
logging.info("T100: PWM on Y1")
success, result = pyb.init_gpio("foo", "Y1", PYB_PIN_OUT_PP, PYB_PIN_PULLNONE)
logging.info("{} {}".format(success, result))
if _success and not success:
_success = False
logging.error("failed")
success, result = pyb.pwm("foo", "foo", 8, 1, 1000, 50)
logging.info("{} {}".format(success, result))
if _success and not success: _success = False
if did_something: return _success
else: logging.error("No Tests were specified")
return False
def test_misc(args, pyb):
did_something = False
_all = False
if args._cmd == "adc": _all = args.all
all = args.all_funcs or _all
_success = True
logging.info("test_misc:")
if all or args.t100:
did_something = True
logging.info("T100: Reading unique id...")
success, result = pyb.unique_id()
logging.info("{} {}".format(success, result))
if _success and not success: _success = False
if all or args.t200:
did_something = True
logging.info("T200: Reading version and uname...")
success, result = pyb.version()
logging.info("{} {}".format(success, result))
if _success and not success: _success = False
if all or args.t300:
did_something = True
logging.info("T300: Resetting...")
success, result = pyb.reset()
logging.info("{} {}".format(success, result))
if _success and not success: _success = False
if all or args.t400:
did_something = True
logging.info("T400: Long Running Example...")
success, result = pyb.long_running_example(5)
logging.info("{} {}".format(success, result))
if success:
done = False
while not done:
time.sleep(1) # poll the target for completion
success, result = pyb.get_server_method("long_running_example")
logging.info("Polling long_running_example: {} {}".format(success, result))
if success and result[0].get("value", {}).get("value", False) == "completed":
done = True
if _success and not success: _success = False
if all or args.t500:
did_something = True
logging.info("T500: init GPIO Y1...")
success, result = pyb.init_gpio("foo", "Y1", PYB_PIN_OUT_PP, PYB_PIN_PULLNONE)
logging.info("{} {}".format(success, result))
if _success and not success: _success = False
if all or args.t501:
did_something = True
logging.info("T501: init GPIO X12...")
success, result = pyb.init_gpio("X12", "X12", PYB_PIN_IN, PYB_PIN_PULLUP)
logging.info("{} {}".format(success, result))
if _success and not success: _success = False
if did_something: return _success
else: logging.error("No Tests were specified")
return False
if __name__ == '__main__':
args = parse_args()
all_funcs = args.all_funcs
pyb = None
if args.verbose == 0:
logging.basicConfig(level=logging.INFO, format='%(filename)20s %(levelname)6s %(lineno)4s %(message)s')
else:
logging.basicConfig(level=logging.DEBUG, format='%(filename)20s %(levelname)6s %(lineno)4s %(message)s')
pyb = UPYRPC(args.port, loggerIn=logging)
success, result = pyb.start_server()
if not success:
logging.error("Unable to start server")
pyb.close()
exit(1)
if args.debug:
logging.info("Debug: enabling...")
success, result = pyb.debug()
logging.info("{} {}".format(success, result))
if not success:
logging.error("Failed to set debug mode")
pyb.close()
exit(1)
if args._cmd == 'led_toggle' or all_funcs:
success = test_led_toggle(args, pyb)
if not success:
logging.error("Failed testing led_toggle")
pyb.close()
exit(1)
if args._cmd == "adc" or all_funcs:
success = test_adc(args, pyb)
if not success:
logging.error("Failed testing adc")
pyb.close()
exit(1)
if args._cmd == "pwm" or all_funcs:
success = test_pwm(args, pyb)
if not success:
logging.error("Failed testing adc")
pyb.close()
exit(1)
if args._cmd == "misc" or all_funcs:
success = test_misc(args, pyb)
if not success:
logging.error("Failed testing misc")
pyb.close()
exit(1)
logging.info("all tests passed")
pyb.close()