2022-08-18 16:23:17 -04:00
|
|
|
/*
|
|
|
|
* 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"
|
2022-09-05 12:11:49 -04:00
|
|
|
#include "shared-bindings/analogbufio/BufferedIn.h"
|
2022-08-18 16:23:17 -04:00
|
|
|
#include "shared-bindings/util.h"
|
|
|
|
|
2022-09-05 12:11:49 -04:00
|
|
|
//| class BufferedIn:
|
2022-09-04 15:57:59 -04:00
|
|
|
//| """Capture multiple analog voltage levels to the supplied buffer"""
|
2022-08-24 17:41:51 -04:00
|
|
|
//|
|
|
|
|
//| def __init__(self, pin: microcontroller.Pin, buffer: WriteableBuffer, *, sample_rate: int = 500000) -> None:
|
2022-09-05 12:11:49 -04:00
|
|
|
//| """Create a `BufferedIn` on the given pin. ADC values will be read
|
2022-09-04 15:57:59 -04:00
|
|
|
//| into the given buffer at the supplied sample_rate. Depending on the
|
|
|
|
//| buffer typecode, 'b', 'B', 'h', 'H', samples are 8-bit byte-arrays or
|
|
|
|
//| 16-bit half-words and are signed or unsigned.
|
|
|
|
//| The ADC most significant bits of the ADC are kept. Please see:
|
|
|
|
//| `https://docs.circuitpython.org/en/latest/docs/library/array.html`
|
2022-08-24 17:41:51 -04:00
|
|
|
//|
|
2022-08-25 17:09:50 -04:00
|
|
|
//| :param ~microcontroller.Pin pin: the pin to read from
|
2022-08-24 17:41:51 -04:00
|
|
|
//| :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples
|
2022-09-04 15:57:59 -04:00
|
|
|
//| :param ~int sample_rate: rate: sampling frequency, in samples per second
|
2022-08-24 17:41:51 -04:00
|
|
|
//|
|
|
|
|
//| Usage::
|
|
|
|
//|
|
|
|
|
//| import board
|
2022-09-05 12:11:49 -04:00
|
|
|
//| import analogbufio
|
2022-08-24 17:41:51 -04:00
|
|
|
//| import array
|
|
|
|
//|
|
|
|
|
//| length = 1000
|
2022-09-05 12:11:49 -04:00
|
|
|
//| mybuffer = array.array("H", 0x0000 for i in range(length))
|
2022-08-24 17:41:51 -04:00
|
|
|
//| rate = 500000
|
2022-09-05 12:11:49 -04:00
|
|
|
//| adcbuf = analogbufio.BufferedIn(board.GP26, mybuffer, rate)
|
2022-09-04 15:57:59 -04:00
|
|
|
//| adcbuf.read()
|
2022-08-24 17:41:51 -04:00
|
|
|
//| adcbuf.deinit()
|
|
|
|
//| for i in range(length):
|
|
|
|
//| print(i, mybuffer[i])
|
|
|
|
//|
|
2022-08-25 11:57:07 -04:00
|
|
|
//| (TODO) The reference voltage varies by platform so use
|
|
|
|
//| ``reference_voltage`` to read the configured setting.
|
|
|
|
//| (TODO) Provide mechanism to read CPU Temperature."""
|
|
|
|
//| ...
|
2022-08-24 17:41:51 -04:00
|
|
|
//|
|
2022-09-05 12:11:49 -04:00
|
|
|
STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
2022-08-18 16:23:17 -04:00
|
|
|
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 },
|
2022-08-21 11:44:40 -04:00
|
|
|
{ MP_QSTR_sample_rate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 500000} },
|
2022-08-18 16:23:17 -04:00
|
|
|
};
|
|
|
|
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) {
|
2022-09-04 15:57:59 -04:00
|
|
|
mp_raise_ValueError_varg(translate("%q must`` be a bytearray or array of type 'h', 'H', 'b' or 'B'"), MP_QSTR_buffer);
|
2022-08-18 16:23:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate sample rate here
|
2022-08-21 11:44:40 -04:00
|
|
|
uint32_t sample_rate = (uint32_t)mp_arg_validate_int_range(args[ARG_sample_rate].u_int, 1, 500000, MP_QSTR_sample_rate);
|
2022-08-18 16:23:17 -04:00
|
|
|
|
|
|
|
// Create local object
|
2022-09-05 12:11:49 -04:00
|
|
|
analogbufio_bufferedin_obj_t *self = m_new_obj(analogbufio_bufferedin_obj_t);
|
|
|
|
self->base.type = &analogbufio_bufferedin_type;
|
2022-08-18 16:23:17 -04:00
|
|
|
|
2022-09-05 12:11:49 -04:00
|
|
|
// Call local intereface in ports/common-hal/analogbufio
|
|
|
|
common_hal_analogbufio_bufferedin_construct(self,
|
2022-08-18 16:23:17 -04:00
|
|
|
pin,
|
|
|
|
((uint8_t *)bufinfo.buf),
|
|
|
|
bufinfo.len,
|
|
|
|
bytes_per_sample,
|
|
|
|
signed_samples,
|
|
|
|
sample_rate
|
|
|
|
);
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2022-08-24 17:41:51 -04:00
|
|
|
//| def deinit(self) -> None:
|
2022-09-05 12:11:49 -04:00
|
|
|
//| """Shut down the `BufferedIn` and release the pin for other use."""
|
2022-08-24 17:41:51 -04:00
|
|
|
//| ...
|
|
|
|
//|
|
2022-09-05 12:11:49 -04:00
|
|
|
STATIC mp_obj_t analogbufio_bufferedin_deinit(mp_obj_t self_in) {
|
|
|
|
analogbufio_bufferedin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_analogbufio_bufferedin_deinit(self);
|
2022-08-18 16:23:17 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-09-05 12:11:49 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(analogbufio_bufferedin_deinit_obj, analogbufio_bufferedin_deinit);
|
2022-08-18 16:23:17 -04:00
|
|
|
|
2022-09-05 12:11:49 -04:00
|
|
|
STATIC void check_for_deinit(analogbufio_bufferedin_obj_t *self) {
|
|
|
|
if (common_hal_analogbufio_bufferedin_deinited(self)) {
|
2022-08-18 16:23:17 -04:00
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
}
|
2022-08-24 17:41:51 -04:00
|
|
|
|
|
|
|
//| 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."""
|
|
|
|
//| ...
|
|
|
|
//|
|
2022-09-05 12:11:49 -04:00
|
|
|
STATIC mp_obj_t analogbufio_bufferedin___exit__(size_t n_args, const mp_obj_t *args) {
|
2022-08-18 16:23:17 -04:00
|
|
|
(void)n_args;
|
2022-09-05 12:11:49 -04:00
|
|
|
common_hal_analogbufio_bufferedin_deinit(args[0]);
|
2022-08-18 16:23:17 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-09-05 12:11:49 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogbufio_bufferedin___exit___obj, 4, 4, analogbufio_bufferedin___exit__);
|
2022-08-18 16:23:17 -04:00
|
|
|
|
2022-09-04 15:57:59 -04:00
|
|
|
//|
|
|
|
|
//| def read(self) -> None:
|
|
|
|
//| """Fills the provided buffer with ADC voltage values."""
|
2022-08-24 17:41:51 -04:00
|
|
|
//|
|
2022-09-05 12:11:49 -04:00
|
|
|
STATIC mp_obj_t analogbufio_bufferedin_obj_read(mp_obj_t self_in) {
|
|
|
|
analogbufio_bufferedin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2022-08-18 16:23:17 -04:00
|
|
|
check_for_deinit(self);
|
2022-09-05 12:11:49 -04:00
|
|
|
common_hal_analogbufio_bufferedin_read(self);
|
2022-08-27 14:47:12 -04:00
|
|
|
return mp_const_none;
|
2022-08-18 16:23:17 -04:00
|
|
|
}
|
2022-09-05 12:11:49 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(analogbufio_bufferedin_read_obj, analogbufio_bufferedin_obj_read);
|
2022-08-18 16:23:17 -04:00
|
|
|
|
2022-09-05 12:11:49 -04:00
|
|
|
STATIC const mp_rom_map_elem_t analogbufio_bufferedin_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogbufio_bufferedin_deinit_obj) },
|
2022-09-04 15:57:59 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
2022-09-05 12:11:49 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogbufio_bufferedin___exit___obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&analogbufio_bufferedin_read_obj)},
|
2022-08-18 16:23:17 -04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2022-09-05 12:11:49 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(analogbufio_bufferedin_locals_dict, analogbufio_bufferedin_locals_dict_table);
|
2022-08-18 16:23:17 -04:00
|
|
|
|
2022-09-05 12:11:49 -04:00
|
|
|
const mp_obj_type_t analogbufio_bufferedin_type = {
|
2022-08-18 16:23:17 -04:00
|
|
|
{ &mp_type_type },
|
2022-09-05 12:11:49 -04:00
|
|
|
.name = MP_QSTR_BufferedIn,
|
|
|
|
.make_new = analogbufio_bufferedin_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&analogbufio_bufferedin_locals_dict,
|
2022-08-18 16:23:17 -04:00
|
|
|
};
|