User | Revision | Line number | New contents of line |
nexpaq |
0:6c56fb4bc5f0
|
1
|
# mbedRPC.py - mbed RPC interface for Python
|
nexpaq |
0:6c56fb4bc5f0
|
2
|
#
|
nexpaq |
0:6c56fb4bc5f0
|
3
|
##Copyright (c) 2010 ARM Ltd
|
nexpaq |
0:6c56fb4bc5f0
|
4
|
##
|
nexpaq |
0:6c56fb4bc5f0
|
5
|
##Permission is hereby granted, free of charge, to any person obtaining a copy
|
nexpaq |
0:6c56fb4bc5f0
|
6
|
##of this software and associated documentation files (the "Software"), to deal
|
nexpaq |
0:6c56fb4bc5f0
|
7
|
##in the Software without restriction, including without limitation the rights
|
nexpaq |
0:6c56fb4bc5f0
|
8
|
##to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
nexpaq |
0:6c56fb4bc5f0
|
9
|
##copies of the Software, and to permit persons to whom the Software is
|
nexpaq |
0:6c56fb4bc5f0
|
10
|
##furnished to do so, subject to the following conditions:
|
nexpaq |
0:6c56fb4bc5f0
|
11
|
##
|
nexpaq |
0:6c56fb4bc5f0
|
12
|
##The above copyright notice and this permission notice shall be included in
|
nexpaq |
0:6c56fb4bc5f0
|
13
|
##all copies or substantial portions of the Software.
|
nexpaq |
0:6c56fb4bc5f0
|
14
|
##
|
nexpaq |
0:6c56fb4bc5f0
|
15
|
##THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
nexpaq |
0:6c56fb4bc5f0
|
16
|
##IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
nexpaq |
0:6c56fb4bc5f0
|
17
|
##FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
nexpaq |
0:6c56fb4bc5f0
|
18
|
##AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
nexpaq |
0:6c56fb4bc5f0
|
19
|
##LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
nexpaq |
0:6c56fb4bc5f0
|
20
|
##OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
nexpaq |
0:6c56fb4bc5f0
|
21
|
##THE SOFTWARE.
|
nexpaq |
0:6c56fb4bc5f0
|
22
|
#
|
nexpaq |
0:6c56fb4bc5f0
|
23
|
# Example:
|
nexpaq |
0:6c56fb4bc5f0
|
24
|
# >from mbedRPC import*
|
nexpaq |
0:6c56fb4bc5f0
|
25
|
# >mbed = SerialRPC("COM5",9600)
|
nexpaq |
0:6c56fb4bc5f0
|
26
|
# >myled = DigitalOut(mbed,"myled") <--- Where the text in quotations matches your RPC pin definition's second parameter, in this case it could be RpcDigitalOut myled(LED1,"myled");
|
nexpaq |
0:6c56fb4bc5f0
|
27
|
# >myled.write(1)
|
nexpaq |
0:6c56fb4bc5f0
|
28
|
# >
|
nexpaq |
0:6c56fb4bc5f0
|
29
|
|
nexpaq |
0:6c56fb4bc5f0
|
30
|
import serial, urllib2, time
|
nexpaq |
0:6c56fb4bc5f0
|
31
|
|
nexpaq |
0:6c56fb4bc5f0
|
32
|
# mbed super class
|
nexpaq |
0:6c56fb4bc5f0
|
33
|
class mbed:
|
nexpaq |
0:6c56fb4bc5f0
|
34
|
def __init__(self):
|
nexpaq |
0:6c56fb4bc5f0
|
35
|
print("This will work as a demo but no transport mechanism has been selected")
|
nexpaq |
0:6c56fb4bc5f0
|
36
|
|
nexpaq |
0:6c56fb4bc5f0
|
37
|
def rpc(self, name, method, args):
|
nexpaq |
0:6c56fb4bc5f0
|
38
|
print("Superclass method not overridden")
|
nexpaq |
0:6c56fb4bc5f0
|
39
|
|
nexpaq |
0:6c56fb4bc5f0
|
40
|
|
nexpaq |
0:6c56fb4bc5f0
|
41
|
# Transport mechanisms, derived from mbed
|
nexpaq |
0:6c56fb4bc5f0
|
42
|
class SerialRPC(mbed):
|
nexpaq |
0:6c56fb4bc5f0
|
43
|
def __init__(self, port, baud):
|
nexpaq |
0:6c56fb4bc5f0
|
44
|
self.ser = serial.Serial(port)
|
nexpaq |
0:6c56fb4bc5f0
|
45
|
self.ser.setBaudrate(baud)
|
nexpaq |
0:6c56fb4bc5f0
|
46
|
|
nexpaq |
0:6c56fb4bc5f0
|
47
|
def rpc(self, name, method, args):
|
nexpaq |
0:6c56fb4bc5f0
|
48
|
# creates the command to be sent serially - /name/method arg1 arg2 arg3 ... argN
|
nexpaq |
0:6c56fb4bc5f0
|
49
|
str = "/" + name + "/" + method + " " + " ".join(args) + "\n"
|
nexpaq |
0:6c56fb4bc5f0
|
50
|
# prints the command being executed
|
nexpaq |
0:6c56fb4bc5f0
|
51
|
print str
|
nexpaq |
0:6c56fb4bc5f0
|
52
|
# writes the command to serial
|
nexpaq |
0:6c56fb4bc5f0
|
53
|
self.ser.write(str)
|
nexpaq |
0:6c56fb4bc5f0
|
54
|
# strips trailing characters from the line just written
|
nexpaq |
0:6c56fb4bc5f0
|
55
|
ret_val = self.ser.readline().strip()
|
nexpaq |
0:6c56fb4bc5f0
|
56
|
return ret_val
|
nexpaq |
0:6c56fb4bc5f0
|
57
|
|
nexpaq |
0:6c56fb4bc5f0
|
58
|
|
nexpaq |
0:6c56fb4bc5f0
|
59
|
class HTTPRPC(mbed):
|
nexpaq |
0:6c56fb4bc5f0
|
60
|
def __init__(self, ip):
|
nexpaq |
0:6c56fb4bc5f0
|
61
|
self.host = "http://" + ip
|
nexpaq |
0:6c56fb4bc5f0
|
62
|
|
nexpaq |
0:6c56fb4bc5f0
|
63
|
def rpc(self, name, method, args):
|
nexpaq |
0:6c56fb4bc5f0
|
64
|
response = urllib2.urlopen(self.host + "/rpc/" + name + "/" + method + "%20" + "%20".join(args))
|
nexpaq |
0:6c56fb4bc5f0
|
65
|
return response.read().strip()
|
nexpaq |
0:6c56fb4bc5f0
|
66
|
|
nexpaq |
0:6c56fb4bc5f0
|
67
|
|
nexpaq |
0:6c56fb4bc5f0
|
68
|
# generic mbed interface super class
|
nexpaq |
0:6c56fb4bc5f0
|
69
|
class mbed_interface():
|
nexpaq |
0:6c56fb4bc5f0
|
70
|
# initialize an mbed interface with a transport mechanism and pin name
|
nexpaq |
0:6c56fb4bc5f0
|
71
|
def __init__(self, this_mbed, mpin):
|
nexpaq |
0:6c56fb4bc5f0
|
72
|
self.mbed = this_mbed
|
nexpaq |
0:6c56fb4bc5f0
|
73
|
if isinstance(mpin, str):
|
nexpaq |
0:6c56fb4bc5f0
|
74
|
self.name = mpin
|
nexpaq |
0:6c56fb4bc5f0
|
75
|
|
nexpaq |
0:6c56fb4bc5f0
|
76
|
def __del__(self):
|
nexpaq |
0:6c56fb4bc5f0
|
77
|
r = self.mbed.rpc(self.name, "delete", [])
|
nexpaq |
0:6c56fb4bc5f0
|
78
|
|
nexpaq |
0:6c56fb4bc5f0
|
79
|
def new(self, class_name, name, pin1, pin2 = "", pin3 = ""):
|
nexpaq |
0:6c56fb4bc5f0
|
80
|
args = [arg for arg in [pin1,pin2,pin3,name] if arg != ""]
|
nexpaq |
0:6c56fb4bc5f0
|
81
|
r = self.mbed.rpc(class_name, "new", args)
|
nexpaq |
0:6c56fb4bc5f0
|
82
|
|
nexpaq |
0:6c56fb4bc5f0
|
83
|
# generic read
|
nexpaq |
0:6c56fb4bc5f0
|
84
|
def read(self):
|
nexpaq |
0:6c56fb4bc5f0
|
85
|
r = self.mbed.rpc(self.name, "read", [])
|
nexpaq |
0:6c56fb4bc5f0
|
86
|
return int(r)
|
nexpaq |
0:6c56fb4bc5f0
|
87
|
|
nexpaq |
0:6c56fb4bc5f0
|
88
|
|
nexpaq |
0:6c56fb4bc5f0
|
89
|
# for classes that need write functionality - inherits from the generic reading interface
|
nexpaq |
0:6c56fb4bc5f0
|
90
|
class mbed_interface_write(mbed_interface):
|
nexpaq |
0:6c56fb4bc5f0
|
91
|
def __init__(self, this_mbed, mpin):
|
nexpaq |
0:6c56fb4bc5f0
|
92
|
mbed_interface.__init__(self, this_mbed, mpin)
|
nexpaq |
0:6c56fb4bc5f0
|
93
|
|
nexpaq |
0:6c56fb4bc5f0
|
94
|
# generic write
|
nexpaq |
0:6c56fb4bc5f0
|
95
|
def write(self, value):
|
nexpaq |
0:6c56fb4bc5f0
|
96
|
r = self.mbed.rpc(self.name, "write", [str(value)])
|
nexpaq |
0:6c56fb4bc5f0
|
97
|
|
nexpaq |
0:6c56fb4bc5f0
|
98
|
|
nexpaq |
0:6c56fb4bc5f0
|
99
|
# mbed interfaces
|
nexpaq |
0:6c56fb4bc5f0
|
100
|
class DigitalOut(mbed_interface_write):
|
nexpaq |
0:6c56fb4bc5f0
|
101
|
def __init__(self, this_mbed, mpin):
|
nexpaq |
0:6c56fb4bc5f0
|
102
|
mbed_interface_write.__init__(self, this_mbed, mpin)
|
nexpaq |
0:6c56fb4bc5f0
|
103
|
|
nexpaq |
0:6c56fb4bc5f0
|
104
|
|
nexpaq |
0:6c56fb4bc5f0
|
105
|
class AnalogIn(mbed_interface):
|
nexpaq |
0:6c56fb4bc5f0
|
106
|
def __init__(self, this_mbed, mpin):
|
nexpaq |
0:6c56fb4bc5f0
|
107
|
mbed_interface.__init__(self, this_mbed, mpin)
|
nexpaq |
0:6c56fb4bc5f0
|
108
|
|
nexpaq |
0:6c56fb4bc5f0
|
109
|
def read_u16(self):
|
nexpaq |
0:6c56fb4bc5f0
|
110
|
r = self.mbed.rpc(self.name, "read_u16", [])
|
nexpaq |
0:6c56fb4bc5f0
|
111
|
return int(r)
|
nexpaq |
0:6c56fb4bc5f0
|
112
|
|
nexpaq |
0:6c56fb4bc5f0
|
113
|
|
nexpaq |
0:6c56fb4bc5f0
|
114
|
class AnalogOut(mbed_interface_write):
|
nexpaq |
0:6c56fb4bc5f0
|
115
|
def __init__(self, this_mbed, mpin):
|
nexpaq |
0:6c56fb4bc5f0
|
116
|
mbed_interface_write.__init__(self, this_mbed, mpin)
|
nexpaq |
0:6c56fb4bc5f0
|
117
|
|
nexpaq |
0:6c56fb4bc5f0
|
118
|
def write_u16(self, value):
|
nexpaq |
0:6c56fb4bc5f0
|
119
|
self.mbed.rpc(self.name, "write_u16", [str(value)])
|
nexpaq |
0:6c56fb4bc5f0
|
120
|
|
nexpaq |
0:6c56fb4bc5f0
|
121
|
def read(self):
|
nexpaq |
0:6c56fb4bc5f0
|
122
|
r = self.mbed.rpc(self.name, "read", [])
|
nexpaq |
0:6c56fb4bc5f0
|
123
|
return float(r)
|
nexpaq |
0:6c56fb4bc5f0
|
124
|
|
nexpaq |
0:6c56fb4bc5f0
|
125
|
|
nexpaq |
0:6c56fb4bc5f0
|
126
|
class DigitalIn(mbed_interface):
|
nexpaq |
0:6c56fb4bc5f0
|
127
|
def __init__(self, this_mbed, mpin):
|
nexpaq |
0:6c56fb4bc5f0
|
128
|
mbed_interface.__init__(self, this_mbed, mpin)
|
nexpaq |
0:6c56fb4bc5f0
|
129
|
|
nexpaq |
0:6c56fb4bc5f0
|
130
|
|
nexpaq |
0:6c56fb4bc5f0
|
131
|
class PwmOut(mbed_interface_write):
|
nexpaq |
0:6c56fb4bc5f0
|
132
|
def __init__(self, this_mbed, mpin):
|
nexpaq |
0:6c56fb4bc5f0
|
133
|
mbed_interface_write.__init__(self, this_mbed, mpin)
|
nexpaq |
0:6c56fb4bc5f0
|
134
|
|
nexpaq |
0:6c56fb4bc5f0
|
135
|
def read(self):
|
nexpaq |
0:6c56fb4bc5f0
|
136
|
r = self.mbed.rpc(self.name, "read", [])
|
nexpaq |
0:6c56fb4bc5f0
|
137
|
return r
|
nexpaq |
0:6c56fb4bc5f0
|
138
|
|
nexpaq |
0:6c56fb4bc5f0
|
139
|
def period(self, value):
|
nexpaq |
0:6c56fb4bc5f0
|
140
|
self.mbed.rpc(self.name, "period", [str(value)])
|
nexpaq |
0:6c56fb4bc5f0
|
141
|
|
nexpaq |
0:6c56fb4bc5f0
|
142
|
def period_ms(self, value):
|
nexpaq |
0:6c56fb4bc5f0
|
143
|
self.mbed.rpc(self.name, "period_ms", [str(value)])
|
nexpaq |
0:6c56fb4bc5f0
|
144
|
|
nexpaq |
0:6c56fb4bc5f0
|
145
|
def period_us(self, value):
|
nexpaq |
0:6c56fb4bc5f0
|
146
|
self.mbed.rpc(self.name, "period_us", [str(value)])
|
nexpaq |
0:6c56fb4bc5f0
|
147
|
|
nexpaq |
0:6c56fb4bc5f0
|
148
|
def pulsewidth(self, value):
|
nexpaq |
0:6c56fb4bc5f0
|
149
|
self.mbed.rpc(self.name, "pulsewidth", [str(value)])
|
nexpaq |
0:6c56fb4bc5f0
|
150
|
|
nexpaq |
0:6c56fb4bc5f0
|
151
|
def pulsewidth_ms(self, value):
|
nexpaq |
0:6c56fb4bc5f0
|
152
|
self.mbed.rpc(self.name, "pulsewidth_ms", [str(value)])
|
nexpaq |
0:6c56fb4bc5f0
|
153
|
|
nexpaq |
0:6c56fb4bc5f0
|
154
|
def pulsewidth_us(self, value):
|
nexpaq |
0:6c56fb4bc5f0
|
155
|
self.mbed.rpc(self.name, "pulsewidth_us", [str(value)])
|
nexpaq |
0:6c56fb4bc5f0
|
156
|
|
nexpaq |
0:6c56fb4bc5f0
|
157
|
|
nexpaq |
0:6c56fb4bc5f0
|
158
|
class RPCFunction(mbed_interface):
|
nexpaq |
0:6c56fb4bc5f0
|
159
|
def __init__(self, this_mbed, name):
|
nexpaq |
0:6c56fb4bc5f0
|
160
|
mbed_interface.__init__(self, this_mbed, name)
|
nexpaq |
0:6c56fb4bc5f0
|
161
|
|
nexpaq |
0:6c56fb4bc5f0
|
162
|
def run(self, input):
|
nexpaq |
0:6c56fb4bc5f0
|
163
|
r = self.mbed.rpc(self.name, "run", [input])
|
nexpaq |
0:6c56fb4bc5f0
|
164
|
return r
|
nexpaq |
0:6c56fb4bc5f0
|
165
|
|
nexpaq |
0:6c56fb4bc5f0
|
166
|
|
nexpaq |
0:6c56fb4bc5f0
|
167
|
class RPCVariable(mbed_interface_write):
|
nexpaq |
0:6c56fb4bc5f0
|
168
|
def __init__(self, this_mbed, name):
|
nexpaq |
0:6c56fb4bc5f0
|
169
|
mbed_interface_write.__init__(self, this_mbed, name)
|
nexpaq |
0:6c56fb4bc5f0
|
170
|
|
nexpaq |
0:6c56fb4bc5f0
|
171
|
def read(self):
|
nexpaq |
0:6c56fb4bc5f0
|
172
|
r = self.mbed.rpc(self.name, "read", [])
|
nexpaq |
0:6c56fb4bc5f0
|
173
|
return r
|
nexpaq |
0:6c56fb4bc5f0
|
174
|
|
nexpaq |
0:6c56fb4bc5f0
|
175
|
class Timer(mbed_interface):
|
nexpaq |
0:6c56fb4bc5f0
|
176
|
def __init__(self, this_mbed, name):
|
nexpaq |
0:6c56fb4bc5f0
|
177
|
mbed_interface.__init__(self, this_mbed, name)
|
nexpaq |
0:6c56fb4bc5f0
|
178
|
|
nexpaq |
0:6c56fb4bc5f0
|
179
|
def start(self):
|
nexpaq |
0:6c56fb4bc5f0
|
180
|
r = self.mbed.rpc(self.name, "start", [])
|
nexpaq |
0:6c56fb4bc5f0
|
181
|
|
nexpaq |
0:6c56fb4bc5f0
|
182
|
def stop(self):
|
nexpaq |
0:6c56fb4bc5f0
|
183
|
r = self.mbed.rpc(self.name, "stop", [])
|
nexpaq |
0:6c56fb4bc5f0
|
184
|
|
nexpaq |
0:6c56fb4bc5f0
|
185
|
def reset(self):
|
nexpaq |
0:6c56fb4bc5f0
|
186
|
r = self.mbed.rpc(self.name, "reset", [])
|
nexpaq |
0:6c56fb4bc5f0
|
187
|
|
nexpaq |
0:6c56fb4bc5f0
|
188
|
def read(self):
|
nexpaq |
0:6c56fb4bc5f0
|
189
|
r = self.mbed.rpc(self.name, "read", [])
|
nexpaq |
0:6c56fb4bc5f0
|
190
|
return float(re.search('\d+\.*\d*', r).group(0))
|
nexpaq |
0:6c56fb4bc5f0
|
191
|
|
nexpaq |
0:6c56fb4bc5f0
|
192
|
def read_ms(self):
|
nexpaq |
0:6c56fb4bc5f0
|
193
|
r = self.mbed.rpc(self.name, "read_ms", [])
|
nexpaq |
0:6c56fb4bc5f0
|
194
|
return float(re.search('\d+\.*\d*', r).group(0))
|
nexpaq |
0:6c56fb4bc5f0
|
195
|
|
nexpaq |
0:6c56fb4bc5f0
|
196
|
def read_us(self):
|
nexpaq |
0:6c56fb4bc5f0
|
197
|
r = self.mbed.rpc(self.name, "read_us", [])
|
nexpaq |
0:6c56fb4bc5f0
|
198
|
return float(re.search('\d+\.*\d*', r).group(0))
|
nexpaq |
0:6c56fb4bc5f0
|
199
|
|
nexpaq |
0:6c56fb4bc5f0
|
200
|
# Serial
|
nexpaq |
0:6c56fb4bc5f0
|
201
|
class Serial():
|
nexpaq |
0:6c56fb4bc5f0
|
202
|
def __init__(self, this_mbed, tx, rx=""):
|
nexpaq |
0:6c56fb4bc5f0
|
203
|
self.mbed = this_mbed
|
nexpaq |
0:6c56fb4bc5f0
|
204
|
if isinstance(tx, str):
|
nexpaq |
0:6c56fb4bc5f0
|
205
|
self.name = tx
|
nexpaq |
0:6c56fb4bc5f0
|
206
|
|
nexpaq |
0:6c56fb4bc5f0
|
207
|
def __del__(self):
|
nexpaq |
0:6c56fb4bc5f0
|
208
|
r = self.mbed.rpc(self.name, "delete", [])
|
nexpaq |
0:6c56fb4bc5f0
|
209
|
|
nexpaq |
0:6c56fb4bc5f0
|
210
|
def baud(self, value):
|
nexpaq |
0:6c56fb4bc5f0
|
211
|
r = self.mbed.rpc(self.name, "baud", [str(value)])
|
nexpaq |
0:6c56fb4bc5f0
|
212
|
|
nexpaq |
0:6c56fb4bc5f0
|
213
|
def putc(self, value):
|
nexpaq |
0:6c56fb4bc5f0
|
214
|
r = self.mbed.rpc(self.name, "putc", [str(value)])
|
nexpaq |
0:6c56fb4bc5f0
|
215
|
|
nexpaq |
0:6c56fb4bc5f0
|
216
|
def puts(self, value):
|
nexpaq |
0:6c56fb4bc5f0
|
217
|
r = self.mbed.rpc(self.name, "puts", ["\"" + str(value) + "\""])
|
nexpaq |
0:6c56fb4bc5f0
|
218
|
|
nexpaq |
0:6c56fb4bc5f0
|
219
|
def getc(self):
|
nexpaq |
0:6c56fb4bc5f0
|
220
|
r = self.mbed.rpc(self.name, "getc", [])
|
nexpaq |
0:6c56fb4bc5f0
|
221
|
return int(r)
|
nexpaq |
0:6c56fb4bc5f0
|
222
|
|
nexpaq |
0:6c56fb4bc5f0
|
223
|
|
nexpaq |
0:6c56fb4bc5f0
|
224
|
def wait(s):
|
nexpaq |
0:6c56fb4bc5f0
|
225
|
time.sleep(s)
|