Clone of official tools

Committer:
screamer
Date:
Thu May 19 19:44:41 2016 +0100
Revision:
0:66f3b5499f7f
Initial revision

Who changed what in which revision?

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