BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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