From ba3428822d61af7653f65f6a1b3b46813e104439 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 8 Jun 2018 17:02:20 -0400 Subject: [PATCH] stepcompress: Allow stepcompress_alloc() to be called early Don't require an mcu connection to allocate the stepcompress object. Signed-off-by: Kevin O'Connor --- klippy/chelper/__init__.py | 7 ++++--- klippy/chelper/stepcompress.c | 20 +++++++++++++------- klippy/chelper/stepcompress.h | 7 ++++--- klippy/mcu.py | 18 +++++++++--------- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/klippy/chelper/__init__.py b/klippy/chelper/__init__.py index 00fec80c..32b065d3 100644 --- a/klippy/chelper/__init__.py +++ b/klippy/chelper/__init__.py @@ -22,9 +22,10 @@ DEST_LIB = "c_helper.so" OTHER_FILES = ['list.h', 'serialqueue.h', 'stepcompress.h', 'pyhelper.h'] defs_stepcompress = """ - struct stepcompress *stepcompress_alloc(uint32_t max_error - , uint32_t queue_step_msgid, uint32_t set_next_step_dir_msgid - , uint32_t invert_sdir, uint32_t oid); + struct stepcompress *stepcompress_alloc(uint32_t oid); + void stepcompress_fill(struct stepcompress *sc, uint32_t max_error + , uint32_t invert_sdir, uint32_t queue_step_msgid + , uint32_t set_next_step_dir_msgid); void stepcompress_free(struct stepcompress *sc); int stepcompress_reset(struct stepcompress *sc, uint64_t last_step_clock); int stepcompress_set_homing(struct stepcompress *sc, uint64_t homing_clock); diff --git a/klippy/chelper/stepcompress.c b/klippy/chelper/stepcompress.c index e6b4da0b..ebab287a 100644 --- a/klippy/chelper/stepcompress.c +++ b/klippy/chelper/stepcompress.c @@ -228,22 +228,28 @@ check_line(struct stepcompress *sc, struct step_move move) // Allocate a new 'stepcompress' object struct stepcompress * __visible -stepcompress_alloc(uint32_t max_error, uint32_t queue_step_msgid - , uint32_t set_next_step_dir_msgid, uint32_t invert_sdir - , uint32_t oid) +stepcompress_alloc(uint32_t oid) { struct stepcompress *sc = malloc(sizeof(*sc)); memset(sc, 0, sizeof(*sc)); - sc->max_error = max_error; list_init(&sc->msg_queue); - sc->queue_step_msgid = queue_step_msgid; - sc->set_next_step_dir_msgid = set_next_step_dir_msgid; sc->oid = oid; sc->sdir = -1; - sc->invert_sdir = !!invert_sdir; return sc; } +// Fill message id information +void __visible +stepcompress_fill(struct stepcompress *sc, uint32_t max_error + , uint32_t invert_sdir, uint32_t queue_step_msgid + , uint32_t set_next_step_dir_msgid) +{ + sc->max_error = max_error; + sc->invert_sdir = !!invert_sdir; + sc->queue_step_msgid = queue_step_msgid; + sc->set_next_step_dir_msgid = set_next_step_dir_msgid; +} + // Free memory associated with a 'stepcompress' object void __visible stepcompress_free(struct stepcompress *sc) diff --git a/klippy/chelper/stepcompress.h b/klippy/chelper/stepcompress.h index 2a2fe725..a926409c 100644 --- a/klippy/chelper/stepcompress.h +++ b/klippy/chelper/stepcompress.h @@ -5,9 +5,10 @@ #define ERROR_RET -989898989 -struct stepcompress *stepcompress_alloc( - uint32_t max_error, uint32_t queue_step_msgid - , uint32_t set_next_step_dir_msgid, uint32_t invert_sdir, uint32_t oid); +struct stepcompress *stepcompress_alloc(uint32_t oid); +void stepcompress_fill(struct stepcompress *sc, uint32_t max_error + , uint32_t invert_sdir, uint32_t queue_step_msgid + , uint32_t set_next_step_dir_msgid); void stepcompress_free(struct stepcompress *sc); int set_next_step_dir(struct stepcompress *sc, int sdir); int stepcompress_reset(struct stepcompress *sc, uint64_t last_step_clock); diff --git a/klippy/mcu.py b/klippy/mcu.py index d0be7c0e..4a34499a 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -14,7 +14,7 @@ STEPCOMPRESS_ERROR_RET = -989898989 class MCU_stepper: def __init__(self, mcu, pin_params): self._mcu = mcu - self._oid = self._mcu.create_oid() + self._oid = oid = self._mcu.create_oid() self._step_pin = pin_params['pin'] self._invert_step = pin_params['invert'] self._dir_pin = self._invert_dir = None @@ -22,8 +22,12 @@ class MCU_stepper: self._step_dist = self._inv_step_dist = 1. self._min_stop_interval = 0. self._reset_cmd_id = self._get_position_cmd = None - self._ffi_lib = self._stepqueue = None + ffi_main, self._ffi_lib = chelper.get_ffi() + self._stepqueue = ffi_main.gc(self._ffi_lib.stepcompress_alloc(oid), + self._ffi_lib.stepcompress_free) + self._mcu.register_stepqueue(self._stepqueue) self._stepcompress_push_const = self._stepcompress_push_delta = None + self.set_ignore_move(False) def get_mcu(self): return self._mcu def setup_dir_pin(self, pin_params): @@ -55,13 +59,9 @@ class MCU_stepper: "reset_step_clock oid=%c clock=%u") self._get_position_cmd = self._mcu.lookup_command( "stepper_get_position oid=%c") - ffi_main, self._ffi_lib = chelper.get_ffi() - self._stepqueue = ffi_main.gc(self._ffi_lib.stepcompress_alloc( - self._mcu.seconds_to_clock(max_error), step_cmd_id, dir_cmd_id, - self._invert_dir, self._oid), - self._ffi_lib.stepcompress_free) - self._mcu.register_stepqueue(self._stepqueue) - self.set_ignore_move(False) + self._ffi_lib.stepcompress_fill( + self._stepqueue, self._mcu.seconds_to_clock(max_error), + self._invert_dir, step_cmd_id, dir_cmd_id) def get_oid(self): return self._oid def get_step_dist(self):