From 30043d562245828c27b0c85975ea3e28600516ef Mon Sep 17 00:00:00 2001 From: Mikhael Skvortsov Date: Fri, 28 Mar 2025 11:19:50 +0300 Subject: [PATCH] Improve patching --- bin/platformio-custom.py | 49 ++++++++++++------- ...P32 OLED driver for SSD1306 displays.patch | 34 +++++++++++++ patches/INA3221.patch | 24 +++++++++ patches/config.yaml | 10 ++++ 4 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 patches/ESP8266 and ESP32 OLED driver for SSD1306 displays.patch create mode 100644 patches/INA3221.patch create mode 100644 patches/config.yaml diff --git a/bin/platformio-custom.py b/bin/platformio-custom.py index 72e68c1ed..fc10fb7a5 100644 --- a/bin/platformio-custom.py +++ b/bin/platformio-custom.py @@ -136,26 +136,41 @@ for lb in env.GetLibBuilders(): ############################# Libraries Patching ############################# -env.Execute("$PYTHONEXE -m pip install patch") +env.Execute("$PYTHONEXE -m pip install patch pyyaml") -import pathlib -import glob import os import patch +import yaml -libsToPatch = {} -for entry in glob.glob("patches/*.patch"): - p = pathlib.Path(entry).stem - libsToPatch[p] = entry +patches = {} +config_path = os.path.join(env["PROJECT_DIR"], "patches/config.yaml") +with open(config_path, "r") as file: + y = yaml.safe_load(file) + for p in y["patches"]: + name = p["name"] + p.pop("name", None) + patches[name] = p for lb in env.GetLibBuilders(): - if lb.name in libsToPatch: - marker_path = os.path.join(pathlib.Path(lb.src_dir), ".patched") - if not os.path.exists(marker_path): - patch_path = libsToPatch[lb.name] - ps = patch.fromfile(patch_path) - if not ps.apply(0, lb.src_dir): - print(f"Failed to apply patch {patch_path}") - continue # XXX - print(f"Patched {lb.name}") - open(marker_path, "w").close() + if not lb.name in patches: + continue + p = patches[lb.name] + if "version" in p and not (lb.version == p["version"]): + print(f"Skipping {lb.name}.patch: version doesn't match") + continue + if "targets" in p and not (env.get("PIOENV") in p["targets"]): + print(f"Skipping {lb.name}.patch: target doesn't match") + continue + + marker_path = os.path.join(lb.src_dir, ".patched") + if os.path.exists(marker_path): + print(f"Skipping {lb.name}.patch: already patched") + continue + + patch_path = env["PROJECT_DIR"] + "/patches/" + lb.name + ".patch" + ps = patch.fromfile(patch_path) + if not ps.apply(0, lb.src_dir): + print(f"Failed to apply patch {patch_path}") + exit(1) + print(f"Patched {lb.name}") + open(marker_path, "w").close() diff --git a/patches/ESP8266 and ESP32 OLED driver for SSD1306 displays.patch b/patches/ESP8266 and ESP32 OLED driver for SSD1306 displays.patch new file mode 100644 index 000000000..76d6c6230 --- /dev/null +++ b/patches/ESP8266 and ESP32 OLED driver for SSD1306 displays.patch @@ -0,0 +1,34 @@ +Fix compiler warning + +diff --git a/OLEDDisplay.cpp b/OLEDDisplay.cpp +index dca2ad1..35eb456 100644 +--- a/OLEDDisplay.cpp ++++ b/OLEDDisplay.cpp +@@ -941,15 +941,6 @@ size_t OLEDDisplay::write(uint8_t c) { + return 1; + } + +-size_t OLEDDisplay::write(const char* str) { +- if (str == NULL) return 0; +- size_t length = strlen(str); +- for (size_t i = 0; i < length; i++) { +- write(str[i]); +- } +- return length; +-} +- + #ifdef __MBED__ + int OLEDDisplay::_putc(int c) { + +diff --git a/OLEDDisplay.h b/OLEDDisplay.h +index 23e6845..07cb9a2 100644 +--- a/OLEDDisplay.h ++++ b/OLEDDisplay.h +@@ -330,7 +330,6 @@ class OLEDDisplay : public Stream { + + // Implement needed function to be compatible with Print class + size_t write(uint8_t c); +- size_t write(const char* s); + + // Implement needed function to be compatible with Stream class + #ifdef __MBED__ diff --git a/patches/INA3221.patch b/patches/INA3221.patch new file mode 100644 index 000000000..1ad815afb --- /dev/null +++ b/patches/INA3221.patch @@ -0,0 +1,24 @@ +Fix compiler warning + +diff --git a/INA3221.h b/INA3221.h +index bf09c8e..3fc7ad7 100644 +--- a/INA3221.h ++++ b/INA3221.h +@@ -109,7 +109,7 @@ class INA3221 { + uint16_t ch2_en : 1; + uint16_t ch1_en : 1; + uint16_t reset : 1; +- } __attribute__((packed)) conf_reg_t; ++ } __attribute__((packed, aligned(sizeof(uint16_t)))) conf_reg_t; + + // Mask/Enable register + typedef struct { +@@ -129,7 +129,7 @@ class INA3221 { + uint16_t shunt_sum_en_ch2 : 1; + uint16_t shunt_sum_en_ch1 : 1; + uint16_t reserved : 1; +- } __attribute__((packed)) masken_reg_t; ++ } __attribute__((packed, aligned(sizeof(uint16_t)))) masken_reg_t; + + // Arduino's I2C library + TwoWire *_i2c; diff --git a/patches/config.yaml b/patches/config.yaml new file mode 100644 index 000000000..9bfb04d2b --- /dev/null +++ b/patches/config.yaml @@ -0,0 +1,10 @@ +patches: + - name: ESP8266 and ESP32 OLED driver for SSD1306 displays + - name: ETHClass2 + - name: INA3221 + - name: LovyanGFX + version: 1.1.16 + # targets: + # - seeed-sensecap-indicator-tft + - name: NonBlockingRTTTL + - name: SdFat