atomvm_gleam/adc

Analog to digital conversion on the ESP32 family.

Module esp_adc has two APIs, and an application uses one of them. init, acquire, and sample take resource handles. start_pin, read, and stop go through a gen_server that owns those handles itself.

Types

Input attenuation. Db12 reads the full 3.3 V window. Db11 is the default used by acquire_default and start_pin. Db2Point5 is the Erlang atom db_2_5.

See attenuation().

pub type Attenuation {
  Db0
  Db2Point5
  Db6
  Db11
  Db12
}

Constructors

  • Db0
  • Db2Point5
  • Db6
  • Db11
  • Db12

Sample resolution. BitMax selects the widest width the chip supports.

See bit_width().

pub type BitWidth {
  Bit9
  Bit10
  Bit11
  Bit12
  Bit13
  BitMax
}

Constructors

  • Bit9
  • Bit10
  • Bit11
  • Bit12
  • Bit13
  • BitMax

Opaque ADC channel handle from acquire.

See adc_rsrc().

pub type Channel

Errors from the AtomVM ADC driver.

Known reason atoms are Gleam constructors (so {error, timeout} is Error(Timeout)). Bare AtomVM error becomes Failed. Anything else lands in Other as a string because AtomVM types reasons as open term().

See Module esp_adc.

pub type Error {
  Failed
  NotSupported
  Badarg
  Timeout
  Other(String)
}

Constructors

  • Failed
  • NotSupported
  • Badarg
  • Timeout
  • Other(String)

One ADC reading. None means that field was not requested (undefined from AtomVM).

See reading().

pub type Reading {
  Reading(
    raw: option.Option(Int),
    millivolts: option.Option(Int),
  )
}

Constructors

What sample_with and read_with ask the driver to return.

raw and voltage select the fields of Reading. samples is how many conversions to average. The short arities (sample, read) use both fields and 64 samples.

See read_option().

pub type SampleOptions {
  SampleOptions(raw: Bool, voltage: Bool, samples: Int)
}

Constructors

  • SampleOptions(raw: Bool, voltage: Bool, samples: Int)

Opaque ADC unit handle from init.

See adc_rsrc().

pub type Unit

Values

pub fn acquire(
  pin: Int,
  unit: Unit,
  bit_width: BitWidth,
  attenuation: Attenuation,
) -> Result(Channel, Error)

Configure pin and return a channel handle.

This is a resource function. It cannot be used in an application that calls start, start_pin, read, or stop.

See esp_adc:acquire/4.

pub fn acquire_default(
  pin: Int,
  unit: Unit,
) -> Result(Channel, Error)

Configure pin at BitMax and Db11.

This is a resource function. It cannot be used in an application that calls start, start_pin, read, or stop.

See esp_adc:acquire/2.

pub fn deinit(unit: Unit) -> Result(Nil, Error)

Release the ADC unit from init. Release each channel first.

This is a resource function. It cannot be used in an application that calls start, start_pin, read, or stop.

See esp_adc:deinit/1.

pub fn error_to_string(error: Error) -> String

Format an Error for logging.

pub fn init() -> Result(Unit, Error)

Initialize the ADC unit. The returned handle is required by acquire and sample.

This is a resource function. It cannot be used in an application that calls start, start_pin, read, or stop.

See esp_adc:init/0.

pub fn read(pin: Int) -> Result(Reading, Error)

Read a pin previously configured with start_pin. Returns the raw count and pin millivolts, averaged over 64 samples.

This is a convenience function. It cannot be used in an application that calls init, acquire, or sample.

See esp_adc:read/1.

pub fn read_with(
  pin: Int,
  options: SampleOptions,
) -> Result(Reading, Error)

Read a pin previously configured with start_pin, with explicit fields and sample count.

This is a convenience function. It cannot be used in an application that calls init, acquire, or sample.

See esp_adc:read/2.

pub fn release_channel(channel: Channel) -> Result(Nil, Error)

Release a channel from acquire.

This is a resource function. It cannot be used in an application that calls start, start_pin, read, or stop.

See esp_adc:release_channel/1.

pub fn sample(
  channel: Channel,
  unit: Unit,
) -> Result(Reading, Error)

Read a channel. Returns the raw count and pin millivolts, averaged over 64 samples.

This is a resource function. It cannot be used in an application that calls start, start_pin, read, or stop.

See esp_adc:sample/2.

pub fn sample_with(
  channel: Channel,
  unit: Unit,
  options: SampleOptions,
) -> Result(Reading, Error)

Read a channel with explicit fields and sample count.

This is a resource function. It cannot be used in an application that calls start, start_pin, read, or stop.

See esp_adc:sample/3.

pub fn start() -> Result(process.Pid, Error)

Start the gen_server ADC driver without configuring a pin. The process is registered as adc_driver.

This is a convenience function. It cannot be used in an application that calls init, acquire, or sample.

See esp_adc:start/0.

pub fn start_pin(pin: Int) -> Result(Nil, Error)

Configure pin on the gen_server driver at BitMax and Db11.

This is a convenience function. It cannot be used in an application that calls init, acquire, or sample.

See esp_adc:start/1.

pub fn start_pin_with(
  pin: Int,
  bit_width: BitWidth,
  attenuation: Attenuation,
) -> Result(Nil, Error)

Configure pin on the gen_server driver with the given resolution and attenuation.

This is a convenience function. It cannot be used in an application that calls init, acquire, or sample.

See esp_adc:start/2.

pub fn stop() -> Result(Nil, Error)

Stop the gen_server driver and release every pin.

This is a convenience function. It cannot be used in an application that calls init, acquire, or sample.

See esp_adc:stop/0.

pub fn stop_pin(pin: Int) -> Result(Nil, Error)

Release one pin from the gen_server driver.

This is a convenience function. It cannot be used in an application that calls init, acquire, or sample.

See esp_adc:stop/1.

✨ Search Document