-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples and documentation for ESP32 ADC driver
Adds Erlang and Elixir examples for the ESP32 ADC driver, and documentation to the Programmers Guide. Signed-off-by: Winford <winford@object.stream>
- Loading branch information
1 parent
a3aa68d
commit aabb956
Showing
4 changed files
with
182 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
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,45 @@ | ||
# | ||
# This file is part of AtomVM. | ||
# | ||
# Copyright 2024 Winford <winford@object.stream> | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later | ||
# | ||
|
||
|
||
defmodule ADCnifs do | ||
# suppress warnings when compiling the VM | ||
@compile {:no_warn_undefined, [Esp.ADC]} | ||
@pin 33 | ||
|
||
def start() do | ||
IO.puts("Testing ADC on pin #{@pin}") | ||
{:ok, unit} = Esp.ADC.init() | ||
{:ok, chan} = Esp.ADC.acquire(@pin, unit, :bit_max, :db_12) | ||
loop(@pin, unit, chan) | ||
end | ||
|
||
defp loop(pin, unit, chan) do | ||
case Esp.ADC.sample(chan, unit, [:raw, :voltage, {:samples, 64}]) do | ||
{:ok, {raw, mv}} -> | ||
IO.puts("Pin #{pin} value = #{raw}, millivolts = #{mv}") | ||
error -> | ||
IO.puts("Error taking ADC sample from pin #{pin}: #{error}") | ||
end | ||
Process.sleep(500) | ||
loop(pin, unit, chan) | ||
end | ||
|
||
end |
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,40 @@ | ||
%% Copyright (c) 2020 dushin.net | ||
%% Copyright (c) 2024 Winford <winford@object.stream> | ||
%% All rights reserved. | ||
%% | ||
%% Licensed under the Apache License, Version 2.0 (the "License"); | ||
%% you may not use this file except in compliance with the License. | ||
%% You may obtain a copy of the License at | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% Unless required by applicable law or agreed to in writing, software | ||
%% distributed under the License is distributed on an "AS IS" BASIS, | ||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
%% See the License for the specific language governing permissions and | ||
%% limitations under the License. | ||
%% | ||
% | ||
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later | ||
% | ||
|
||
-module(adc_example). | ||
|
||
-export([start/0]). | ||
|
||
-define(Pin, 34). | ||
|
||
start() -> | ||
io:format("Testing ADC on pin ~p~n", [?Pin]), | ||
ok = esp_adc:start(?Pin), | ||
loop(?Pin). | ||
|
||
loop(Pin) -> | ||
case esp_adc:read(Pin) of | ||
{ok, {Raw, MilliVolts}} -> | ||
io:format("Raw: ~p Voltage: ~pmV~n", [Raw, MilliVolts]); | ||
Error -> | ||
io:format("Error taking reading: ~p~n", [Error]) | ||
end, | ||
timer:sleep(1000), | ||
loop(Pin). |
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,40 @@ | ||
%% | ||
%% Copyright (c) 2024 Winford <winford@object.stream> | ||
%% All rights reserved. | ||
%% | ||
%% Licensed under the Apache License, Version 2.0 (the "License"); | ||
%% you may not use this file except in compliance with the License. | ||
%% You may obtain a copy of the License at | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% Unless required by applicable law or agreed to in writing, software | ||
%% distributed under the License is distributed on an "AS IS" BASIS, | ||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
%% See the License for the specific language governing permissions and | ||
%% limitations under the License. | ||
%% | ||
% | ||
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later | ||
% | ||
|
||
-module(adc_nif_example). | ||
|
||
-export([start/0]). | ||
-define(Pin, 34). | ||
|
||
start() -> | ||
io:format("Testing ADC resource NIFs on pin ~p~n", [?Pin]), | ||
{ok, Unit} = esp_adc:init(), | ||
{ok, Chan} = esp_adc:acquire(?Pin, Unit), | ||
loop(Chan, Unit). | ||
|
||
loop(Chan, Unit) -> | ||
case esp_adc:sample(Chan, Unit) of | ||
{ok, {Raw, MilliVolts}} -> | ||
io:format("Raw: ~p Voltage: ~pmV~n", [Raw, MilliVolts]); | ||
Error -> | ||
io:format("Error taking reading: ~p~n", [Error]) | ||
end, | ||
timer:sleep(1000), | ||
loop(Chan, Unit). |