Skip to content

Commit d7a1db5

Browse files
committed
Changes per review
1 parent 6fe2ea4 commit d7a1db5

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

ports/raspberrypi/common-hal/adcbuffer/BufferedInput.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t
6262
self->buffer = buffer;
6363
self->len = len;
6464

65-
// Set sample rate - used in readmultiple
65+
// Set sample rate - used in read
6666
self->bytes_per_sample = bytes_per_sample;
6767
self->sample_rate = sample_rate;
6868

@@ -74,6 +74,13 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t
7474
adc_gpio_init(pin->number);
7575
adc_select_input(self->chan); // chan = pin - 26 ??
7676

77+
// RP2040 Implementation Detail
78+
// Fills the supplied buffer with ADC values using DMA transfer.
79+
// If the buffer is 8-bit, then values are 8-bit shifted and error bit is off.
80+
// If buffer is 16-bit, then values are not shifted and error bit is present.
81+
// Number of transfers is always the number of samples which is the array
82+
// byte length divided by the bytes_per_sample.
83+
7784
// self->bytes_per_sample == 1
7885
uint dma_size = DMA_SIZE_8;
7986
bool show_error_bit = false;
@@ -141,7 +148,7 @@ void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *se
141148
dma_channel_unclaim(self->dma_chan);
142149
}
143150

144-
void common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self) {
151+
void common_hal_adcbuffer_bufferedinput_read(adcbuffer_bufferedinput_obj_t *self) {
145152

146153
uint32_t cdl = self->len / self->bytes_per_sample;
147154

shared-bindings/adcbuffer/BufferedInput.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,19 @@
3838
#include "shared-bindings/util.h"
3939

4040
//| class BufferedInput:
41-
//| """Input analog voltage level to supplied buffer using DMA Capture"""
41+
//| """Capture multiple analog voltage levels to the supplied buffer"""
4242
//|
4343
//| def __init__(self, pin: microcontroller.Pin, buffer: WriteableBuffer, *, sample_rate: int = 500000) -> None:
44-
//| """Use the BufferedInput on the given pin. Fill the given buffer from ADC read values at the supplied
45-
//| sample_rate.
44+
//| """Create a `BufferedInput` on the given pin. ADC values will be read
45+
//| into the given buffer at the supplied sample_rate. Depending on the
46+
//| buffer typecode, 'b', 'B', 'h', 'H', samples are 8-bit byte-arrays or
47+
//| 16-bit half-words and are signed or unsigned.
48+
//| The ADC most significant bits of the ADC are kept. Please see:
49+
//| `https://docs.circuitpython.org/en/latest/docs/library/array.html`
4650
//|
4751
//| :param ~microcontroller.Pin pin: the pin to read from
4852
//| :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples
49-
//| :param ~int sample_rate: rate: The desired playback sample rate
53+
//| :param ~int sample_rate: rate: sampling frequency, in samples per second
5054
//|
5155
//| Usage::
5256
//|
@@ -58,7 +62,7 @@
5862
//| mybuffer = array.array("H", [0] * length)
5963
//| rate = 500000
6064
//| adcbuf = adcbuffer.BufferedInput(board.GP26, mybuffer, rate)
61-
//| adcbuf.readmultiple()
65+
//| adcbuf.read()
6266
//| adcbuf.deinit()
6367
//| for i in range(length):
6468
//| print(i, mybuffer[i])
@@ -93,7 +97,7 @@ STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size
9397
if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') {
9498
bytes_per_sample = 2;
9599
} else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) {
96-
mp_raise_ValueError(translate("sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or 'B'"));
100+
mp_raise_ValueError_varg(translate("%q must`` be a bytearray or array of type 'h', 'H', 'b' or 'B'"), MP_QSTR_buffer);
97101
}
98102

99103
// Validate sample rate here
@@ -117,7 +121,7 @@ STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size
117121
}
118122

119123
//| def deinit(self) -> None:
120-
//| """Turn off the BufferedInput and release the pin for other use."""
124+
//| """Shut down the `BufferedInput` and release the pin for other use."""
121125
//| ...
122126
//|
123127
STATIC mp_obj_t adcbuffer_bufferedinput_deinit(mp_obj_t self_in) {
@@ -147,26 +151,23 @@ STATIC mp_obj_t adcbuffer_bufferedinput___exit__(size_t n_args, const mp_obj_t *
147151
}
148152
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adcbuffer_bufferedinput___exit___obj, 4, 4, adcbuffer_bufferedinput___exit__);
149153

150-
//| value: None
151-
//| """Fills the supplied buffer with ADC values using DMA transfer.
152-
//| If the buffer is 8-bit, then values are 8-bit shifted and error bit is off.
153-
//| If buffer is 16-bit, then values are not shifted and error bit is present.
154-
//| Number of transfers is always the number of samples which is the array
155-
//| byte length divided by the bytes_per_sample."""
154+
//|
155+
//| def read(self) -> None:
156+
//| """Fills the provided buffer with ADC voltage values."""
156157
//|
157-
STATIC mp_obj_t adcbuffer_bufferedinput_obj_readmultiple(mp_obj_t self_in) {
158+
STATIC mp_obj_t adcbuffer_bufferedinput_obj_read(mp_obj_t self_in) {
158159
adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in);
159160
check_for_deinit(self);
160-
common_hal_adcbuffer_bufferedinput_readmultiple(self);
161+
common_hal_adcbuffer_bufferedinput_read(self);
161162
return mp_const_none;
162163
}
163-
MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_readmultiple_obj, adcbuffer_bufferedinput_obj_readmultiple);
164+
MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_read_obj, adcbuffer_bufferedinput_obj_read);
164165

165166
STATIC const mp_rom_map_elem_t adcbuffer_bufferedinput_locals_dict_table[] = {
166-
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&adcbuffer_bufferedinput_deinit_obj) },
167-
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
168-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&adcbuffer_bufferedinput___exit___obj) },
169-
{ MP_ROM_QSTR(MP_QSTR_readmultiple), MP_ROM_PTR(&adcbuffer_bufferedinput_readmultiple_obj)},
167+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&adcbuffer_bufferedinput_deinit_obj) },
168+
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
169+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&adcbuffer_bufferedinput___exit___obj) },
170+
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&adcbuffer_bufferedinput_read_obj)},
170171

171172
};
172173

shared-bindings/adcbuffer/BufferedInput.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ extern const mp_obj_type_t adcbuffer_bufferedinput_type;
3535
void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, uint32_t sample_rate);
3636
void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *self);
3737
bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self);
38-
void common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self);
38+
void common_hal_adcbuffer_bufferedinput_read(adcbuffer_bufferedinput_obj_t *self);
3939

4040
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H__

shared-bindings/adcbuffer/__init__.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@
4848
//|
4949
//| length = 5000000
5050
//| mybuffer = array.array("H", [0] * length)
51-
//| adcbuf_obj = adcbuffer.BufferedInput(GP26, mybuffer, length)
52-
//| adcbuffer.readmultiple()
51+
//| adc_in = adcbuffer.BufferedInput(GP26, mybuffer, length)
52+
//| adcbuffer.read()
5353
//| print(*mybuffer)
54-
//| adcbuf_obj.deinit()
54+
//| adc_in.deinit()
5555
//|
5656
//| This example will initialize the the device, read and fill
57-
//| :py:data:`~adcbuffer.BufferedInPut` to mybuffer and then
58-
//| :py:meth:`~adcbuffer.BufferedInPut.deinit` the hardware. The last step is optional
59-
//| because CircuitPython will do it automatically after the program finishes.
57+
//| :py:data:`~adcbuffer.BufferedInPut` to mybuffer
6058
//|
6159
//| TODO: For the essentials of `adcbuffer`, see the `CircuitPython Essentials
6260
//| Learn guide <https://learn.adafruit.com/circuitpython-essentials/circuitpython-adcbuffer>`_

0 commit comments

Comments
 (0)