/* marderchen Thanks to TM1638 master library and /***********************************************************/ // Demo for the Serial MP3 Player Catalex (YX5300 chip) // Hardware: Serial MP3 Player *1 // Board: Arduino UNO // http://www.dx.com/p/uart-control-serial-mp3-music-player-module-for-arduino-avr-arm-pic-blue-silver-342439#.VfHyobPh5z0 // // // static int8_t Send_buf[8] = {0}; // Buffer for Send commands. // BETTER LOCALLY static uint8_t ansbuf[10] = {0}; // Buffer for the answers. // BETTER LOCALLY static int8_t pre_vol, volume = 0x0f; // Volume. 0-30 DEC values. 0x0f = 15. String mp3Answer; // Answer from the MP3. boolean playing = false; // Sending 'p' the module switch to Play to Pause or viceversa. /************ Command byte **************************/ #define CMD_NEXT_SONG 0X01 // Play next song. #define CMD_PREV_SONG 0X02 // Play previous song. #define CMD_PLAY_W_INDEX 0X03 #define CMD_VOLUME_UP 0X04 #define CMD_VOLUME_DOWN 0X05 #define CMD_SET_VOLUME 0X06 #define CMD_SNG_CYCL_PLAY 0X08 // Single Cycle Play. #define CMD_SEL_DEV 0X09 #define CMD_SLEEP_MODE 0X0A #define CMD_WAKE_UP 0X0B #define CMD_RESET 0X0C #define CMD_PLAY 0X0D #define CMD_PAUSE 0X0E #define CMD_PLAY_FOLDER_FILE 0X0F #define CMD_STOP_PLAY 0X16 #define CMD_FOLDER_CYCLE 0X17 #define CMD_SHUFFLE_PLAY 0x18 // #define CMD_SET_SNGL_CYCL 0X19 // Set single cycle. #define CMD_SET_DAC 0X1A #define DAC_ON 0X00 #define DAC_OFF 0X01 #define CMD_PLAY_W_VOL 0X22 #define CMD_PLAYING_N 0x4C /************ Opitons **************************/ #define DEV_TF 0X02 #define SINGLE_CYCLE_ON 0X00 #define SINGLE_CYCLE_OFF 0X01 /*********************************************************************/ #include #include #include TM1638QYF module(PB12, PB13, PB14); word mode; word buttons; String cach = ""; unsigned long startTime; int einzi=0; #define LEDDATA PB8 #define herein PA1 #define bell PA0 #define grun PB11 #define UV PB10 #define light PA5 #define BRIGHTNESS 255 #define LED_TYPE WS2812 #define COLOR_ORDER GRB #define screeny 17 CRGB leds[screeny]; CRGBPalette16 currentPalette; TBlendType currentBlending; #define UPDATES_PER_SECOND 5000 extern CRGBPalette16 myRedWhiteBluePalette; int speedso=1; int einz =0; uint8_t ix = 0; int bright =7; int woo =0; int zeitz =0; int fcount1 =0; int zeit2 =0; int duty[4]; int upd[4]; int was =0; boolean merk =false; int slowsz=0; long allfcount = micros(); unsigned long notime; boolean merks = true; String texte[60] = {" "," "," "," "," "," ","M","A","R","D","E","R","C","H","E","N","S","-","H","L","I","N","G","E","L"," "," "," ","P","A","E","S","S"," ", "B","U","T","O","N"," ","C","A","T"," ","S","T","M","3","2","-","I","N","S","I","D","E"}; void setup() { pinMode(UV, OUTPUT); pinMode(grun, OUTPUT); pinMode(light, OUTPUT); pinMode(bell, INPUT); pinMode(herein, INPUT); startTime = millis(); FastLED.addLeds(leds, screeny).setCorrection( TypicalLEDStrip ); FastLED.setBrightness( BRIGHTNESS ); currentPalette = RainbowColors_p; currentBlending = BLEND; notime = millis(); Serial2.begin(9600); module.setupDisplay(true, 7); mode = 2; delay(500); sendCommand(CMD_SEL_DEV, DEV_TF); delay(200); } String incomming =""; void update(TM1638QYF* module, word* mode) { if (analogRead(bell) <= 50) {buttons = module->getButtons(); einz=0;} if (analogRead(bell) >= 50) {buttons = 6666; einz=0;} if (Serial2.available()>0) { // module->setDisplayToString(decodeMP3Answer(),4); //module->setDisplayToString(String(ansbuf[6],DEC),3); } // delay(50); if(einzi ==1) { switch (buttons) { case 1: notime =millis(); module->setDisplayToString(String("PREV "),2); sendCommand(CMD_PREV_SONG, 0); sendCommand(CMD_PLAYING_N, 0x0000); // ask for the number of file is playing speedso=1; break; case 2: speedso=3; break; case 4: module->setDisplayToString(String("PAUSE"),1); sendCommand(CMD_PAUSE, 0); speedso=2; playing = false; break; case 8: module->setDisplayToString(String("NEXT"),1); notime =millis(); sendCommand(CMD_NEXT_SONG, 0); sendCommand(CMD_PLAYING_N, 0x0000); // ask for the number of file is playing speedso=4; break; case 16: module->setDisplayToString(String("UP MEOW"),1); sendCommand(CMD_VOLUME_UP, 0); break; case 32: module->setDisplayToString(String("DOWN CAT"),1); sendCommand(CMD_VOLUME_DOWN, 0); case 64: module->setDisplayToString(String("RANDOM"),1); sendCommand(CMD_SHUFFLE_PLAY, 0); break; case 128: module->setDisplayToString(String("DAC ON"),1); sendCommand(DAC_ON, 0); break; case 256: notime =millis(); module->setDisplayToString(String("RAND"),1); sendCommand(DAC_OFF, 0); break; case 512: if (duty[2] <=4) {duty[2]++;} break; case 1024: if (duty[2] >=1) {duty[2]--;}break; case 2048: break; case 4096: if (duty[0] <=4) {duty[0]++;} break; case 8126: break; case 16384: if (duty[2] >=1) {duty[2]--;} break; case 32768: if (duty[0] >=1) {duty[0]--;} break; case 6666: if(!playing){ notime =millis(); module->setDisplayToString(String("PLAY"),1); //sendCommand(CMD_PLAY_W_VOL, 0X0F01);//play the first song with volume 15 class sendCommand(CMD_PLAY, 0); playing = true; }else{ module->setDisplayToString(String("STOP"),3); sendCommand(CMD_STOP_PLAY , 0); playing = false;} speedso=3; einz =0; break; } } zeitz++; if (playing ==false && zeitz >60) { cach =""; if (woo >= 53) { merk =false;} if (woo == 0) { merk =true;} if (merk == true) { woo++; } if (merk == false) {woo--; } for( int it = woo; it < (8+woo); it++) { cach += texte[it];} module->setDisplayToString(cach,1); zeitz=0;} if(playing ==true && ((millis() - notime) / 1000) >1){ unsigned long gotime = (millis() - notime) / 10; if ((((gotime/100)/60)%60) <=9){ cach+="0";} cach +=((gotime/100)/60)%60; cach += "-"; if (((gotime/100)%60) <=9){ cach+="0";} cach +=(gotime/100)%60; cach += "-"; if ((gotime%100) <=9){ cach+="0";} if ((gotime%100) <=99){ cach +=gotime%100;} module->setDisplayToString(cach,(woo%10)); cach ="";} ix=0; if (einz ==0) { einzi++; } while (Serial2.read()> 0) {uint8_t be =Serial2.read(); cach += String(be,DEC); ix++;}//module->setDisplayToString(decodeMP3Answer(),4); } if (einzi >70) {einz =1; einzi =0;}// module->setDisplayToString(String(cach.length()),4); } // module->setDisplayToString(String(buttons),2); if (duty[1] >5) {upd[1] =1;} if (duty[1] <1) {upd[1] =0;} if (zeit2 >500) { was++; if (was < 10){speedso = random(1,20);} if (was > 10) {speedso = random(100,500); } if (was > 13) {was =0; if (playing == true) {buttons=6666; einz=0; }} if(upd[1] ==1) {duty[1]--;} if(upd[1] ==0) {duty[1]++;} zeit2=0; } } void loop() { update(&module, &mode); WS2812rainbow(); machlicht(); // Check for the answer. } int colorw =0; int colorIndex =0; int colort=0; void WS2812rainbow() { colorw +=1; if (colorw > 500) {colorw=0;} for( int iy = 0; iy < screeny; iy++) { slowsz++; if (slowsz > 200) { colort+=1; colorIndex+=speedso; zeit2++; if (colorIndex >=1024) {colorIndex=0;} if (colort >=1024) {colort=0;} slowsz=0;} //leds[iy] = rainbow[round(colorw)]; if (iy == 0) {leds[iy] = ColorFromPalette( currentPalette, colort, 255, currentBlending);} if (iy >= 1) {leds[iy] = ColorFromPalette( currentPalette, colorIndex, 255, currentBlending);} if (digitalRead(herein) ==HIGH && iy >=9 && iy <= 17) { leds[iy] =CRGB::Black; } if (digitalRead(herein) ==LOW && iy >=1 && iy <= 8) { leds[iy] =CRGB::Black; } } FastLED.show(); FastLED.delay(1000 / 100000); } String decodeMP3Answer(){ String decodedMP3Answer=""; decodedMP3Answer+=sanswer(); if (ansbuf[3] == 0x4C) // currently planying { decodedMP3Answer+=""+String(ansbuf[6],DEC); } switch (ansbuf[3]) { case 0x3A: decodedMP3Answer+="NO CARD"; break; case 0x3D: decodedMP3Answer+="DONE "+String(ansbuf[6],DEC); break; case 0x4C: decodedMP3Answer+=""+String(ansbuf[6],DEC); break; case 0x41: decodedMP3Answer+="OK"; break; } return decodedMP3Answer; } /********************************************************************************/ /*Function: Send command to the MP3 */ /*Parameter:-int8_t command */ /*Parameter:-int16_ dat parameter for the command */ void sendCommand(int8_t command, int16_t dat) { delay(20); Send_buf[0] = 0x7e; // Send_buf[1] = 0xff; // Send_buf[2] = 0x06; // Len Send_buf[3] = command;// Send_buf[4] = 0x01; // 0x00 NO, 0x01 feedback Send_buf[5] = (int8_t)(dat >> 8); //datah Send_buf[6] = (int8_t)(dat); //datal Send_buf[7] = 0xef; // for(uint8_t i=0; i<8; i++) { Serial2.write(Send_buf[i]) ; } } String sbyte2hex(uint8_t b) { String shex; //Serial.print("0x"); shex="0X"; //if (b < 16) Serial.print("0"); if (b < 16) shex+="0"; //Serial.print(b, HEX); shex+=String(b,HEX); //Serial.print(" "); shex+=" "; return shex; } String sanswer(void) { uint8_t i = 0; String mp3answer=""; // Get only 10 Bytes while(Serial2.available() && (i < 10)) { uint8_t b = Serial2.read(); ansbuf[i] = b; i++; //Serial.print(sbyte2hex(b)); mp3answer+=sbyte2hex(b); } //odule->setDisplayToString(mp3answer,4); // if the answer format is correct. if ((ansbuf[0] == 0x7E) && (ansbuf[9] == 0xEF)) { //return true; return mp3answer; } //return false; //return "X"+mp3answer; } void machlicht(void) { fcount1++; if (fcount1 == (5-duty[0])) { digitalWrite(UV, HIGH );} if (fcount1 == (5-duty[1])) { digitalWrite(grun, HIGH );} if (fcount1 == (5-duty[2])) { digitalWrite(light, HIGH );} if (fcount1 >= 6 ) { digitalWrite(UV, LOW ); digitalWrite(grun, LOW ); digitalWrite(light, LOW ); fcount1 = 0; } } //MEOW! its free use it or parts if you want =^.^=