bin2array to dynamically create C array on build
This commit is contained in:
163
resources/bin2array/.gitignore
vendored
Normal file
163
resources/bin2array/.gitignore
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
|
||||
# Created by https://www.gitignore.io/api/macos,python,windows,visualstudiocode
|
||||
|
||||
### macOS ###
|
||||
*.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
.com.apple.timemachine.donotpresent
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
### Python ###
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
.hypothesis/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# pyenv
|
||||
.python-version
|
||||
|
||||
# celery beat schedule file
|
||||
celerybeat-schedule
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
|
||||
### VisualStudioCode ###
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
.history
|
||||
|
||||
### Windows ###
|
||||
# Windows thumbnail cache files
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
ehthumbs_vista.db
|
||||
|
||||
# Folder config file
|
||||
Desktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# End of https://www.gitignore.io/api/macos,python,windows,visualstudiocode
|
||||
21
resources/bin2array/LICENSE
Normal file
21
resources/bin2array/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 James Swineson <github@public.swineson.me>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
95
resources/bin2array/README.md
Normal file
95
resources/bin2array/README.md
Normal file
@@ -0,0 +1,95 @@
|
||||
# bin2array
|
||||
|
||||
Converts binary file to C-style array initializer.
|
||||
|
||||
Ever wanted to embed a binary file in your program? Trying to serve images and executables from a tiny web server on Arduino or ESP8266? This utility is here to help.
|
||||
|
||||
## Requirements
|
||||
|
||||
* Python 3
|
||||
|
||||
## Usage
|
||||
|
||||
I guess it is self-explanatory.
|
||||
|
||||
```
|
||||
usage: bin2array.py [-h] [-O OUTPUT] [-l LINEBREAK] [-L LINEBREAK_STRING]
|
||||
[-S SEPARATOR_STRING] [-H ELEMENT_PREFIX]
|
||||
[-T ELEMENT_SUFFIX] [-U] [-n]
|
||||
filename
|
||||
|
||||
Convert binary file to C-style array initializer.
|
||||
|
||||
positional arguments:
|
||||
filename the file to be converted
|
||||
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
-O OUTPUT, --output OUTPUT
|
||||
write output to a file
|
||||
-l LINEBREAK, --linebreak LINEBREAK
|
||||
add linebreak after every N element
|
||||
-L LINEBREAK_STRING, --linebreak-string LINEBREAK_STRING
|
||||
use what to break link, defaults to "\n"
|
||||
-S SEPARATOR_STRING, --separator-string SEPARATOR_STRING
|
||||
use what to separate elements, defaults to ", "
|
||||
-H ELEMENT_PREFIX, --element-prefix ELEMENT_PREFIX
|
||||
string to be added to the head of element, defaults to
|
||||
"0x"
|
||||
-T ELEMENT_SUFFIX, --element-suffix ELEMENT_SUFFIX
|
||||
string to be added to the tail of element, defaults to
|
||||
none
|
||||
-U, --force-uppercase
|
||||
force uppercase HEX representation
|
||||
-n, --newline add a newline on file end
|
||||
```
|
||||
|
||||
## Caveats
|
||||
|
||||
### Arduino IDE
|
||||
|
||||
**Do not put large source code files in the root folder of your project.** Otherwise some of the following events will happen:
|
||||
|
||||
* One of your CPU cores been eaten up by java
|
||||
* The splash screen shows up but never loads
|
||||
* 3rd World War
|
||||
|
||||
Make a new folder inside project root, put the converted file (use `.h` as extension, otherwise may not be recognized) in, then use the following grammer to use it:
|
||||
|
||||
```C++
|
||||
const char great_image[] PROGMEM = {
|
||||
#include "data/great_image.png.h"
|
||||
}
|
||||
```
|
||||
|
||||
If you are using ESP8266WebServer to serve static binary files, you can use the following code:
|
||||
|
||||
```C++
|
||||
#include <ESP8266WebServer.h>
|
||||
|
||||
// create server
|
||||
ESP8266WebServer server(80);
|
||||
|
||||
// include the image data
|
||||
const char image[] PROGMEM = {
|
||||
#include "data/image.png.h"
|
||||
};
|
||||
|
||||
// statis binary file handler
|
||||
void handleImage() {
|
||||
server.sendHeader("Cache-Control", "max-age=31536000", false);
|
||||
server.send_P(200, "image/png", image, sizeof(image));
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// do other things...
|
||||
// register image handler before server.begin()
|
||||
server.on("/image.png", handleImage);
|
||||
// do other things...
|
||||
server.begin();
|
||||
}
|
||||
```
|
||||
|
||||
### Windows
|
||||
|
||||
Do not use command line redirection (`python bin2array.py test.png > test.png.h`) since CMD will save the file using UTF-16 which is not recognized by some compiler. Use `-O` option to save output to file, or manually convert UTF-16 to UTF-8 for maximum compatibility.
|
||||
48
resources/bin2array/bin2array.py
Normal file
48
resources/bin2array/bin2array.py
Normal file
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
import binascii
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description='Convert binary file to C-style array initializer.')
|
||||
parser.add_argument("filename", help="the file to be converted")
|
||||
parser.add_argument("-O", "--output", help="write output to a file")
|
||||
parser.add_argument("-l", "--linebreak", type=int, help="add linebreak after every N element")
|
||||
parser.add_argument("-L", "--linebreak-string", default="\n", help="use what to break link, defaults to \"\\n\"")
|
||||
parser.add_argument("-S", "--separator-string", default=", ", help="use what to separate elements, defaults to \", \"")
|
||||
parser.add_argument("-H", "--element-prefix", default="0x", help="string to be added to the head of element, defaults to \"0x\"")
|
||||
parser.add_argument("-T", "--element-suffix", default="", help="string to be added to the tail of element, defaults to none")
|
||||
parser.add_argument("-U", "--force-uppercase", action='store_true', help="force uppercase HEX representation")
|
||||
parser.add_argument("-n", "--newline", action='store_true', help="add a newline on file end")
|
||||
args = parser.parse_args()
|
||||
|
||||
def make_sublist_group(lst: list, grp: int) -> list:
|
||||
"""
|
||||
Group list elements into sublists.
|
||||
|
||||
make_sublist_group([1, 2, 3, 4, 5, 6, 7], 3) = [[1, 2, 3], [4, 5, 6], 7]
|
||||
"""
|
||||
return [lst[i:i+grp] for i in range(0, len(lst), grp)]
|
||||
|
||||
def do_convension(content: bytes, to_uppercase: bool=False) -> str:
|
||||
hexstr = binascii.hexlify(content).decode("UTF-8")
|
||||
if to_uppercase:
|
||||
hexstr = hexstr.upper()
|
||||
array = [args.element_prefix + hexstr[i:i + 2] + args.element_suffix for i in range(0, len(hexstr), 2)]
|
||||
if args.linebreak:
|
||||
array = make_sublist_group(array, args.linebreak)
|
||||
else:
|
||||
array = [array,]
|
||||
|
||||
return args.linebreak_string.join([args.separator_string.join(e) + args.separator_string for e in array])
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open(args.filename, 'rb') as f:
|
||||
file_content = f.read()
|
||||
ret = do_convension(file_content, to_uppercase=args.force_uppercase)
|
||||
if args.newline:
|
||||
ret = ret + args.linebreak_string
|
||||
if args.output:
|
||||
with open(args.output, 'w') as f:
|
||||
f.write(ret)
|
||||
else:
|
||||
print(ret)
|
||||
Reference in New Issue
Block a user