-
Notifications
You must be signed in to change notification settings - Fork 1
/
Counter.elm
59 lines (47 loc) · 1.23 KB
/
Counter.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
module Counter exposing (Model, init, Msg, tickMsg, update, view)
import Html exposing (..)
import Html.Attributes exposing (..)
import Time
import Debug
import String exposing (concat)
import SevenSeg exposing (sevenSeg)
-- MODEL
type alias Model =
{ num : Int
, limit : Int
, color : String
, period : Time.Time
, start : Time.Time
, fontSize : Float
}
init : Float -> Float -> Model
init fontSize period =
{ num = -1
, limit = 10
, color = "rgb(50,100,255)"
, period = period
, fontSize = fontSize
, start = 0
}
-- UPDATE
type Msg = Tick Time.Time
tickMsg : Time.Time -> Msg
tickMsg t = Tick t
update : Msg -> Model -> Model
-- Update checks if the last tick was over one period ago. If so, increment the state.
update msg model =
case msg of
Tick time ->
if (time - model.start) > model.period
then { model
| num = (model.num + 1) % model.limit
, start = time
}
else model
-- VIEW
view : Model -> Html Msg
-- Each counter is just some coloured text and a number.
view model =
div []
[ div [] [ sevenSeg (model.fontSize*0.7) model.fontSize model.color model.num ]
]