Atomic TF Card
This program uses the SD card for file and folder creation, deletion, renaming, and reading/writing operations. The main steps are as follows:
- Configure SD card pins and delete the
m5test.txt
file and m5 folder
folder (if they exist). - Create and write a text file
m5.txt
with content “welcome to M5,” then read and print the file content. - Check if the
m5test.txt
file exists and print the check result. - Rename
m5.txt
to m5test.txt
and check its existence again. - Create
m5test.txt
within m5 folder
and append “hello to M5,” then read and print all content.
Example
from m5stack import *
from m5ui import *
from uiflow import *
from base.TF_Card import TFCARD
tf = TFCARD()
tf.init_sdcard(33, 19, 23, 20000000)
tf.delete_file('m5test.txt')
tf.delete_folder('m5 folder')
with open('/sd/m5.txt', 'w+') as fs:
fs.write('welcome to M5')
with open('/sd/m5.txt', 'r+') as fs:
print(fs.read())
print(tf.show_directory(''))
if tf.is_file_exist('m5test.txt'):
print('file is exist')
else:
print('file is not exist')
tf.rename_file('m5.txt','m5test.txt')
tf.create_folder('m5 folder')
print(tf.show_directory(''))
if tf.is_file_exist('m5test.txt'):
print('file is exist')
else:
print('file is not exist')
with open('/sd/m5test.txt', 'a') as fs:
fs.write('hello to M5')
with open('/sd/m5test.txt', 'r+') as fs:
print(fs.read())
API
tf.init_sdcard(33, 19, 23, 20000000)
- Initializes the SD card interface configuration, specifying MISO, MOSI, SCK pins, and operating frequency. This operation is used to set up SD card communication.
fs.tell()
- Retrieves the current file reading position (seek). This is used to keep track of the file pointer position during file operations.
fs.read(0)
- Reads a specified number of bytes from the file. This command reads file content, and the number of bytes to read can be set in the parameter.
fs.read()
- Reads all content from the file. This block is used to read the entire content of a file at once, ideal for handling smaller files.
fs.readline()
- Reads one line of content from the file. This is used for line-by-line processing of data in text files.
fs.seek(0)
- Sets the reading position in the file. This can move the file pointer to a specific location within the file to perform read or write operations.
fs.write('')
- Writes text to a file. This command writes the specified text content to the file, typically used to record or save data to the SD card.
tf.is_folder_exist('')
- Checks if a folder exists at the specified path. Returns true if the folder exists, otherwise returns false.
tf.is_file_exist('')
- Checks if a file exists at the specified path. Returns a similar result as the folder existence check.
tf.show_directory('')
- Displays the list of files and folders at the specified path. This operation lists the contents of a directory, allowing users to view stored files and folders.
tf.create_folder('')
- Creates a new folder at the specified path. If the path is valid and permissions allow, a new folder will be created in the specified location.
with open('/sd/', 'a')
- Opens a file at the specified path with the set mode (e.g., read, write, etc.). Different modes control how the file is opened (e.g., read, write, append).
tf.delete_file('')
- Deletes a file at the specified path. This block removes a file from the storage medium.
tf.rename_file('','')
- Renames a file from the old path to the new path. By providing the old and new paths, you can rename a file.
tf.delete_folder('')
- Deletes a folder at the specified path. When given the folder path, the system will remove that folder and its contents (if non-empty folders are allowed to be deleted).