Created
March 22, 2021 18:20
-
-
Save jenschr/4602ecb986948187f362b8965f8d2f89 to your computer and use it in GitHub Desktop.
Arduino code snippet for controlling a MY1680U-12P using the MY1680U-16S chip for MP3 playback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Arduino snippet for controlling a MY1680U-12P | |
* using the MY1680U-16S chip for MP3 playback | |
*/ | |
#include <SoftwareSerial.h> | |
SoftwareSerial mySerial(8, 9); | |
#define START_CODE 0x7E | |
#define END_CODE 0xEF | |
#define CMD_PLAY 0x11 | |
#define CMD_PAUSE 0x1C | |
#define CMD_STOP 0x1E | |
#define CMD_STATUS 0x20 | |
#define CMD_VOLP 0x15 | |
#define CMD_VOLM 0x16 | |
#define CMD_NEXT 0x13 | |
#define CMD_PREV 0x14 | |
#define CMD_TARGET 0x41 | |
#define CMD_NAME 0x2E | |
void setup() | |
{ | |
delay(500); | |
Serial.begin(9600); | |
mySerial.begin(9600); | |
pinMode(13, OUTPUT); | |
Serial.println("0 = Play"); | |
Serial.println("1 = Pause"); | |
Serial.println("2 = Stop"); | |
Serial.println("3 = Query"); | |
Serial.println("4 = Vol up"); | |
Serial.println("5 = Vol down"); | |
Serial.println("6 = Next"); | |
Serial.println("7 = Prev"); | |
Serial.println("8 = Play song 6 (hardcoded)"); | |
Serial.println("9 = File name"); | |
} | |
int counter = 0; | |
void sendCommand( int cmd, int param = -1 ) | |
{ | |
mySerial.write(START_CODE); | |
if ( (cmd >= 0x11 && cmd <= 0x2F) || cmd == 0x1E ) // length 3 commands | |
{ | |
Serial.print("(3)"); | |
mySerial.write(0x03); // length 3 | |
mySerial.write(cmd); // cmd | |
mySerial.write(0x03 ^ cmd); // checksum | |
} | |
else if ( (cmd >= 0x31 && cmd <= 0x38) ) // length 4 commands | |
{ | |
Serial.print("(4)"); | |
mySerial.write(0x04); // length 4 | |
mySerial.write(cmd); // cmd | |
mySerial.write(param); // param | |
mySerial.write(0x04 ^ cmd); // checksum | |
} | |
else if ( (cmd >= 0x41 && cmd <= 0x44) ) // length 5 commands | |
{ | |
int check = 0x05 ^ cmd ^ param; | |
Serial.print("(cmd) 0x"); | |
Serial.print(cmd,HEX); | |
Serial.print(" (5) 0x"); | |
Serial.print(check,HEX); | |
mySerial.write(0x05); // length 5 | |
mySerial.write(cmd); // cmd | |
mySerial.write((byte)0x00); // upper bits | |
mySerial.write(param); // lower bits | |
mySerial.write(0x05 ^ cmd ^ param); // checksum | |
} | |
mySerial.write(END_CODE); | |
} | |
void loop() | |
{ | |
/* send everything received from the hardware uart to usb serial & vv */ | |
if (Serial.available() > 0) { | |
Serial.print(">"); | |
while ( Serial.available() ) { | |
char ch = Serial.read(); | |
Serial.print(ch); | |
if ( ch >= 48 && ch <= 57 ) | |
{ | |
switch ( ch ) | |
{ | |
case 48: sendCommand(CMD_PLAY); break; // "0" play | |
case 49: sendCommand(CMD_PAUSE); break; // "1" pause | |
case 50: sendCommand(CMD_STOP); break; // "2" stop | |
case 51: sendCommand(CMD_STATUS); break; // "3" status | |
case 52: sendCommand(CMD_VOLP); break; // "4" VolUp | |
case 53: sendCommand(CMD_VOLM); break; // "5" VolDown | |
case 54: sendCommand(CMD_NEXT); break; // "6" Next | |
case 55: sendCommand(CMD_PREV); break; // "7" Prev | |
case 56: sendCommand(CMD_TARGET, 6); break; // "8" Play song 6 | |
case 57: sendCommand(CMD_NAME); break; // "9" Name | |
default: sendCommand(CMD_STOP); | |
} | |
} | |
} | |
} | |
if (mySerial.available() > 0) { | |
Serial.print(":"); | |
while ( mySerial.available() ) { | |
digitalWrite(13, HIGH); // Blink the LED when something happens | |
char ch = mySerial.read(); | |
if ( ch ) { | |
Serial.print(ch); | |
} | |
} | |
} else { | |
digitalWrite(13, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment