Skip to content

Commit

Permalink
Include binary file for default patches in firmware (#1)
Browse files Browse the repository at this point in the history
firmware: include binary patch file
  • Loading branch information
Len42 authored Jul 26, 2023
1 parent 92b6cd6 commit 91e5476
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions firmware/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/version.h
/default.dexy.h
16 changes: 14 additions & 2 deletions firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ add_executable(Dexy ${PROJ_CPP_FILES} ssd1306.c)
pico_set_program_name(Dexy "Dexy")
#pico_set_program_version(Dexy "0.0") # No - this will be set by MakeVersionFile & Dexy

set(CMD_PYTHON "py") # for Windows
#set(CMD_PYTHON "/usr/local/bin/python3.11") # for macOS, Linux

# Update version info file from the latest git tag
list(APPEND VERSION_FILES "${PROJECT_SOURCE_DIR}/Version.h" "${PROJECT_SOURCE_DIR}/doc/Doxyfile.doxy")
list(TRANSFORM VERSION_FILES APPEND ".template" OUTPUT_VARIABLE VERSION_TEMPLATES)
target_sources(Dexy PRIVATE ${VERSION_FILES})
set(CMD_PYTHON "py") # for Windows
#set(CMD_PYTHON "/usr/local/bin/python3.11") # for macOS, Linux
set(VERSION_TEMP_FILE "${PROJECT_BINARY_DIR}/version-temp")
set(VERSION_INFO_FILE "${PROJECT_BINARY_DIR}/version-info")
add_custom_target(MakeVersionFile
Expand All @@ -51,6 +52,17 @@ add_custom_command(
${VERSION_FILES}
)

# Convert binary patch data file to C syntax (until C++ gets #embed)
set(PATCH_FILENAME "default.dexy")
set(PATCH_FILE_BIN "${PROJECT_SOURCE_DIR}/../patches/${PATCH_FILENAME}")
set(PATCH_FILE_INC "${PROJECT_SOURCE_DIR}/${PATCH_FILENAME}.h")
target_sources(Dexy PRIVATE ${PATCH_FILE_INC})
add_custom_command(
OUTPUT ${PATCH_FILE_INC}
DEPENDS ${PATCH_FILE_BIN}
COMMAND ${CMD_PYTHON} ${PROJECT_SOURCE_DIR}/make-binary-inc-file.py ${PATCH_FILE_BIN}
)

# Set compiler options
target_compile_options(Dexy PUBLIC "-O3")
# KLUDGE: -fwhole-program makes it do more inlining and fewer redundant out-of-line functions.
Expand Down
3 changes: 2 additions & 1 deletion firmware/Patches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ static consteval SerializedPatchBank patchBankToBytes()
/// over-written by the SDK flash programming API.
IN_FLASH("PatchData")
static constinit Flash::Wrapper<SerializedPatchBank> initialPatchData {
patchBankToBytes<makeDefaultPatchBank()>() // #embed pls?
#include "default.dexy.h" // #embed pls?
// old: patchBankToBytes<makeDefaultPatchBank()>()
};

/// @brief The patch bank that is currently loaded
Expand Down
40 changes: 40 additions & 0 deletions firmware/make-binary-inc-file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
""" make-binary-inc-file - Convert a binary file to C-compatible data.
Usage: make-binary-inc-file.py <input-filename> ...
For each input file, write an output file with '.h' appended to the name.
The output file contains the binary data from the input file, formatted in a way
that can be #included in a C or C++ declaration.
"""

import sys
import os

cmdName, *inputFiles = sys.argv
# Set outputDir to the same directory as this script - that is where output files
# will be written.
# If that's not desired, set outputDir = '' to write output files next to the input files.
outputDir = os.path.dirname(cmdName)
#outputDir = ''
cmdName = os.path.basename(cmdName)
for inputFile in inputFiles:
outputFile = inputFile + '.h'
if outputDir:
outputFile = os.path.join(outputDir, os.path.basename(outputFile))
try:
with open(inputFile, 'rb') as input:
try:
with open(outputFile, 'w') as output:
i = 0
while char := input.read(1):
i += 1
if i % 16 == 0:
endLine = '\n'
else:
endLine = ''
print(f'{ord(char):3}, ', end=endLine, file=output)
print(file=output)
except Exception as ex:
print(f'{cmdName}: {ex}')
except Exception as ex:
print(f'{cmdName}: {ex}')
Binary file added patches/default-old.dexy
Binary file not shown.
Binary file modified patches/default.dexy
Binary file not shown.
Binary file removed patches/new patchbank.dexy
Binary file not shown.

0 comments on commit 91e5476

Please sign in to comment.