|
43 | 43 | #include "usrsw.h" |
44 | 44 | #include "adc.h" |
45 | 45 | #include "rtc.h" |
| 46 | +#include "file.h" |
46 | 47 |
|
47 | 48 | int errno; |
48 | 49 |
|
@@ -544,90 +545,6 @@ mp_obj_t pyb_hid_send_report(mp_obj_t arg) { |
544 | 545 | return mp_const_none; |
545 | 546 | } |
546 | 547 |
|
547 | | -typedef struct _pyb_file_obj_t { |
548 | | - mp_obj_base_t base; |
549 | | - FIL fp; |
550 | | -} pyb_file_obj_t; |
551 | | - |
552 | | -void file_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
553 | | - printf("<file %p>", self_in); |
554 | | -} |
555 | | - |
556 | | -mp_obj_t file_obj_read(mp_obj_t self_in, mp_obj_t arg) { |
557 | | - pyb_file_obj_t *self = self_in; |
558 | | - int n = mp_obj_get_int(arg); |
559 | | - byte *buf = m_new(byte, n); |
560 | | - UINT n_out; |
561 | | - f_read(&self->fp, buf, n, &n_out); |
562 | | - return mp_obj_new_str(buf, n_out, false); |
563 | | -} |
564 | | - |
565 | | -mp_obj_t file_obj_write(mp_obj_t self_in, mp_obj_t arg) { |
566 | | - pyb_file_obj_t *self = self_in; |
567 | | - uint l; |
568 | | - const byte *s = mp_obj_str_get_data(arg, &l); |
569 | | - UINT n_out; |
570 | | - FRESULT res = f_write(&self->fp, s, l, &n_out); |
571 | | - if (res != FR_OK) { |
572 | | - printf("File error: could not write to file; error code %d\n", res); |
573 | | - } else if (n_out != l) { |
574 | | - printf("File error: could not write all data to file; wrote %d / %d bytes\n", n_out, l); |
575 | | - } |
576 | | - return mp_const_none; |
577 | | -} |
578 | | - |
579 | | -mp_obj_t file_obj_close(mp_obj_t self_in) { |
580 | | - pyb_file_obj_t *self = self_in; |
581 | | - f_close(&self->fp); |
582 | | - return mp_const_none; |
583 | | -} |
584 | | - |
585 | | -static MP_DEFINE_CONST_FUN_OBJ_2(file_obj_read_obj, file_obj_read); |
586 | | -static MP_DEFINE_CONST_FUN_OBJ_2(file_obj_write_obj, file_obj_write); |
587 | | -static MP_DEFINE_CONST_FUN_OBJ_1(file_obj_close_obj, file_obj_close); |
588 | | - |
589 | | -// TODO gc hook to close the file if not already closed |
590 | | - |
591 | | -static const mp_method_t file_methods[] = { |
592 | | - { "read", &file_obj_read_obj }, |
593 | | - { "write", &file_obj_write_obj }, |
594 | | - { "close", &file_obj_close_obj }, |
595 | | - {NULL, NULL}, |
596 | | -}; |
597 | | - |
598 | | -static const mp_obj_type_t file_obj_type = { |
599 | | - { &mp_const_type }, |
600 | | - "File", |
601 | | - .print = file_obj_print, |
602 | | - .methods = file_methods, |
603 | | -}; |
604 | | - |
605 | | -mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode) { |
606 | | - const char *filename = mp_obj_str_get_str(o_filename); |
607 | | - const char *mode = mp_obj_str_get_str(o_mode); |
608 | | - pyb_file_obj_t *self = m_new_obj(pyb_file_obj_t); |
609 | | - self->base.type = &file_obj_type; |
610 | | - if (mode[0] == 'r') { |
611 | | - // open for reading |
612 | | - FRESULT res = f_open(&self->fp, filename, FA_READ); |
613 | | - if (res != FR_OK) { |
614 | | - printf("FileNotFoundError: [Errno 2] No such file or directory: '%s'\n", filename); |
615 | | - return mp_const_none; |
616 | | - } |
617 | | - } else if (mode[0] == 'w') { |
618 | | - // open for writing, truncate the file first |
619 | | - FRESULT res = f_open(&self->fp, filename, FA_WRITE | FA_CREATE_ALWAYS); |
620 | | - if (res != FR_OK) { |
621 | | - printf("?FileError: could not create file: '%s'\n", filename); |
622 | | - return mp_const_none; |
623 | | - } |
624 | | - } else { |
625 | | - printf("ValueError: invalid mode: '%s'\n", mode); |
626 | | - return mp_const_none; |
627 | | - } |
628 | | - return self; |
629 | | -} |
630 | | - |
631 | 548 | mp_obj_t pyb_rng_get(void) { |
632 | 549 | return mp_obj_new_int(RNG_GetRandomNumber() >> 16); |
633 | 550 | } |
|
0 commit comments