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