|
| 1 | +% |
| 2 | +% This file is part of AtomVM. |
| 3 | +% |
| 4 | +% Copyright 2022 Paul Guyot <[email protected]> |
| 5 | +% |
| 6 | +% Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +% you may not use this file except in compliance with the License. |
| 8 | +% You may obtain a copy of the License at |
| 9 | +% |
| 10 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +% |
| 12 | +% Unless required by applicable law or agreed to in writing, software |
| 13 | +% distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +% See the License for the specific language governing permissions and |
| 16 | +% limitations under the License. |
| 17 | +% |
| 18 | +% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later |
| 19 | +% |
| 20 | + |
| 21 | +% This example demonstrates the usage of the Pico's hardware RTC. |
| 22 | + |
| 23 | +-module(pico_rtc). |
| 24 | +-export([start/0]). |
| 25 | + |
| 26 | +-define(START_DATE, {{2022, 11, 16}, {22, 09, 00}}). |
| 27 | + |
| 28 | +start() -> |
| 29 | + case atomvm:platform() of |
| 30 | + pico -> |
| 31 | + pico_rtc_loop(); |
| 32 | + Other -> |
| 33 | + erlang:display({unexpected, Other}) |
| 34 | + end. |
| 35 | + |
| 36 | +pico_rtc_loop() -> |
| 37 | + Date = erlang:universaltime(), |
| 38 | + print_date(Date), |
| 39 | + receive |
| 40 | + ok -> ok |
| 41 | + after 5000 -> ok |
| 42 | + end, |
| 43 | + if |
| 44 | + Date < ?START_DATE -> |
| 45 | + pico:rtc_set_datetime(?START_DATE); |
| 46 | + true -> |
| 47 | + ok |
| 48 | + end, |
| 49 | + pico_rtc_loop(). |
| 50 | + |
| 51 | +print_date({{Year, Month, Day}, {Hour, Min, Sec}}) -> |
| 52 | + % Only ~p and ~s seem to exist for now. |
| 53 | + YearStr = integer_to_list(Year), |
| 54 | + MonthStr = format_2_digits(Month), |
| 55 | + DayStr = format_2_digits(Day), |
| 56 | + HourStr = format_2_digits(Hour), |
| 57 | + MinStr = format_2_digits(Min), |
| 58 | + SecStr = format_2_digits(Sec), |
| 59 | + console:puts( |
| 60 | + lists:flatten([ |
| 61 | + YearStr, "-", MonthStr, "-", DayStr, "T", HourStr, ":", MinStr, ":", SecStr, "\n" |
| 62 | + ]) |
| 63 | + ). |
| 64 | + |
| 65 | +format_2_digits(Num) when Num < 10 -> |
| 66 | + ["0", integer_to_list(Num)]; |
| 67 | +format_2_digits(Num) -> |
| 68 | + integer_to_list(Num). |
0 commit comments