aboutsummaryrefslogtreecommitdiff
path: root/lib/elements/timer.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2022-02-07 15:38:37 +0100
committerSébastien Dailly <sebastien@dailly.me>2022-02-07 16:01:12 +0100
commit77544bdfad2af41514ec1435f706fee87ea2969e (patch)
tree4de23870e08711da25ff92e9670370fc0a74e459 /lib/elements/timer.ml
parentad526111f0dd619ae9e0e98ef2253146b58a068f (diff)
Added viz.js code
Diffstat (limited to 'lib/elements/timer.ml')
-rwxr-xr-xlib/elements/timer.ml38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/elements/timer.ml b/lib/elements/timer.ml
new file mode 100755
index 0000000..28516fc
--- /dev/null
+++ b/lib/elements/timer.ml
@@ -0,0 +1,38 @@
+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
+ send span) in
+ t.id <- timer_id
+
+
+let delay : t -> float
+ = fun t -> Time.counter_value t.counter