Skip to content

Instantly share code, notes, and snippets.

@russelldavies
Created September 26, 2018 20:32
Show Gist options
  • Save russelldavies/c5e54ab90f474cd81c31066587f30819 to your computer and use it in GitHub Desktop.
Save russelldavies/c5e54ab90f474cd81c31066587f30819 to your computer and use it in GitHub Desktop.
module Lcd exposing (main)
import Html exposing (Html, div, input)
import Html.Events exposing (onInput)
import Svg exposing (..)
import Svg.Attributes exposing (..)
import List.Extra exposing (zip)
import Array exposing (Array)
import Maybe.Extra
type alias Model =
List Int
type Msg
= SetNum String
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SetNum str ->
( String.split "" str
|> List.map (String.toInt >> Result.toMaybe)
|> Maybe.Extra.values
, Cmd.none
)
type Status
= On
| Off
segmentPoints : List (Svg.Attribute msg)
segmentPoints =
[ points "1,1 2,0 8,0 9,1 8,2 2,2"
, points "9,1 10,2 10,8 9,9 8,8 8,2"
, points "9,9 10,10 10,16 9,17 8,16 8,10"
, points "9,17 8,18 2,18 1,17 2,16 8,16"
, points "1,17 0,16 0,10 1,9 2,10 2,16"
, points "1,9 0,8 0,2 1,1 2,2 2,8"
, points "1,9 2,8 8,8 9,9 8,10 2,10"
]
segmentStatus : Array (List Status)
segmentStatus =
Array.fromList
[ [ On, On, On, On, On, On, Off ]
, [ Off, On, On, Off, Off, Off, Off ]
, [ On, On, Off, On, On, Off, On ]
, [ On, On, On, On, Off, Off, On ]
, [ Off, On, On, Off, Off, On, On ]
, [ On, Off, On, On, Off, On, On ]
, [ On, Off, On, On, On, On, On ]
, [ On, On, On, Off, Off, Off, Off ]
, [ On, On, On, On, On, On, On ]
, [ On, On, On, On, Off, On, On ]
]
segment status points =
let
color =
case status of
On ->
"#FF0000"
Off ->
"#DDD"
in
polygon [ fill color, points ] []
viewDigit : Int -> Html msg
viewDigit n =
svg
[ viewBox "0 0 100 20" ]
(zip
(Array.get n segmentStatus
|> Maybe.withDefault []
)
segmentPoints
|> List.map (\( status, points ) -> segment status points)
)
view : Model -> Html Msg
view model =
div [] <|
input [ onInput SetNum ] []
:: (List.map viewDigit model)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = (\_ -> Sub.none)
}
init =
( [], Cmd.none )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment