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