Skip to content

Commit 22b0851

Browse files
fbmrkzherczeg
authored andcommitted
target: mbedos5: Modify generate_pins.py (#2524)
JerryScript-DCO-1.0-Signed-off-by: Marko Fabo mfabo@inf.u-szeged.hu
1 parent 49a0836 commit 22b0851

File tree

1 file changed

+14
-40
lines changed

1 file changed

+14
-40
lines changed

targets/mbedos5/tools/generate_pins.py

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
"""
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
1818
mbed OS source tree.
1919
2020
It's expecting to be run from the targets/mbedos5 directory.
@@ -28,9 +28,8 @@
2828
import sys
2929
import os
3030

31-
from simpleeval import SimpleEval, DEFAULT_OPERATORS
3231
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
3433

3534
# import mbed tools
3635
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'mbed-os'))
@@ -113,27 +112,7 @@ def visit_typedecl(self, node):
113112
Visit a node.
114113
"""
115114
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]
137116

138117

139118
def enumerate_pins(c_source_file, include_dirs, definitions):
@@ -158,39 +137,39 @@ def enumerate_pins(c_source_file, include_dirs, definitions):
158137
return visitor.visit(parsed_ast)
159138

160139

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):
162141
"""
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.
164143
"""
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)
167146

168147
count = '''
169148
unsigned int jsmbed_js_magic_string_count = {};
170149
'''.format(len(pins))
171150

172-
lengths = ',\n '.join(str(len(pin[0])) for pin in pins)
151+
lengths = ',\n '.join(str(len(pin)) for pin in pins)
173152
lenghts_source = '''
174153
unsigned int jsmbed_js_magic_string_lengths[] = {
175154
%s
176155
};
177156
''' % lengths
178157

179-
magic_values = ',\n '.join(str(pin[1]) for pin in pins)
158+
magic_values = ',\n '.join(pins)
180159
magic_source = '''
181160
unsigned int jsmbed_js_magic_string_values[] = {
182161
%s
183162
};
184163
''' % magic_values
185164

186-
magic_strings = ',\n '.join('"' + pin[0] + '"' for pin in pins)
165+
magic_strings = ',\n '.join('"' + pin + '"' for pin in pins)
187166
magic_string_source = '''
188167
const char * jsmbed_js_magic_strings[] = {
189168
%s
190169
};
191170
''' % magic_strings
192171

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)
194173

195174

196175
def main():
@@ -203,17 +182,13 @@ def main():
203182
sys.exit(1)
204183

205184
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
207186
mbed OS source tree.
208187
"""
209188

210189
parser = argparse.ArgumentParser(description=description)
211190

212191
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'))
217192
parser.add_argument('-c',
218193
help='Output C++ file (default: %(default)s)',
219194
default='source/pins.cpp',
@@ -237,10 +212,9 @@ def main():
237212
pins = enumerate_pins(pins_file, ['./tools'] + list(includes), defines)
238213

239214
# 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()))
242216

243-
write_pins_to_files(pins, args.o, args.c)
217+
write_pins_to_file(pins, pins_file, args.c)
244218

245219

246220
if __name__ == "__main__":

0 commit comments

Comments
 (0)