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