-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1264 from UncleGrumpy/esp32_adc
Introduce ESP32 ADC driver This set of changes adds support for the ESP32 ADC peripheral, with documentation and examples. Closes #250 These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
- Loading branch information
Showing
10 changed files
with
1,535 additions
and
1 deletion.
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
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 <[email protected]> | ||
# | ||
# 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 <[email protected]> | ||
%% 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 <[email protected]> | ||
%% 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). |
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 |
---|---|---|
|
@@ -29,6 +29,7 @@ set(ERLANG_MODULES | |
console | ||
emscripten | ||
esp | ||
esp_adc | ||
gpio | ||
i2c | ||
http_server | ||
|
Oops, something went wrong.