Skip to content

Commit df77663

Browse files
committed
Add example to display acceleration values
Here's a very simple example that renders small displays for acceleration, acceleration including gravity, and angular velocity.
1 parent 0b81ff2 commit df77663

File tree

4 files changed

+140
-19
lines changed

4 files changed

+140
-19
lines changed

elm-package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"src"
88
],
99
"exposed-modules": [
10+
"Device.Motion",
1011
"Device.Orientation"
1112
],
1213
"dependencies": {

example/PlotAccelerationExample.elm

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
module PlotAccelerationExample exposing (main)
2+
3+
import Html exposing (Html)
4+
import Html.Attributes exposing (style)
5+
import Time
6+
import Device.Motion exposing (Motion)
7+
8+
9+
main =
10+
Html.program
11+
{ init = init
12+
, update = update
13+
, view = view
14+
, subscriptions = subscriptions
15+
}
16+
17+
18+
type Msg
19+
= Move Motion
20+
21+
22+
type alias Model =
23+
Motion
24+
25+
26+
init : ( Model, Cmd Msg )
27+
init =
28+
( Device.Motion.initial, Cmd.none )
29+
30+
31+
update : Msg -> Model -> ( Model, Cmd Msg )
32+
update msg model =
33+
case msg of
34+
Move angles ->
35+
( angles, Cmd.none )
36+
37+
38+
subscriptions : Motion -> Sub Msg
39+
subscriptions _ =
40+
Device.Motion.changes Move
41+
42+
43+
view : Model -> Html Msg
44+
view model =
45+
let
46+
styles =
47+
[ ( "padding", "0.618em" )
48+
, ( "font-family", "-apple-system, BlinkMacSystemFont, sans-serif" )
49+
, ( "color", "white" )
50+
, ( "background-color", "rgba(255, 255, 255, 0.1)" )
51+
, ( "box-shadow", "0 1px 3px rgba(1, 1, 1, 0.1)" )
52+
, ( "border-radius", "3px" )
53+
, ( "min-width", "2.2em" )
54+
, ( "text-align", "right" )
55+
]
56+
in
57+
center <|
58+
Html.div
59+
[ style styles ]
60+
[ accelerationView "acceleration" model.acceleration
61+
, accelerationView "including gravity" model.accelerationIncludingGravity
62+
, rotationView "angular velocity" model.rotationRate
63+
]
64+
65+
66+
accelerationView title acceleration =
67+
Html.div [ style [ ( "margin-bottom", "1em" ) ] ]
68+
[ Html.div [ style [ ( "text-align", "center" ) ] ] [ Html.text title ]
69+
, Html.div []
70+
[ accelerationPartView "x" acceleration.x
71+
, accelerationPartView "y" acceleration.y
72+
, accelerationPartView "z" acceleration.z
73+
]
74+
]
75+
76+
77+
rotationView title velocity =
78+
Html.div []
79+
[ Html.text title
80+
, Html.div []
81+
[ accelerationPartView "alpha" velocity.alpha
82+
, accelerationPartView "beta" velocity.beta
83+
, accelerationPartView "gamma" velocity.gamma
84+
]
85+
]
86+
87+
88+
accelerationPartView part amount =
89+
Html.div
90+
[ style [ ( "display", "flex" ) ] ]
91+
[ Html.span [] [ Html.text (part ++ ": ") ]
92+
, Html.span [ style [ ( "flex", "1" ) ] ] [ Html.text (round amount |> toString) ]
93+
]
94+
95+
96+
center : Html Msg -> Html Msg
97+
center node =
98+
let
99+
styles =
100+
[ ( "position", "absolute" )
101+
, ( "top", "0" )
102+
, ( "bottom", "0" )
103+
, ( "left", "0" )
104+
, ( "right", "0" )
105+
, ( "display", "flex" )
106+
, ( "align-items", "center" )
107+
, ( "justify-content", "center" )
108+
, ( "background-color", "#60B5CC" )
109+
]
110+
in
111+
Html.div [ style styles ] [ node ]

src/Device/Motion.elm

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ effect module Device.Motion
66
, changes
77
)
88

9-
{-| This library lets you listen to the acceleration of the device, if the
10-
device supports
11-
the [experimental device motion
12-
apis](https://w3c.github.io/deviceorientation/spec-source-orientation.html).
9+
{-| This library lets you listen to changes in acceleration of the device
10+
running the Elm program, if the device supports the [experimental device motion
11+
apis](https://w3c.github.io/deviceorientation/spec-source-orientation.html#devicemotion).
1312
14-
@docs EulerRotation, initial, changes
13+
@docs Motion, initial, changes
1514
1615
-}
1716

@@ -20,7 +19,7 @@ import Dom.LowLevel as Dom
2019
import Json.Decode as Json
2120
import Process
2221
import Task exposing (Task)
23-
import Device.Orientation exposing (EulerRotation)
22+
import Device.Orientation exposing (EulerRotation, eulerRotation)
2423

2524

2625
-- ORIENTATION
@@ -43,20 +42,21 @@ type alias Acceleration =
4342
-}
4443
type alias Motion =
4544
{ acceleration : Acceleration
46-
, accelerationDueToGravity : Acceleration
45+
, accelerationIncludingGravity : Acceleration
4746
, rotationRate : EulerRotation
48-
, interval : Int
47+
, interval : Float
4948
}
5049

5150

5251
{-| This is a default orientation, useful for representing initial orientation
5352
state.
5453
-}
55-
initial : EulerRotation
54+
initial : Motion
5655
initial =
57-
{ alpha = 0
58-
, beta = 0
59-
, gamma = 0
56+
{ acceleration = { x = 0, y = 0, z = 0 }
57+
, accelerationIncludingGravity = { x = 0, y = 0, z = 0 }
58+
, rotationRate = { alpha = 0, beta = 0, gamma = 0 }
59+
, interval = 0.0
6060
}
6161

6262

@@ -69,13 +69,21 @@ changes tagger =
6969

7070
{-| The decoder used to extract acceleration info from a motion event.
7171
-}
72-
motion : Json.Decoder motion
72+
motion : Json.Decoder Motion
7373
motion =
74-
let
75-
acceleration =
76-
Json.field "acceleration" Json.object
77-
in
78-
Motion acceleration
74+
Json.map4 Motion
75+
(Json.field "acceleration" accelerationDecoder)
76+
(Json.field "accelerationIncludingGravity" accelerationDecoder)
77+
(Json.field "rotationRate" eulerRotation)
78+
(Json.field "interval" Json.float)
79+
80+
81+
accelerationDecoder : Json.Decoder Acceleration
82+
accelerationDecoder =
83+
Json.map3 Acceleration
84+
(Json.field "x" Json.float)
85+
(Json.field "y" Json.float)
86+
(Json.field "z" Json.float)
7987

8088

8189

src/Device/Orientation.elm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ effect module Device.Orientation
22
where { subscription = MySub }
33
exposing
44
( EulerRotation
5+
, eulerRotation
56
, initial
67
, changes
78
)
@@ -10,7 +11,7 @@ effect module Device.Orientation
1011
so long as it supports the [experimental device orientation
1112
apis](https://w3c.github.io/deviceorientation/spec-source-orientation.html).
1213
13-
@docs EulerRotation, initial, changes
14+
@docs EulerRotation, eulerRotation, initial, changes
1415
1516
-}
1617

0 commit comments

Comments
 (0)