From 1d525cc80430f06de9d79f14d611f09db2f00732 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 28 Mar 2024 16:11:18 +0100 Subject: [PATCH] docs: Document API changes and new classes. Signed-off-by: iabdalkader --- docs/api.md | 405 ++++++++++++++++++++++++++++++++++++------------- docs/readme.md | 50 ++++-- 2 files changed, 343 insertions(+), 112 deletions(-) diff --git a/docs/api.md b/docs/api.md index 7d4e444..11ef7f4 100644 --- a/docs/api.md +++ b/docs/api.md @@ -4,7 +4,7 @@ ### `AdvancedADC` -Creates an object associated to a specific pin. +Creates an ADC object using the specified pin(s). The ADC object can sample a single channel or multiple channels successively if more than one pin is passed to the constructor. In this case, data from multiple channels will be interleaved in sample buffers. Additionally, the ADC channel of the first pin determines the ADC instance used, and the remaining channels (if any) must all belong to the same ADC instance. #### Syntax @@ -14,18 +14,15 @@ AdvancedADC adc(analogPin); #### Parameters -- Pin `A0` through `A11` can be associated. +- Pin `A0` through `A11` can be used. #### Returns -Nothing. +`void`. ### `begin()` - -Initializes the ADC with the specific parameters. The `begin()` method is firstly used to initialize the library. - -If reconfigured during program execution, use `stop()` first. +Initializes and configures the ADC with the specified parameters. To reconfigure the ADC, `stop()` must be called first. #### Syntax @@ -35,15 +32,25 @@ adc0.begin(resolution, sample_rate, n_samples, n_buffers) #### Parameters -- `enum` - analog read resolution (choose from 8, 10, 12, 14, 16 bit). +- `enum` - **resolution** the sampling resolution (can be 8, 10, 12, 14 or 16 bits). - `AN_RESOLUTION_8` - `AN_RESOLUTION_10` - `AN_RESOLUTION_12` - `AN_RESOLUTION_14` - `AN_RESOLUTION_16` -- `int` - **sample_rate** - the sample rate / frequency in Hertz, e.g. `16000`. -- `int` - **n_samples** - number of samples we want to acquire, e.g. `32`. When reading the ADC, we store these samples into a specific buffer (see [SampleBuffer](#samplebuffer)), and read them via `buffer[x]`, where `x` is the sample you want to retrieve. -- `int` - **n_buffers** - the number of buffers in the queue. +- `int` - **sample_rate** - the sampling rate / frequency in Hertz, e.g. `16000`. +- `int` - **n_samples** - the number of samples per sample buffer. See [SampleBuffer](#samplebuffer) for more details. +- `int` - **n_buffers** - the number of sample buffers in the queue. See [SampleBuffer](#samplebuffer) for more details. +- `bool` - **start** - if true (the default) the ADC will start sampling immediately, otherwise `start()` can be called later to start the ADC. +- `enum` - **sample_time** - the sampling time in cycles (the default is 8.5 cycles). + - `AN_ADC_SAMPLETIME_1_5` + - `AN_ADC_SAMPLETIME_2_5` + - `AN_ADC_SAMPLETIME_8_5` + - `AN_ADC_SAMPLETIME_16_5` + - `AN_ADC_SAMPLETIME_32_5` + - `AN_ADC_SAMPLETIME_64_5` + - `AN_ADC_SAMPLETIME_387_5` + - `AN_ADC_SAMPLETIME_810_5` #### Returns @@ -51,7 +58,7 @@ adc0.begin(resolution, sample_rate, n_samples, n_buffers) ### `available()` -Bool to check if there's any available data on the ADC channel. +Checks if the ADC is readable. #### Syntax @@ -65,15 +72,29 @@ None. #### Returns -1 on success, 0 on failure. +Returns true, if there's at least one sample buffer in the read queue, otherwise returns false. ### `read()` -Reads the first available byte in the buffer. +Returns a sample buffer from the queue for reading. + +### `start()` + +Starts the ADC sampling. + +#### Syntax + +``` +adc.start() +``` + +#### Returns + +1 on success, 0 on failure. ### `stop()` -Stops the ADC and buffer transfer, and releases any memory allocated for the buffer array. +Stops the ADC and releases all of its resources. #### Syntax @@ -85,12 +106,71 @@ adc.stop() - `1` +## AdvancedADCDual + +### `AdvancedADCDual` + +The AdvancedADCDual class enables the configuration of two ADCs in Dual ADC mode. In this mode, the two ADCs are synchronized, and can be sampled simultaneously, with one ADC acting as the master ADC. Note: This mode is only supported on ADC1 and ADC2, and they must be passed to `begin()` in that order. + +#### Syntax + +``` +AdvancedADCDual adc_dual(adc1, adc2); +``` + +#### Parameters + +- `AdvancedADC` - **adc1** - the first ADC (must be ADC1). +- `AdvancedADC` - **adc2** - the second ADC (must be ADC2). + +#### Returns + +`void`. + +### `begin()` + +Initializes and starts the two ADCs with the specified parameters. + +#### Syntax + +``` +adc_dual.begin(resolution, sample_rate, n_samples, n_buffers) +``` + +#### Parameters + +- `enum` - **resolution** the sampling resolution (can be 8, 10, 12, 14 or 16 bits). + - `AN_RESOLUTION_8` + - `AN_RESOLUTION_10` + - `AN_RESOLUTION_12` + - `AN_RESOLUTION_14` + - `AN_RESOLUTION_16` +- `int` - **sample_rate** - the sampling rate / frequency in Hertz, e.g. `16000`. +- `int` - **n_samples** - the number of samples per sample buffer. See [SampleBuffer](#samplebuffer) for more details. +- `int` - **n_buffers** - the number of sample buffers in the queue. See [SampleBuffer](#samplebuffer) for more details. +- `enum` - **sample_time** - the sampling time in cycles (the default is 8.5 cycles). + - `AN_ADC_SAMPLETIME_1_5` + - `AN_ADC_SAMPLETIME_2_5` + - `AN_ADC_SAMPLETIME_8_5` + - `AN_ADC_SAMPLETIME_16_5` + - `AN_ADC_SAMPLETIME_32_5` + - `AN_ADC_SAMPLETIME_64_5` + - `AN_ADC_SAMPLETIME_387_5` + - `AN_ADC_SAMPLETIME_810_5` + +#### Returns + +1 on success, 0 on failure. + +### `stop()` + +Stops the dual ADCs and releases all resources. + ## AdvancedDAC ### `AdvancedDAC` - -Creates a DAC object on a specific pin. +Creates a DAC object using the specified pin. #### Syntax @@ -105,31 +185,27 @@ AdvancedDAC dac1(A13); #### Returns -Nothing. +`void`. ### `begin()` - -Initializes the DAC with the specific parameters. The `begin()` method is firstly used to initialize the library. - -If reconfigured during program execution, use `stop()` first. +Initializes the DAC with the specified parameters. To reconfigure the DAC, `stop()` must be called first. #### Syntax ``` -dac0.begin(resolution, frequency, n_samples, n_buffers) +dac.begin(resolution, frequency, n_samples, n_buffers) ``` #### Parameters -- `enum` - resolution (choose from 8, 10, 12 bit) +- `enum` - resolution (can be 8, 10, 12 bits). - `AN_RESOLUTION_8` - `AN_RESOLUTION_10` - `AN_RESOLUTION_12` -- `int` - **frequency** - the frequency in Hertz, e.g. `8000`. -- `int` - **n_samples** - number of samples we want to write, e.g. `32`. When writing to the DAC, we first write the samples into a buffer (see [SampleBuffer](#samplebuffer)), and write it to the DAC using `dac_out.write(buf)`. -- `int` - **n_buffers** - the number of buffers in the queue. - +- `int` - **frequency** - the output frequency in Hertz, e.g. `8000`. +- `int` - **n_samples** - the number of samples per sample buffer. See [SampleBuffer](#samplebuffer) for more details. +- `int` - **n_buffers** - the number of sample buffers in the queue. See [SampleBuffer](#samplebuffer) for more details. #### Returns @@ -137,7 +213,7 @@ dac0.begin(resolution, frequency, n_samples, n_buffers) ### `available()` -Checks if the DAC channel is available to write to. +Checks if the DAC is writable. #### Syntax @@ -151,13 +227,11 @@ None. #### Returns -1 on success, 0 on failure. - +Returns true, if there's at least one free sample buffer in the write queue, otherwise returns false. ### `dequeue()` - -Creates a buffer object and waits until a buffer to become available. +Returns a sample buffer from the queue for writing. #### Syntax @@ -165,7 +239,7 @@ Creates a buffer object and waits until a buffer to become available. SampleBuffer buf = dac.dequeue(); for (size_t i=0; i + +// WS, CK, SDI, SDO, MCK +AdvancedI2S i2s(PG_10, PG_11, PG_9, PB_5, PC_4); + +void setup() { + Serial.begin(9600); + + // Resolution, sample rate, number of samples per channel, queue depth. + if (!i2s.begin(AN_I2S_MODE_IN, 32000, 512, 32)) { + Serial.println("Failed to start I2S"); + while (1); + } +} + +void loop() { + if (i2s.available()) { + SampleBuffer buf = i2s.read(); + // process samples. + buf.release(); + } +} +``` + ## Examples -- **[Advanced](../examples/Advanced):** This folder contains examples showing how to configure ADC/DAC to read/write data. -- **[Beginner](../examples/Beginner):** This folder contains examples showing how to generate waveforms with DAC. +- **[Beginner](../examples/Beginner):** This folder contains full applications, like audio playback and a waveform generator. +- **[Advanced](../examples/Advanced):** This folder contains more specific examples showing advanced API configurations. ## API