Skip to content

Commit

Permalink
digital_out to use sync_queue
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Pieper <[email protected]>
  • Loading branch information
Cirromulus committed Oct 28, 2020
1 parent 7b89695 commit 1233ee3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
3 changes: 1 addition & 2 deletions klippy/chelper/stepcompress.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ stepcompress_queue_msg(struct stepcompress *sc, uint32_t *data, int len)
return 0;
}

int __visible
void __visible
sync_channel_queue_msg(struct sync_channel *pc, uint32_t *data, int len,
uint64_t req_clock)
{
Expand All @@ -553,7 +553,6 @@ sync_channel_queue_msg(struct sync_channel *pc, uint32_t *data, int len,
qm->min_clock = req_clock;

list_add_tail(&qm->node, &pc->msg_queue);
return 0;
}


Expand Down
2 changes: 1 addition & 1 deletion klippy/chelper/stepcompress.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int stepcompress_queue_msg(struct stepcompress *sc, uint32_t *data, int len);

struct sync_channel *sync_channel_alloc(uint32_t oid);
void sync_channel_free(struct sync_channel *pc);
int sync_channel_queue_msg(struct sync_channel *pc, uint32_t *data, int len,
void sync_channel_queue_msg(struct sync_channel *pc, uint32_t *data, int len,
uint64_t req_clock);

struct serialqueue;
Expand Down
36 changes: 20 additions & 16 deletions klippy/mcu.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def query_endstop(self, print_time):
class MCU_digital_out:
def __init__(self, mcu, pin_params):
self._mcu = mcu
self._th = None
self._oid = None
self._mcu.register_config_callback(self._build_config)
self._pin = pin_params['pin']
Expand All @@ -127,6 +128,7 @@ def __init__(self, mcu, pin_params):
self._is_static = False
self._max_duration = 2.
self._last_clock = 0
self._ffi_main, self._ffi_lib = chelper.get_ffi()
self._set_cmd = None
def get_mcu(self):
return self._mcu
Expand All @@ -144,6 +146,11 @@ def _build_config(self):
% (self._pin, self._start_value))
return
self._oid = self._mcu.create_oid()
self._th = self._mcu.get_printer().lookup_object('toolhead')
self._sync_channel = self._ffi_main.gc(
self._ffi_lib.sync_channel_alloc(self._oid),
self._ffi_lib.sync_channel_free)
self._mcu.register_sync_channel(self._sync_channel)
self._mcu.add_config_cmd(
"config_digital_out oid=%d pin=%s value=%d default_value=%d"
" max_duration=%d" % (
Expand All @@ -153,13 +160,15 @@ def _build_config(self):
% (self._oid, self._start_value),
on_restart=True)
cmd_queue = self._mcu.alloc_command_queue()
self._set_cmd = self._mcu.lookup_command(
"schedule_digital_out oid=%c clock=%u value=%c", cq=cmd_queue)
self._set_cmd = self._mcu.lookup_command_id(
"schedule_digital_out oid=%c clock=%u value=%c")
def set_digital(self, print_time, value):
clock = self._mcu.print_time_to_clock(print_time)
# TODO: Is this the appropriate way to "send it out as fast as you can"?
self._set_cmd.send([self._oid, clock, (not not value) ^ self._invert],
minclock=0, reqclock=clock)
data = (self._set_cmd, self._oid, clock & 0xFFFFFFFF,
(not not value) ^ self._invert)
self._ffi_lib.sync_channel_queue_msg(
self._sync_channel, data, len(data), clock)
self._th.note_synchronous_command(print_time)
self._last_clock = clock
def set_pwm(self, print_time, value, cycle_time=None):
self.set_digital(print_time, value >= 0.5)
Expand All @@ -178,16 +187,13 @@ def __init__(self, mcu, pin_params):
self._is_static = False
self._last_clock = 0
self._pwm_max = 0.
self._set_cmd_id = None
self._set_cmd_if_soft_pwm = None
#
self._set_cmd = None
self._oid = self._mcu.create_oid()
self._ffi_main, self._ffi_lib = chelper.get_ffi()
self._sync_channel = self._ffi_main.gc(
self._ffi_lib.sync_channel_alloc(self._oid),
self._ffi_lib.sync_channel_free)
self._mcu.register_sync_channel(self._sync_channel)
#
def get_mcu(self):
return self._mcu
def setup_max_duration(self, max_duration):
Expand Down Expand Up @@ -230,7 +236,7 @@ def _build_config(self):
self._mcu.add_config_cmd("schedule_pwm_out oid=%d clock=%d value=%d"
% (self._oid, self._last_clock, svalue),
on_restart=True)
self._set_cmd_id = self._mcu.lookup_command_id(
self._set_cmd = self._mcu.lookup_command_id(
"schedule_pwm_out oid=%c clock=%u value=%hu")
return
# Software PWM
Expand All @@ -252,7 +258,7 @@ def _build_config(self):
"schedule_soft_pwm_out oid=%d clock=%d on_ticks=%d off_ticks=%d"
% (self._oid, self._last_clock, svalue, cycle_ticks - svalue),
is_init=True)
self._set_cmd_if_soft_pwm = self._mcu.lookup_command(
self._set_cmd = self._mcu.lookup_command(
"schedule_soft_pwm_out oid=%c clock=%u on_ticks=%u off_ticks=%u",
cq=cmd_queue)
def set_pwm(self, print_time, value, cycle_time=None):
Expand All @@ -264,11 +270,9 @@ def set_pwm(self, print_time, value, cycle_time=None):

if self._hardware_pwm:
v = int(max(0., min(1., value)) * self._pwm_max + 0.5)
data = (self._set_cmd_id, self._oid, clock & 0xFFFFFFFF, v)
ret = self._ffi_lib.sync_channel_queue_msg(
data = (self._set_cmd, self._oid, clock & 0xFFFFFFFF, v)
self._ffi_lib.sync_channel_queue_msg(
self._sync_channel, data, len(data), clock)
if ret:
raise error("Internal error in pwm send")
self._th.note_synchronous_command(print_time)
return

Expand All @@ -277,7 +281,7 @@ def set_pwm(self, print_time, value, cycle_time=None):
cycle_time = self._cycle_time
cycle_ticks = self._mcu.seconds_to_clock(cycle_time)
on_ticks = int(max(0., min(1., value)) * float(cycle_ticks) + 0.5)
self._set_cmd_if_soft_pwm.send(
self._set_cmd.send(
[self._oid, clock, on_ticks, cycle_ticks - on_ticks],
minclock=minclock, reqclock=clock)

Expand Down

0 comments on commit 1233ee3

Please sign in to comment.