-
Notifications
You must be signed in to change notification settings - Fork 6
/
example_scripts.py
123 lines (97 loc) · 3.23 KB
/
example_scripts.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
# This is non working code !!! Just some examples.
# (c) Anton Vanhoucke & Ste7an
####### master side #######
ur=UartRemote()
# Download slave script
ur.repl_activate()
ur.repl_run("""
# some script (see slave side)
""")
# Run loop on slave, this will not REPLy.
ur.repl_run("ur.loop()", reply=False)
# send receive command
# identical to current situation, except for the extra () around encoding and payload.
reply = ur.call('my_function', 'B2s', [[1,2,3],"hi"])
reply == ('my_functionack', ['hi', 'hihi', 'hihihi'])
reply = ur.call(
'total',
'repr',
[1,2,3,4],
)
reply == ('totalack', 10)
# default testing command. Returns strings you throw at it.
reply = ur.call('echo', '5s','hello')
reply == ('echoack', 'hello')
reply = ur.call('echo', '2B',3,5)
# Echo always returns strings
reply == ('echoack', '[3,5]')
# Turn off encoding to speed it up. Unpacker will stay default ur.unpack()
# If ur.unpack() 'excepts' because of bad formatting it stops and returns the raw bytes.
reply = ur.call('echo', b'hello')
# Echo always returns strings
reply == ('echoack', "b'hello'")
reply = ur.call('raw_echo', b'hello')
reply == ('raw_echoack', b'hello')
reply = ur.call('raw_echo', 'raw', b'hello')
reply == ('raw_echoack', b'hello')
# send command only, don't bother reveceiving and don't block.
ur.call('sleep', 'i', 2000, reply=False)
####### slave side #######
ur=UartRemote()
def echo(*args):
return str(args)
def raw_echo(my_bytes):
return my_bytes
def my_function(my_list, my_str):
return [n*my_str for n in my_list]
def total(my_encoded_list):
my_list = struct.unpack('BBBB', my_encoded_list)
total=sum(my_list)
return struct.pack('i', total)
# Examples
ur.add_command(echo, 'repr')
ur.add_command(echo, 'repr', name='echo') # equivalent
ur.add_command(my_function, 'repr')
# Void or raw return
ur.add_command(raw_echo)
ur.add_command(total)
# start loop
ur.loop()
# Alternatively, Create custom loop, handling any 'call()' from master
def loop():
while True:
# Non-blocking processing of any available calls over uart
# Also disables local repl for convenience
ur.process_uart()
# Do your stuff here
pass
ur.enable_local_repl()
loop()
# Alternatively get commands and their data.
def loop():
while True:
# Non-blocking receipt if any available calls over uart
# Also disables local repl for convenience
if ur.available():
command, value = ur.receive_command()
ur.ack_ok() # The other side expects a reply.
# Do your stuff here
if command == 'wait':
utime.sleep_ms(value)
loop()
# Alternatively get commands and their data and send custom reply.
def loop():
while True:
# Non-blocking receipt if any available calls over uart
# Also disables local repl for convenience
# Auto acks receipt of call
if ur.available():
command, value = ur.receive_command()
# Do your stuff here
if command == 'local_ticks':
result = utime.ticks_ms()
ur.ack_ok(command, 'I', result)
else:
if command != 'err': value = 'no such command'
ur.ack_err(value)
loop()