Skip to content

Commit 5c9453e

Browse files
committed
Merge branch 'master' into m5stack-dev
2 parents df336a2 + 8c5b384 commit 5c9453e

24 files changed

Lines changed: 261 additions & 89 deletions

MicroPython_BUILD/components/micropython/esp32/libs/ftp.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,10 +1018,20 @@ static void ftp_process_cmd (void) {
10181018
}
10191019
break;
10201020
case E_FTP_CMD_DELE:
1021+
ftp_get_param_and_open_child(&bufptr);
1022+
if ((strlen(ftp_path) > 0) && (ftp_path[strlen(ftp_path)-1] != '/')) {
1023+
if (unlink(ftp_path) == 0) {
1024+
vTaskDelay(20 / portTICK_PERIOD_MS);
1025+
ftp_send_reply(250, NULL);
1026+
}
1027+
else ftp_send_reply(550, NULL);
1028+
}
1029+
else ftp_send_reply(250, NULL);
1030+
break;
10211031
case E_FTP_CMD_RMD:
10221032
ftp_get_param_and_open_child(&bufptr);
10231033
if ((strlen(ftp_path) > 0) && (ftp_path[strlen(ftp_path)-1] != '/')) {
1024-
if (unlink(ftp_path) >= 0) {
1034+
if (rmdir(ftp_path) == 0) {
10251035
vTaskDelay(20 / portTICK_PERIOD_MS);
10261036
ftp_send_reply(250, NULL);
10271037
}

MicroPython_BUILD/components/micropython/esp32/machine_neopixel.c

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ STATIC void machine_neopixel_print(const mp_print_t *print, mp_obj_t self_in, mp
6464
machine_neopixel_obj_t *self = self_in;
6565

6666
if (self->px.pixels != NULL) {
67-
mp_printf(print, "Neopixel(Pin=%d, Pixels: %d, bit/pix=%d, RMTChannel=%d, PixBufLen=%u, Color order: %s\n",
68-
self->gpio_num, self->px.pixel_count, self->px.nbits, self->channel, sizeof(uint32_t) * self->px.pixel_count, self->px.color_order);
67+
char colorder[5];
68+
sprintf(colorder, self->px.color_order);
69+
if (self->px.nbits == 24) colorder[3] = '\0';
70+
mp_printf(print, "Neopixel(Pin=%d, Pixels: %d, bit/pix=%d, RMTChannel=%d, PixBufLen=%u, Color order: '%s'\n",
71+
self->gpio_num, self->px.pixel_count, self->px.nbits, self->channel, sizeof(uint32_t) * self->px.pixel_count, colorder);
6972
mp_printf(print, " Timings (ns): T1H=%d, T1L=%d, T0H=%d, T0L=%d, Treset=%d\n)",
7073
self->px.timings.mark.duration0 * RMT_PERIOD_NS, self->px.timings.mark.duration1 * RMT_PERIOD_NS,
7174
self->px.timings.space.duration0 * RMT_PERIOD_NS, self->px.timings.space.duration1 * RMT_PERIOD_NS,
@@ -400,32 +403,56 @@ STATIC mp_obj_t machine_neopixel_setHSB_int(size_t n_args, const mp_obj_t *pos_a
400403
}
401404
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_neopixel_setHSB_int_obj, 5, machine_neopixel_setHSB_int);
402405

403-
//---------------------------------------------------------------------
404-
STATIC mp_obj_t machine_neopixel_get(mp_obj_t self_in, mp_obj_t pos_in)
406+
//-----------------------------------------------------------------------
407+
STATIC mp_obj_t machine_neopixel_get(size_t n_args, const mp_obj_t *args)
405408
{
406-
machine_neopixel_obj_t *self = self_in;
409+
machine_neopixel_obj_t *self = args[0];
407410
np_check(self);
408411

409-
int pos = mp_obj_get_int(pos_in);
412+
uint8_t white;
413+
uint32_t icolor;
414+
int pos = mp_obj_get_int(args[1]);
410415
if (pos < 1) pos = 1;
411416
if (pos > self->px.pixel_count) pos = self->px.pixel_count;
412-
413-
uint8_t white;
414-
uint32_t icolor = np_get_pixel_color(&self->px, pos-1, &white);
415-
if (self->px.nbits == 24) {
416-
return mp_obj_new_int(icolor);
417+
int cnt = 0;
418+
if (n_args > 2) {
419+
cnt = mp_obj_get_int(args[2]);
420+
if (cnt < 1) cnt = 1;
421+
if ((cnt + pos - 1) > self->px.pixel_count) cnt = self->px.pixel_count - pos + 1;
417422
}
418-
else {
419-
mp_obj_t tuple[2];
423+
if (cnt < 2) {
424+
icolor = np_get_pixel_color(&self->px, pos-1, &white);
425+
if (self->px.nbits == 24) {
426+
return mp_obj_new_int(icolor);
427+
}
428+
else {
429+
mp_obj_t tuple[2];
420430

421-
tuple[0] = mp_obj_new_int(icolor);
422-
tuple[1] = mp_obj_new_int(white);
431+
tuple[0] = mp_obj_new_int(icolor);
432+
tuple[1] = mp_obj_new_int(white);
423433

424-
return mp_obj_new_tuple(2, tuple);
434+
return mp_obj_new_tuple(2, tuple);
435+
}
436+
}
437+
else {
438+
mp_obj_t pix_tuple[cnt];
439+
mp_obj_t tuple[2];
440+
for (int i=0; i<cnt; i++) {
441+
icolor = np_get_pixel_color(&self->px, pos+i-1, &white);
442+
if (self->px.nbits == 24) {
443+
pix_tuple[i] = mp_obj_new_int(icolor);
444+
}
445+
else {
446+
tuple[0] = mp_obj_new_int(icolor);
447+
tuple[1] = mp_obj_new_int(white);
448+
449+
pix_tuple[i] = mp_obj_new_tuple(2, tuple);
450+
}
451+
}
452+
return mp_obj_new_tuple(cnt, pix_tuple);
425453
}
426-
427454
}
428-
MP_DEFINE_CONST_FUN_OBJ_2(machine_neopixel_get_obj, machine_neopixel_get);
455+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_neopixel_get_obj, 2, 3, machine_neopixel_get);
429456

430457
//-----------------------------------------------------------------------------------------------------
431458
STATIC mp_obj_t machine_neopixel_brightness(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
@@ -667,6 +694,30 @@ STATIC mp_obj_t machine_neopixel_rainbow(mp_obj_t self_in, mp_obj_t pos_in, mp_o
667694
}
668695
MP_DEFINE_CONST_FUN_OBJ_3(machine_neopixel_rainbow_obj, machine_neopixel_rainbow);
669696

697+
//-----------------------------------------------------
698+
STATIC mp_obj_t machine_neopixel_info(mp_obj_t self_in)
699+
{
700+
machine_neopixel_obj_t *self = self_in;
701+
np_check(self);
702+
703+
char colorder[5];
704+
sprintf(colorder, self->px.color_order);
705+
if (self->px.nbits == 24) colorder[3] = '\0';
706+
707+
mp_obj_t tuple[6];
708+
709+
tuple[0] = mp_obj_new_int(self->gpio_num);
710+
tuple[1] = mp_obj_new_int(self->px.pixel_count);
711+
tuple[2] = mp_obj_new_int(self->px.nbits);
712+
tuple[3] = mp_obj_new_int(self->channel);
713+
tuple[4] = mp_obj_new_int(sizeof(uint32_t) * self->px.pixel_count);
714+
tuple[5] = mp_obj_new_str(colorder, strlen(colorder), false);
715+
716+
return mp_obj_new_tuple(6, tuple);
717+
718+
}
719+
MP_DEFINE_CONST_FUN_OBJ_1(machine_neopixel_info_obj, machine_neopixel_info);
720+
670721

671722
//=====================================================================
672723
STATIC const mp_rom_map_elem_t machine_neopixel_locals_dict_table[] = {
@@ -685,6 +736,7 @@ STATIC const mp_rom_map_elem_t machine_neopixel_locals_dict_table[] = {
685736
{ MP_ROM_QSTR(MP_QSTR_timings), (mp_obj_t)&machine_neopixel_timings_obj },
686737
{ MP_ROM_QSTR(MP_QSTR_color_order),(mp_obj_t)&machine_neopixel_corder_obj },
687738
{ MP_ROM_QSTR(MP_QSTR_rainbow), (mp_obj_t)&machine_neopixel_rainbow_obj },
739+
{ MP_ROM_QSTR(MP_QSTR_info), (mp_obj_t)&machine_neopixel_info_obj },
688740

689741
{ MP_ROM_QSTR(MP_QSTR_BLACK), MP_ROM_INT(0x000000) },
690742
{ MP_ROM_QSTR(MP_QSTR_WHITE), MP_ROM_INT(0xFFFFFF) },

MicroPython_BUILD/components/micropython/esp32/machine_rtc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ STATIC const mp_map_elem_t mach_rtc_locals_dict_table[] = {
619619
// Constants
620620
{ MP_ROM_QSTR(MP_QSTR_EXT1_ANYHIGH), MP_ROM_INT(ESP_EXT1_WAKEUP_ANY_HIGH) },
621621
{ MP_ROM_QSTR(MP_QSTR_EXT1_ALLLOW), MP_ROM_INT(ESP_EXT1_WAKEUP_ALL_LOW) },
622-
{ MP_ROM_QSTR(MP_QSTR_EXT1_ANYLOW), MP_ROM_INT(EXT1_WAKEUP_ALL_HIGH) },
622+
{ MP_ROM_QSTR(MP_QSTR_EXT1_ALLHIGH), MP_ROM_INT(EXT1_WAKEUP_ALL_HIGH) },
623623
};
624624
STATIC MP_DEFINE_CONST_DICT(mach_rtc_locals_dict, mach_rtc_locals_dict_table);
625625

MicroPython_BUILD/components/micropython/esp32/machine_uart.c

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static int uart_buf_get(uart_ringbuf_t *r, uint8_t *dest, uint16_t len) {
103103
if (r->iget == r->iput) break;
104104
}
105105
// move the buffer and adjust the pointers
106-
memmove(r->buf, r->buf+res, res);
106+
memmove(r->buf, r->buf+res, r->iput - res);
107107
r->iget -= res;
108108
r->iput -= res;
109109

@@ -120,15 +120,16 @@ static int uart_buf_put(uart_ringbuf_t *r, uint8_t *source, uint16_t len) {
120120
return res;
121121
}
122122

123-
//---------------------------------------------------------------------------------------
124-
int pattern_match(uint8_t *text, int text_length, uint8_t *pattern, int pattern_length) {
123+
//---------------------------------------------------------------------------------------------
124+
static int match_pattern(uint8_t *text, int text_length, uint8_t *pattern, int pattern_length)
125+
{
125126
int c, d, e, position = -1;
126127

127128
if (pattern_length > text_length) return -1;
128129

129130
for (c = 0; c <= (text_length - pattern_length); c++) {
130131
position = e = c;
131-
132+
// check pattern
132133
for (d = 0; d < pattern_length; d++) {
133134
if (pattern[d] == text[e]) e++;
134135
else break;
@@ -197,7 +198,7 @@ static void uart_event_task(void *pvParameters)
197198
}
198199
else if (self->pattern_cb) {
199200
// ** callback on pattern received
200-
res = pattern_match(uart_buf[self->uart_num]->buf, uart_buf[self->uart_num]->iput, self->pattern, self->pattern_len);
201+
res = match_pattern(uart_buf[self->uart_num]->buf, uart_buf[self->uart_num]->iput, self->pattern, self->pattern_len);
201202
if (res >= 0) {
202203
// found, pull data, including pattern from buffer
203204
uart_buf_get(uart_buf[self->uart_num], dtmp, res+self->pattern_len);
@@ -288,10 +289,33 @@ STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_pri
288289
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
289290
uint32_t baudrate;
290291
uart_get_baudrate(self->uart_num+1, &baudrate);
292+
char lnend[16] = {'\0'};
293+
int lnend_idx = 0;
294+
for (int i=0; i < strlen((char *)self->lineend); i++) {
295+
if (self->lineend[i] == 0) break;
296+
if ((self->lineend[i] < 32) || (self->lineend[i] > 126)) {
297+
if (self->lineend[i] == '\r') {
298+
sprintf(lnend+lnend_idx, "\\r");
299+
lnend_idx += 2;
300+
}
301+
else if (self->lineend[i] == '\n') {
302+
sprintf(lnend+lnend_idx, "\\n");
303+
lnend_idx += 2;
304+
}
305+
else {
306+
sprintf(lnend+lnend_idx, "\\x%2x", self->lineend[i]);
307+
lnend_idx += 4;
308+
}
309+
}
310+
else {
311+
sprintf(lnend+lnend_idx, "%c", self->lineend[i]);
312+
lnend_idx++;
313+
}
314+
}
291315

292-
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d, rts=%d, cts=%d, timeout=%u, buf_size=%u)",
316+
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d, rts=%d, cts=%d, timeout=%u, buf_size=%u, lineend=b'%s')",
293317
self->uart_num+1, baudrate, self->bits, _parity_name[self->parity],
294-
self->stop, self->tx, self->rx, self->rts, self->cts, self->timeout, self->buffer_size);
318+
self->stop, self->tx, self->rx, self->rts, self->cts, self->timeout, self->buffer_size, lnend);
295319
if (self->data_cb) {
296320
mp_printf(print, "\n data CB: True, on len: %d", self->data_cb_size);
297321
}
@@ -418,7 +442,7 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co
418442
if (MP_OBJ_IS_STR(args[ARG_lineend].u_obj)) {
419443
size_t lnendlen;
420444
const char *lnend = mp_obj_str_get_data(args[ARG_lineend].u_obj, &lnendlen);
421-
if ((lnend) && (lnendlen > 0) && (lnendlen > 0)) sprintf((char *)self->lineend, "%s", lnend);
445+
if ((lnend) && (lnendlen > 0) && (lnendlen < 3)) sprintf((char *)self->lineend, "%s", lnend);
422446
}
423447
}
424448

@@ -445,7 +469,8 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
445469
.parity = UART_PARITY_DISABLE,
446470
.stop_bits = UART_STOP_BITS_1,
447471
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
448-
.rx_flow_ctrl_thresh = 0
472+
.rx_flow_ctrl_thresh = 0,
473+
.use_ref_tick = true
449474
};
450475

451476
if (uart_mutex == NULL) {
@@ -493,12 +518,14 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
493518
mp_arg_val_t kargs[MP_ARRAY_SIZE(allowed_args)];
494519
mp_arg_parse_all(n_args-1, args+1, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, kargs);
495520

521+
// Set buffer size
496522
int bufsize = kargs[ARG_buffer_size].u_int;
497523
if (bufsize < 512) bufsize = 512;
498524
if (bufsize > 8192) bufsize = 8192;
499525
self->buffer_size = bufsize;
500526

501527
if (uart_buf[self->uart_num] == NULL) {
528+
// First time, create ring buffer
502529
uart_ringbuf_alloc(self->uart_num, bufsize);
503530
if (uart_buf[self->uart_num] == NULL) {
504531
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART(%d) Error allocating ring buffer", uart_num));
@@ -569,9 +596,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_flush_obj, machine_uart_flush);
569596
STATIC mp_obj_t machine_uart_readln(size_t n_args, const mp_obj_t *args) {
570597
machine_uart_obj_t *self = MP_OBJ_TO_PTR(args[0]);
571598

572-
vstr_t vstr;
573-
int res = -1;
599+
uint8_t *rdstr = NULL;
600+
int rdlen = -1;
574601
int lnendlen = strlen((char *)self->lineend);
602+
if (lnendlen == 0) return mp_const_none;
603+
575604
int timeout = self->timeout;
576605
if (n_args == 2) timeout = mp_obj_get_int(args[1]);
577606

@@ -582,14 +611,18 @@ STATIC mp_obj_t machine_uart_readln(size_t n_args, const mp_obj_t *args) {
582611
if (uart_mutex) xSemaphoreGive(uart_mutex);
583612
return mp_const_none;
584613
}
585-
res = pattern_match(uart_buf[self->uart_num]->buf, uart_buf[self->uart_num]->iput, self->lineend, lnendlen);
586-
if (res >= 0) {
614+
rdlen = match_pattern(uart_buf[self->uart_num]->buf, uart_buf[self->uart_num]->iput, self->lineend, lnendlen);
615+
if (rdlen >= 0) {
587616
// found, pull data, including pattern from buffer
588-
vstr_init_len(&vstr, res+lnendlen);
589-
uart_buf_get(uart_buf[self->uart_num], (uint8_t *)vstr.buf, res+lnendlen);
617+
rdlen += lnendlen;
618+
rdstr = calloc(rdlen+1, 1);
619+
if (rdstr) {
620+
uart_buf_get(uart_buf[self->uart_num], rdstr, rdlen);
621+
rdstr[rdlen] = 0;
622+
}
590623
}
591624
if (uart_mutex) xSemaphoreGive(uart_mutex);
592-
if (res < 0) return mp_const_none;
625+
if (rdlen < 0) return mp_const_none;
593626
}
594627
else {
595628
// wait until line end received or timeout
@@ -610,11 +643,15 @@ STATIC mp_obj_t machine_uart_readln(size_t n_args, const mp_obj_t *args) {
610643
mp_hal_reset_wdt();
611644
continue;
612645
}
613-
res = pattern_match(uart_buf[self->uart_num]->buf, uart_buf[self->uart_num]->iput, self->lineend, lnendlen);
614-
if (res >= 0) {
646+
rdlen = match_pattern(uart_buf[self->uart_num]->buf, uart_buf[self->uart_num]->iput, self->lineend, lnendlen);
647+
if (rdlen >= 0) {
648+
rdlen += lnendlen;
615649
// found, pull data, including pattern from buffer
616-
vstr_init_len(&vstr, res+lnendlen);
617-
uart_buf_get(uart_buf[self->uart_num], (uint8_t *)vstr.buf, res+lnendlen);
650+
rdstr = calloc(rdlen+1, 1);
651+
if (rdstr) {
652+
uart_buf_get(uart_buf[self->uart_num], rdstr, rdlen);
653+
rdstr[rdlen] = 0;
654+
}
618655
if (uart_mutex) xSemaphoreGive(uart_mutex);
619656
break;
620657
}
@@ -624,10 +661,12 @@ STATIC mp_obj_t machine_uart_readln(size_t n_args, const mp_obj_t *args) {
624661
mp_hal_reset_wdt();
625662
}
626663
MP_THREAD_GIL_ENTER();
627-
if (res < 0) return mp_const_none;
664+
if (rdlen < 0) return mp_const_none;
628665
}
629-
630-
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
666+
if (rdstr == NULL) return mp_const_none;
667+
mp_obj_t res_str = mp_obj_new_str((const char *)rdstr, rdlen, false);
668+
free(rdstr);
669+
return res_str;
631670
}
632671
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_uart_readln_obj, 1, 2, machine_uart_readln);
633672

@@ -728,6 +767,9 @@ STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
728767
};
729768
STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_table);
730769

770+
771+
// === Stream UART functions ===
772+
731773
//------------------------------------------------------------------------------------------------
732774
STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
733775
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);

0 commit comments

Comments
 (0)