5252
5353-behaviour (gen_server ).
5454
55- -export ([start /1 , start /2 , stop /1 , take_reading /1 , chip_id /1 , version /1 , soft_reset /1 ]).
55+ -export ([
56+ start /1 ,
57+ start /2 ,
58+ start_link /1 ,
59+ start_link /2 ,
60+ stop /1 ,
61+ take_reading /1 ,
62+ chip_id /1 ,
63+ version /1 ,
64+ soft_reset /1
65+ ]).
66+
5667-export ([init /1 , handle_call /3 , handle_cast /2 , handle_info /2 , terminate /2 , code_change /3 ]).
5768
5869% -define(TRACE_ENABLED, true).
@@ -137,6 +148,42 @@ start(I2CBus) ->
137148start (I2CBus , Options ) ->
138149 gen_server :start (? MODULE , {I2CBus , Options }, []).
139150
151+ % %-----------------------------------------------------------------------------
152+ % % @param SDAPin pin number for I2C SDA channel
153+ % % @param SCLPin pin number for the I2C SCL channel
154+ % % @returns {ok, BME} on success, or {error, Reason}, on failure
155+ % % @doc Start and link the BME280 driver.
156+ % % @end
157+ % %-----------------------------------------------------------------------------
158+ -spec start_link (I2CBus :: i2c_bus :i2c_bus ()) -> {ok , BME :: bme ()} | {error , Reason :: term ()}.
159+ start_link (I2CBus ) ->
160+ start_link (I2CBus , []).
161+
162+ % %-----------------------------------------------------------------------------
163+ % % @param SDAPin pin number for I2C SDA channel
164+ % % @param SCLPin pin number for the I2C SCL channel
165+ % % @param Options additional driver options
166+ % % @returns {ok, BME} on success, or {error, Reason}, on failure
167+ % % @doc Start and link the BME280 driver.
168+ % %
169+ % % This operation will start and link the BME driver. Use the
170+ % % returned reference in subsequent operations, such as for taking a
171+ % % reading.
172+ % %
173+ % % The Options parameter may be used to fine-tune behavior of the sensor,
174+ % % but the default values should be sufficient for weather-station based
175+ % % scenarios.
176+ % %
177+ % % Notes: The default oversampling rates for temperature, pressure, and humidity
178+ % % is `x4'. A sampling rate of `ignore' is not tested.
179+ % %
180+ % % The default `mode' is `forced'. Other modes are not tested.
181+ % % @end
182+ % %-----------------------------------------------------------------------------
183+ -spec start_link (I2CBus :: i2c_bus :i2c_bus (), Options :: options ()) -> {ok , BME :: bme ()} | {error , Reason :: term ()}.
184+ start_link (I2CBus , Options ) ->
185+ gen_server :start_link (? MODULE , {I2CBus , Options }, []).
186+
140187% %-----------------------------------------------------------------------------
141188% % @param BME a reference to the BME instance created via start
142189% % @returns ok if successful; {error, Reason}, otherwise
@@ -209,7 +256,7 @@ soft_reset(BME) ->
209256init ({I2CBus , Options }) ->
210257 Address = proplists :get_value (address , Options , ? BME280_BASE_ADDR ),
211258 Calibration = read_calibration_data (I2CBus , Address ),
212- ? TRACE (" Caligbration data: ~p~n " , [Calibration ]),
259+ ? TRACE (" Calibration data: ~p~n " , [Calibration ]),
213260 {ok , # state {
214261 i2c_bus = I2CBus ,
215262 calibration_data = Calibration ,
@@ -220,7 +267,7 @@ init({I2CBus, Options}) ->
220267 mode = normalize_mode (proplists :get_value (mode , Options , ? DEFAULT_MODE ))
221268 }}.
222269
223- % % private
270+ % % @ private
224271normalize_oversampling (OverSampling ) ->
225272 case OverSampling of
226273 ignore -> 16#00 ;
@@ -231,7 +278,7 @@ normalize_oversampling(OverSampling) ->
231278 _ -> 16#05
232279 end .
233280
234- % % private
281+ % % @ private
235282normalize_mode (Mode ) ->
236283 case Mode of
237284 sleep -> 16#0 ;
@@ -327,14 +374,14 @@ read_bytes(I2CBus, Register, Len, Address) ->
327374% % @private
328375read_byte (I2CBus , Register , Address ) ->
329376 Bytes = read_bytes (I2CBus , Register , 1 , Address ),
330- ? TRACE (" read bytes ~p from register ~p in address ~p~n " , [Bytes , Register , Address ]),
377+ ? TRACE (" Read bytes ~p from register ~p in address ~p~n " , [Bytes , Register , Address ]),
331378 <<Value :8 >> = Bytes ,
332379 Value .
333380
334381% % @private
335382write_byte (I2CBus , Register , Byte , Address ) ->
336383 Value = <<Byte :8 >>,
337- ? TRACE (" writing byte ~p to register ~p in address ~p~n " , [Byte , Register , Address ]),
384+ ? TRACE (" Writing byte ~p to register ~p in address ~p~n " , [Byte , Register , Address ]),
338385 i2c_bus :write_bytes (I2CBus , Address , Register , Value ).
339386
340387% % @private
0 commit comments