41
41
//| def __init__(self) -> None:
42
42
//| """You cannot create an instance of `usb_cdc.Serial`.
43
43
//|
44
- //| Serial objects are constructed for every corresponding entry in the USB
44
+ //| Serial objects are pre- constructed for each CDC device in the USB
45
45
//| descriptor and added to the ``usb_cdc.ports`` tuple."""
46
46
//| ...
47
47
//|
48
48
49
- // These are standard stream methods. Code is in py/stream.c.
50
- //
51
- //| def read(self, nbytes: Optional[int] = None) -> Optional[bytes]:
52
- //| """Read characters. If ``nbytes`` is specified then read at most that many
53
- //| bytes. Otherwise, read everything that arrives until the connection
54
- //| times out. Providing the number of bytes expected is highly recommended
55
- //| because it will be faster.
49
+ //| def read(self, size: int = 1) -> bytes:
50
+ //| """Read at most ``size`` bytes. If ``size`` exceeds the internal buffer size
51
+ //| only the bytes in the buffer will be read. If `timeout` is > 0 or ``None``,
52
+ //| and fewer than ``size`` bytes are available, keep waiting until the timeout
53
+ //| expires or ``size`` bytes are available.
56
54
//|
57
55
//| :return: Data read
58
- //| :rtype: bytes or None """
56
+ //| :rtype: bytes"""
59
57
//| ...
60
58
//|
61
- //| def readinto(self, buf: WriteableBuffer, nbytes: Optional[int] = None ) -> Optional[ bytes] :
59
+ //| def readinto(self, buf: WriteableBuffer) -> bytes:
62
60
//| """Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
63
- //| that many bytes. Otherwise, read at most ``len(buf)`` bytes.
61
+ //| that many bytes, subject to `timeout` . Otherwise, read at most ``len(buf)`` bytes.
64
62
//|
65
63
//| :return: number of bytes read and stored into ``buf``
66
- //| :rtype: bytes or None """
64
+ //| :rtype: bytes"""
67
65
//| ...
68
66
//|
69
- //| def write(self, buf: ReadableBuffer) -> Optional[ int] :
67
+ //| def write(self, buf: ReadableBuffer) -> int:
70
68
//| """Write as many bytes as possible from the buffer of bytes.
71
69
//|
72
70
//| :return: the number of bytes written
73
- //| :rtype: int or None """
71
+ //| :rtype: int"""
74
72
//| ...
75
73
//|
76
74
//| def flush(self) -> None:
79
77
//|
80
78
81
79
// These three methods are used by the shared stream methods.
82
- STATIC mp_uint_t usb_cdc_serial_read (mp_obj_t self_in , void * buf_in , mp_uint_t size , int * errcode ) {
80
+ STATIC mp_uint_t usb_cdc_serial_read_stream (mp_obj_t self_in , void * buf_in , mp_uint_t size , int * errcode ) {
83
81
usb_cdc_serial_obj_t * self = MP_OBJ_TO_PTR (self_in );
84
82
byte * buf = buf_in ;
85
83
@@ -91,16 +89,16 @@ STATIC mp_uint_t usb_cdc_serial_read(mp_obj_t self_in, void *buf_in, mp_uint_t s
91
89
return common_hal_usb_cdc_serial_read (self , buf , size , errcode );
92
90
}
93
91
94
- STATIC mp_uint_t usb_cdc_serial_write (mp_obj_t self_in , const void * buf_in , mp_uint_t size , int * errcode ) {
92
+ STATIC mp_uint_t usb_cdc_serial_write_stream (mp_obj_t self_in , const void * buf_in , mp_uint_t size , int * errcode ) {
95
93
usb_cdc_serial_obj_t * self = MP_OBJ_TO_PTR (self_in );
96
94
const byte * buf = buf_in ;
97
95
98
96
return common_hal_usb_cdc_serial_write (self , buf , size , errcode );
99
97
}
100
98
101
- STATIC mp_uint_t usb_cdc_serial_ioctl (mp_obj_t self_in , mp_uint_t request , mp_uint_t arg , int * errcode ) {
99
+ STATIC mp_uint_t usb_cdc_serial_ioctl_stream (mp_obj_t self_in , mp_uint_t request , mp_uint_t arg , int * errcode ) {
102
100
usb_cdc_serial_obj_t * self = MP_OBJ_TO_PTR (self_in );
103
- mp_uint_t ret ;
101
+ mp_uint_t ret = 0 ;
104
102
switch (request ) {
105
103
case MP_IOCTL_POLL : {
106
104
mp_uint_t flags = arg ;
@@ -134,7 +132,7 @@ STATIC mp_obj_t usb_cdc_serial_get_connected(mp_obj_t self_in) {
134
132
}
135
133
MP_DEFINE_CONST_FUN_OBJ_1 (usb_cdc_serial_get_connected_obj , usb_cdc_serial_get_connected );
136
134
137
- const mp_obj_property_t usb_cdc_serial__connected_obj = {
135
+ const mp_obj_property_t usb_cdc_serial_connected_obj = {
138
136
.base .type = & mp_type_property ,
139
137
.proxy = {(mp_obj_t )& usb_cdc_serial_get_connected_obj ,
140
138
(mp_obj_t )& mp_const_none_obj ,
@@ -195,6 +193,32 @@ STATIC mp_obj_t usb_cdc_serial_reset_output_buffer(mp_obj_t self_in) {
195
193
}
196
194
MP_DEFINE_CONST_FUN_OBJ_1 (usb_cdc_serial_reset_output_buffer_obj , usb_cdc_serial_reset_output_buffer );
197
195
196
+ //| timeout: Optional[float]
197
+ //| """The initial value of `timeout` is ``None``. If ``None``, wait indefinitely to satisfy
198
+ //| the conditions of a read operation. If 0, do not wait. If > 0, wait only ``timeout`` seconds."""
199
+ //|
200
+ STATIC mp_obj_t usb_cdc_serial_get_timeout (mp_obj_t self_in ) {
201
+ usb_cdc_serial_obj_t * self = MP_OBJ_TO_PTR (self_in );
202
+ mp_float_t timeout = common_hal_usb_cdc_serial_get_timeout (self );
203
+ return (timeout < 0.0f ) ? mp_const_none : mp_obj_new_float (self -> timeout );
204
+ }
205
+ MP_DEFINE_CONST_FUN_OBJ_1 (usb_cdc_serial_get_timeout_obj , usb_cdc_serial_get_timeout );
206
+
207
+ STATIC mp_obj_t usb_cdc_serial_set_timeout (mp_obj_t self_in , mp_obj_t timeout_in ) {
208
+ usb_cdc_serial_obj_t * self = MP_OBJ_TO_PTR (self_in );
209
+ common_hal_usb_cdc_serial_set_timeout (self ,
210
+ timeout_in == mp_const_none ? -1.0f : mp_obj_get_float (timeout_in ));
211
+ return mp_const_none ;
212
+ }
213
+ MP_DEFINE_CONST_FUN_OBJ_2 (usb_cdc_serial_set_timeout_obj , usb_cdc_serial_set_timeout );
214
+
215
+ const mp_obj_property_t usb_cdc_serial_timeout_obj = {
216
+ .base .type = & mp_type_property ,
217
+ .proxy = {(mp_obj_t )& usb_cdc_serial_get_timeout_obj ,
218
+ (mp_obj_t )& usb_cdc_serial_set_timeout_obj ,
219
+ (mp_obj_t )& mp_const_none_obj },
220
+ };
221
+
198
222
199
223
STATIC const mp_rom_map_elem_t usb_cdc_serial_locals_dict_table [] = {
200
224
// Standard stream methods.
@@ -210,9 +234,10 @@ STATIC const mp_rom_map_elem_t usb_cdc_serial_locals_dict_table[] = {
210
234
{ MP_OBJ_NEW_QSTR (MP_QSTR_out_waiting ), MP_ROM_PTR (& usb_cdc_serial_out_waiting_obj ) },
211
235
{ MP_OBJ_NEW_QSTR (MP_QSTR_reset_input_buffer ), MP_ROM_PTR (& usb_cdc_serial_reset_input_buffer_obj ) },
212
236
{ MP_OBJ_NEW_QSTR (MP_QSTR_reset_output_buffer ), MP_ROM_PTR (& usb_cdc_serial_reset_output_buffer_obj ) },
237
+ { MP_OBJ_NEW_QSTR (MP_QSTR_timeout ), MP_ROM_PTR (& usb_cdc_serial_timeout_obj ) },
213
238
214
239
// Not in pyserial protocol.
215
- { MP_OBJ_NEW_QSTR (MP_QSTR_connected ), MP_ROM_PTR (& usb_cdc_serial_get_connected_obj ) },
240
+ { MP_OBJ_NEW_QSTR (MP_QSTR_connected ), MP_ROM_PTR (& usb_cdc_serial_connected_obj ) },
216
241
217
242
218
243
@@ -221,10 +246,13 @@ STATIC MP_DEFINE_CONST_DICT(usb_cdc_serial_locals_dict, usb_cdc_serial_locals_di
221
246
222
247
STATIC const mp_stream_p_t usb_cdc_serial_stream_p = {
223
248
MP_PROTO_IMPLEMENT (MP_QSTR_protocol_stream )
224
- .read = usb_cdc_serial_read ,
225
- .write = usb_cdc_serial_write ,
226
- .ioctl = usb_cdc_serial_ioctl ,
249
+ .read = usb_cdc_serial_read_stream ,
250
+ .write = usb_cdc_serial_write_stream ,
251
+ .ioctl = usb_cdc_serial_ioctl_stream ,
227
252
.is_text = false,
253
+ .pyserial_read_compatibility = true,
254
+ .pyserial_readinto_compatibility = true,
255
+ .pyserial_dont_return_none_compatibility = true,
228
256
};
229
257
230
258
const mp_obj_type_t usb_cdc_serial_type = {
0 commit comments