14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
"""
17
- Generate pins.js for a specified target, using target definitions from the
17
+ Generate pins.cpp for a specified target, using target definitions from the
18
18
mbed OS source tree.
19
19
20
20
It's expecting to be run from the targets/mbedos5 directory.
28
28
import sys
29
29
import os
30
30
31
- from simpleeval import SimpleEval , DEFAULT_OPERATORS
32
31
from pycparserext .ext_c_parser import GnuCParser
33
- from pycparser import parse_file , c_ast , c_generator
32
+ from pycparser import parse_file , c_ast
34
33
35
34
# import mbed tools
36
35
sys .path .append (os .path .join (os .path .dirname (__file__ ), '..' , 'mbed-os' ))
@@ -113,27 +112,7 @@ def visit_typedecl(self, node):
113
112
Visit a node.
114
113
"""
115
114
if node .declname in self .names :
116
- c_gen = c_generator .CGenerator ()
117
- pins = {}
118
-
119
- operators = DEFAULT_OPERATORS
120
- operators [ast .BitOr ] = lambda a , b : a | b
121
- operators [ast .LShift ] = lambda a , b : a << b
122
- operators [ast .RShift ] = lambda a , b : a << b
123
- evaluator = SimpleEval (DEFAULT_OPERATORS )
124
-
125
- for pin in node .type .values .enumerators :
126
- expr = c_gen .visit (pin .value )
127
-
128
- if "(int)" in expr :
129
- expr = expr .replace ('(int)' , '' )
130
-
131
- if expr in pins :
132
- pins [pin .name ] = pins [expr ]
133
- else :
134
- pins [pin .name ] = evaluator .eval (expr .strip ())
135
-
136
- return pins
115
+ return [pin .name for pin in node .type .values .enumerators ]
137
116
138
117
139
118
def enumerate_pins (c_source_file , include_dirs , definitions ):
@@ -158,39 +137,39 @@ def enumerate_pins(c_source_file, include_dirs, definitions):
158
137
return visitor .visit (parsed_ast )
159
138
160
139
161
- def write_pins_to_files (pins , out_js_file , out_cpp_file ):
140
+ def write_pins_to_file (pins , pins_file , out_cpp_file ):
162
141
"""
163
- Write the generated pins for a specified mbed board into the output JS and C++ files .
142
+ Write the generated pins for a specified mbed board into the output C++ file .
164
143
"""
165
- out_js = ' \r \n ' . join ([ 'var %s = %s;' % pin for pin in pins ])
166
- out_js_file . write ( out_js )
144
+
145
+ include = ' \n #include "../{}"' . format ( pins_file )
167
146
168
147
count = '''
169
148
unsigned int jsmbed_js_magic_string_count = {};
170
149
''' .format (len (pins ))
171
150
172
- lengths = ',\n ' .join (str (len (pin [ 0 ] )) for pin in pins )
151
+ lengths = ',\n ' .join (str (len (pin )) for pin in pins )
173
152
lenghts_source = '''
174
153
unsigned int jsmbed_js_magic_string_lengths[] = {
175
154
%s
176
155
};
177
156
''' % lengths
178
157
179
- magic_values = ',\n ' .join (str ( pin [ 1 ]) for pin in pins )
158
+ magic_values = ',\n ' .join (pins )
180
159
magic_source = '''
181
160
unsigned int jsmbed_js_magic_string_values[] = {
182
161
%s
183
162
};
184
163
''' % magic_values
185
164
186
- magic_strings = ',\n ' .join ('"' + pin [ 0 ] + '"' for pin in pins )
165
+ magic_strings = ',\n ' .join ('"' + pin + '"' for pin in pins )
187
166
magic_string_source = '''
188
167
const char * jsmbed_js_magic_strings[] = {
189
168
%s
190
169
};
191
170
''' % magic_strings
192
171
193
- out_cpp_file .write (LICENSE + count + lenghts_source + magic_source + magic_string_source )
172
+ out_cpp_file .write (LICENSE + include + count + lenghts_source + magic_source + magic_string_source )
194
173
195
174
196
175
def main ():
@@ -203,17 +182,13 @@ def main():
203
182
sys .exit (1 )
204
183
205
184
description = """
206
- Generate pins.js for a specified mbed board, using target definitions from the
185
+ Generate pins.cpp for a specified mbed board, using target definitions from the
207
186
mbed OS source tree.
208
187
"""
209
188
210
189
parser = argparse .ArgumentParser (description = description )
211
190
212
191
parser .add_argument ('board' , help = 'mbed board name' )
213
- parser .add_argument ('-o' ,
214
- help = 'Output JavaScript file (default: %(default)s)' ,
215
- default = 'js/pins.js' ,
216
- type = argparse .FileType ('w' ))
217
192
parser .add_argument ('-c' ,
218
193
help = 'Output C++ file (default: %(default)s)' ,
219
194
default = 'source/pins.cpp' ,
@@ -237,10 +212,9 @@ def main():
237
212
pins = enumerate_pins (pins_file , ['./tools' ] + list (includes ), defines )
238
213
239
214
# first sort alphabetically, then by length.
240
- pins = [(x , pins [x ]) for x in pins ] # turn dict into tuples, which can be sorted
241
- pins = sorted (pins , key = lambda x : (len (x [0 ]), x [0 ].lower ()))
215
+ pins = sorted (pins , key = lambda x : (len (x ), x .lower ()))
242
216
243
- write_pins_to_files (pins , args . o , args .c )
217
+ write_pins_to_file (pins , pins_file , args .c )
244
218
245
219
246
220
if __name__ == "__main__" :
0 commit comments