Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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