Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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