Skip to content

Commit 8af2359

Browse files
authored
Feature/report filament usage (#40)
* Add filament usage reporting patch Patch that adds to PrintStats JSON output to include cumulative and 250ms delta filament usage. See readme for more info * Update README.md * Make patch.sh executable
1 parent e94a0e6 commit 8af2359

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

oc-patches/patch_config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ HOME_POSITION_FRONT_RIGHT=true
2626
REPLACE_BOOTLOGO=true
2727
SET_FIRMWARE_VERSION=true
2828
ALWAYS_ALLOW_Z_OFFSET_ADJUST=true
29-
WAIT_FOR_CHAMBER_TEMP=true
29+
WAIT_FOR_CHAMBER_TEMP=true
30+
REPORT_FILAMENT_USAGE=true
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
if [ $UID -ne 0 ]; then
4+
echo "Error: Please run as root."
5+
exit 1
6+
fi
7+
8+
set -e
9+
10+
project_root="$REPOSITORY_ROOT"
11+
source "$project_root/TOOLS/helpers/utils.sh" "$project_root"
12+
check_tools "bspatch"
13+
14+
cd "$SQUASHFS_ROOT/app"
15+
bspatch ./app ./app-patch "$CURRENT_PATCH_PATH/report_filament_usage_patch.bsdiff"
16+
rm ./app
17+
mv ./app-patch ./app
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id = "report_filament_usage"
2+
name = "Adds both cumulative filament usage (per print) and filament requested per 250ms exports in PrintStats JSON"
3+
after = ["base"]
4+
compatible_versions = ["1.1.40"]
487 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)