-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevent_handler.ex
More file actions
63 lines (45 loc) · 1.43 KB
/
Copy pathevent_handler.ex
File metadata and controls
63 lines (45 loc) · 1.43 KB
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
defmodule HMC5883L.EventHandler do
use GenEvent
alias HMC5883L.SensorState
import HMC5883L.Utilities
require Logger
def init(args) do
{:ok, args}
end
def handle_event(event, args) do
process_event(event, args)
{:ok, args}
end
defp process_event({:error, error}, _), do: Logger.warn("Driver error #{error}")
defp process_event({:raw_reading, msg}, args) do
update_state({:raw, msg}, args)
{x, y, z} = msg
%{axis_gauss: {xy_gauss,z_gauss}} = SensorState.axis_gauss(args.state_name)
sx = x / xy_gauss * 100
sy = y / xy_gauss * 100
sz = z / z_gauss * 100
GenEvent.notify(args.evtmgr_name, {:scaled_reading, {sx, sy, sz}})
end
defp process_event({:scaled_reading, msg}, args) do
update_state({:scaled, msg}, args)
{x, y, _z} = msg
heading = :math.atan2(y,x) |> bearing_to_degrees
GenEvent.notify(args.evtmgr_name, {:heading, heading})
end
defp process_event({type, _val} = event, args)
when type in [:heading, :available] do
update_state(event, args)
GenEvent.notify(args.evtmgr_name, event)
end
defp process_event({type, msg}, _args)
when is_atom(type) do
Logger.warn("Unknown event received.\nType: #{type}\nMsg: #{inspect msg}")
end
defp process_event(event, _args) do
Logger.warn("Unknown event received.\n#{inspect event}")
end
defp update_state(event, args) do
SensorState.update(args.state_name, event)
event
end
end