This program configures the SD card to play WAV audio files and perform file read/write operations. When button A is pressed, the flag variable flag
is set to 1, triggering a random number to be written to the file “test.txt.” If the SD card write is successful, the RGB LED turns yellow; otherwise, it turns red. Then, it attempts to read data from the file and play it as an audio frequency. If successful, the RGB LED turns green; otherwise, it turns red. Finally, flag
resets to 0.
from m5stack import *
from m5ui import *
from uiflow import *
from base.Speaker import Speaker
import time
flag = None
random_num = None
read_data = None
spk = Speaker()
import random
def buttonA_wasPressed():
global flag, random_num, read_data
flag = 1
pass
btnA.wasPressed(buttonA_wasPressed)
flag = 0
spk._tf.init_sdcard(33, 19, 23, 20000000)
spk.playWAV('res/mix.wav', rate=44100, data_format=Speaker.F32B, channel=Speaker.CHN_L, volume=0)
while True:
random_num = random.randint(100000000, 999999999)
if flag:
try :
with open('/sd/test.txt', 'w+') as fs:
fs.write(str(random_num))
rgb.setColorAll(0xffff00)
pass
except:
print('cannot write, please check the sdcard')
rgb.setColorAll(0xff0000)
wait_ms(100)
try :
with open('/sd/test.txt', 'r+') as fs:
read_data = fs.read()
print(read_data)
spk.playTone(int((int(read_data) / 250000)), 1, volume=0)
rgb.setColorAll(0x33cc00)
pass
except:
print('cannot read, please check the sdcard')
rgb.setColorAll(0xff0000)
flag = 0
wait_ms(2)
spk._tf.init_sdcard(33, 19, 23, 20000000)
with open('/sd/', 'a')
pass
passspk.playCloudWAV('', volume=0)
spk.playTone(220, 1, volume=0)
spk.playTone(220, 1, volume=0)
/
spk.playWAV('', volume=0)
spk.playWAV('', rate=44100, data_format=Speaker.F16B, channel=Speaker.CHN_LR, volume=0)
fs.tell()
fs.read(0)
fs.read()
fs.readline()
fs.seek(0)
fs.write('')
spk._tf.is_folder_exist('')
spk._tf.is_file_exist('')
spk._tf.show_directory('')
showing the directory contents.
spk._tf.create_folder('')
spk._tf.delete_file('')
spk._tf.rename_file('','')
spk._tf.delete_folder('')