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