aboutsummaryrefslogtreecommitdiff
path: root/elements/timer.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2020-12-23 19:11:31 +0100
committerSébastien Dailly <sebastien@chimrod.com>2020-12-23 19:11:31 +0100
commitec812521b31471ce9ac3d9bdf1288b1569defbc8 (patch)
treed384c959b9e9bb2a04141ab56077026fe6e7c7f3 /elements/timer.ml
parent6354358caa1dfbf2fe1d481f6ac5fba3775938fc (diff)
Add svg output
Diffstat (limited to 'elements/timer.ml')
-rwxr-xr-xelements/timer.ml41
1 files changed, 41 insertions, 0 deletions
diff --git a/elements/timer.ml b/elements/timer.ml
new file mode 100755
index 0000000..0a75e12
--- /dev/null
+++ b/elements/timer.ml
@@ -0,0 +1,41 @@
+open Brr_note_kit
+
+type t =
+ { mutable id : Brr.G.timer_id
+ ; send : float Note.E.send
+ ; mutable counter : Time.counter
+ }
+
+let create
+ : unit -> (t * Brr_note_kit.Time.span Note.E.t)
+ = fun () ->
+ let event, send = Note.E.create ()
+ and counter = (Time.counter ()) in
+ {id = (-1); send; counter}, event
+
+let stop
+ : t -> unit
+ = fun {id; _} ->
+ Brr.G.stop_timer id
+
+let start
+ : t -> float -> unit
+ = fun t d ->
+ let {id; send; _} = t in
+ t.counter <- Time.counter ();
+
+
+ Brr.G.stop_timer id;
+ let timer_id = Brr.G.set_interval
+ ~ms:(int_of_float @@ d *. 1000.)
+ (fun () ->
+
+ let span = Time.counter_value t.counter in
+ t.counter <- Time.counter ();
+ send span) in
+ ignore @@ Brr.G.set_timeout ~ms:0 (fun () -> send 0.);
+ t.id <- timer_id
+
+
+let delay : t -> float
+ = fun t -> Time.counter_value t.counter