mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
909f5a070f
Add hw-adc gem with analog-to-digital conversion support: - Common Ruby API with read, read_raw, and read_voltage - ports/esp32/ using ESP-IDF ADC oneshot driver - ports/rp2040/ using Pico SDK ADC hardware Based on picoruby-adc by HASUMI Hitoshi. Co-authored-by: Claude <noreply@anthropic.com>
hw-adc - ADC peripheral interface for mruby
This gem provides the ADC class for reading analog input from
mruby. It is designed for embedded platforms such as ESP32 and
RP2040.
Architecture
Platform-specific HAL implementations are in ports/ directories:
ports/esp32/- ESP32 using ESP-IDF ADC oneshot driverports/rp2040/- RP2040 using Pico SDK ADC hardware
Build Configuration
MRuby::CrossBuild.new('esp32') do |conf|
conf.ports :esp32
conf.gem core: 'hw-adc'
end
Ruby API
ADC.new
adc = ADC.new(pin)
pin- ADC-capable GPIO pin number (Integer)- Raises
ArgumentErrorif the pin is not valid for ADC
ADC#read / ADC#read_voltage
Read the analog value as voltage (Float).
voltage = adc.read # => 1.65
voltage = adc.read_voltage # same
ADC#read_raw
Read the raw ADC value (Integer, typically 0-4095 for 12-bit).
raw = adc.read_raw # => 2048
ADC#input
Returns the ADC input channel number assigned during initialization.
HAL Interface
To add support for a new platform, create a ports/<name>/
directory and implement the following C functions declared in
<mruby/adc.h>:
int mrb_adc_init(uint8_t pin);
uint32_t mrb_adc_read_raw(uint8_t input);
float mrb_adc_read_voltage(uint8_t input);
mrb_adc_initreturns the input channel number (>= 0) on success, negative on errorinputparameter for read functions is the value returned bymrb_adc_init
License
MIT