UIFlow Guide
该程序配置了SD卡以播放WAV音频文件,并执行文件读写操作。当按下按钮A时,标志变量flag
设置为1,触发写入随机数到“test.txt”文件。如果SD卡写入成功,RGB灯变为黄色,否则变为红色。然后尝试从文件中读取数据并将其转换为音频频率播放,成功时RGB灯变为绿色,否则为红色。最后,flag
复位为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('')
spk._tf.create_folder('')
spk._tf.delete_file('')
spk._tf.rename_file('','')
spk._tf.delete_folder('')