Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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