mirror of https://github.com/ARMmbed/mbed-os.git
Merge pull request #1133 from sarahmarshy/mbedrpc_python_update
Updated to mbedrpc python scriptpull/1140/head
commit
03fff9249b
|
@ -18,50 +18,15 @@ limitations under the License.
|
||||||
Example:
|
Example:
|
||||||
> from mbedRPC import*
|
> from mbedRPC import*
|
||||||
> mbed = SerialRPC("COM5",9600);
|
> mbed = SerialRPC("COM5",9600);
|
||||||
> myled = DigitalOut(mbed, LED1);
|
> 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");
|
||||||
> myled.write(1)
|
> myled.write(1)
|
||||||
>
|
>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import serial, urllib2, time
|
import serial, urllib2, time
|
||||||
|
|
||||||
|
|
||||||
class pin():
|
|
||||||
def __init__(self, id):
|
|
||||||
self.name = id
|
|
||||||
|
|
||||||
LED1 = pin("LED1")
|
|
||||||
LED2 = pin("LED2")
|
|
||||||
LED3 = pin("LED3")
|
|
||||||
LED4 = pin("LED4")
|
|
||||||
|
|
||||||
p5 = pin("p5")
|
|
||||||
p6 = pin("p6")
|
|
||||||
p7 = pin("p7")
|
|
||||||
p8 = pin("p8")
|
|
||||||
p9 = pin("p9")
|
|
||||||
p10 = pin("p10")
|
|
||||||
p11 = pin("p11")
|
|
||||||
p12 = pin("p12")
|
|
||||||
p13 = pin("p13")
|
|
||||||
p14 = pin("p14")
|
|
||||||
p15 = pin("p15")
|
|
||||||
p16 = pin("p16")
|
|
||||||
p17 = pin("p17")
|
|
||||||
p18 = pin("p18")
|
|
||||||
p19 = pin("p19")
|
|
||||||
p20 = pin("p20")
|
|
||||||
p21 = pin("p21")
|
|
||||||
p22 = pin("p22")
|
|
||||||
p23 = pin("p23")
|
|
||||||
p24 = pin("p24")
|
|
||||||
p25 = pin("p25")
|
|
||||||
p26 = pin("p26")
|
|
||||||
p27 = pin("p27")
|
|
||||||
p28 = pin("p28")
|
|
||||||
p29 = pin("p29")
|
|
||||||
p30 = pin("p30")
|
|
||||||
|
|
||||||
|
|
||||||
#mbed super class
|
#mbed super class
|
||||||
class mbed:
|
class mbed:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -71,37 +36,25 @@ class mbed:
|
||||||
print("Superclass method not overridden")
|
print("Superclass method not overridden")
|
||||||
|
|
||||||
#Transport mechanisms, derived from mbed
|
#Transport mechanisms, derived from mbed
|
||||||
|
|
||||||
class SerialRPC(mbed):
|
class SerialRPC(mbed):
|
||||||
def __init__(self, port, baud=9600, reset=True, debug=False):
|
|
||||||
|
def __init__(self,port, baud):
|
||||||
self.ser = serial.Serial(port)
|
self.ser = serial.Serial(port)
|
||||||
self.ser.setBaudrate(baud)
|
self.ser.setBaudrate(baud)
|
||||||
self.ser.flushInput()
|
|
||||||
self.ser.flushOutput()
|
|
||||||
self.debug = debug
|
|
||||||
if reset:
|
|
||||||
if debug:
|
|
||||||
print "Reset mbed"
|
|
||||||
self.ser.sendBreak()
|
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
def rpc(self, name, method, args):
|
def rpc(self, name, method, args):
|
||||||
request = "/" + name + "/" + method + " " + " ".join(args)
|
#creates the command to be sent serially - /name/method arg1 arg2 arg3 ... argN
|
||||||
if self.debug:
|
str = "/" + name + "/" + method + " " + " ".join(args) + "\n"
|
||||||
print "[RPC::TX] %s" % request
|
#prints the command being executed
|
||||||
self.ser.write(request + "\n")
|
print str
|
||||||
|
#writes the command to serial
|
||||||
while True:
|
self.ser.write(str)
|
||||||
response = self.ser.readline().strip()
|
#strips trailing characters from the line just written
|
||||||
if self.debug:
|
ret_val = self.ser.readline().strip()
|
||||||
print "[RPC::RX] %s" % response
|
return ret_val
|
||||||
|
|
||||||
# Ignore comments
|
|
||||||
if not response.startswith('#'): break
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
class HTTPRPC(mbed):
|
class HTTPRPC(mbed):
|
||||||
|
|
||||||
def __init__(self, ip):
|
def __init__(self, ip):
|
||||||
self.host = "http://" + ip
|
self.host = "http://" + ip
|
||||||
|
|
||||||
|
@ -110,130 +63,112 @@ class HTTPRPC(mbed):
|
||||||
return response.read().strip()
|
return response.read().strip()
|
||||||
|
|
||||||
|
|
||||||
#mbed Interfaces
|
#generic mbed interface super class
|
||||||
|
class mbed_interface():
|
||||||
class DigitalOut():
|
#initialize an mbed interface with a transport mechanism and pin name
|
||||||
def __init__(self, this_mbed , mpin):
|
def __init__(self, this_mbed , mpin):
|
||||||
self.mbed = this_mbed
|
self.mbed = this_mbed
|
||||||
if isinstance(mpin,str):
|
if isinstance(mpin,str):
|
||||||
self.name = mpin
|
self.name = mpin
|
||||||
elif isinstance(mpin, pin):
|
|
||||||
self.name = self.mbed.rpc("DigitalOut", "new", [mpin.name])
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
r = self.mbed.rpc(self.name, "delete", [])
|
r = self.mbed.rpc(self.name, "delete", [])
|
||||||
|
#generic read
|
||||||
def write(self, value):
|
|
||||||
r = self.mbed.rpc(self.name, "write", [str(value)])
|
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
r = self.mbed.rpc(self.name, "read", [])
|
r = self.mbed.rpc(self.name, "read", [])
|
||||||
return int(r)
|
return int(r)
|
||||||
|
|
||||||
|
#for classes that need write functionality - inherits from the generic reading interface
|
||||||
|
class mbed_interface_write(mbed_interface):
|
||||||
|
|
||||||
class AnalogIn():
|
|
||||||
def __init__(self, this_mbed , mpin):
|
def __init__(self, this_mbed , mpin):
|
||||||
self.mbed = this_mbed
|
mbed_interface.__init__(self,this_mbed,mpin)
|
||||||
if isinstance(mpin, str):
|
#generic write
|
||||||
self.name = mpin
|
def write(self, value):
|
||||||
elif isinstance(mpin, pin):
|
r = self.mbed.rpc(self.name, "write", [str(value)])
|
||||||
self.name = self.mbed.rpc("AnalogIn", "new", [mpin.name])
|
|
||||||
|
|
||||||
def __del__(self):
|
#mbed interfaces
|
||||||
r = self.mbed.rpc(self.name, "delete", [])
|
class DigitalOut(mbed_interface_write):
|
||||||
|
|
||||||
def read(self):
|
def __init__(self, this_mbed , mpin):
|
||||||
r = self.mbed.rpc(self.name, "read", [])
|
mbed_interface_write.__init__(self,this_mbed,mpin)
|
||||||
return float(r)
|
|
||||||
|
class AnalogIn(mbed_interface):
|
||||||
|
|
||||||
|
def __init__(self, this_mbed , mpin):
|
||||||
|
mbed_interface.__init__(self,this_mbed,mpin)
|
||||||
|
|
||||||
def read_u16(self):
|
def read_u16(self):
|
||||||
r = self.mbed.rpc(self.name, "read_u16", [])
|
r = self.mbed.rpc(self.name, "read_u16", [])
|
||||||
return int(r)
|
return int(r)
|
||||||
|
|
||||||
|
class AnalogOut(mbed_interface_write):
|
||||||
|
|
||||||
class AnalogOut():
|
|
||||||
def __init__(self, this_mbed , mpin):
|
def __init__(self, this_mbed , mpin):
|
||||||
self.mbed = this_mbed
|
mbed_interface_write.__init__(self,this_mbed,mpin)
|
||||||
if isinstance(mpin, str):
|
|
||||||
self.name = mpin
|
|
||||||
elif isinstance(mpin, pin):
|
|
||||||
self.name = self.mbed.rpc("AnalogOut", "new", [mpin.name])
|
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
r = self.mbed.rpc(self.name, "delete", [])
|
|
||||||
|
|
||||||
def write(self, value):
|
|
||||||
r = self.mbed.rpc(self.name, "write", [str(value)])
|
|
||||||
|
|
||||||
def write_u16(self, value):
|
def write_u16(self, value):
|
||||||
r = self.mbed.rpc(self.name, "write_u16", [str(value)])
|
self.mbed.rpc(self.name, "write_u16", [str(value)])
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
r = self.mbed.rpc(self.name, "read", [])
|
r = self.mbed.rpc(self.name, "read", [])
|
||||||
return float(r)
|
return float(r)
|
||||||
|
|
||||||
|
class DigitalIn(mbed_interface):
|
||||||
|
|
||||||
class DigitalIn():
|
|
||||||
def __init__(self, this_mbed , mpin):
|
def __init__(self, this_mbed , mpin):
|
||||||
self.mbed = this_mbed
|
mbed_interface.__init__(self,this_mbed,mpin)
|
||||||
if isinstance(mpin, str):
|
|
||||||
self.name = mpin
|
|
||||||
elif isinstance(mpin, pin):
|
|
||||||
self.name = self.mbed.rpc("DigitalIn", "new", [mpin.name])
|
|
||||||
|
|
||||||
def __del__(self):
|
class PwmOut(mbed_interface_write):
|
||||||
r = self.mbed.rpc(self.name, "delete", [])
|
|
||||||
|
def __init__(self, this_mbed , mpin):
|
||||||
|
mbed_interface_write.__init__(self,this_mbed,mpin)
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
r = self.mbed.rpc(self.name, "read", [])
|
r = self.mbed.rpc(self.name, "read", [])
|
||||||
return int(r)
|
return r
|
||||||
|
|
||||||
|
|
||||||
class PwmOut():
|
|
||||||
def __init__(self, this_mbed , mpin):
|
|
||||||
self.mbed = this_mbed
|
|
||||||
if isinstance(mpin, str):
|
|
||||||
self.name = mpin
|
|
||||||
elif isinstance(mpin, pin):
|
|
||||||
self.name = self.mbed.rpc("PwmOut", "new", [mpin.name])
|
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
r = self.mbed.rpc(self.name, "delete", [])
|
|
||||||
|
|
||||||
def write(self, value):
|
|
||||||
r = self.mbed.rpc(self.name, "write", [str(value)])
|
|
||||||
|
|
||||||
def read(self):
|
|
||||||
r = self.mbed.rpc(self.name, "read", [])
|
|
||||||
return float(r)
|
|
||||||
|
|
||||||
def period(self, value):
|
def period(self, value):
|
||||||
r = self.mbed.rpc(self.name, "period", [str(value)])
|
self.mbed.rpc(self.name, "period", [str(value)])
|
||||||
|
|
||||||
def period_ms(self, value):
|
def period_ms(self, value):
|
||||||
r = self.mbed.rpc(self.name, "period_ms", [str(value)])
|
self.mbed.rpc(self.name, "period_ms", [str(value)])
|
||||||
|
|
||||||
def period_us(self, value):
|
def period_us(self, value):
|
||||||
r = self.mbed.rpc(self.name, "period_us", [str(value)])
|
self.mbed.rpc(self.name, "period_us", [str(value)])
|
||||||
|
|
||||||
def puslewidth(self, value):
|
def pulsewidth(self, value):
|
||||||
r = self.mbed.rpc(self.name, "pulsewidth", [str(value)])
|
self.mbed.rpc(self.name, "pulsewidth", [str(value)])
|
||||||
|
|
||||||
def puslewidth_ms(self, value):
|
def pulsewidth_ms(self, value):
|
||||||
r = self.mbed.rpc(self.name, "pulsewidth_ms", [str(value)])
|
self.mbed.rpc(self.name, "pulsewidth_ms", [str(value)])
|
||||||
|
|
||||||
def puslewidth_us(self, value):
|
def pulsewidth_us(self, value):
|
||||||
r = self.mbed.rpc(self.name, "pulsewidth_us", [str(value)])
|
self.mbed.rpc(self.name, "pulsewidth_us", [str(value)])
|
||||||
|
|
||||||
|
class RPCFunction(mbed_interface):
|
||||||
|
|
||||||
|
def __init__(self, this_mbed , name):
|
||||||
|
mbed_interface.__init__(self,this_mbed,name)
|
||||||
|
|
||||||
|
def run(self, input):
|
||||||
|
r = self.mbed.rpc(self.name, "run", [input])
|
||||||
|
return r
|
||||||
|
|
||||||
|
class RPCVariable(mbed_interface_write):
|
||||||
|
|
||||||
|
def __init__(self, this_mbed , name):
|
||||||
|
mbed_interface_write.__init__(self,this_mbed,name)
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
r = self.mbed.rpc(self.name, "read", [])
|
||||||
|
return r
|
||||||
|
#Serial
|
||||||
class Serial():
|
class Serial():
|
||||||
|
|
||||||
def __init__(self, this_mbed , tx, rx = ""):
|
def __init__(self, this_mbed , tx, rx = ""):
|
||||||
self.mbed = this_mbed
|
self.mbed = this_mbed
|
||||||
if isinstance(tx, str):
|
if isinstance(tx, str):
|
||||||
self.name = mpin
|
self.name = tx
|
||||||
elif isinstance(mpin, pin):
|
|
||||||
self.name = self.mbed.rpc("Serial", "new", [tx.name, rx.name])
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
r = self.mbed.rpc(self.name, "delete", [])
|
r = self.mbed.rpc(self.name, "delete", [])
|
||||||
|
|
||||||
|
@ -248,40 +183,5 @@ class Serial():
|
||||||
return int(r)
|
return int(r)
|
||||||
|
|
||||||
|
|
||||||
class RPCFunction():
|
|
||||||
def __init__(self, this_mbed , name):
|
|
||||||
self.mbed = this_mbed
|
|
||||||
if isinstance(name, str):
|
|
||||||
self.name = name
|
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
r = self.mbed.rpc(self.name, "delete", [])
|
|
||||||
|
|
||||||
def read(self):
|
|
||||||
r = self.mbed.rpc(self.name, "read", [])
|
|
||||||
return int(r)
|
|
||||||
|
|
||||||
def run(self, input):
|
|
||||||
r = self.mbed.rpc(self.name, "run", [input])
|
|
||||||
return r
|
|
||||||
|
|
||||||
|
|
||||||
class RPCVariable():
|
|
||||||
def __init__(self, this_mbed , name):
|
|
||||||
self.mbed = this_mbed
|
|
||||||
if isinstance(name, str):
|
|
||||||
self.name = name
|
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
r = self.mbed.rpc(self.name, "delete", [])
|
|
||||||
|
|
||||||
def write(self, value):
|
|
||||||
self.mbed.rpc(self.name, "write", [str(value)])
|
|
||||||
|
|
||||||
def read(self):
|
|
||||||
r = self.mbed.rpc(self.name, "read", [])
|
|
||||||
return r
|
|
||||||
|
|
||||||
|
|
||||||
def wait(s):
|
def wait(s):
|
||||||
time.sleep(s)
|
time.sleep(s)
|
||||||
|
|
Loading…
Reference in New Issue