forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferedIn.c
More file actions
154 lines (141 loc) · 6.58 KB
/
Copy pathBufferedIn.c
File metadata and controls
154 lines (141 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2022 Lee Atkinson, MeanStride Technology, Inc.
//
// SPDX-License-Identifier: MIT
#include <string.h>
#include "shared/runtime/context_manager_helpers.h"
#include "py/binary.h"
#include "py/mphal.h"
#include "py/nlr.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/analogbufio/BufferedIn.h"
#include "shared-bindings/util.h"
//| class BufferedIn:
//| """Capture multiple analog voltage levels to the supplied buffer
//|
//| Usage::
//|
//| import board
//| import analogbufio
//| import array
//|
//| length = 1000
//| mybuffer = array.array("H", [0x0000] * length)
//| rate = 500000
//| adcbuf = analogbufio.BufferedIn(board.GP26, sample_rate=rate)
//| adcbuf.readinto(mybuffer)
//| 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."""
//|
//| def __init__(self, pin: microcontroller.Pin, *, sample_rate: int) -> None:
//| """Create a `BufferedIn` on the given pin and given sample rate.
//|
//| :param ~microcontroller.Pin pin: the pin to read from
//| :param ~int sample_rate: rate: sampling frequency, in samples per second"""
//| ...
//|
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) {
enum { ARG_pin, ARG_sample_rate };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_pin, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_sample_rate, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
};
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, MP_QSTR_pin);
// Create local object
analogbufio_bufferedin_obj_t *self = mp_obj_malloc_with_finaliser(analogbufio_bufferedin_obj_t, &analogbufio_bufferedin_type);
// Call local interface in ports/common-hal/analogbufio
common_hal_analogbufio_bufferedin_construct(self, pin, args[ARG_sample_rate].u_int);
return MP_OBJ_FROM_PTR(self);
}
//| def deinit(self) -> None:
//| """Shut down the `BufferedIn` and release the pin for other use."""
//| ...
//|
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);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(analogbufio_bufferedin_deinit_obj, analogbufio_bufferedin_deinit);
static void check_for_deinit(analogbufio_bufferedin_obj_t *self) {
if (common_hal_analogbufio_bufferedin_deinited(self)) {
raise_deinited_error();
}
}
//| def __enter__(self) -> BufferedIn:
//| """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."""
//| ...
//|
// Provided by context manager helper.
//| def readinto(self, buffer: WriteableBuffer, loop: bool = False) -> int:
//| """Fills the provided buffer with ADC voltage values.
//|
//| ADC values will be read into the given buffer at the supplied sample_rate.
//| Depending on the buffer typecode, 'B', 'H', samples are 8-bit byte-arrays or
//| 16-bit half-words and are always unsigned.
//| (See https://docs.circuitpython.org/en/latest/docs/library/array.html)
//| For 8-bit samples, the most significant bits of the 12-bit ADC values are kept.
//| For 16-bit samples, if loop=False, the 12-bit ADC values are scaled up to fill the 16 bit range.
//| If loop=True, ADC values are stored without scaling.
//|
//| :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples
//| :param ~bool loop: loop: Set to true for continuous conversions, False to fill buffer once then stop
//| """
//| ...
//|
//|
static mp_obj_t analogbufio_bufferedin_obj_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_buffer, ARG_loop };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED, {} },
{ MP_QSTR_loop, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
};
analogbufio_bufferedin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
check_for_deinit(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_obj_t buffer = args[ARG_buffer].u_obj;
// Buffer defined and allocated by user
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_READ);
uint8_t bytes_per_sample = 1;
if (bufinfo.typecode == 'H') {
bytes_per_sample = 2;
} else if (bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) {
mp_raise_ValueError_varg(MP_ERROR_TEXT("%q must be a bytearray or array of type 'H' or 'B'"), MP_QSTR_buffer);
}
mp_uint_t captured = common_hal_analogbufio_bufferedin_readinto(self, bufinfo.buf, bufinfo.len, bytes_per_sample, args[ARG_loop].u_bool);
return MP_OBJ_NEW_SMALL_INT(captured);
}
MP_DEFINE_CONST_FUN_OBJ_KW(analogbufio_bufferedin_readinto_obj, 1, analogbufio_bufferedin_obj_readinto);
static const mp_rom_map_elem_t analogbufio_bufferedin_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&analogbufio_bufferedin_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogbufio_bufferedin_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&analogbufio_bufferedin_readinto_obj)},
};
static MP_DEFINE_CONST_DICT(analogbufio_bufferedin_locals_dict, analogbufio_bufferedin_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
analogbufio_bufferedin_type,
MP_QSTR_BufferedIn,
MP_TYPE_FLAG_NONE,
make_new, analogbufio_bufferedin_make_new,
locals_dict, &analogbufio_bufferedin_locals_dict
);