Skip to content

Commit 6fd0848

Browse files
committed
Tidying code for PR/ Minor Issues
1 parent e23b621 commit 6fd0848

File tree

6 files changed

+95
-127
lines changed

6 files changed

+95
-127
lines changed

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,24 @@
4646

4747
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) {
4848

49-
// Set pin and channel
50-
51-
self->pin = pin;
52-
claim_pin(pin);
53-
54-
// validate pin number
49+
// Make sure pin number is in range for ADC
5550
if (pin->number < ADC_FIRST_PIN_NUMBER && pin->number >= (ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT)) {
5651
raise_ValueError_invalid_pins();
5752
}
5853

59-
// TODO: find a wat to accept ADC4 for temperature
60-
self->chan = pin->number - ADC_FIRST_PIN_NUMBER;
54+
// Set pin and channel
55+
self->pin = pin;
56+
claim_pin(pin);
6157

62-
// TODO: Checks on chan value here
58+
// TODO: find a way to accept ADC4 for temperature
59+
self->chan = pin->number - ADC_FIRST_PIN_NUMBER;
6360

6461
// Set buffer and length
6562
self->buffer = buffer;
6663
self->len = len;
6764

68-
// TODO: checks on length here
69-
70-
// Set sample rate
71-
// NOTE: bits_per_sample = bytes_per_sample * 8;
65+
// Set sample rate - used in readmultiple
7266
self->bytes_per_sample = bytes_per_sample;
73-
74-
// TODO: Possibly check Rate values here, already u_int
75-
// NOTE: Anything over 500000 for RP2040 will not
76-
// exceed DMA conversion sampling rate.
7767
self->sample_rate = sample_rate;
7868

7969
// Standard IO Init
@@ -111,9 +101,9 @@ void common_hal_adcbuffer_bufferedinput_construct(adcbuffer_bufferedinput_obj_t
111101
// intervals). This is all timed by the 48 MHz ADC clock.
112102
// sample rate determines divisor, not zero.
113103

104+
// sample_rate is forced to be >= 1 in shared-bindings
114105
adc_set_clkdiv((float)48000000.0 / (float)self->sample_rate);
115106

116-
// sleep_ms(1000);
117107
// Set up the DMA to start transferring data as soon as it appears in FIFO
118108
uint dma_chan = dma_claim_unused_channel(true);
119109
self->dma_chan = dma_chan;

ports/raspberrypi/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ CIRCUITPY_PULSEIO ?= 1
2727
CIRCUITPY_WATCHDOG ?= 1
2828

2929
# Use of adcbuffer
30-
#CIRCUITPYTHON_ADCBUFFER = 1
3130
CIRCUITPY_ADCBUFFER = 1
31+
3232
# Audio via PWM
3333
CIRCUITPY_AUDIOIO = 0
3434
CIRCUITPY_AUDIOBUSIO ?= 1

py/circuitpy_defns.mk

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -484,14 +484,6 @@ SRC_C += \
484484

485485
endif
486486

487-
#ifeq ($(CIRCUITPYTHON_ADCBUFFER),1)
488-
# Needed for ADCBUFFER
489-
#SRC_COMMON_HAL_ALL += \
490-
# adcbuffer/BufferedInput.c \
491-
#
492-
#endif
493-
494-
495487
SRC_COMMON_HAL = $(filter $(SRC_PATTERNS), $(SRC_COMMON_HAL_ALL))
496488

497489
# These don't have corresponding files in each port but are still located in

shared-bindings/adcbuffer/BufferedInput.c

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,37 @@
3737
#include "shared-bindings/adcbuffer/BufferedInput.h"
3838
#include "shared-bindings/util.h"
3939

40-
/// class BufferedInput:
41-
/// """Input analog voltage level to supplied buffer using DMA Capture"""
42-
///
43-
/// 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.
46-
///
47-
/// :param ~microcontroller.Pin pin: the pin to read from"""
48-
/// :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples
49-
/// :param ~int sample_rate: rate: The desired playback sample rate
50-
///
51-
/// Usage::
52-
///
53-
/// import board
54-
/// import adcbuffer
55-
/// import array
56-
///
57-
/// length = 1000
58-
/// mybuffer = array.array("H", [0] * length)
59-
/// rate = 500000
60-
/// adcbuf = adcbuffer.BufferedInput(board.GP26, mybuffer, rate)
61-
/// adcbuf.readmultiple()
62-
/// adcbuf.deinit()
63-
/// for i in range(length):
64-
/// print(i, mybuffer[i])
65-
///
66-
/// (TODO) The reference voltage varies by platform so use ``reference_voltage`` to read the configured setting.
67-
/// (TODO) Provide mechanism to read CPU Temperature
68-
/// """
69-
/// ...
70-
///
40+
//| class BufferedInput:
41+
//| """Input analog voltage level to supplied buffer using DMA Capture"""
42+
//|
43+
//| 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.
46+
//|
47+
//| :param ~microcontroller.Pin pin: the pin to read from"""
48+
//| :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples
49+
//| :param ~int sample_rate: rate: The desired playback sample rate
50+
//|
51+
//| Usage::
52+
//|
53+
//| import board
54+
//| import adcbuffer
55+
//| import array
56+
//|
57+
//| length = 1000
58+
//| mybuffer = array.array("H", [0] * length)
59+
//| rate = 500000
60+
//| adcbuf = adcbuffer.BufferedInput(board.GP26, mybuffer, rate)
61+
//| adcbuf.readmultiple()
62+
//| adcbuf.deinit()
63+
//| for i in range(length):
64+
//| print(i, mybuffer[i])
65+
//|
66+
//| (TODO) The reference voltage varies by platform so use ``reference_voltage`` to read the configured setting.
67+
//| (TODO) Provide mechanism to read CPU Temperature
68+
//| """
69+
//| ...
70+
//|
7171
STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
7272
enum { ARG_pin, ARG_buffer, ARG_sample_rate };
7373
static const mp_arg_t allowed_args[] = {
@@ -116,10 +116,10 @@ STATIC mp_obj_t adcbuffer_bufferedinput_make_new(const mp_obj_type_t *type, size
116116
return MP_OBJ_FROM_PTR(self);
117117
}
118118

119-
/// def deinit(self) -> None:
120-
/// """Turn off the BufferedInput and release the pin for other use."""
121-
/// ...
122-
///
119+
//| def deinit(self) -> None:
120+
//| """Turn off the BufferedInput and release the pin for other use."""
121+
//| ...
122+
//|
123123
STATIC mp_obj_t adcbuffer_bufferedinput_deinit(mp_obj_t self_in) {
124124
adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in);
125125
common_hal_adcbuffer_bufferedinput_deinit(self);
@@ -132,46 +132,36 @@ STATIC void check_for_deinit(adcbuffer_bufferedinput_obj_t *self) {
132132
raise_deinited_error();
133133
}
134134
}
135-
/// def __enter__(self) -> BufferedInput:
136-
/// """No-op used by Context Managers."""
137-
/// ...
138-
///
139-
/// Provided by context manager helper.
140-
///
141-
/// def __exit__(self) -> None:
142-
/// """Automatically deinitializes the hardware when exiting a context. See
143-
/// :ref:`lifetime-and-contextmanagers` for more info."""
144-
/// ...
145-
///
135+
136+
//| Provided by context manager helper.
137+
//|
138+
//| def __exit__(self) -> None:
139+
//| """Automatically deinitializes the hardware when exiting a context. See
140+
//| :ref:`lifetime-and-contextmanagers` for more info."""
141+
//| ...
142+
//|
146143
STATIC mp_obj_t adcbuffer_bufferedinput___exit__(size_t n_args, const mp_obj_t *args) {
147144
(void)n_args;
148145
common_hal_adcbuffer_bufferedinput_deinit(args[0]);
149146
return mp_const_none;
150147
}
151148
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adcbuffer_bufferedinput___exit___obj, 4, 4, adcbuffer_bufferedinput___exit__);
152149

153-
/// value: int
154-
/// """The value on the analog pin between 0 and 65535 inclusive (16-bit). (read-only)
155-
///
156-
/// Even if the underlying analog to digital converter (ADC) is lower
157-
/// resolution, the value is 16-bit."""
158-
///
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 byte_per_sample. """
156+
//| ...
157+
//|
159158
STATIC mp_obj_t adcbuffer_bufferedinput_obj_readmultiple(mp_obj_t self_in) {
160159
adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in);
161160
check_for_deinit(self);
162161
return MP_OBJ_NEW_SMALL_INT(common_hal_adcbuffer_bufferedinput_readmultiple(self));
163162
}
164163
MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_readmultiple_obj, adcbuffer_bufferedinput_obj_readmultiple);
165164

166-
/// MP_PROPERTY_GETTER(adcbuffer_bufferedinput_value_obj,
167-
/// (mp_obj_t)&adcbuffer_bufferedinput_get_value_obj);
168-
///
169-
/// reference_voltage: float
170-
/// """The maximum voltage measurable (also known as the reference voltage) as a
171-
/// `float` in Volts. Note the ADC value may not scale to the actual voltage linearly
172-
/// at ends of the analog range."""
173-
///
174-
175165
STATIC const mp_rom_map_elem_t adcbuffer_bufferedinput_locals_dict_table[] = {
176166
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&adcbuffer_bufferedinput_deinit_obj) },
177167
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },

shared-bindings/adcbuffer/BufferedInput.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
#include "common-hal/microcontroller/Pin.h"
3131
#include "common-hal/adcbuffer/BufferedInput.h"
3232

33-
// #ifdef CIRCUITPY_BUFFEREDINPUT #endif
34-
3533
extern const mp_obj_type_t adcbuffer_bufferedinput_type;
3634

3735
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);

shared-bindings/adcbuffer/__init__.c

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,41 @@
3030
#include "shared-bindings/adcbuffer/__init__.h"
3131
#include "shared-bindings/adcbuffer/BufferedInput.h"
3232

33-
/// #ifdef CIRCUITPY_BUFFEREDINPUT#endif
34-
35-
/// """Analog buffered hardware support
36-
///
37-
/// The `adcbuffer` module contains classes to provide access to analog-to-digital
38-
/// conversion and digital-to-analog (DAC) for multiple value transfer.
39-
///
40-
/// All classes change hardware state and should be deinitialized when they
41-
/// are no longer needed if the program continues after use. To do so, either
42-
/// call :py:meth:`!deinit` or use a context manager. See
43-
/// :ref:`lifetime-and-contextmanagers` for more info.
44-
///
45-
/// For example::
46-
///
47-
/// import adcbuffer
48-
/// import array
49-
/// from board import *
50-
///
51-
/// length = 5000000
52-
/// mybuffer = array.array("H", [0] * length)
53-
/// adcbuf_obj = adcbuffer.BufferedInPut(GP26, mybuffer, length)
54-
/// adcbuffer.readmultiple()
55-
/// print(*mybuffer)
56-
/// adcbuf_obj.deinit()
57-
///
58-
/// This example will initialize the the device, read and fill
59-
/// :py:data:`~adcbuffer.BufferedInPut` to mybuffer and then
60-
/// :py:meth:`~adcbuffer.BufferedInPut.deinit` the hardware. The last step is optional
61-
/// because CircuitPython will do it automatically after the program finishes.
62-
///
63-
/// TODO: For the essentials of `adcbuffer`, see the `CircuitPython Essentials
64-
/// Learn guide <https://learn.adafruit.com/circuitpython-essentials/circuitpython-adcbuffer>`_
65-
///
66-
/// TODO: For more information on using `adcbuffer`, see `this additional Learn guide
67-
/// <https://learn.adafruit.com/circuitpython-advanced-analog-inputs-and-outputs>`_
68-
/// """
69-
///
33+
//| """Analog buffered hardware support
34+
//|
35+
//| The `adcbuffer` module contains classes to provide access to analog-to-digital
36+
//| conversion and digital-to-analog (DAC) for multiple value transfer.
37+
//|
38+
//| All classes change hardware state and should be deinitialized when they
39+
//| are no longer needed if the program continues after use. To do so, either
40+
//| call :py:meth:`!deinit` or use a context manager. See
41+
//| :ref:`lifetime-and-contextmanagers` for more info.
42+
//|
43+
//| For example::
44+
//|
45+
//| import adcbuffer
46+
//| import array
47+
//| from board import *
48+
//|
49+
//| length = 5000000
50+
//| mybuffer = array.array("H", [0] * length)
51+
//| adcbuf_obj = adcbuffer.BufferedInput(GP26, mybuffer, length)
52+
//| adcbuffer.readmultiple()
53+
//| print(*mybuffer)
54+
//| adcbuf_obj.deinit()
55+
//|
56+
//| 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.
60+
//|
61+
//| TODO: For the essentials of `adcbuffer`, see the `CircuitPython Essentials
62+
//| Learn guide <https://learn.adafruit.com/circuitpython-essentials/circuitpython-adcbuffer>`_
63+
//|
64+
//| TODO: For more information on using `adcbuffer`, see `this additional Learn guide
65+
//| <https://learn.adafruit.com/circuitpython-advanced-analog-inputs-and-outputs>`_
66+
//| """
67+
//|
7068

7169
STATIC const mp_rom_map_elem_t adcbuffer_module_globals_table[] = {
7270
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adcbuffer) },

0 commit comments

Comments
 (0)