|
| 1 | +# Filament Usage JSON Patch |
| 2 | + |
| 3 | +This patch extends the existing `PrintStats` JSON |
| 4 | +structure to report filament usage in two forms: |
| 5 | + |
| 6 | +- **Cumulative usage** (total extruded E position for the print, retractions included as negative values to preserve actual length) |
| 7 | +- **Current usage delta** (current total minus last total, 250ms cycle) |
| 8 | + |
| 9 | +The new values are reported as additional JSON key–value pairs built by the |
| 10 | +patched JSON writer. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Technical |
| 15 | + |
| 16 | +- Hooks an existing **PrintStats JSON writer** and branches into a new |
| 17 | + function in unused executable space. |
| 18 | +- The function... |
| 19 | + - Loads the current total E position from the motion planner via pointer offset. |
| 20 | + - Stores this value into an unused `.bss` location for later comparison. |
| 21 | + - Computes the delta between the current E position and the last stored |
| 22 | + E position (representing filament called over the last 250 ms). |
| 23 | + - Builds and reports two new JSON key–value pairs and injects them into the |
| 24 | + existing PrintStats output. |
| 25 | +- The JSON keys themselves are **hex strings**, stored in the binary at fixed |
| 26 | + addresses and interpreted by the Home Assistant integration |
| 27 | + (TODO: push the HA fork that decodes these keys). |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## Patched regions |
| 32 | + |
| 33 | +All offsets below are **file offsets** (0-based) as seen in Binary Ninja. |
| 34 | + |
| 35 | +### 1. Branch |
| 36 | + |
| 37 | +- **Address**: `0x002DEB18` (also clobber `0x002DEB20`, redundant due to in-function registers) |
| 38 | +- Patches an existing JSON writer site so that execution branches to the new |
| 39 | + asm, then returns back to the stock code path. |
| 40 | + |
| 41 | +### 2. New assembly instructions |
| 42 | + |
| 43 | +- **Range**: `0x00392680` – `0x00392747` |
| 44 | +- Implements the new logic: |
| 45 | + |
| 46 | + - Saves/restores the necessary registers and VFP state. |
| 47 | + - Loads the current E‐axis commanded position from motion planner (Printer pointer in the transpiled cpp) |
| 48 | + and stores it into safe `.bss` locations. |
| 49 | + - Computes the 250 ms extrusion delta using the previous value stored in `.bss`. |
| 50 | + - Builds two additional JSON key–value pairs and calls the existing JSON helper |
| 51 | + used elsewhere in the firmware. |
| 52 | + - Returns back to the original JSON writer using the loaded address target. |
| 53 | + |
| 54 | +### 3. String / key storage |
| 55 | + |
| 56 | +Two constant strings (hex key representations) are stored near the code cave: |
| 57 | + |
| 58 | +1. **Total filament usage key** |
| 59 | + |
| 60 | + - **Address**: `0x003925A0` |
| 61 | + - **Contents**: first key string (hex) used for **total filament usage**. |
| 62 | + |
| 63 | +2. **Current delta usage key** |
| 64 | + |
| 65 | + - **Address**: `0x00392630` |
| 66 | + - **Contents**: second key string (hex) used for the **current 250 ms delta**. |
| 67 | + |
| 68 | +These strings are referenced only by the new and are placed before it to preserve patchable 0-byte addresses. |
0 commit comments