67
67
//| ...
68
68
//|
69
69
//| def write(self, buf: ReadableBuffer) -> Optional[int]:
70
- //| """Write the buffer of bytes to the bus .
70
+ //| """Write as many bytes as possible from the buffer of bytes .
71
71
//|
72
72
//| :return: the number of bytes written
73
73
//| :rtype: int or None"""
74
74
//| ...
75
75
//|
76
+ //| def flush(self) -> None:
77
+ //| """Force out any unwritten bytes, waiting until they are written."""
78
+ //| ...
79
+ //|
76
80
77
81
// These three methods are used by the shared stream methods.
78
82
STATIC mp_uint_t usb_cdc_serial_read (mp_obj_t self_in , void * buf_in , mp_uint_t size , int * errcode ) {
@@ -97,27 +101,121 @@ STATIC mp_uint_t usb_cdc_serial_write(mp_obj_t self_in, const void *buf_in, mp_u
97
101
STATIC mp_uint_t usb_cdc_serial_ioctl (mp_obj_t self_in , mp_uint_t request , mp_uint_t arg , int * errcode ) {
98
102
usb_cdc_serial_obj_t * self = MP_OBJ_TO_PTR (self_in );
99
103
mp_uint_t ret ;
100
- if (request == MP_IOCTL_POLL ) {
101
- mp_uint_t flags = arg ;
102
- ret = 0 ;
103
- if ((flags & MP_IOCTL_POLL_RD ) && common_hal_usb_cdc_serial_bytes_available (self ) > 0 ) {
104
- ret |= MP_IOCTL_POLL_RD ;
105
- }
106
- if ((flags & MP_IOCTL_POLL_WR ) && common_hal_usb_cdc_serial_ready_to_tx (self )) {
107
- ret |= MP_IOCTL_POLL_WR ;
104
+ switch (request ) {
105
+ case MP_IOCTL_POLL : {
106
+ mp_uint_t flags = arg ;
107
+ ret = 0 ;
108
+ if ((flags & MP_IOCTL_POLL_RD ) && common_hal_usb_cdc_serial_get_in_waiting (self ) > 0 ) {
109
+ ret |= MP_IOCTL_POLL_RD ;
110
+ }
111
+ if ((flags & MP_IOCTL_POLL_WR ) && common_hal_usb_cdc_serial_get_out_waiting (self ) == 0 ) {
112
+ ret |= MP_IOCTL_POLL_WR ;
113
+ }
114
+ break ;
108
115
}
109
- } else {
110
- * errcode = MP_EINVAL ;
111
- ret = MP_STREAM_ERROR ;
116
+
117
+ case MP_STREAM_FLUSH :
118
+ common_hal_usb_cdc_serial_flush (self );
119
+ break ;
120
+
121
+ default :
122
+ * errcode = MP_EINVAL ;
123
+ ret = MP_STREAM_ERROR ;
112
124
}
113
125
return ret ;
114
126
}
115
127
128
+ //| connected: bool
129
+ //| """True if this Serial is connected to a host. (read-only)"""
130
+ //|
131
+ STATIC mp_obj_t usb_cdc_serial_get_connected (mp_obj_t self_in ) {
132
+ usb_cdc_serial_obj_t * self = MP_OBJ_TO_PTR (self_in );
133
+ return mp_obj_new_bool (common_hal_usb_cdc_serial_get_connected (self ));
134
+ }
135
+ MP_DEFINE_CONST_FUN_OBJ_1 (usb_cdc_serial_get_connected_obj , usb_cdc_serial_get_connected );
136
+
137
+ const mp_obj_property_t usb_cdc_serial__connected_obj = {
138
+ .base .type = & mp_type_property ,
139
+ .proxy = {(mp_obj_t )& usb_cdc_serial_get_connected_obj ,
140
+ (mp_obj_t )& mp_const_none_obj ,
141
+ (mp_obj_t )& mp_const_none_obj },
142
+ };
143
+
144
+ //| in_waiting: int
145
+ //| """Returns the number of bytes waiting to be read on the USB serial input. (read-only)"""
146
+ //|
147
+ STATIC mp_obj_t usb_cdc_serial_get_in_waiting (mp_obj_t self_in ) {
148
+ usb_cdc_serial_obj_t * self = MP_OBJ_TO_PTR (self_in );
149
+ return mp_obj_new_int (common_hal_usb_cdc_serial_get_in_waiting (self ));
150
+ }
151
+ MP_DEFINE_CONST_FUN_OBJ_1 (usb_cdc_serial_get_in_waiting_obj , usb_cdc_serial_get_in_waiting );
152
+
153
+ const mp_obj_property_t usb_cdc_serial_in_waiting_obj = {
154
+ .base .type = & mp_type_property ,
155
+ .proxy = {(mp_obj_t )& usb_cdc_serial_get_in_waiting_obj ,
156
+ (mp_obj_t )& mp_const_none_obj ,
157
+ (mp_obj_t )& mp_const_none_obj },
158
+ };
159
+
160
+ //| out_waiting: int
161
+ //| """Returns the number of bytes waiting to be written on the USB serial output. (read-only)"""
162
+ //|
163
+ STATIC mp_obj_t usb_cdc_serial_get_out_waiting (mp_obj_t self_in ) {
164
+ usb_cdc_serial_obj_t * self = MP_OBJ_TO_PTR (self_in );
165
+ return mp_obj_new_int (common_hal_usb_cdc_serial_get_out_waiting (self ));
166
+ }
167
+ MP_DEFINE_CONST_FUN_OBJ_1 (usb_cdc_serial_get_out_waiting_obj , usb_cdc_serial_get_out_waiting );
168
+
169
+ const mp_obj_property_t usb_cdc_serial_out_waiting_obj = {
170
+ .base .type = & mp_type_property ,
171
+ .proxy = {(mp_obj_t )& usb_cdc_serial_get_out_waiting_obj ,
172
+ (mp_obj_t )& mp_const_none_obj ,
173
+ (mp_obj_t )& mp_const_none_obj },
174
+ };
175
+
176
+ //| def reset_input_buffer(self) -> None:
177
+ //| """Clears any unread bytes."""
178
+ //| ...
179
+ //|
180
+ STATIC mp_obj_t usb_cdc_serial_reset_input_buffer (mp_obj_t self_in ) {
181
+ usb_cdc_serial_obj_t * self = MP_OBJ_TO_PTR (self_in );
182
+ common_hal_usb_cdc_serial_reset_input_buffer (self );
183
+ return mp_const_none ;
184
+ }
185
+ MP_DEFINE_CONST_FUN_OBJ_1 (usb_cdc_serial_reset_input_buffer_obj , usb_cdc_serial_reset_input_buffer );
186
+
187
+ //| def reset_output_buffer(self) -> None:
188
+ //| """Clears any unwritten bytes."""
189
+ //| ...
190
+ //|
191
+ STATIC mp_obj_t usb_cdc_serial_reset_output_buffer (mp_obj_t self_in ) {
192
+ usb_cdc_serial_obj_t * self = MP_OBJ_TO_PTR (self_in );
193
+ common_hal_usb_cdc_serial_reset_output_buffer (self );
194
+ return mp_const_none ;
195
+ }
196
+ MP_DEFINE_CONST_FUN_OBJ_1 (usb_cdc_serial_reset_output_buffer_obj , usb_cdc_serial_reset_output_buffer );
197
+
198
+
116
199
STATIC const mp_rom_map_elem_t usb_cdc_serial_locals_dict_table [] = {
117
200
// Standard stream methods.
201
+ { MP_ROM_QSTR (MP_QSTR_flush ), MP_ROM_PTR (& mp_stream_flush_obj ) },
118
202
{ MP_OBJ_NEW_QSTR (MP_QSTR_read ), MP_ROM_PTR (& mp_stream_read_obj ) },
119
203
{ MP_OBJ_NEW_QSTR (MP_QSTR_readinto ), MP_ROM_PTR (& mp_stream_readinto_obj ) },
204
+ { MP_ROM_QSTR (MP_QSTR_readline ), MP_ROM_PTR (& mp_stream_unbuffered_readline_obj )},
205
+ { MP_ROM_QSTR (MP_QSTR_readlines ), MP_ROM_PTR (& mp_stream_unbuffered_readlines_obj )},
120
206
{ MP_OBJ_NEW_QSTR (MP_QSTR_write ), MP_ROM_PTR (& mp_stream_write_obj ) },
207
+
208
+ // Other pyserial-inspired attributes.
209
+ { MP_OBJ_NEW_QSTR (MP_QSTR_in_waiting ), MP_ROM_PTR (& usb_cdc_serial_in_waiting_obj ) },
210
+ { MP_OBJ_NEW_QSTR (MP_QSTR_out_waiting ), MP_ROM_PTR (& usb_cdc_serial_out_waiting_obj ) },
211
+ { MP_OBJ_NEW_QSTR (MP_QSTR_reset_input_buffer ), MP_ROM_PTR (& usb_cdc_serial_reset_input_buffer_obj ) },
212
+ { MP_OBJ_NEW_QSTR (MP_QSTR_reset_output_buffer ), MP_ROM_PTR (& usb_cdc_serial_reset_output_buffer_obj ) },
213
+
214
+ // Not in pyserial protocol.
215
+ { MP_OBJ_NEW_QSTR (MP_QSTR_connected ), MP_ROM_PTR (& usb_cdc_serial_get_connected_obj ) },
216
+
217
+
218
+
121
219
};
122
220
STATIC MP_DEFINE_CONST_DICT (usb_cdc_serial_locals_dict , usb_cdc_serial_locals_dict_table );
123
221
0 commit comments