Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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