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