forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-generate the stmhal/pybcdc_inf header file from static files
The USB VID&PID are automatically extracted from usbd_desc_cdc_msc.c and inserted into pybcdc_inf.template, ensuring that the same USB IDs get used everywhere
- Loading branch information
Showing
6 changed files
with
85 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Reads in a text file, and performs the necessary escapes so that it | ||
# can be #included as a static string like: | ||
# static const char string_from_textfile[] = | ||
# #include "build/textfile.h" | ||
# ; | ||
# This script simply prints the escaped string straight to stdout | ||
|
||
from __future__ import print_function | ||
|
||
import sys | ||
|
||
# Can either be set explicitly, or left blank to auto-detect | ||
line_end = '' | ||
|
||
if __name__ == "__main__": | ||
filename = sys.argv[1] | ||
for line in open(filename, 'r').readlines(): | ||
if not line_end: | ||
for ending in ('\r\n', '\r', '\n'): | ||
if line.endswith(ending): | ||
line_end = ending.replace('\r', '\\r').replace('\n', '\\n') | ||
break | ||
if not line_end: | ||
raise Exception("Couldn't auto-detect line-ending of %s" % filename) | ||
line = line.rstrip('\r\n') | ||
line = line.replace('\\', '\\\\') | ||
line = line.replace('"', '\\"') | ||
print('"%s%s"' % (line, line_end)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Reads the USB VID and PID from the file specifed by sys.arg[1] and then | ||
# inserts those values into the template file specified by sys.argv[2], | ||
# printing the result to stdout | ||
|
||
from __future__ import print_function | ||
|
||
import sys | ||
import re | ||
import string | ||
|
||
def parse_usb_ids(filename): | ||
rv = dict() | ||
if filename == 'usbd_desc_cdc_msc.c': | ||
for line in open(filename).readlines(): | ||
line = line.rstrip('\r\n') | ||
match = re.match('^#define\s+(\w+)\s+0x(\d+)$', line) | ||
if match: | ||
if match.group(1) == 'USBD_VID': | ||
rv['USB_VID'] = match.group(2) | ||
elif match.group(1) == 'USBD_PID': | ||
rv['USB_PID'] = match.group(2) | ||
if 'USB_VID' in rv and 'USB_PID' in rv: | ||
break | ||
else: | ||
raise Exception("Don't (yet) know how to parse USB IDs from %s" % filename) | ||
for k in ('USB_PID', 'USB_VID'): | ||
if k not in rv: | ||
raise Exception("Unable to parse %s from %s" % (k, filename)) | ||
return rv | ||
|
||
if __name__ == "__main__": | ||
usb_ids_file = sys.argv[1] | ||
template_file = sys.argv[2] | ||
replacements = parse_usb_ids(usb_ids_file) | ||
for line in open(template_file, 'r').readlines(): | ||
print(string.Template(line).safe_substitute(replacements), end='') |