Skip to content

Commit

Permalink
I2C: migrate deprecated keys
Browse files Browse the repository at this point in the history
In order to provide compatibility deprecated config entries are translated,
such as `scl_io_num` to `scl` and a warning is printed.

Signed-off-by: Davide Bettio <davide@uninstall.it>
  • Loading branch information
bettio committed Dec 16, 2023
1 parent 05cb863 commit 929b150
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion libs/eavmlib/src/i2c.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
%%-----------------------------------------------------------------------------
-spec open(Param :: params()) -> i2c().
open(Param) ->
open_port({spawn, "i2c"}, Param).
open_port({spawn, "i2c"}, migrate_config(Param)).

%%-----------------------------------------------------------------------------
%% @param I2C I2C instance created via `open/1'
Expand Down Expand Up @@ -200,3 +200,24 @@ write_bytes(I2C, Address, BinOrInt) ->
) -> ok | {error, Reason :: term()}.
write_bytes(I2C, Address, Register, BinOrInt) ->
port:call(I2C, {write_bytes, Address, BinOrInt, Register}).

migrate_config([]) ->
[];
migrate_config([{K, V} | T]) ->
NewK = rename_key(K),
warn_deprecated(K, NewK),
[{NewK, V} | migrate_config(T)].

rename_key(Key) ->
case Key of
scl_io_num -> scl;
sda_io_num -> sda;
i2c_clock_hz -> clock_speed_hz;
i2c_num -> peripheral;
Any -> Any
end.

warn_deprecated(Key, Key) ->
ok;
warn_deprecated(OldKey, NewKey) ->
io:format("I2C: found deprecated ~p, use ~p instead!!!~n", [OldKey, NewKey]).

0 comments on commit 929b150

Please sign in to comment.