38
38
#include "shared-bindings/util.h"
39
39
40
40
//| class BufferedIn:
41
- //| """Capture multiple analog voltage levels to the supplied buffer"""
41
+ //| """Capture multiple analog voltage levels to the supplied buffer
42
42
//|
43
+ //| Usage::
44
+ //|
45
+ //| import board
46
+ //| import analogbufio
47
+ //| import array
48
+ //|
49
+ //| length = 1000
50
+ //| mybuffer = array.array("H", 0x0000 for i in range(length))
51
+ //| rate = 500000
52
+ //| adcbuf = analogbufio.BufferedIn(board.GP26, mybuffer, rate)
53
+ //| adcbuf.read()
54
+ //| adcbuf.deinit()
55
+ //| for i in range(length):
56
+ //| print(i, mybuffer[i])
57
+ //|
58
+ //| (TODO) The reference voltage varies by platform so use
59
+ //| ``reference_voltage`` to read the configured setting.
60
+ //| (TODO) Provide mechanism to read CPU Temperature."""
61
+ //|
62
+
43
63
//| def __init__(self, pin: microcontroller.Pin, buffer: WriteableBuffer, *, sample_rate: int = 500000) -> None:
44
64
//| """Create a `BufferedIn` on the given pin. ADC values will be read
45
65
//| into the given buffer at the supplied sample_rate. Depending on the
46
66
//| buffer typecode, 'b', 'B', 'h', 'H', samples are 8-bit byte-arrays or
47
67
//| 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`
68
+ //| The ADC most significant bits of the ADC are kept. (See
69
+ //| https://docs.circuitpython.org/en/latest/docs/library/array.html)
50
70
//|
51
71
//| :param ~microcontroller.Pin pin: the pin to read from
52
72
//| :param ~circuitpython_typing.WriteableBuffer buffer: buffer: A buffer for samples
53
- //| :param ~int sample_rate: rate: sampling frequency, in samples per second
54
- //|
55
- //| Usage::
56
- //|
57
- //| import board
58
- //| import analogbufio
59
- //| import array
60
- //|
61
- //| length = 1000
62
- //| mybuffer = array.array("H", 0x0000 for i in range(length))
63
- //| rate = 500000
64
- //| adcbuf = analogbufio.BufferedIn(board.GP26, mybuffer, rate)
65
- //| adcbuf.read()
66
- //| adcbuf.deinit()
67
- //| for i in range(length):
68
- //| print(i, mybuffer[i])
69
- //|
70
- //| (TODO) The reference voltage varies by platform so use
71
- //| ``reference_voltage`` to read the configured setting.
72
- //| (TODO) Provide mechanism to read CPU Temperature."""
73
- //| ...
73
+ //| :param ~int sample_rate: rate: sampling frequency, in samples per second"""
74
+ //| ...
74
75
//|
75
76
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 ) {
76
77
enum { ARG_pin , ARG_buffer , ARG_sample_rate };
77
78
static const mp_arg_t allowed_args [] = {
78
- { MP_QSTR_pin , MP_ARG_OBJ | MP_ARG_REQUIRED },
79
- { MP_QSTR_buffer , MP_ARG_OBJ | MP_ARG_REQUIRED },
80
- { MP_QSTR_sample_rate , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 500000 } },
79
+ { MP_QSTR_pin , MP_ARG_OBJ | MP_ARG_REQUIRED },
80
+ { MP_QSTR_buffer , MP_ARG_OBJ | MP_ARG_REQUIRED },
81
+ { MP_QSTR_sample_rate , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 500000 } },
81
82
};
82
83
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
83
84
mp_arg_parse_all_kw_array (n_args , n_kw , all_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
@@ -95,7 +96,7 @@ STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_
95
96
96
97
// Bytes Per Sample
97
98
if (bufinfo .typecode == 'h' || bufinfo .typecode == 'H' ) {
98
- bytes_per_sample = 2 ;
99
+ bytes_per_sample = 2 ;
99
100
} else if (bufinfo .typecode != 'b' && bufinfo .typecode != 'B' && bufinfo .typecode != BYTEARRAY_TYPECODE ) {
100
101
mp_raise_ValueError_varg (translate ("%q must`` be a bytearray or array of type 'h', 'H', 'b' or 'B'" ), MP_QSTR_buffer );
101
102
}
@@ -109,13 +110,13 @@ STATIC mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_
109
110
110
111
// Call local intereface in ports/common-hal/analogbufio
111
112
common_hal_analogbufio_bufferedin_construct (self ,
112
- pin ,
113
- ((uint8_t * )bufinfo .buf ),
114
- bufinfo .len ,
115
- bytes_per_sample ,
116
- signed_samples ,
117
- sample_rate
118
- );
113
+ pin ,
114
+ ((uint8_t * )bufinfo .buf ),
115
+ bufinfo .len ,
116
+ bytes_per_sample ,
117
+ signed_samples ,
118
+ sample_rate
119
+ );
119
120
120
121
return MP_OBJ_FROM_PTR (self );
121
122
}
@@ -133,12 +134,15 @@ MP_DEFINE_CONST_FUN_OBJ_1(analogbufio_bufferedin_deinit_obj, analogbufio_buffere
133
134
134
135
STATIC void check_for_deinit (analogbufio_bufferedin_obj_t * self ) {
135
136
if (common_hal_analogbufio_bufferedin_deinited (self )) {
136
- raise_deinited_error ();
137
+ raise_deinited_error ();
137
138
}
138
139
}
139
-
140
- //| Provided by context manager helper.
140
+ //| def __enter__(self) -> AnalogIn:
141
+ //| """No-op used by Context Managers."""
142
+ //| ...
141
143
//|
144
+ // Provided by context manager helper.
145
+
142
146
//| def __exit__(self) -> None:
143
147
//| """Automatically deinitializes the hardware when exiting a context. See
144
148
//| :ref:`lifetime-and-contextmanagers` for more info."""
@@ -151,9 +155,9 @@ STATIC mp_obj_t analogbufio_bufferedin___exit__(size_t n_args, const mp_obj_t *a
151
155
}
152
156
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (analogbufio_bufferedin___exit___obj , 4 , 4 , analogbufio_bufferedin___exit__ );
153
157
154
- //|
155
158
//| def read(self) -> None:
156
159
//| """Fills the provided buffer with ADC voltage values."""
160
+ //| ...
157
161
//|
158
162
STATIC mp_obj_t analogbufio_bufferedin_obj_read (mp_obj_t self_in ) {
159
163
analogbufio_bufferedin_obj_t * self = MP_OBJ_TO_PTR (self_in );
0 commit comments