diff --git a/create_fwversion.py b/create_fwversion.py new file mode 100755 index 0000000..0fa74c1 --- /dev/null +++ b/create_fwversion.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import subprocess + +def get_git_version(): + """Retrieve the current version from `git describe --tags`.""" + try: + # Run the git command to get the version string + version = subprocess.check_output( + ["git", "describe", "--tags", "--dirty", "--always"], + stderr=subprocess.STDOUT, + text=True + ).strip() + return version + except subprocess.CalledProcessError: + # Return a default value if the command fails + return "unknown" + +def generate_version_header(output_file): + """Generate the version.h file with the Git version.""" + git_version = get_git_version() + with open(output_file, "w") as header: + header.write('// automatically generated using create_fwversion.py\n') + header.write(f'const char* FW_VERSION = "{git_version}";\n\n') + print(f"Generated C file with with GIT_VERSION={git_version}: {output_file}") + +# Create the version file +output_file = "fwversion.h" +generate_version_header(output_file) diff --git a/data/update.html b/data/update.html index 67fc505..c97321f 100644 --- a/data/update.html +++ b/data/update.html @@ -8,7 +8,8 @@ -

/home

+

/home

+

Current version: %fw_version%

Firmware update over HTTP diff --git a/fwupdate.cpp b/fwupdate.cpp index b9d5448..14910ae 100644 --- a/fwupdate.cpp +++ b/fwupdate.cpp @@ -7,6 +7,7 @@ #include #include "fwupdate.h" +#include "fwversion.h" static FS *_fs; static WiFiClientSecure wifiClientSecure; @@ -32,9 +33,17 @@ void fwupdate_begin(FS & fs) Update.runAsync(true); } +static String template_processor(const String & string) +{ + if (string == "fw_version") { + return FW_VERSION; + } + return string; +} + static void handleGet(AsyncWebServerRequest *request) { - request->send(*_fs, _update_page, "text/html"); + request->send(*_fs, _update_page, "text/html", false, template_processor); } /* diff --git a/platformio.ini b/platformio.ini index ec8bda9..8935de2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,7 +19,9 @@ framework = arduino monitor_speed = 115200 upload_speed = 1000000 board_build.filesystem = littlefs -extra_scripts = pre:create_fsimage.py +extra_scripts = + pre:create_fwversion.py + pre:create_fsimage.py lib_deps = fastled/FastLED@3.6.0 alanswx/ESPAsyncWiFiManager@^0.31.0 bblanchon/ArduinoJson@^6.21.5