Skip to content

Commit

Permalink
use AtomVM 0.6.5 and built-in adc (#2)
Browse files Browse the repository at this point in the history
minimal updates for 0.6.5
  • Loading branch information
petermm authored Nov 18, 2024
1 parent affdd9c commit 9e9c137
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 21 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/esp32-mkimage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:
- name: "Clone AtomVM"
run: |
git clone https://github.com/atomvm/AtomVM
git clone https://github.com/atomvm/AtomVM -b release-0.6
- name: "Build: create build dir"
run: mkdir build
Expand Down Expand Up @@ -133,7 +133,6 @@ jobs:
run: |
git clone https://github.com/atomvm/atomvm_neopixel
git clone https://github.com/atomvm/atomvm_mqtt_client
git clone --branch resource_nif https://github.com/arpunk/atomvm_adc
- name: "Build ${{ matrix.soc }} with idf.py"
shell: bash
Expand Down
3 changes: 1 addition & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
{atomvm_ssd1306, {git, "https://github.com/arpunk/atomvm_ssd1306", {branch, "erlang_driver"}}},
{atomvm_neopixel, {git, "https://github.com/atomvm/atomvm_neopixel", {branch, "master"}}},
{atomvm_mqtt_client,
{git, "https://github.com/atomvm/atomvm_mqtt_client.git", {branch, "master"}}},
{adc, {git, "https://github.com/arpunk/atomvm_adc.git", {branch, "resource_nif"}}}
{git, "https://github.com/atomvm/atomvm_mqtt_client.git", {branch, "master"}}}
]}.

{pre_hooks, [
Expand Down
6 changes: 1 addition & 5 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
[{<<"adc">>,
{git,"https://github.com/arpunk/atomvm_adc.git",
{ref,"3d0e189d450e57d5dc2409f8e7e2cb2ba755e6c6"}},
0},
{<<"aht20">>,
[{<<"aht20">>,
{git,"https://git.sr.ht/~arpunk/aht20",
{ref,"81c5dab52da9d659e5ea4730a29ab973a5d10037"}},
0},
Expand Down
3 changes: 1 addition & 2 deletions src/c3card.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
scd40,
neopixel,
ssd1306,
atomvm_mqtt_client,
adc
atomvm_mqtt_client
]},
{env, []},
{modules, []},
Expand Down
15 changes: 5 additions & 10 deletions src/c3card_battery.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,15 @@ start_link(Config) ->
%% gen_server callbacks

init(_Config) ->
ADCOpts = [
{pin, ?DEFAULT_VBATTERY_PIN},
{bitwidth, bit_max},
%% 0 mV ~ 2500 mV with ADC_11db
{attenuation, db_11}
],
ADC = adc:open(ADCOpts),
{ok, ADC_UNIT} = esp_adc:init(),
{ok, ADC_CHAN} = esp_adc:acquire(?DEFAULT_VBATTERY_PIN, ADC_UNIT),
?LOG_NOTICE("starting battery status helper"),
{ok, #{adc => ADC}}.
{ok, #{adc_chan => ADC_CHAN, adc_unit => ADC_UNIT}}.

handle_call(current_state, _From, State) ->
#{adc := ADC} = State,
#{adc_chan := ADC_CHAN, adc_unit := ADC_UNIT} = State,
ReadingOpts = [{samples, ?SAMPLES}, {raw, true}, {voltage, true}],
{ok, _BattRaw, BattVoltage} = adc:take_reading(ADC, ReadingOpts),
{ok, {_BattRaw, BattVoltage}} = esp_adc:sample(ADC_CHAN, ADC_UNIT, ReadingOpts),
Reply = #{
voltage => BattVoltage,
level => calculate_battery_level(BattVoltage)
Expand Down

0 comments on commit 9e9c137

Please sign in to comment.