jq8900
micropython 播放 52CM 娃娃主板 TF 卡上 mp3 文件
A、首先要确定TF中有什么MP3文件,文件在什么目录下,文件名叫什么。
B、使用“串口调试软件”得到播放的16进制代码
现在假设在TF卡上有一个目录为“系统声音”,该目录下有一个名为”欢迎访问.mp3”的文件,利用“串口调试软件”,可以得以到如下代码
0xAA, 0x08, 0x10, 0x01, 0x2F, 0xCF, 0xB5, 0xCD, 0xB3, 0x2A, 0x2F, 0xBB, 0xB6, 0xD3, 0xAD, 0x2A, 0x3F, 0x3F, 0x3F, 0x27,
A、上面的代码的设置截图
SD卡就是主板上的TF卡。 系统后面的 * 是多字符通配符。 ?是单字符通配符,???通配mp3,也通配mp4。
A、micropython代码
python
import machine
uart = machine.UART(1, baudrate=9600, tx=4, rx=19)
# 发送数据
data = bytes(
[0xAA, 0x08, 0x10, 0x01, 0x2F, 0xCF, 0xB5, 0xCD, 0xB3, 0x2A, 0x2F, 0xBB, 0xB6,
0xD3, 0xAD, 0x2A, 0x3F, 0x3F, 0x3F, 0x27, ])
uart.write(data)
上面的 tx=4 表示 esp32 的 IO4 做为发送数据,tx19 表示 IO19 接收数据,语音识别时,语音芯片向 ESP32 发数据。 uart.write() 发数据,uart.read() 读数据。
py
# 位运算,返回高八位,低8位
# to get HighByte,LowByte
def split(num):
return num >> 8, num & 0xFF
# to get SM,
def get_SM(b):
message_length = len(b)
bit_sum = 0X00
# 按位与运算
for i in range((message_length)):
bit_sum += b[i]
# print("bit_sum:",bit_sum)
# 和检验 :为之前所有字节之和的低 8 位,即起始码到数据相加后取低 8 位
SM_Code = (0xAA + bit_sum) & 0xFF
# print("SM_Code:",SM_Code)
# 验证:07 02 00 01,bit_sum=
return SM_Code
def command_base():
command = bytearray()
command.append(0xAA) ##[0]
command.append(0x00) ##[1]
command.append(0x00) ##[2]
command.append(0x00) ##[3]
return command
# 下一曲
def play_next():
command = bytearray()
command = command_base()
command[1] = 0x06
command[3] = 0xB0
return command
# 上一曲
def play_previous():
command = bytearray()
command = command_base()
command[1] = 0x05
command[3] = 0xAF
return command
# 曲目id从1-65536,拆分成高低,否则应该append
# track_id ,1-65536
def play_track(track_id):
command = bytearray()
command = command_base()
command[1] = 0x07
command[2] = 0x02
HighByte, LowByte = split(track_id)
command[3] = HighByte
command.append(LowByte)
# print("HighByte:",HighByte)
# print("LowByte:",LowByte)
b = [command[1], command[2], command[3], command[4]]
SM_Code = get_SM(b)
command.append(SM_Code)
return command
def volume_up():
command = bytearray()
command = command_base()
command[1] = 0x14
command[3] = 0xBE
return command
def volume_down():
command = bytearray()
command = command_base()
command[1] = 0x15
command[3] = 0xBF
return command
def set_volume(level):
command = bytearray()
command = command_base()
command[1] = 0x13
command[2] = 0x01
command[3] = level
b = [command[1], command[2], command[3]]
SM_Code = get_SM(b)
command.append(SM_Code)
return command
def pause():
command = bytearray()
command = command_base()
command[1] = 0x03
command[3] = 0xAD
return command
def stop():
command = bytearray()
command = command_base()
command[1] = 0x04
command[3] = 0xAE
return command
# 切换为 flash 卡
# AA 0B 01 02 B8 切换到 FLASH 卡,切换后处于停止状态
# use flash to play ,it's by default
def use_flash():
command = bytearray()
command = command_base()
command[1] = 0x0B
command[2] = 0x01
command[3] = 0x02
command.append(0xB8)
return command
# 指令:AA 16 03 盘符 曲目高 曲目低 SM
# 返回:无
# 盘符定义: 切换盘符后处于停止状态
# USB:00 SD:01 FLASH:02 NO_DEVICE:FF
# 例如:AA 16 03 00 00 09 CC 插播 U 盘里的第 9 首
# 说明:插播结束后返回插播点继续播放
def insert_play(track_id):
command = bytearray()
command = command_base()
command[1] = 0x16
command[2] = 0x03
command[3] = 0x02
HighByte, LowByte = split(track_id)
command.append(HighByte)
command.append(LowByte)
b = [command[1], command[2], command[3], command[4], command[5]]
SM_Code = get_SM(b)
command.append(SM_Code)
return command
def chinese_to_unicode(text):
r = []
for c in text:
if ord(c) < 128:
t = ord(c)
r.append(t)
else:
for i in c.encode('gbk'):
r.append(i)
return r
def order_play(name):
"""指定路径播放 esp32不支持gbk编码,无法东塔"""
command = bytearray()
command = command_base()
command[1] = 0x08
command[3] = 0x01 # 盘符
content = chinese_to_unicode(name)
length = len(content)
# 计算路径
command[2] = 1 + length
# 追加路径
for item in content:
command.append(item)
b = command[1:]
SM_Code = get_SM(b)
command.append(SM_Code)
return command
JQ8900 资料包
链接:https://pan.baidu.com/s/1Np84lWv7bpTZH_0E_cLkFw?pwd=kt59 提取码:kt59
串口处理语音指令
python
import machine
from machine import UART
import time
uart = machine.UART(1, baudrate=9600, tx=4, rx=19)
# 发送数据
data = bytes(
[0xAA, 0x08, 0x10, 0x01, 0x2F, 0xCF, 0xB5, 0xCD, 0xB3, 0x2A, 0x2F, 0xBB, 0xB6,200
0xD3, 0xAD, 0x2A, 0x3F, 0x3F, 0x3F, 0x27, ])
uart.write(data)
cnt = 0
while True:
if uart.any():
content = uart.read()
print("Received data: ", content)
else:
cnt += 1
if cnt % 1000 == 0:
uart.write("ESP32C3 data, count: %s" % cnt)
time.sleep_ms(1)