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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-06-08 17:02:20 -04:00
parent 9a2eb4bedd
commit ba3428822d
4 changed files with 30 additions and 22 deletions

View File

@ -22,9 +22,10 @@ DEST_LIB = "c_helper.so"
OTHER_FILES = ['list.h', 'serialqueue.h', 'stepcompress.h', 'pyhelper.h'] OTHER_FILES = ['list.h', 'serialqueue.h', 'stepcompress.h', 'pyhelper.h']
defs_stepcompress = """ defs_stepcompress = """
struct stepcompress *stepcompress_alloc(uint32_t max_error struct stepcompress *stepcompress_alloc(uint32_t oid);
, uint32_t queue_step_msgid, uint32_t set_next_step_dir_msgid void stepcompress_fill(struct stepcompress *sc, uint32_t max_error
, uint32_t invert_sdir, uint32_t oid); , uint32_t invert_sdir, uint32_t queue_step_msgid
, uint32_t set_next_step_dir_msgid);
void stepcompress_free(struct stepcompress *sc); void stepcompress_free(struct stepcompress *sc);
int stepcompress_reset(struct stepcompress *sc, uint64_t last_step_clock); int stepcompress_reset(struct stepcompress *sc, uint64_t last_step_clock);
int stepcompress_set_homing(struct stepcompress *sc, uint64_t homing_clock); int stepcompress_set_homing(struct stepcompress *sc, uint64_t homing_clock);

View File

@ -228,22 +228,28 @@ check_line(struct stepcompress *sc, struct step_move move)
// Allocate a new 'stepcompress' object // Allocate a new 'stepcompress' object
struct stepcompress * __visible struct stepcompress * __visible
stepcompress_alloc(uint32_t max_error, uint32_t queue_step_msgid stepcompress_alloc(uint32_t oid)
, uint32_t set_next_step_dir_msgid, uint32_t invert_sdir
, uint32_t oid)
{ {
struct stepcompress *sc = malloc(sizeof(*sc)); struct stepcompress *sc = malloc(sizeof(*sc));
memset(sc, 0, sizeof(*sc)); memset(sc, 0, sizeof(*sc));
sc->max_error = max_error;
list_init(&sc->msg_queue); 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->oid = oid;
sc->sdir = -1; sc->sdir = -1;
sc->invert_sdir = !!invert_sdir;
return sc; 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 // Free memory associated with a 'stepcompress' object
void __visible void __visible
stepcompress_free(struct stepcompress *sc) stepcompress_free(struct stepcompress *sc)

View File

@ -5,9 +5,10 @@
#define ERROR_RET -989898989 #define ERROR_RET -989898989
struct stepcompress *stepcompress_alloc( struct stepcompress *stepcompress_alloc(uint32_t oid);
uint32_t max_error, uint32_t queue_step_msgid void stepcompress_fill(struct stepcompress *sc, uint32_t max_error
, uint32_t set_next_step_dir_msgid, uint32_t invert_sdir, uint32_t oid); , uint32_t invert_sdir, uint32_t queue_step_msgid
, uint32_t set_next_step_dir_msgid);
void stepcompress_free(struct stepcompress *sc); void stepcompress_free(struct stepcompress *sc);
int set_next_step_dir(struct stepcompress *sc, int sdir); int set_next_step_dir(struct stepcompress *sc, int sdir);
int stepcompress_reset(struct stepcompress *sc, uint64_t last_step_clock); int stepcompress_reset(struct stepcompress *sc, uint64_t last_step_clock);

View File

@ -14,7 +14,7 @@ STEPCOMPRESS_ERROR_RET = -989898989
class MCU_stepper: class MCU_stepper:
def __init__(self, mcu, pin_params): def __init__(self, mcu, pin_params):
self._mcu = mcu self._mcu = mcu
self._oid = self._mcu.create_oid() self._oid = oid = self._mcu.create_oid()
self._step_pin = pin_params['pin'] self._step_pin = pin_params['pin']
self._invert_step = pin_params['invert'] self._invert_step = pin_params['invert']
self._dir_pin = self._invert_dir = None self._dir_pin = self._invert_dir = None
@ -22,8 +22,12 @@ class MCU_stepper:
self._step_dist = self._inv_step_dist = 1. self._step_dist = self._inv_step_dist = 1.
self._min_stop_interval = 0. self._min_stop_interval = 0.
self._reset_cmd_id = self._get_position_cmd = None 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._stepcompress_push_const = self._stepcompress_push_delta = None
self.set_ignore_move(False)
def get_mcu(self): def get_mcu(self):
return self._mcu return self._mcu
def setup_dir_pin(self, pin_params): def setup_dir_pin(self, pin_params):
@ -55,13 +59,9 @@ class MCU_stepper:
"reset_step_clock oid=%c clock=%u") "reset_step_clock oid=%c clock=%u")
self._get_position_cmd = self._mcu.lookup_command( self._get_position_cmd = self._mcu.lookup_command(
"stepper_get_position oid=%c") "stepper_get_position oid=%c")
ffi_main, self._ffi_lib = chelper.get_ffi() self._ffi_lib.stepcompress_fill(
self._stepqueue = ffi_main.gc(self._ffi_lib.stepcompress_alloc( self._stepqueue, self._mcu.seconds_to_clock(max_error),
self._mcu.seconds_to_clock(max_error), step_cmd_id, dir_cmd_id, self._invert_dir, 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)
def get_oid(self): def get_oid(self):
return self._oid return self._oid
def get_step_dist(self): def get_step_dist(self):