Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dispatch to fpswhen for time-varying rate #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ function fpswhen(switch, rate; name=auto_name!("$rate fpswhen", switch))
n
end

function fpswhen(switch, rate::Signal; name=auto_name!("fpswhen_rate", rate, switch))
#Dispatch for the case where the rate is a time-varying signal.
n = Signal(Float64, 0.0, (switch,rate ,); name=name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having rate as a parent of n seems reasonable. I'm curious, was it needed, or you just thought it was sensible?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter. I haven't tried it without rate as a parent though.

fpswhen_connect(rate, switch, n, name)
n
end

function setup_next_tick(outputref, switchref, dt, wait_dt)
Timer(t -> begin
if value(switchref.value)
Expand All @@ -127,13 +134,13 @@ end

function fpswhen_connect(rate, switch, output, name)
prev_time = time()
dt = 1.0/rate
outputref = WeakRef(output)
switchref = WeakRef(switch)
timer = Timer(identity, 0) # dummy timer to initialise
function fpswhen_runner()
# this function will run if switch gets a new value (i.e. is "active")
# and if output is pushed to (assumed to be by the timer)
dt = 1.0/value(rate)
if switch.value
start_time = time()
timer = setup_next_tick(outputref, switchref, start_time-prev_time, dt)
Expand Down