From 53ffa5d85bee370d69591e558100f1918cf08c1d Mon Sep 17 00:00:00 2001 From: Thomas Friedrichsmeier Date: Wed, 5 Apr 2023 21:43:08 +0200 Subject: [PATCH 1/2] Allow setting list of pins to sample after construction Arguably, in user code, the list of pins to sample will typically be known and fixed at compile-time. However, when wrapping this into a library (Mozzi; for the purpose of providing a cross-platform analog read mechanism), it will be very helpful to have a way to adjust the pins to sample after construction. --- src/AdvancedADC.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/AdvancedADC.h b/src/AdvancedADC.h index 647250b..46321d1 100644 --- a/src/AdvancedADC.h +++ b/src/AdvancedADC.h @@ -42,10 +42,18 @@ class AdvancedADC { adc_pins[n_channels++] = analogPinToPinName(p); } } + AdvancedADC(): n_channels(0), descr(nullptr) {} ~AdvancedADC(); bool available(); SampleBuffer read(); int begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers); + int begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers, size_t n_pins, pin_size_t *pins) { + static_assert(n_pins < AN_MAX_ADC_CHANNELS, "A maximum of 5 channels can be sampled successively."); + for (size_t i = 0; i < n_pins; ++i) { + adc_pins[i] = analogPinToPinName(pins[i]); + } + n_channels = n_pins; + } int stop(); }; From 04f127541f158381d63418c35be36b92c5d34e1e Mon Sep 17 00:00:00 2001 From: Thomas Friedrichsmeier Date: Wed, 5 Apr 2023 21:50:52 +0200 Subject: [PATCH 2/2] Enforce limit at runtime rather than static assertion --- src/AdvancedADC.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AdvancedADC.h b/src/AdvancedADC.h index 46321d1..3754bf1 100644 --- a/src/AdvancedADC.h +++ b/src/AdvancedADC.h @@ -48,7 +48,7 @@ class AdvancedADC { SampleBuffer read(); int begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers); int begin(uint32_t resolution, uint32_t sample_rate, size_t n_samples, size_t n_buffers, size_t n_pins, pin_size_t *pins) { - static_assert(n_pins < AN_MAX_ADC_CHANNELS, "A maximum of 5 channels can be sampled successively."); + if (n_pins > AN_MAX_ADC_CHANNELS) n_pins = AN_MAX_ADC_CHANNELS; for (size_t i = 0; i < n_pins; ++i) { adc_pins[i] = analogPinToPinName(pins[i]); }