From d9b5c7ccde062f0d419cf47156fc65d4fceecbe9 Mon Sep 17 00:00:00 2001 From: s-ol Date: Wed, 18 Mar 2020 15:52:29 +0100 Subject: add util/route --- lib/util.moon | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'lib') diff --git a/lib/util.moon b/lib/util.moon index 3ed903b..79ee721 100644 --- a/lib/util.moon +++ b/lib/util.moon @@ -39,6 +39,38 @@ when i is a num, it is (floor)ed and the matching argument (starting from 0) is values[i] @out\set active and active! +class route extends Op + @doc: "(route i v0 [v1 v2...]) - route between multiple inputs + +when i is true, the first value is reproduced. +when i is false, the second value is reproduced. +when i is a num, it is (floor)ed and the matching argument (starting from 0) is reproduced." + + setup: (inputs) => + { i, values } = match 'any *any', inputs + + i_type = i\type! + assert i_type == 'bool' or i_type == 'num', "#{@}: i has to be bool or num" + typ = all_same [v\type! for v in *values] + @out = Value typ if not @out or typ != @out.type + + super + i: Input.value i + values: [Input.value v for v in *values] + + tick: => + { :i, :values } = @inputs + active = switch i! + when true + values[1] + when false + values[2] + else + i = 1 + (math.floor i!) % #values + values[i] + if active and active\dirty! + @out\set active! + --class switch_pause extends Op -- @doc: "(switch- i v0 [v1 v2...]) - switch and pause multiple inputs -- @@ -104,6 +136,7 @@ default defaults to zero." { 'switch': switch_ + :route :edge :default } -- cgit v1.2.3