mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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