Skip to content

Commit 40048ad

Browse files
committed
Move file obj to separate module
1 parent 2b2cb7b commit 40048ad

4 files changed

Lines changed: 97 additions & 84 deletions

File tree

stm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ SRC_C = \
6565
usrsw.c \
6666
adc.c \
6767
rtc.c \
68+
file.c \
6869
# pybwlan.c \
6970
7071
SRC_S = \

stm/file.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <stdio.h>
2+
#include <stm32f4xx.h>
3+
4+
#include "misc.h"
5+
#include "mpconfig.h"
6+
#include "mpconfigport.h"
7+
#include "qstr.h"
8+
#include "obj.h"
9+
#include "file.h"
10+
#include "ff.h"
11+
12+
typedef struct _pyb_file_obj_t {
13+
mp_obj_base_t base;
14+
FIL fp;
15+
} pyb_file_obj_t;
16+
17+
void file_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
18+
printf("<file %p>", self_in);
19+
}
20+
21+
mp_obj_t file_obj_read(mp_obj_t self_in, mp_obj_t arg) {
22+
pyb_file_obj_t *self = self_in;
23+
int n = mp_obj_get_int(arg);
24+
byte *buf = m_new(byte, n);
25+
UINT n_out;
26+
f_read(&self->fp, buf, n, &n_out);
27+
return mp_obj_new_str(buf, n_out, false);
28+
}
29+
30+
mp_obj_t file_obj_write(mp_obj_t self_in, mp_obj_t arg) {
31+
pyb_file_obj_t *self = self_in;
32+
uint l;
33+
const byte *s = mp_obj_str_get_data(arg, &l);
34+
UINT n_out;
35+
FRESULT res = f_write(&self->fp, s, l, &n_out);
36+
if (res != FR_OK) {
37+
printf("File error: could not write to file; error code %d\n", res);
38+
} else if (n_out != l) {
39+
printf("File error: could not write all data to file; wrote %d / %d bytes\n", n_out, l);
40+
}
41+
return mp_const_none;
42+
}
43+
44+
mp_obj_t file_obj_close(mp_obj_t self_in) {
45+
pyb_file_obj_t *self = self_in;
46+
f_close(&self->fp);
47+
return mp_const_none;
48+
}
49+
50+
static MP_DEFINE_CONST_FUN_OBJ_2(file_obj_read_obj, file_obj_read);
51+
static MP_DEFINE_CONST_FUN_OBJ_2(file_obj_write_obj, file_obj_write);
52+
static MP_DEFINE_CONST_FUN_OBJ_1(file_obj_close_obj, file_obj_close);
53+
54+
// TODO gc hook to close the file if not already closed
55+
56+
static const mp_method_t file_methods[] = {
57+
{ "read", &file_obj_read_obj },
58+
{ "write", &file_obj_write_obj },
59+
{ "close", &file_obj_close_obj },
60+
{NULL, NULL},
61+
};
62+
63+
static const mp_obj_type_t file_obj_type = {
64+
{ &mp_const_type },
65+
"File",
66+
.print = file_obj_print,
67+
.methods = file_methods,
68+
};
69+
70+
mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode) {
71+
const char *filename = mp_obj_str_get_str(o_filename);
72+
const char *mode = mp_obj_str_get_str(o_mode);
73+
pyb_file_obj_t *self = m_new_obj(pyb_file_obj_t);
74+
self->base.type = &file_obj_type;
75+
if (mode[0] == 'r') {
76+
// open for reading
77+
FRESULT res = f_open(&self->fp, filename, FA_READ);
78+
if (res != FR_OK) {
79+
printf("FileNotFoundError: [Errno 2] No such file or directory: '%s'\n", filename);
80+
return mp_const_none;
81+
}
82+
} else if (mode[0] == 'w') {
83+
// open for writing, truncate the file first
84+
FRESULT res = f_open(&self->fp, filename, FA_WRITE | FA_CREATE_ALWAYS);
85+
if (res != FR_OK) {
86+
printf("?FileError: could not create file: '%s'\n", filename);
87+
return mp_const_none;
88+
}
89+
} else {
90+
printf("ValueError: invalid mode: '%s'\n", mode);
91+
return mp_const_none;
92+
}
93+
return self;
94+
}

stm/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode);

stm/main.c

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "usrsw.h"
4444
#include "adc.h"
4545
#include "rtc.h"
46+
#include "file.h"
4647

4748
int errno;
4849

@@ -544,90 +545,6 @@ mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
544545
return mp_const_none;
545546
}
546547

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-
631548
mp_obj_t pyb_rng_get(void) {
632549
return mp_obj_new_int(RNG_GetRandomNumber() >> 16);
633550
}

0 commit comments

Comments
 (0)