Open Font Render 1.2
|
Control of file systems such as SD/TF cards and SPIFFS is strongly hardware-dependent.
OpenFontRender supports the following reads as presets.
ofrfs/M5Stack_SD_Preset.h
)ofrfs/M5Stack_SPIFFS_Preset.h
)ofrfs/WioTerminal_SD_Preset.h
)If you only use a preset, simply include a header in your code.
However, the file system initialization code must be written in the user code.
See example for complete code.
If you want to use a microcomputer or file system for which no presets are provided, you have to write a custom code.
There are five global functions you have to write.
OFR_fclose (fclose)
OFR_fopen (fopen)
OFR_fread (fread)
OFR_fseek (fseek)
OFR_ftell (ftell)
The processing performed by these is the same as the C language functions inside the parentheses.
A step-by-step implementation flow is shown below.
In this explain, the hardware will use M5Stack and assume reading from an SD card.
1. Declare file object
First, declare the file object in global scope.
Since the file object will continue to be used for the entire time the file is open, we declare it in global scope, not local scope.
2. Implement the OFR_fopen
Next, implement the OFR_fopen
function, which handles file opening.
The arguments and return values are as follows.
type | variable | mean |
---|---|---|
argument | filename | The path to the file to be opened. |
argument | mode | File access mode. |
return | Pointer to the file object. |
The details of the arguments filename
and mode
are the same as for the C language fopen
function.
The reference implementation below opens a file with SD.open
and returns a pointer to the file object.
3. Implement the OFR_fclose
Next, implement the OFR_close
function, which handles file closing.
The arguments and return values are as follows.
type | variable | mean |
---|---|---|
argument | stream | Pointer to the file object. In this example, it is a pointer to myFile . |
return | None |
The reference implementation below.
Also ((File *)stream)->close()
has the same meaning as myFile.close()
.
4. Implement the OFR_fread
Next, implement the OFR_fread
function, which handles file reading.
The arguments and return values are as follows.
type | variable | mean |
---|---|---|
argument | ptr | Pointer to the array where the read objects are stored. |
argument | size | Size of each object in bytes. |
argument | nmemb | The number of the objects to be read. |
argument | stream | Pointer to the file object. |
return | Number of objects read successfully. |
The details of the arguments are the same as for the C language fread
function.
The reference implementation below.
5. Implement the OFR_fseek
Next, implement the OFR_fseek
function, which handles moving file pointer.
The arguments and return values are as follows.
type | variable | mean |
---|---|---|
argument | stream | Pointer to the file object. |
argument | offset | Number of characters to shift the position relative to origin. |
argument | whence | Position to which offset is added. |
return | 0 upon success, nonzero value otherwise. |
The details of the arguments are the same as for the C language fseek
function.
The reference implementation below.
6. Implement the OFR_ftell
Finally, implement the OFR_ftell
function, which get current position of the file pointer within a file.
The arguments and return values are as follows.
type | variable | mean |
---|---|---|
argument | stream | Pointer to the file object. |
return | File position indicator on success or -1L if failure occurs. |
The details of the arguments are the same as for the C language ftell
function.
The reference implementation below.
All implementation is now complete. The entire sample program is shown. However, unnecessary parts before and after are omitted (...).
See example for complete code.