Development mbed library for MAX32630FTHR
Dependents: blinky_max32630fthr
tools/dev/rpc_classes.py@0:5c4d7b2438d3, 2016-11-11 (annotated)
- Committer:
- switches
- Date:
- Fri Nov 11 20:59:50 2016 +0000
- Revision:
- 0:5c4d7b2438d3
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | """ |
switches | 0:5c4d7b2438d3 | 2 | mbed SDK |
switches | 0:5c4d7b2438d3 | 3 | Copyright (c) 2011-2013 ARM Limited |
switches | 0:5c4d7b2438d3 | 4 | |
switches | 0:5c4d7b2438d3 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
switches | 0:5c4d7b2438d3 | 6 | you may not use this file except in compliance with the License. |
switches | 0:5c4d7b2438d3 | 7 | You may obtain a copy of the License at |
switches | 0:5c4d7b2438d3 | 8 | |
switches | 0:5c4d7b2438d3 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
switches | 0:5c4d7b2438d3 | 10 | |
switches | 0:5c4d7b2438d3 | 11 | Unless required by applicable law or agreed to in writing, software |
switches | 0:5c4d7b2438d3 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
switches | 0:5c4d7b2438d3 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
switches | 0:5c4d7b2438d3 | 14 | See the License for the specific language governing permissions and |
switches | 0:5c4d7b2438d3 | 15 | limitations under the License. |
switches | 0:5c4d7b2438d3 | 16 | """ |
switches | 0:5c4d7b2438d3 | 17 | from os.path import join |
switches | 0:5c4d7b2438d3 | 18 | from jinja2 import Template |
switches | 0:5c4d7b2438d3 | 19 | |
switches | 0:5c4d7b2438d3 | 20 | from tools.paths import TOOLS_DATA, MBED_RPC |
switches | 0:5c4d7b2438d3 | 21 | |
switches | 0:5c4d7b2438d3 | 22 | RPC_TEMPLATES_PATH = join(TOOLS_DATA, "rpc") |
switches | 0:5c4d7b2438d3 | 23 | |
switches | 0:5c4d7b2438d3 | 24 | RPC_TEMPLATE = "RPCClasses.h" |
switches | 0:5c4d7b2438d3 | 25 | CLASS_TEMPLATE = "class.cpp" |
switches | 0:5c4d7b2438d3 | 26 | RPC_CLASSES_PATH = join(MBED_RPC, RPC_TEMPLATE) |
switches | 0:5c4d7b2438d3 | 27 | |
switches | 0:5c4d7b2438d3 | 28 | |
switches | 0:5c4d7b2438d3 | 29 | def get_template(name): |
switches | 0:5c4d7b2438d3 | 30 | return Template(open(join(RPC_TEMPLATES_PATH, name)).read()) |
switches | 0:5c4d7b2438d3 | 31 | |
switches | 0:5c4d7b2438d3 | 32 | |
switches | 0:5c4d7b2438d3 | 33 | def write_rpc_classes(classes): |
switches | 0:5c4d7b2438d3 | 34 | template = get_template(RPC_TEMPLATE) |
switches | 0:5c4d7b2438d3 | 35 | open(RPC_CLASSES_PATH, "w").write(template.render({"classes":classes})) |
switches | 0:5c4d7b2438d3 | 36 | |
switches | 0:5c4d7b2438d3 | 37 | |
switches | 0:5c4d7b2438d3 | 38 | RPC_CLASSES = ( |
switches | 0:5c4d7b2438d3 | 39 | { |
switches | 0:5c4d7b2438d3 | 40 | "name": "DigitalOut", |
switches | 0:5c4d7b2438d3 | 41 | "cons_args": ["PinName"], |
switches | 0:5c4d7b2438d3 | 42 | "methods": [ |
switches | 0:5c4d7b2438d3 | 43 | (None , "write", ["int"]), |
switches | 0:5c4d7b2438d3 | 44 | ("int", "read" , []), |
switches | 0:5c4d7b2438d3 | 45 | ] |
switches | 0:5c4d7b2438d3 | 46 | }, |
switches | 0:5c4d7b2438d3 | 47 | { |
switches | 0:5c4d7b2438d3 | 48 | "name": "DigitalIn", |
switches | 0:5c4d7b2438d3 | 49 | "cons_args": ["PinName"], |
switches | 0:5c4d7b2438d3 | 50 | "methods": [ |
switches | 0:5c4d7b2438d3 | 51 | ("int", "read" , []), |
switches | 0:5c4d7b2438d3 | 52 | ] |
switches | 0:5c4d7b2438d3 | 53 | }, |
switches | 0:5c4d7b2438d3 | 54 | { |
switches | 0:5c4d7b2438d3 | 55 | "name": "DigitalInOut", |
switches | 0:5c4d7b2438d3 | 56 | "cons_args": ["PinName"], |
switches | 0:5c4d7b2438d3 | 57 | "methods": [ |
switches | 0:5c4d7b2438d3 | 58 | ("int", "read" , []), |
switches | 0:5c4d7b2438d3 | 59 | (None , "write" , ["int"]), |
switches | 0:5c4d7b2438d3 | 60 | (None , "input" , []), |
switches | 0:5c4d7b2438d3 | 61 | (None , "output", []), |
switches | 0:5c4d7b2438d3 | 62 | ] |
switches | 0:5c4d7b2438d3 | 63 | }, |
switches | 0:5c4d7b2438d3 | 64 | { |
switches | 0:5c4d7b2438d3 | 65 | "name": "AnalogIn", |
switches | 0:5c4d7b2438d3 | 66 | "required": "ANALOGIN", |
switches | 0:5c4d7b2438d3 | 67 | "cons_args": ["PinName"], |
switches | 0:5c4d7b2438d3 | 68 | "methods": [ |
switches | 0:5c4d7b2438d3 | 69 | ("float" , "read" , []), |
switches | 0:5c4d7b2438d3 | 70 | ("unsigned short", "read_u16", []), |
switches | 0:5c4d7b2438d3 | 71 | ] |
switches | 0:5c4d7b2438d3 | 72 | }, |
switches | 0:5c4d7b2438d3 | 73 | { |
switches | 0:5c4d7b2438d3 | 74 | "name": "AnalogOut", |
switches | 0:5c4d7b2438d3 | 75 | "required": "ANALOGOUT", |
switches | 0:5c4d7b2438d3 | 76 | "cons_args": ["PinName"], |
switches | 0:5c4d7b2438d3 | 77 | "methods": [ |
switches | 0:5c4d7b2438d3 | 78 | ("float", "read" , []), |
switches | 0:5c4d7b2438d3 | 79 | (None , "write" , ["float"]), |
switches | 0:5c4d7b2438d3 | 80 | (None , "write_u16", ["unsigned short"]), |
switches | 0:5c4d7b2438d3 | 81 | ] |
switches | 0:5c4d7b2438d3 | 82 | }, |
switches | 0:5c4d7b2438d3 | 83 | { |
switches | 0:5c4d7b2438d3 | 84 | "name": "PwmOut", |
switches | 0:5c4d7b2438d3 | 85 | "required": "PWMOUT", |
switches | 0:5c4d7b2438d3 | 86 | "cons_args": ["PinName"], |
switches | 0:5c4d7b2438d3 | 87 | "methods": [ |
switches | 0:5c4d7b2438d3 | 88 | ("float", "read" , []), |
switches | 0:5c4d7b2438d3 | 89 | (None , "write" , ["float"]), |
switches | 0:5c4d7b2438d3 | 90 | (None , "period" , ["float"]), |
switches | 0:5c4d7b2438d3 | 91 | (None , "period_ms" , ["int"]), |
switches | 0:5c4d7b2438d3 | 92 | (None , "pulsewidth" , ["float"]), |
switches | 0:5c4d7b2438d3 | 93 | (None , "pulsewidth_ms", ["int"]), |
switches | 0:5c4d7b2438d3 | 94 | ] |
switches | 0:5c4d7b2438d3 | 95 | }, |
switches | 0:5c4d7b2438d3 | 96 | { |
switches | 0:5c4d7b2438d3 | 97 | "name": "SPI", |
switches | 0:5c4d7b2438d3 | 98 | "required": "SPI", |
switches | 0:5c4d7b2438d3 | 99 | "cons_args": ["PinName", "PinName", "PinName"], |
switches | 0:5c4d7b2438d3 | 100 | "methods": [ |
switches | 0:5c4d7b2438d3 | 101 | (None , "format" , ["int", "int"]), |
switches | 0:5c4d7b2438d3 | 102 | (None , "frequency", ["int"]), |
switches | 0:5c4d7b2438d3 | 103 | ("int", "write" , ["int"]), |
switches | 0:5c4d7b2438d3 | 104 | ] |
switches | 0:5c4d7b2438d3 | 105 | }, |
switches | 0:5c4d7b2438d3 | 106 | { |
switches | 0:5c4d7b2438d3 | 107 | "name": "Serial", |
switches | 0:5c4d7b2438d3 | 108 | "required": "SERIAL", |
switches | 0:5c4d7b2438d3 | 109 | "cons_args": ["PinName", "PinName"], |
switches | 0:5c4d7b2438d3 | 110 | "methods": [ |
switches | 0:5c4d7b2438d3 | 111 | (None , "baud" , ["int"]), |
switches | 0:5c4d7b2438d3 | 112 | ("int", "readable" , []), |
switches | 0:5c4d7b2438d3 | 113 | ("int", "writeable", []), |
switches | 0:5c4d7b2438d3 | 114 | ("int", "putc" , ["int"]), |
switches | 0:5c4d7b2438d3 | 115 | ("int", "getc" , []), |
switches | 0:5c4d7b2438d3 | 116 | ("int", "puts" , ["const char *"]), |
switches | 0:5c4d7b2438d3 | 117 | ] |
switches | 0:5c4d7b2438d3 | 118 | }, |
switches | 0:5c4d7b2438d3 | 119 | { |
switches | 0:5c4d7b2438d3 | 120 | "name": "Timer", |
switches | 0:5c4d7b2438d3 | 121 | "cons_args": [], |
switches | 0:5c4d7b2438d3 | 122 | "methods": [ |
switches | 0:5c4d7b2438d3 | 123 | (None , "start" , []), |
switches | 0:5c4d7b2438d3 | 124 | (None , "stop" , []), |
switches | 0:5c4d7b2438d3 | 125 | (None , "reset" , []), |
switches | 0:5c4d7b2438d3 | 126 | ("float", "read" , []), |
switches | 0:5c4d7b2438d3 | 127 | ("int" , "read_ms", []), |
switches | 0:5c4d7b2438d3 | 128 | ("int" , "read_us", []), |
switches | 0:5c4d7b2438d3 | 129 | ] |
switches | 0:5c4d7b2438d3 | 130 | } |
switches | 0:5c4d7b2438d3 | 131 | ) |
switches | 0:5c4d7b2438d3 | 132 | |
switches | 0:5c4d7b2438d3 | 133 | |
switches | 0:5c4d7b2438d3 | 134 | def get_args_proto(args_types, extra=None): |
switches | 0:5c4d7b2438d3 | 135 | args = ["%s a%d" % (s, n) for n, s in enumerate(args_types)] |
switches | 0:5c4d7b2438d3 | 136 | if extra: |
switches | 0:5c4d7b2438d3 | 137 | args.extend(extra) |
switches | 0:5c4d7b2438d3 | 138 | return ', '.join(args) |
switches | 0:5c4d7b2438d3 | 139 | |
switches | 0:5c4d7b2438d3 | 140 | |
switches | 0:5c4d7b2438d3 | 141 | def get_args_call(args): |
switches | 0:5c4d7b2438d3 | 142 | return ', '.join(["a%d" % (n) for n in range(len(args))]) |
switches | 0:5c4d7b2438d3 | 143 | |
switches | 0:5c4d7b2438d3 | 144 | |
switches | 0:5c4d7b2438d3 | 145 | classes = [] |
switches | 0:5c4d7b2438d3 | 146 | class_template = get_template(CLASS_TEMPLATE) |
switches | 0:5c4d7b2438d3 | 147 | |
switches | 0:5c4d7b2438d3 | 148 | for c in RPC_CLASSES: |
switches | 0:5c4d7b2438d3 | 149 | c_args = c['cons_args'] |
switches | 0:5c4d7b2438d3 | 150 | data = { |
switches | 0:5c4d7b2438d3 | 151 | 'name': c['name'], |
switches | 0:5c4d7b2438d3 | 152 | 'cons_type': ', '.join(c_args + ['const char*']), |
switches | 0:5c4d7b2438d3 | 153 | "cons_proto": get_args_proto(c_args, ["const char *name=NULL"]), |
switches | 0:5c4d7b2438d3 | 154 | "cons_call": get_args_call(c_args) |
switches | 0:5c4d7b2438d3 | 155 | } |
switches | 0:5c4d7b2438d3 | 156 | |
switches | 0:5c4d7b2438d3 | 157 | c_name = "Rpc" + c['name'] |
switches | 0:5c4d7b2438d3 | 158 | |
switches | 0:5c4d7b2438d3 | 159 | methods = [] |
switches | 0:5c4d7b2438d3 | 160 | rpc_methods = [] |
switches | 0:5c4d7b2438d3 | 161 | for r, m, a in c['methods']: |
switches | 0:5c4d7b2438d3 | 162 | ret_proto = r if r else "void" |
switches | 0:5c4d7b2438d3 | 163 | args_proto = "void" |
switches | 0:5c4d7b2438d3 | 164 | |
switches | 0:5c4d7b2438d3 | 165 | ret_defin = "return " if r else "" |
switches | 0:5c4d7b2438d3 | 166 | args_defin = "" |
switches | 0:5c4d7b2438d3 | 167 | |
switches | 0:5c4d7b2438d3 | 168 | if a: |
switches | 0:5c4d7b2438d3 | 169 | args_proto = get_args_proto(a) |
switches | 0:5c4d7b2438d3 | 170 | args_defin = get_args_call(a) |
switches | 0:5c4d7b2438d3 | 171 | |
switches | 0:5c4d7b2438d3 | 172 | proto = "%s %s(%s)" % (ret_proto, m, args_proto) |
switches | 0:5c4d7b2438d3 | 173 | defin = "{%so.%s(%s);}" % (ret_defin, m, args_defin) |
switches | 0:5c4d7b2438d3 | 174 | methods.append("%s %s" % (proto, defin)) |
switches | 0:5c4d7b2438d3 | 175 | |
switches | 0:5c4d7b2438d3 | 176 | rpc_method_type = [r] if r else [] |
switches | 0:5c4d7b2438d3 | 177 | rpc_method_type.append(c_name) |
switches | 0:5c4d7b2438d3 | 178 | rpc_method_type.extend(a) |
switches | 0:5c4d7b2438d3 | 179 | rpc_methods.append('{"%s", rpc_method_caller<%s, &%s::%s>}' % (m, ', '.join(rpc_method_type), c_name, m)) |
switches | 0:5c4d7b2438d3 | 180 | |
switches | 0:5c4d7b2438d3 | 181 | data['methods'] = "\n ".join(methods) |
switches | 0:5c4d7b2438d3 | 182 | data['rpc_methods'] = ",\n ".join(rpc_methods) |
switches | 0:5c4d7b2438d3 | 183 | |
switches | 0:5c4d7b2438d3 | 184 | class_decl = class_template.render(data) |
switches | 0:5c4d7b2438d3 | 185 | if 'required' in c: |
switches | 0:5c4d7b2438d3 | 186 | class_decl = "#if DEVICE_%s\n%s\n#endif" % (c['required'], class_decl) |
switches | 0:5c4d7b2438d3 | 187 | |
switches | 0:5c4d7b2438d3 | 188 | classes.append(class_decl) |
switches | 0:5c4d7b2438d3 | 189 | |
switches | 0:5c4d7b2438d3 | 190 | write_rpc_classes('\n\n'.join(classes)) |