From 41bc240b1630ef50826611cc2d10db865cc09abf Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 11 Feb 2026 12:27:24 -0500 Subject: [PATCH] adccmds: Support batching multiple reports into a single message Signed-off-by: Kevin O'Connor --- klippy/mcu.py | 37 ++++++++++++++++++++++++++++++++----- src/adccmds.c | 30 +++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/klippy/mcu.py b/klippy/mcu.py index 909b8ddb5..02c17a067 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -1,9 +1,9 @@ # Interface to Klipper micro-controller code # -# Copyright (C) 2016-2025 Kevin O'Connor +# Copyright (C) 2016-2026 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import sys, os, zlib, logging, math +import sys, os, zlib, logging, math, struct import serialhdl, msgproto, pins, chelper, clocksync class error(Exception): @@ -541,6 +541,7 @@ class MCU_adc: self._oid = self._callback = None self._mcu.register_config_callback(self._build_config) self._inv_max_adc = 0. + self._unpack_from = struct.Struct(' +// Copyright (C) 2016-2026 Kevin O'Connor // // This file may be distributed under the terms of the GNU GPLv3 license. @@ -17,6 +17,8 @@ struct analog_in { struct gpio_adc pin; uint8_t invalid_count, range_check_count; uint8_t state, sample_count; + uint8_t bytes_per_report, data_count; + uint8_t data[48]; }; static struct task_wake analog_wake; @@ -82,16 +84,23 @@ command_query_analog_in(uint32_t *args) a->sample_count = args[3]; a->state = a->sample_count + 1; a->rest_time = args[4]; - a->min_value = args[5]; - a->max_value = args[6]; - a->range_check_count = args[7]; + a->bytes_per_report = args[5]; + a->data_count = 0; + a->min_value = args[6]; + a->max_value = args[7]; + a->range_check_count = args[8]; if (! a->sample_count) return; + if (a->bytes_per_report > ARRAY_SIZE(a->data)) + shutdown("Invalid analog_in bytes_per_report"); sched_add_timer(&a->timer); } DECL_COMMAND(command_query_analog_in, "query_analog_in oid=%c clock=%u sample_ticks=%u sample_count=%c" - " rest_ticks=%u min_value=%hu max_value=%hu range_check_count=%c"); + " rest_ticks=%u bytes_per_report=%c" + " min_value=%hu max_value=%hu range_check_count=%c"); + +#define BYTES_PER_SAMPLE 2 void analog_in_task(void) @@ -112,8 +121,15 @@ analog_in_task(void) uint32_t next_begin_time = a->next_begin_time; a->state++; irq_enable(); - sendf("analog_in_state oid=%c next_clock=%u value=%hu" - , oid, next_begin_time, value); + uint8_t *d = &a->data[a->data_count]; + d[0] = value; + d[1] = value >> 8; + a->data_count += BYTES_PER_SAMPLE; + if (a->data_count + BYTES_PER_SAMPLE > a->bytes_per_report) { + sendf("analog_in_state oid=%c next_clock=%u values=%*s" + , oid, next_begin_time, a->data_count, a->data); + a->data_count = 0; + } } } DECL_TASK(analog_in_task);