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