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