Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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