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