-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.elm
101 lines (79 loc) · 2.19 KB
/
Timer.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
module Timer (timeUp, indicator, incomingToggleTimer) where
import Signal exposing ((<~))
import Time exposing (Time)
import Keyboard
import Util
import Array
timeUp : Signal ()
timeUp =
timer
type Update = Acc | Dec | Clock
type alias Speed = Int
type alias Model =
{ speed : Speed
, count : Int
, clockTriggered : Bool
}
init : Model
init =
{ speed = 4
, count = 0
, clockTriggered = False
}
-- frequency
timer : Signal ()
timer =
Signal.filterMap trigger () rawTimer
indicator : Signal Int
indicator =
.speed <~ rawTimer
rawTimer : Signal Model
rawTimer =
Signal.foldp update init incomingUpdate
trigger : Model -> Maybe ()
trigger { speed, count, clockTriggered } =
if clockTriggered
then if getThreshold speed <= count then (Just ()) else Nothing
else Nothing
threshold = Array.fromList [16, 10, 5, 2, 1, 0]
(speedMin, speedMax) = (0, Array.length threshold - 1)
getThreshold : Speed -> Int
getThreshold spd =
let (Just thres) = Array.get spd threshold
in thres
update : Update -> Model -> Model
update update mdl =
let model = { mdl | clockTriggered <- False }
in case update of
Dec -> if model.speed > speedMin
then { model | speed <- model.speed - 1 }
else model
Acc -> if model.speed < speedMax
then { model | speed <- model.speed + 1 }
else model
Clock -> if model.count >= getThreshold model.speed
then { model | count <- 0, clockTriggered <- True }
else { model | count <- model.count + 1, clockTriggered <- True }
incomingUpdate : Signal Update
incomingUpdate =
Signal.mergeMany [ always Acc <~ clockAcc
, always Dec <~ clockDec
, always Clock <~ clock
]
-- toggle
toggleTimer : Bool -> Bool -> Bool
toggleTimer spacePressed timerState =
if spacePressed then not timerState else timerState
incomingToggleTimer : Signal Bool
incomingToggleTimer =
Signal.foldp toggleTimer False Keyboard.space
-- signal source
clockAcc : Signal ()
clockAcc =
Util.whenPressed (Keyboard.isDown 187)
clockDec : Signal ()
clockDec =
Util.whenPressed (Keyboard.isDown 189)
clock : Signal Time
clock =
Time.fpsWhen 10 incomingToggleTimer