-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-* | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}}} | ||
]}. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
config.hrl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, []} | ||
]}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |