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