Python code for AtomS3U, working to play first track in root directory
import os, sys, io
import M5
from M5 import *
import time
from hardware import UART
uart1 = None
str2 = None
name = None
number = None
ok = None
command = None
msg = None
pm = None
bytes2 = None
reply = None
# Describe this function...
def WriteMP3Player(str2):
global name, number, ok, command, msg, pm, bytes2, reply, uart1
ok = False
print(str2)
uart1.write((str2.encode()))
return True
# Describe this function...
def getReply():
global str2, name, number, ok, command, msg, pm, bytes2, reply, uart1
time.sleep(1)
msg = bytearray(100)
while uart1.any():
bytes2 = uart1.read()
msg = msg + bytes2
time.sleep_ms(100)
return (msg.decode()).strip()
# Describe this function...
def prettyPrintReply(str2):
global name, number, ok, command, msg, pm, bytes2, reply, uart1
print(str((len(str2))))
print(str2)
if str2 == 'ERR:1':
print('error 1 received')
# Describe this function...
def playByName(name):
global str2, number, ok, command, msg, pm, bytes2, reply, uart1
command = 'AT+SPLAY=sd0,'
command = command + name
ok = WriteMP3Player(command)
reply = getReply()
# Describe this function...
def playByNumber(number):
global str2, name, ok, command, msg, pm, bytes2, reply, uart1
command = 'AT+PLAY=sd0,'
command = command + (str(number))
ok = WriteMP3Player(command)
reply = getReply()
def setup():
global uart1, ok, command, msg, str2, pm, bytes2, name, reply, number
M5.begin()
uart1 = UART(1, baudrate=115200, bits=8, parity=None, stop=1, tx=2, rx=1)
pm = 0
msg = ''
def loop():
global uart1, ok, command, msg, str2, pm, bytes2, name, reply, number
M5.update()
if pm == 0:
ok = WriteMP3Player('AT+BAUD?')
reply = getReply()
print(reply)
pm = pm + 1
if pm == 1:
ok = WriteMP3Player('AT+GVER?')
reply = getReply()
print(reply)
pm = pm + 1
if pm == 2:
ok = WriteMP3Player('AT+GCFGVER?')
reply = getReply()
print(reply)
prettyPrintReply(reply)
pm = pm + 1
if pm == 3:
playByNumber(3)
reply = getReply()
print(reply)
prettyPrintReply(reply)
pm = pm + 1
if pm == 4:
ok = WriteMP3Player('AT+REPEATMODE=4')
reply = getReply()
print(reply)
prettyPrintReply(reply)
pm = pm + 1
if pm == 5:
ok = WriteMP3Player('AT+VOL=3')
reply = getReply()
print(reply)
prettyPrintReply(reply)
pm = pm + 1
if __name__ == '__main__':
try:
setup()
while True:
loop()
except (Exception, KeyboardInterrupt) as e:
try:
from utility import print_error_msg
print_error_msg(e)
except ImportError:
print("please update to latest firmware")
Arduino test sketch
https://static-cdn.m5stack.com/resource/arduino/package_m5stack_index.json
#include <M5Unified.h>
// Hardware Serial 1 configuration
HardwareSerial uart1(1);
const int RX_PIN = 1;
const int TX_PIN = 2;
// Global Variables
bool ok = false;
String str2 = "";
String name = "";
int number = 0;
String command = "";
String msg = "";
int pm = 0;
String reply = "";
// Forward declarations of functions
bool WriteMP3Player(String str2);
String getReply();
void prettyPrintReply(String str2);
void playByName(String name);
void playByNumber(int number);
void setup() {
// Initialize the M5 device
auto cfg = M5.config();
M5.begin(cfg);
// Initialize standard Serial for debug printing to PC
Serial.begin(115200);
// Initialize UART1 for the MP3 player (Baud rate, Config, RX Pin, TX Pin)
uart1.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
pm = 0;
msg = "";
}
void loop() {
M5.update();
if (pm == 0) {
ok = WriteMP3Player("AT+BAUD?");
reply = getReply();
Serial.println(reply);
pm = pm + 1;
}
else if (pm == 1) {
ok = WriteMP3Player("AT+GVER?");
reply = getReply();
Serial.println(reply);
pm = pm + 1;
}
else if (pm == 2) {
ok = WriteMP3Player("AT+GCFGVER?");
reply = getReply();
Serial.println(reply);
prettyPrintReply(reply);
pm = pm + 1;
}
else if (pm == 3) {
playByNumber(3);
reply = getReply();
Serial.println(reply);
prettyPrintReply(reply);
pm = pm + 1;
}
else if (pm == 4) {
ok = WriteMP3Player("AT+REPEATMODE=4");
reply = getReply();
Serial.println(reply);
prettyPrintReply(reply);
pm = pm + 1;
}
else if (pm == 5) {
ok = WriteMP3Player("AT+VOL=3");
reply = getReply();
Serial.println(reply);
prettyPrintReply(reply);
pm = pm + 1;
}
}
// Function to write data to the MP3 Player over UART
bool WriteMP3Player(String str2) {
ok = false;
Serial.println(str2); // Print to computer console for debugging
uart1.print(str2); // Send AT command to MP3 Player
return true;
}
// Function to gather responses from the MP3 Player
String getReply() {
delay(1000); // Wait 1 second for device to start processing
msg = "";
while (uart1.available()) {
char c = uart1.read();
msg += c;
delay(10); // Short delay to let next byte arrive in buffer
}
msg.trim(); // Removes whitespace/newlines from ends (equivalent to .strip())
return msg;
}
// Function to print details about the reply
void prettyPrintReply(String str2) {
Serial.println(str2.length());
Serial.println(str2);
if (str2 == "ERR:1") {
Serial.println("error 1 received");
}
}
// Function to play a track by its string name
void playByName(String name) {
command = "AT+SPLAY=sd0,";
command = command + name;
ok = WriteMP3Player(command);
reply = getReply();
}
// Function to play a track by its index number
void playByNumber(int number) {
command = "AT+PLAY=sd0,";
command = command + String(number);
ok = WriteMP3Player(command);
reply = getReply();
}