Introduced odule adcbuffer / removed analogio/AnalogFastIn

This commit is contained in:
Lee Atkinson 2022-08-18 16:23:17 -04:00
parent 34b8fbaf14
commit f91af513b7
13 changed files with 387 additions and 262 deletions

View File

@ -398,12 +398,10 @@ msgstr ""
msgid "All CAN peripherals are in use"
msgstr ""
#: ports/espressif/common-hal/busio/I2C.c ports/nrf/common-hal/busio/I2C.c
msgid "All I2C peripherals are in use"
msgstr ""
#: ports/espressif/common-hal/busio/I2C.c
#: ports/espressif/common-hal/i2ctarget/I2CTarget.c
msgid "All I2C targets are in use"
#: ports/nrf/common-hal/busio/I2C.c
msgid "All I2C peripherals are in use"
msgstr ""
#: ports/espressif/common-hal/countio/Counter.c
@ -1061,6 +1059,7 @@ msgid "I2C init error"
msgstr ""
#: ports/raspberrypi/common-hal/busio/I2C.c
#: ports/raspberrypi/common-hal/i2ctarget/I2CTarget.c
msgid "I2C peripheral in use"
msgstr ""
@ -1592,6 +1591,7 @@ msgid "Only one TouchAlarm can be set in deep sleep."
msgstr ""
#: ports/espressif/common-hal/i2ctarget/I2CTarget.c
#: ports/raspberrypi/common-hal/i2ctarget/I2CTarget.c
msgid "Only one address is allowed"
msgstr ""
@ -3863,11 +3863,11 @@ msgstr ""
msgid "rsplit(None,n)"
msgstr ""
#: shared-bindings/analogio/AnalogFastIn.c
#: shared-bindings/adcbuffer/BufferdInput.c
msgid "sample rate must be 1.0-500000.0 per second"
msgstr ""
#: shared-bindings/analogio/AnalogFastIn.c
#: shared-bindings/adcbuffer/BufferdInput.c
#: shared-bindings/audiocore/RawSample.c
msgid ""
"sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or "

View File

@ -4,8 +4,13 @@
* The MIT License (MIT)
*
* SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc.
* Taken from AnalogIn by Scott Shawcroft for Adafruit Industries
* Also from DMA_Capture by Luke Wren of Raspberry Pi (Trading) Ltd.
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
* https://github.com/raspberrypi/pico-examples/blob/master/adc/dma_capture/dma_capture.c
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -26,38 +31,47 @@
* THE SOFTWARE.
*/
#include "common-hal/analogio/AnalogFastIn.h"
#include "shared-bindings/analogio/AnalogFastIn.h"
#include "common-hal/adcbuffer/Bufferedinput.h"
#include "shared-bindings/adcbuffer/Bufferedinput.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "py/runtime.h"
#include "supervisor/shared/translate/translate.h"
#include "src/rp2_common/hardware_adc/include/hardware/adc.h"
#include "src/rp2_common/hardware_dma/include/hardware/dma.h"
// /sdk/src/rp2_common/hardware_dma/include/hardware/dma.h
// ports/raspberrypi/
#include "sdk/src/common/pico_stdlib/include/pico/stdlib.h"
#include "src/common/pico_stdlib/include/pico/stdlib.h"
#define ADC_FIRST_PIN_NUMBER 26
#define ADC_PIN_COUNT 4
void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, mp_float_t sample_rate) {
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, mp_float_t sample_rate) {
// Set pin and channel
self->pin = pin;
claim_pin(pin);
// validate pin number
if (pin->number < ADC_FIRST_PIN_NUMBER) {
and(pin->number >= ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT) {
raise_ValueError_invalid_pins();
}
}
// TODO: find a wat to accept ADC4 for temperature
self->chan = pin->number - ADC_FIRST_PIN_NUMBER;
// Checks on chan value here
// TODO: Checks on chan value here
// Set buffer and length
self->buffer = buffer;
self->len = len;
// checks on length here
// TODO: checks on length here
// uint8_t bytes_per_sample
// Set sample rate
// self->bits_per_sample = bytes_per_sample * 8;
// TODO: Possibly check Rate values here, already u_int
// NOTE: Anything over 500000 for RP2040 will not
// exceed DMA conversion sampling rate.
self->sample_rate = sample_rate;
// Standard IO Init
@ -110,12 +124,12 @@ void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *sel
}
bool common_hal_analogio_analogfastin_deinited(analogio_analogfastin_obj_t *self) {
bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self) {
return self->pin == NULL;
}
void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self) {
if (common_hal_analogio_analogfastin_deinited(self)) {
void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *self) {
if (common_hal_adcbuffer_bufferedinput_deinited(self)) {
return;
}
@ -127,19 +141,10 @@ void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self)
dma_channel_unclaim(self->dma_chan);
}
// ================================================================
// capture()
// make this a bool so that later we can perform integrity checking
// ================================================================
bool common_hal_analogio_analogfastin_capture(analogio_analogfastin_obj_t *self) {
bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self) {
// uint32_t cdl = self->len / 2 - 1;
// CONSIDER THESE ISSUES
// uint16_t value = adc_read();
// Stretch 12-bit ADC reading to 16-bit range
// return (value << 4) | (value >> 8);
uint32_t cdl = self->len / 2 - 1;
dma_channel_configure(self->dma_chan, &(self->cfg),
self->buffer, // dst
&adc_hw->fifo, // src

View File

@ -4,8 +4,13 @@
* The MIT License (MIT)
*
* SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc.
* Taken from AnalogIn by Scott Shawcroft for Adafruit Industries
* Also from DMA_Capture by Luke Wren of Raspberry Pi (Trading) Ltd.
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
* https://github.com/raspberrypi/pico-examples/blob/master/adc/dma_capture/dma_capture.c
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -26,15 +31,15 @@
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGIO_ANALOGFASTIN_H
#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGIO_ANALOGFASTIN_H
#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ADCBUFFER_BUFFEREDINPUT_H
#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ADCBUFFER_BUFFEREDINPUT_H
#include "common-hal/microcontroller/Pin.h"
#include "src/rp2_common/hardware_dma/include/hardware/dma.h"
#include "py/obj.h"
// We can extend the struct without impact to existing code
// This is the adcbuffer object
typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t *pin;
@ -46,8 +51,8 @@ typedef struct {
uint8_t chan;
uint dma_chan;
dma_channel_config cfg;
} analogio_analogfastin_obj_t;
} adcbuffer_bufferedinput_obj_t;
void analogfastin_init(void);
void bufferedinput_init(void);
#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ANALOGIO_ANALOGFASTIN_H
#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ADCBUFFER_BUFFEREDINPUT_H

View File

@ -0,0 +1 @@
// No analogio module functions.

View File

@ -26,8 +26,8 @@ CIRCUITPY_NVM = 1
CIRCUITPY_PULSEIO ?= 1
CIRCUITPY_WATCHDOG ?= 1
# Use of analogio
CIRCUITPYTHON_ANALOGFASTIN = 1
# Use of adcbuffer
CIRCUITPYTHON_ADCBUFFER = 1
# Audio via PWM
CIRCUITPY_AUDIOIO = 0

View File

@ -102,6 +102,9 @@ endif
###
# Select which builtin modules to compile and include.
ifeq ($(CIRCUITPY_ADCBUFFER),1)
SRC_PATTERNS += adcbuffer/%
endif
ifeq ($(CIRCUITPY_AESIO),1)
SRC_PATTERNS += aesio/%
endif
@ -479,10 +482,10 @@ SRC_C += \
endif
ifeq ($(CIRCUITPYTHON_ANALOGFASTIN),1)
# Needed for AnalogFastIn
ifeq ($(CIRCUITPYTHON_ADCBUFFER),1)
# Needed for ADCBUFFER
SRC_COMMON_HAL_ALL += \
analogio/AnalogFastIn.c \
adcbuffer/BufferedInput.c \
endif

View File

@ -65,8 +65,8 @@ CFLAGS += -DCIRCUITPY_ALARM=$(CIRCUITPY_ALARM)
CIRCUITPY_ANALOGIO ?= 1
CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO)
CIRCUITPY_ANALOGFASTIN ?= 0
CFLAGS += -DCIRCUITPY_ANALOGFASTIN=$(CIRCUITPY_ANALOGFASTIN)
CIRCUITPY_ADCBUFFER ?= 0
CFLAGS += -DCIRCUITPY_ADCBUFFER=$(CIRCUITPY_ADCBUFFER)
CIRCUITPY_ATEXIT ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_ATEXIT=$(CIRCUITPY_ATEXIT)

View File

@ -0,0 +1,197 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc.
*
* TODO: Based on analogio/AnalogIn.c from Scott Shaw
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include "shared/runtime/context_manager_helpers.h"
#include "py/binary.h"
#include "py/mphal.h"
#include "py/nlr.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/adcbuffer/BufferedInput.h"
#include "shared-bindings/util.h"
/// class BufferedInput:
/// """Input analog voltage level to supplied buffer using DMA Capture"""
///
/// def __init__(self, pin: microcontroller.Pin, buffer: WriteableBuffer, *, sample_rate: int = 500000) -> None:
/// """Use the BufferedInput on the given pin. Fill the given buffer from ADC read values at the supplied
/// sample_rate.
///
/// :param ~microcontroller.Pin pin: the pin to read from"""
/// :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples
/// :param ~int sample_rate: rate: The desired playback sample rate
///
/// Usage::
///
/// import board
/// import adcbuffer
/// import array
///
/// length = 1000
/// mybuffer = array.array("H", [0] * length)
/// rate = 500000
/// adcbuf = adcbuffer.BufferedInput(board.GP26, mybuffer, rate)
/// adcbuf.readmultiple()
/// adcbuf.deinit()
/// for i in range(length):
/// print(i, mybuffer[i])
///
/// (TODO) The reference voltage varies by platform so use ``reference_voltage`` to read the configured setting.
/// (TODO) Provide mechanism to read CPU Temperature
/// """
/// ...
///
STATIC void validate_rate(mp_float_t rate) {
if (rate < (mp_float_t)1.0f || rate > (mp_float_t)500000.0f) {
mp_raise_ValueError(translate("sample rate must be 1.0-500000.0 per second"));
}
}
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) {
enum { ARG_pin, ARG_buffer, ARG_sample_rate };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_sample_rate, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_int = 500000} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Validate Pin
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj);
// Buffer Pointer defined and allocated by user
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
// signed or unsigned, byte per sample
bool signed_samples = bufinfo.typecode == 'b' || bufinfo.typecode == 'h';
uint8_t bytes_per_sample = 1;
// Bytes Per Sample
if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') {
bytes_per_sample = 2;
} else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) {
mp_raise_ValueError(translate("sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or 'B'"));
}
// Validate sample rate here
mp_float_t sample_rate = mp_obj_get_float(args[ARG_sample_rate].u_obj);
validate_rate(sample_rate);
// Create local object
adcbuffer_bufferedinput_obj_t *self = m_new_obj(adcbuffer_bufferedinput_obj_t);
self->base.type = &adcbuffer_bufferedinput_type;
// Call local intereface in ports/common-hal/adcbuffer
common_hal_adcbuffer_bufferedinput_construct(self,
pin,
((uint8_t *)bufinfo.buf),
bufinfo.len,
bytes_per_sample,
signed_samples,
sample_rate
);
return MP_OBJ_FROM_PTR(self);
}
/// def deinit(self) -> None:
/// """Turn off the BufferedInput and release the pin for other use."""
/// ...
///
STATIC mp_obj_t adcbuffer_bufferedinput_deinit(mp_obj_t self_in) {
adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_adcbuffer_bufferedinput_deinit(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_deinit_obj, adcbuffer_bufferedinput_deinit);
STATIC void check_for_deinit(adcbuffer_bufferedinput_obj_t *self) {
if (common_hal_adcbuffer_bufferedinput_deinited(self)) {
raise_deinited_error();
}
}
/// def __enter__(self) -> BufferedInput:
/// """No-op used by Context Managers."""
/// ...
///
/// Provided by context manager helper.
///
/// def __exit__(self) -> None:
/// """Automatically deinitializes the hardware when exiting a context. See
/// :ref:`lifetime-and-contextmanagers` for more info."""
/// ...
///
STATIC mp_obj_t adcbuffer_bufferedinput___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_adcbuffer_bufferedinput_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adcbuffer_bufferedinput___exit___obj, 4, 4, adcbuffer_bufferedinput___exit__);
/// value: int
/// """The value on the analog pin between 0 and 65535 inclusive (16-bit). (read-only)
///
/// Even if the underlying analog to digital converter (ADC) is lower
/// resolution, the value is 16-bit."""
///
STATIC mp_obj_t adcbuffer_bufferedinput_obj_readmultiple(mp_obj_t self_in) {
adcbuffer_bufferedinput_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_adcbuffer_bufferedinput_readmultiple(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(adcbuffer_bufferedinput_readmultiple_obj, adcbuffer_bufferedinput_obj_readmultiple);
/// MP_PROPERTY_GETTER(adcbuffer_bufferedinput_value_obj,
/// (mp_obj_t)&adcbuffer_bufferedinput_get_value_obj);
///
/// reference_voltage: float
/// """The maximum voltage measurable (also known as the reference voltage) as a
/// `float` in Volts. Note the ADC value may not scale to the actual voltage linearly
/// at ends of the analog range."""
///
STATIC const mp_rom_map_elem_t adcbuffer_bufferedinput_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&adcbuffer_bufferedinput_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&adcbuffer_bufferedinput___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_readmultiple), MP_ROM_PTR(&adcbuffer_bufferedinput_readmultiple_obj)},
};
STATIC MP_DEFINE_CONST_DICT(adcbuffer_bufferedinput_locals_dict, adcbuffer_bufferedinput_locals_dict_table);
const mp_obj_type_t adcbuffer_bufferedinput_type = {
{ &mp_type_type },
.name = MP_QSTR_BufferedInput,
.make_new = adcbuffer_bufferedinput_make_new,
.locals_dict = (mp_obj_t)&adcbuffer_bufferedinput_locals_dict,
};

View File

@ -4,7 +4,6 @@
* The MIT License (MIT)
*
* SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc.
* Taken from AnalogIn by Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -25,18 +24,19 @@
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGFASTIN_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGFASTIN_H
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H
#include "common-hal/microcontroller/Pin.h"
#ifdef CIRCUITPY_ANALOGFASTIN
#include "common-hal/analogio/AnalogFastIn.h"
#include "common-hal/adcbuffer/Bufferedinput.h"
extern const mp_obj_type_t analogio_analogfastin_type;
// #ifdef CIRCUITPY_BUFFEREDINPUT #endif
void common_hal_analogio_analogfastin_construct(analogio_analogfastin_obj_t *self, const mcu_pin_obj_t *pin, uint8_t *buffer, uint32_t len, uint8_t bytes_per_sample, bool samples_signed, mp_float_t sample_rate);
void common_hal_analogio_analogfastin_deinit(analogio_analogfastin_obj_t *self);
bool common_hal_analogio_analogfastin_deinited(analogio_analogfastin_obj_t *self);
bool common_hal_analogio_analogfastin_capture(analogio_analogfastin_obj_t *self);
#endif
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGFASTIN_H__
extern const mp_obj_type_t adcbuffer_bufferedinput_type;
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, mp_float_t sample_rate);
void common_hal_adcbuffer_bufferedinput_deinit(adcbuffer_bufferedinput_obj_t *self);
bool common_hal_adcbuffer_bufferedinput_deinited(adcbuffer_bufferedinput_obj_t *self);
bool common_hal_adcbuffer_bufferedinput_readmultiple(adcbuffer_bufferedinput_obj_t *self);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER_BUFFEREDINPUT_H__

View File

@ -0,0 +1,84 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/adcbuffer/__init__.h"
#include "shared-bindings/adcbuffer/BufferedInput.h"
// #ifdef CIRCUITPY_BUFFEREDINPUT#endif
//| """Analog buffered hardware support
//|
//| The `adcbuffer` module contains classes to provide access to analog-to-digital
//| conversion and digital-to-analog (DAC) for multiple value transfer.
//|
//| All classes change hardware state and should be deinitialized when they
//| are no longer needed if the program continues after use. To do so, either
//| call :py:meth:`!deinit` or use a context manager. See
//| :ref:`lifetime-and-contextmanagers` for more info.
//|
//| For example::
//|
//| import adcbuffer
/// import array
//| from board import *
//|
/// length = 5000000
/// mybuffer = array.array("H", [0] * length)
//| adcbuf_obj = adcbuffer.BufferdInPut(GP26, mybuffer, length)
//| adcbuffer.readmultiple()
//| print(*mybuffer)
//| adcbuf_obj.deinit()
//|
//| This example will initialize the the device, read and fill
//| :py:data:`~adcbuffer.BufferdInPut` to mybuffer and then
//| :py:meth:`~adcbuffer.BufferedInPut.deinit` the hardware. The last step is optional
//| because CircuitPython will do it automatically after the program finishes.
//|
//| TODO: For the essentials of `adcbuffer`, see the `CircuitPython Essentials
//| Learn guide <https://learn.adafruit.com/circuitpython-essentials/circuitpython-adcbuffer>`_
//|
//| TODO: For more information on using `adcbuffer`, see `this additional Learn guide
//| <https://learn.adafruit.com/circuitpython-advanced-analog-inputs-and-outputs>`_
//| """
//|
STATIC const mp_rom_map_elem_t adcbuffer_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adcbuffer) },
{ MP_ROM_QSTR(MP_QSTR_Bufferedinput), MP_ROM_PTR(&adcbuffer_bufferedinput_type) },
// #ifdef CIRCUITPY_BUFFEREDINPUT #endif
};
STATIC MP_DEFINE_CONST_DICT(adcbuffer_module_globals, adcbuffer_module_globals_table);
const mp_obj_module_t adcbuffer_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&adcbuffer_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_adcbuffer, adcbuffer_module, CIRCUITPY_ADCBUFFER);

View File

@ -0,0 +1,34 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER___INIT___H
#include "py/obj.h"
// Nothing now.
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ADCBUFFER___INIT___H

View File

@ -1,197 +0,0 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc.
* Taken from AnalogIn by Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include "shared/runtime/context_manager_helpers.h"
#include "py/binary.h"
#include "py/mphal.h"
#include "py/nlr.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/analogio/AnalogFastIn.h"
#include "shared-bindings/util.h"
//| class AnalogFastIn:
//| """Read analog voltage levels quickly using DMA Capture"""
//|
//| def __init__(self, pin: microcontroller.Pin, buffer: ReadableBuffer, *, sample_rate: int = 500000) -> None:
//| """Use the AnalogFastIn on the given pin. Fill the given buffer from ADC read values at the supplied
/// sample_rate.
///
//| :param ~microcontroller.Pin pin: the pin to read from"""
//| :param ~circuitpython_typing.WriteableBuffer buffer: A buffer for samples
//| :param int sample_rate: The desired playback sample rate
///
//| Usage::
///
//| import board
//| import analogio
//| import array
///
//| length = 1000
//| mybuffer = array.array("H", [0] * length)
//| fadc = analogio.AnalogFastIn(board.GP26, mybuffer)
//| fadc.capture()
//| fadc.deinit()
//| for i in range(length):
//| print(i, mybuffer[i])
///
/// (Future) The reference voltage varies by platform so use ``reference_voltage`` to read the configured setting.
/// """
//| ...
///
STATIC void validate_rate(mp_float_t rate) {
if (rate < (mp_float_t)1.0f || rate > (mp_float_t)500000.0f) {
mp_raise_ValueError(translate("sample rate must be 1.0-500000.0 per second"));
}
}
STATIC mp_obj_t analogio_analogfastin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_pin, ARG_buffer, ARG_sample_rate };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_sample_rate, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Validate Pin
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj);
// Buffer Pointer defined and allocated by user
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
// signed or unsigned, byte per sample
bool signed_samples = bufinfo.typecode == 'b' || bufinfo.typecode == 'h';
uint8_t bytes_per_sample = 1;
// Bytes Per Sample
if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') {
bytes_per_sample = 2;
} else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) {
mp_raise_ValueError(translate("sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or 'B'"));
}
// Validate sample rate here
mp_float_t sample_rate = mp_obj_get_float(args[ARG_sample_rate].u_obj);
validate_rate(sample_rate);
// Create local object
analogio_analogfastin_obj_t *self = m_new_obj(analogio_analogfastin_obj_t);
self->base.type = &analogio_analogfastin_type;
// Call local intereface in ports/common-hal/analogio
common_hal_analogio_analogfastin_construct(self,
pin,
((uint8_t *)bufinfo.buf),
bufinfo.len,
bytes_per_sample,
signed_samples,
sample_rate
);
return MP_OBJ_FROM_PTR(self);
}
//| def deinit(self) -> None:
//| """Turn off the AnalogFastIn and release the pin for other use."""
//| ...
//|
STATIC mp_obj_t analogio_analogfastin_deinit(mp_obj_t self_in) {
analogio_analogfastin_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_analogio_analogfastin_deinit(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogfastin_deinit_obj, analogio_analogfastin_deinit);
STATIC void check_for_deinit(analogio_analogfastin_obj_t *self) {
if (common_hal_analogio_analogfastin_deinited(self)) {
raise_deinited_error();
}
}
//| def __enter__(self) -> AnalogFastIn:
//| """No-op used by Context Managers."""
//| ...
//|
// Provided by context manager helper.
//| def __exit__(self) -> None:
//| """Automatically deinitializes the hardware when exiting a context. See
//| :ref:`lifetime-and-contextmanagers` for more info."""
//| ...
//|
STATIC mp_obj_t analogio_analogfastin___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_analogio_analogfastin_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogfastin___exit___obj, 4, 4, analogio_analogfastin___exit__);
//| value: int
//| """The value on the analog pin between 0 and 65535 inclusive (16-bit). (read-only)
//|
//| Even if the underlying analog to digital converter (ADC) is lower
//| resolution, the value is 16-bit."""
//|
STATIC mp_obj_t analogio_analogfastin_obj_capture(mp_obj_t self_in) {
analogio_analogfastin_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_analogio_analogfastin_capture(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogfastin_capture_obj, analogio_analogfastin_obj_capture);
// MP_PROPERTY_GETTER(analogio_analogfastin_value_obj,
// (mp_obj_t)&analogio_analogfastin_get_value_obj);
//| reference_voltage: float
//| """The maximum voltage measurable (also known as the reference voltage) as a
//| `float` in Volts. Note the ADC value may not scale to the actual voltage linearly
//| at ends of the analog range."""
//|
STATIC const mp_rom_map_elem_t analogio_analogfastin_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogfastin_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogio_analogfastin___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_capture), MP_ROM_PTR(&analogio_analogfastin_capture_obj)},
};
STATIC MP_DEFINE_CONST_DICT(analogio_analogfastin_locals_dict, analogio_analogfastin_locals_dict_table);
const mp_obj_type_t analogio_analogfastin_type = {
{ &mp_type_type },
.name = MP_QSTR_AnalogFastIn,
.make_new = analogio_analogfastin_make_new,
.locals_dict = (mp_obj_t)&analogio_analogfastin_locals_dict,
};

View File

@ -34,10 +34,6 @@
#include "shared-bindings/analogio/AnalogIn.h"
#include "shared-bindings/analogio/AnalogOut.h"
#ifdef CIRCUITPY_ANALOGFASTIN
#include "shared-bindings/analogio/AnalogFastIn.h"
#endif
//| """Analog hardware support
//|
//| The `analogio` module contains classes to provide access to analog IO
@ -75,9 +71,6 @@ STATIC const mp_rom_map_elem_t analogio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_analogio) },
{ MP_ROM_QSTR(MP_QSTR_AnalogIn), MP_ROM_PTR(&analogio_analogin_type) },
{ MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&analogio_analogout_type) },
#ifdef CIRCUITPY_ANALOGFASTIN
{ MP_ROM_QSTR(MP_QSTR_AnalogFastIn), MP_ROM_PTR(&analogio_analogfastin_type) },
#endif
};
STATIC MP_DEFINE_CONST_DICT(analogio_module_globals, analogio_module_globals_table);