본문 바로가기
아두이노

아두이노 Serial MP3 모듈 -#3

by 오징어땅콩2 2020. 2. 6.
반응형

이전 소스코드에서 공부해야 할 것은 3가지다.

1. 메뉴 출력함수

2. 시리얼 명령창 입력 함수

3. 메인 루프

 

단순한 시리얼 출력 문장이다. 이것 모르는 사람은 없을것으로 본다.

void display_menu()
{
    Serial.print("==========MP3 PLAY 프로그램==========\n\n");   
    Serial.print("     SERIAL MP3 module     \n"); 
    Serial.print("********************\n"); 
    Serial.print("*      1.play     *\n"); 
    Serial.print("*      2.NEXT_SONG      *\n"); 
    Serial.print("*      3.PREV_SONG  *\n"); 
    Serial.print("********************\n"); 
    Serial.print("메뉴을 선택하세요 : "); 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

시리얼 명령입력 함수는 약간 어려울수도 있는데, 2중 while믄징으로 되어 있지만, 

그렇게 복잡하지는 않다. 

처음 while 문장은 엔터를 입력 할때 까지 무한 반복이다.

두번째 while  문장은 시리얼창으로 입력하는 숫자를 모으는 것이다.

여기서 백슬레쉬에 대한 방어 코드는 없다. 

그리고 문자를 입력 했을떄에 대한 방어 코드도 없다.

필요하면 다음에 만들어 보기로 하고 그냥 숫자만 입력하자.

 

void serial_command(char* buf)
{
    int tempPos = 0;
    while1)
    {
        while(Serial.available())
        {
            char c = Serial.read(); 
            if (tempPos < 1000) { buf[tempPos] = c; tempPos++;   }
        }
        buf[tempPos] = 0;
        if (tempPos > 0 && buf[tempPos-2]=='\r'  && buf[tempPos-1]=='\n'break;
    }  
    return ; 
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

이것도 크게 어려운 부분은 없다. 그냥 선택된 메뉴에 따라서 곡을 재생시키는 명령어가 들어 있다.

void loop()
{
    int menuType;
    char message[25];
    
    display_menu();
    serial_command(message);
    menuType = atoi(message);//문자열을 숫자로 
    
    if(!strcmp(message,"\r\n")) return ;//엔터만 입력할 경우 재입력 
    else 
    {
      Serial.print(" 선택하였습니다\r\n");
    }
       
    if(menuType > 0 && menuType < 4
    {  
      if(menuType ==1
      {
          sendCommand(CMD_PLAY_WITHVOLUME, 0X0F02);//play the first song with volume 15 class
      }
      else if(menuType ==2
      {
          sendCommand(NEXT_SONG, 0X0000);
      } 
       else if(menuType ==3
      {
          sendCommand(PREV_SONG, 0X0000);
      }             
    }
    return ;      
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

댓글