@@ -11,37 +11,47 @@ defmodule Jeff.Bus do
11
11
conn: nil ,
12
12
controlling_process: nil
13
13
14
+ @ type t :: % __MODULE__ { }
15
+
16
+ @ spec new ( keyword ( ) ) :: t ( )
14
17
def new ( _opts \\ [ ] ) do
15
18
% __MODULE__ { }
16
19
end
17
20
21
+ @ spec add_device ( t ( ) , keyword ( ) ) :: t ( )
18
22
def add_device ( bus , opts \\ [ ] ) do
19
23
device = Device . new ( opts )
20
24
_bus = register ( bus , device . address , device )
21
25
end
22
26
27
+ @ spec remove_device ( t ( ) , byte ( ) ) :: t ( )
23
28
def remove_device ( % { poll: poll , registry: registry } = bus , address ) do
24
29
registry = Map . delete ( registry , address )
25
30
poll = Enum . reject ( poll , & ( & 1 == address ) )
26
31
% { bus | poll: poll , registry: registry }
27
32
end
28
33
34
+ @ spec get_device ( t ( ) , byte ( ) ) :: Device . t ( )
29
35
def get_device ( % { registry: registry } , address ) do
30
36
Map . fetch! ( registry , address )
31
37
end
32
38
39
+ @ spec registered? ( t ( ) , byte ( ) ) :: boolean ( )
33
40
def registered? ( % { registry: registry } , address ) do
34
41
is_map_key ( registry , address )
35
42
end
36
43
44
+ @ spec put_device ( t ( ) , Device . t ( ) ) :: t ( )
37
45
def put_device ( % { cursor: cursor } = bus , device ) do
38
46
register ( bus , cursor , device )
39
47
end
40
48
49
+ @ spec put_device ( t ( ) , byte ( ) , Device . t ( ) ) :: t ( )
41
50
def put_device ( bus , address , device ) do
42
51
register ( bus , address , device )
43
52
end
44
53
54
+ @ spec current_device ( % __MODULE__ { cursor: byte ( ) , registry: map ( ) } ) :: Device . t ( )
45
55
def current_device ( % { cursor: cursor } = bus ) do
46
56
get_device ( bus , cursor )
47
57
end
@@ -55,6 +65,7 @@ defmodule Jeff.Bus do
55
65
_bus = register ( bus , cursor , device )
56
66
end
57
67
68
+ @ spec tick ( t ( ) ) :: t ( )
58
69
def tick ( % { cursor: nil , poll: [ ] } = bus ) do
59
70
poll = addresses ( bus )
60
71
% { bus | poll: poll }
@@ -93,11 +104,13 @@ defmodule Jeff.Bus do
93
104
register ( bus , device )
94
105
end
95
106
107
+ @ spec send_command ( t ( ) , Jeff.Command . t ( ) ) :: t ( )
96
108
def send_command ( bus , % { address: address } = command ) do
97
109
device = get_device ( bus , address ) |> Device . send_command ( command )
98
- _bus = register ( bus , address , device )
110
+ register ( bus , address , device )
99
111
end
100
112
113
+ @ spec receive_reply ( t ( ) , Jeff.Reply . t ( ) ) :: t ( )
101
114
def receive_reply ( bus , reply ) do
102
115
% { bus | reply: reply }
103
116
end
0 commit comments