Skip to content

Commit d975efe

Browse files
committed
BREAKING CHANGE: Changed return type in bh1750 driver from rational to float
1 parent 57bee8e commit d975efe

File tree

2 files changed

+8
-38
lines changed

2 files changed

+8
-38
lines changed

examples/bh1750_example/src/bh1750_example.erl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ start() ->
3737
one_time_loop(BH) ->
3838
case bh1750:take_reading(BH) of
3939
{ok, Reading} ->
40-
io:format("Luminosity: ~slx\n", [to_string(Reading)]);
40+
io:format("Luminosity: ~slx\n", [Reading]);
4141
ErrorT ->
4242
io:format("Error taking reading temperature: ~p~n", [ErrorT])
4343
end,
@@ -47,9 +47,6 @@ one_time_loop(BH) ->
4747
continuous_loop(BH) ->
4848
receive
4949
Reading ->
50-
io:format("Luminosity: ~slx\n", [to_string(Reading)])
50+
io:format("Luminosity: ~p\n", [Reading])
5151
end,
5252
continuous_loop(BH).
53-
54-
to_string({Integral, {Fractional, _}}) ->
55-
io_lib:format("~p.~p", [Integral, Fractional]).

src/bh1750.erl

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -381,23 +381,13 @@ do_take_reading(State) ->
381381
%% @private
382382
to_reading(Bin, Mode, MtReg) ->
383383
<<Reading:16/big-unsigned>> = Bin,
384-
X = multiply({1, {2, 10}}, {69, MtReg}),
384+
X = 1.2 * (69 / MtReg),
385385
Y = case Mode of
386386
high2 ->
387-
multiply(X, 2);
387+
X * 2;
388388
_ -> X
389389
end,
390-
divide(Reading, Y).
391-
392-
%% @private
393-
multiply(A, B) ->
394-
rational:simplify(rational:reduce(rational:multiply(A, B))).
395-
396-
%% @private
397-
divide(0, _B) ->
398-
{0, {0, 1}};
399-
divide(A, B) ->
400-
rational:to_decimal(rational:reduce(rational:divide(A, B)), 2).
390+
Reading / Y.
401391

402392
%% @private
403393
do_start_continuous_reading(State) ->
@@ -442,13 +432,8 @@ do_powerdown(State) ->
442432

443433
%% @private
444434
send_command(I2CBus, Address, Command) ->
445-
% ok = i2c_bus:begin_transmission(I2CBus, Address),
446-
% ok = i2c_bus:write_byte(I2CBus, Command),
447-
% ok = i2c_bus:end_transmission(I2CBus).
448435
?TRACE("sending command to bus ~p using address ~p command ~p ...", [I2CBus, Address, Command]),
449-
ok = i2c_bus:enqueue(I2CBus, Address, [
450-
fun(Port, _Address) -> ok = i2c:write_byte(Port, Command) end
451-
]).
436+
i2c_bus:write_bytes(I2CBus, Address, Command).
452437

453438
%% @private
454439
get_command(one_time, high) ->
@@ -467,22 +452,10 @@ get_command(continuous, low) ->
467452
%% @private
468453
get_sleep_ms(Resolution, MtReg) ->
469454
Base = get_sleep_ms(Resolution),
470-
Sleep = multiply(Base, divide(MtReg, 69)),
471-
round_sleep(Sleep).
455+
Sleep = Base * (MtReg / 69),
456+
erlang:round(Sleep).
472457

473458
%% @private
474459
get_sleep_ms(high) -> 120;
475460
get_sleep_ms(high2) -> 120;
476461
get_sleep_ms(low) -> 16.
477-
478-
%% @private
479-
round_sleep({I, {_N, _D} = F}) ->
480-
I + round_sleep(F);
481-
round_sleep({0, _D}) ->
482-
0;
483-
round_sleep({N, D}) ->
484-
case N > (D bsr 1) of
485-
true -> 1;
486-
_ -> 0
487-
end;
488-
round_sleep(I) when is_integer(I) -> I.

0 commit comments

Comments
 (0)