Skip to content

Commit

Permalink
Add RTC example
Browse files Browse the repository at this point in the history
  • Loading branch information
pguyot committed Jul 2, 2023
1 parent 95e11e8 commit 0c11231
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/basic/rtc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.rebar3
_*
.eunit
*.o
*.beam
*.plt
*.swp
*.swo
.erlang.cookie
ebin
log
erl_crash.dump
.rebar
logs
_build
.idea
*.iml
rebar3.crashdump
*~
19 changes: 19 additions & 0 deletions examples/basic/rtc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
M5 RTC sample code
==================

![Sample code running on M5 Stick C Plus](./rtc.jpg)

This sample code illustrates how to use RTC and perform synchronization with NTP.

Usage
-----

- Install rebar3.
- Copy `src/config.hrl-template` to `src/config.hrl`
- Edit `src/config.hrl` with your Wifi credentials
- Connect a M5 device with AtomVM (VM and library) preinstalled.
- Compile and flash with:

```
rebar3 esp32_flash -p /dev/tty.usbserial-*
```
8 changes: 8 additions & 0 deletions examples/basic/rtc/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{erl_opts, [debug_info]}.
{deps, [
{atomvm_m5, {path, "../../../"}}
]}.
{plugins, [
rebar3_path_deps,
{atomvm_rebar3_plugin, {git, "https://github.com/atomvm/atomvm_rebar3_plugin.git", {branch, "master"}}}
]}.
Binary file added examples/basic/rtc/rtc.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/basic/rtc/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.hrl
4 changes: 4 additions & 0 deletions examples/basic/rtc/src/config.hrl-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
% Define Wifi settings

-define(WIFI_SSID, "ssid").
-define(WIFI_PSK, "psk").
13 changes: 13 additions & 0 deletions examples/basic/rtc/src/rtc.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{application, rtc,
[{description, "AtomVM M5 RTC example"},
{vsn, "0.1.0"},
{registered, []},
{applications,
[kernel,
stdlib
]},
{env,[]},
{modules, []},
{licenses, ["MIT"]},
{links, []}
]}.
93 changes: 93 additions & 0 deletions examples/basic/rtc/src/rtc.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
%% @doc RTC sample code
-module(rtc).

-export([start/0]).
-include("config.hrl").

start() ->
m5:begin_([{clear_display, true}, {output_power, false}, {internal_mic, false}]),
case m5_rtc:is_enabled() of
true ->
println("RTC found"),
display_rtc_time(),
println("Starting network"),
ok = start_network(),
wait_for_wifi();
false ->
io:format("RTC not found.\n"),
m5_display:print(<<"RTC not found.">>)
end.

start_network() ->
Parent = self(),
WifiConfig = [
{sta, [
{connected, fun() -> Parent ! {wifi, connected} end},
{got_ip, fun(_Addr) -> Parent ! {wifi, got_ip} end},
{ssid, ?WIFI_SSID},
{psk, ?WIFI_PSK}
]},
{sntp, [{host, "pool.ntp.org"}, {synchronized, fun(Timeval) -> Parent ! {ntp, Timeval} end}]}
],
case network:start(WifiConfig) of
{ok, _Pid} ->
println("Network started"),
ok;
Error ->
println(io_lib:format("Network start failed:\n~p", [Error])),
Error
end.

wait_for_wifi() ->
println("Wait for wifi"),
receive
{wifi, connected} ->
wait_for_ip();
Other ->
println(io_lib:format("Unexpected message:\n~p", [Other])),
wait_for_wifi()
after 10000 ->
io:format("Still waiting for wifi\n"),
wait_for_wifi()
end.

wait_for_ip() ->
println("Wait for IP"),
receive
{wifi, got_ip} ->
wait_for_ntp();
Other ->
println(io_lib:format("Unexpected message:\n~p", [Other])),
wait_for_ip()
after 10000 ->
io:format("Still waiting for IP\n"),
wait_for_ntp()
end.

wait_for_ntp() ->
println("Wait for NTP"),
receive
{ntp, Timeval} ->
println(io_lib:format("Synchronized:\n~p", [Timeval])),
set_rtc();
Other ->
println(io_lib:format("Unexpected message:\n~p", [Other])),
wait_for_ntp()
after 10000 ->
io:format("Still waiting for NTP\n"),
wait_for_ntp()
end.

set_rtc() ->
io:format("Setting M5 RTC to : ~p\n", [erlang:universaltime()]),
m5_rtc:set_datetime(erlang:universaltime()),
display_rtc_time().

display_rtc_time() ->
io:format("M5 RTC time is now : ~p\n", [m5_rtc:get_datetime()]),
DateTimeStr = iolist_to_binary(io_lib:format("RTC time:\n~p", [m5_rtc:get_datetime()])),
m5_display:println(DateTimeStr).

println(IOData) ->
io:format("~s\n", [IOData]),
m5_display:println(IOData).

0 comments on commit 0c11231

Please sign in to comment.