Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more API and RTC sample code #1

Merged
merged 6 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: MIT

---
BasedOnStyle: WebKit
StatementMacros:
- M5_NIF_v_v
- M5_NIF_v_i
- M5_NIF_i_v
- M5_NIF_i2_v
- M5_NIF_i3_v
- M5_NIF_i2_i3_v
- M5_NIF_i3_i4_v
- M5_NIF_i4_i5_v
- M5_NIF_i5_i6_v
- M5_NIF_i6_i7_v
51 changes: 49 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ jobs:
- name: Checkout AtomVM
uses: actions/checkout@v3
with:
repository: pguyot/AtomVM
ref: w41/smp-support
repository: atomvm/AtomVM
path: AtomVM
- name: Checkout M5Unified
uses: actions/checkout@v3
Expand All @@ -37,3 +36,51 @@ jobs:
. $IDF_PATH/export.sh
idf.py reconfigure
idf.py build

build-doc:
runs-on: ubuntu-latest
container: erlang:26
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Build edoc
run: |
rebar3 edoc
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
path: doc

clang-format:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install clang-format
run: |
sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
- name: Install run-clang-format
run: |
curl -sSfL https://raw.githubusercontent.com/Sarcasm/run-clang-format/master/run-clang-format.py -o run-clang-format
chmod +x run-clang-format
- name: Check format with clang-format
run: |
./run-clang-format --style=file -r nifs/

erlfmt:
runs-on: ubuntu-latest
container: erlang:26
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Checkout erlfmt
run: |
cd ..
git clone --depth 1 -b v1.1.0 https://github.com/WhatsApp/erlfmt.git
cd erlfmt
rebar3 as release escriptize
- name: Check format with erlfmt
run: |
find . -name *.erl | xargs ../erlfmt/_build/release/bin/erlfmt -c
38 changes: 38 additions & 0 deletions .github/workflows/doc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build and deploy documentation

on:
push:
branches: ["main"]
workflow_dispatch:

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: "pages"
cancel-in-progress: false

jobs:
doc:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
container: erlang:26
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Build edoc
run: |
rebar3 edoc
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
path: doc
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf

# macOS
.DS_Store

# rebar3
_build
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

set(ATOMVM_M5_COMPONENT_SRCS
"nifs/atomvm_m5.cc"
"nifs/atomvm_m5_display.cc"
"nifs/atomvm_m5_i2c.cc"
"nifs/atomvm_m5_power.cc"
"nifs/atomvm_m5_power_axp192.cc"
"nifs/atomvm_m5_rtc.cc"
"nifs/atomvm_m5_speaker.cc"
)

idf_component_register(
Expand Down
36 changes: 36 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,40 @@ config AVM_M5_ENABLE
help
Use this parameter to enable or disable the AtomVM M5 driver.

config AVM_M5_DISPLAY_ENABLE
bool "Enable AtomVM M5 driver display API"
default y
help
Use this parameter to enable or disable the AtomVM M5 driver display API.

config AVM_M5_POWER_ENABLE
bool "Enable AtomVM M5 driver power API"
default y
help
Use this parameter to enable or disable the AtomVM M5 driver power API.

config AVM_M5_POWER_AXP192_ENABLE
bool "Enable AtomVM M5 driver power AXP192 API"
default y
help
Use this parameter to enable or disable the AtomVM M5 driver power AXP192 API.

config AVM_M5_I2C_ENABLE
bool "Enable AtomVM M5 driver I2C API"
default y
help
Use this parameter to enable or disable the AtomVM M5 driver I2C API.

config AVM_M5_RTC_ENABLE
bool "Enable AtomVM M5 driver RTC API"
default y
help
Use this parameter to enable or disable the AtomVM M5 driver RTC API.

config AVM_M5_SPEAKER_ENABLE
bool "Enable AtomVM M5 driver speaker API"
default y
help
Use this parameter to enable or disable the AtomVM M5 driver speaker API.

endmenu
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ This project is a port of M5Unified for the AtomVM platform.

## Usage

The following modules are currently defined:

```
m5
m5_display
m5_i2c
m5_power
m5_power_axp192
m5_rtc
m5_speaker
```
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, []}
]}.
95 changes: 95 additions & 0 deletions examples/basic/rtc/src/rtc.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
%% @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).
Loading