4646-define (DEFAULT_OPTIONS , #{freq_hz => 400000 }).
4747
4848-record (state , {
49- port
49+ i2c
5050}).
5151
5252% %-----------------------------------------------------------------------------
@@ -137,39 +137,38 @@ read_bytes(Bus, Address, Register, Count) ->
137137
138138% % @hidden
139139init (Options ) ->
140- Port = i2c :open ([
141- {sda , maps :get (sda , Options )},
142- {scl , maps :get (scl , Options )},
140+ I2C = i2c :open ([
143141 {clock_speed_hz , maps :get (freq_hz , Options )}
142+ | maps :to_list (Options )
144143 ]),
145- ? TRACE (" Port opened. Options: ~p Port : ~p " , [Options , Port ]),
144+ ? TRACE (" I2C opened. Options: ~p I2C : ~p " , [Options , I2C ]),
146145 State = # state {
147- port = Port
146+ i2c = I2C
148147 },
149148 {ok , State }.
150149
151150% % @hidden
152151handle_call ({enqueue , Address , Operations }, _From , State ) ->
153- Reply = try_enqueue_operations (State # state .port , Address , Operations ),
152+ Reply = try_enqueue_operations (State # state .i2c , Address , Operations ),
154153 {reply , Reply , State };
155154handle_call ({write_bytes , Address , Bytes }, _From , State ) ->
156- ? TRACE (" Writing bytes ~p to address ~p i2c ~p " , [Bytes , Address , State # state .port ]),
157- Reply = i2c :write_bytes (State # state .port , Address , Bytes ),
155+ ? TRACE (" Writing bytes ~p to address ~p i2c ~p " , [Bytes , Address , State # state .i2c ]),
156+ Reply = i2c :write_bytes (State # state .i2c , Address , Bytes ),
158157 ? TRACE (" Reply: ~p " , [Reply ]),
159158 {reply , Reply , State };
160159handle_call ({write_bytes , Address , Register , Bytes }, _From , State ) ->
161- ? TRACE (" Writing bytes ~p to address ~p register ~p i2c ~p " , [Bytes , Address , Register , State # state .port ]),
162- Reply = i2c :write_bytes (State # state .port , Address , Register , Bytes ),
160+ ? TRACE (" Writing bytes ~p to address ~p register ~p i2c ~p " , [Bytes , Address , Register , State # state .i2c ]),
161+ Reply = i2c :write_bytes (State # state .i2c , Address , Register , Bytes ),
163162 ? TRACE (" Reply: ~p " , [Reply ]),
164163 {reply , Reply , State };
165164handle_call ({read_bytes , Address , Count }, _From , State ) ->
166- ? TRACE (" Reading bytes off address ~p count ~p i2c ~p " , [Address , Count , State # state .port ]),
167- Reply = i2c :read_bytes (State # state .port , Address , Count ),
165+ ? TRACE (" Reading bytes off address ~p count ~p i2c ~p " , [Address , Count , State # state .i2c ]),
166+ Reply = i2c :read_bytes (State # state .i2c , Address , Count ),
168167 ? TRACE (" Reply: ~p " , [Reply ]),
169168 {reply , Reply , State };
170169handle_call ({read_bytes , Address , Register , Count }, _From , State ) ->
171- ? TRACE (" Reading bytes off address ~p register ~p count ~p i2c ~p " , [Address , Register , Count , State # state .port ]),
172- Reply = i2c :read_bytes (State # state .port , Address , Register , Count ),
170+ ? TRACE (" Reading bytes off address ~p register ~p count ~p i2c ~p " , [Address , Register , Count , State # state .i2c ]),
171+ Reply = i2c :read_bytes (State # state .i2c , Address , Register , Count ),
173172 ? TRACE (" Reply: ~p " , [Reply ]),
174173 {reply , Reply , State };
175174handle_call (Request , _From , State ) ->
@@ -186,7 +185,7 @@ handle_info(_Info, State) ->
186185% % @hidden
187186terminate (_Reason , State ) ->
188187 io :format (" Closing I2C ... ~n " ),
189- i2c :close (State # state .port ),
188+ i2c :close (State # state .i2c ),
190189 ok .
191190
192191% % @hidden
@@ -203,21 +202,23 @@ maybe_add_defaults(Options) ->
203202 maps :merge (? DEFAULT_OPTIONS , Options ).
204203
205204% % @private
206- try_enqueue_operations (Port , Address , Operations ) ->
207- case i2c :begin_transmission (Port , Address ) of
205+ try_enqueue_operations (I2C , Address , Operations ) ->
206+ ? TRACE (" Enqueing on I2C ~p at address ~p " , [I2C , Address ]),
207+ case i2c :begin_transmission (I2C , Address ) of
208208 ok ->
209209 try
210210 lists :foreach (
211211 fun (Operation ) ->
212- Operation (Port , Address )
212+ ? TRACE (" Executing on I2C ~p at address ~p " , [I2C , Address ]),
213+ Operation (I2C , Address )
213214 end ,
214215 Operations
215216 )
216217 catch
217218 _ :E ->
218219 {error , E }
219220 after
220- i2c :end_transmission (Port )
221+ i2c :end_transmission (I2C )
221222 end ;
222223 E ->
223224 {error , E }
0 commit comments