Skip to content

Commit 2d45429

Browse files
committed
Use memcpy instead of strncpy; add usart.status to stm.
1 parent 0d4cab1 commit 2d45429

4 files changed

Lines changed: 15 additions & 21 deletions

File tree

py/objstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ mp_obj_t str_strip(int n_args, const mp_obj_t *args) {
200200
size_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
201201
//+1 to accomodate '\0'
202202
char *stripped_str = m_new(char, stripped_len + 1);
203-
strncpy(stripped_str, orig_str + first_good_char_pos, stripped_len);
203+
memcpy(stripped_str, orig_str + first_good_char_pos, stripped_len);
204204
stripped_str[stripped_len] = '\0';
205205
return mp_obj_new_str(qstr_from_str_take(stripped_str, stripped_len + 1));
206206
}

stm/main.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,6 @@ static mp_obj_t pyb_standby(void) {
294294
return mp_const_none;
295295
}
296296

297-
mp_obj_t pyb_usart_send(mp_obj_t data) {
298-
usart_tx_char(mp_obj_get_int(data));
299-
return mp_const_none;
300-
}
301-
302-
mp_obj_t pyb_usart_receive(void) {
303-
return mp_obj_new_int(usart_rx_char());
304-
}
305-
306-
mp_obj_t pyb_usart_status(void) {
307-
if (usart_rx_any()) {
308-
return mp_const_true;
309-
} else {
310-
return mp_const_false;
311-
}
312-
}
313-
314297
char *strdup(const char *str) {
315298
uint32_t len = strlen(str);
316299
char *s2 = m_new(char, len + 1);
@@ -846,9 +829,6 @@ int main(void) {
846829
rt_store_attr(m, qstr_from_str_static("mma_mode"), (mp_obj_t)&pyb_mma_write_mode_obj);
847830
rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_1(pyb_hid_send_report));
848831
rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_0(pyb_rtc_read));
849-
rt_store_attr(m, qstr_from_str_static("uout"), rt_make_function_1(pyb_usart_send));
850-
rt_store_attr(m, qstr_from_str_static("uin"), rt_make_function_0(pyb_usart_receive));
851-
rt_store_attr(m, qstr_from_str_static("ustat"), rt_make_function_0(pyb_usart_status));
852832
rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_0(pyb_rng_get));
853833
rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_1(pyb_Led));
854834
rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_1(pyb_Servo));

stm/printf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,13 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) {
219219
void stdout_print_strn(void *data, const char *str, unsigned int len) {
220220
// send stdout to USART, USB CDC VCP, and LCD if nothing else
221221
bool any = false;
222+
223+
// TODO should have a setting for which USART port to send to
222224
if (usart_is_enabled()) {
223225
usart_tx_strn_cooked(str, len);
224226
any = true;
225227
}
228+
226229
if (usb_vcp_is_enabled()) {
227230
usb_vcp_send_strn_cooked(str, len);
228231
any = true;

stm/usart.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ typedef struct _pyb_usart_obj_t {
175175
bool is_enabled;
176176
} pyb_usart_obj_t;
177177

178+
static mp_obj_t usart_obj_status(mp_obj_t self_in) {
179+
// TODO make it check the correct USART port!
180+
if (usart_rx_any()) {
181+
return mp_const_true;
182+
} else {
183+
return mp_const_false;
184+
}
185+
}
186+
178187
static mp_obj_t usart_obj_rx_char(mp_obj_t self_in) {
179188
mp_obj_t ret = mp_const_none;
180189
pyb_usart_obj_t *self = self_in;
@@ -207,11 +216,13 @@ static void usart_obj_print(void (*print)(void *env, const char *fmt, ...), void
207216
print(env, "<Usart %lu>", self->usart_id);
208217
}
209218

219+
static MP_DEFINE_CONST_FUN_OBJ_1(usart_obj_status_obj, usart_obj_status);
210220
static MP_DEFINE_CONST_FUN_OBJ_1(usart_obj_rx_char_obj, usart_obj_rx_char);
211221
static MP_DEFINE_CONST_FUN_OBJ_2(usart_obj_tx_char_obj, usart_obj_tx_char);
212222
static MP_DEFINE_CONST_FUN_OBJ_2(usart_obj_tx_str_obj, usart_obj_tx_str);
213223

214224
static const mp_method_t usart_methods[] = {
225+
{ "status", &usart_obj_status_obj },
215226
{ "recv_chr", &usart_obj_rx_char_obj },
216227
{ "send_chr", &usart_obj_tx_char_obj },
217228
{ "send", &usart_obj_tx_str_obj },

0 commit comments

Comments
 (0)