#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region
' https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Information_derections
Sub Process_Globals
   Public Serial1 As Serial
    Private Softserial1, Softserial2 As SoftwareSerial
       Private Astream, NanoStream, mp3Stream As AsyncStreams
  
     Private udp As WiFiUDP
    Private UDPPort As Double = 11000
  
  
  
    'Private pin5 As Pin
    Private d1 As D1Pins
    
    Private wifi As ESP8266WiFi
    Private server As WiFiServerSocket
    Private eeprom As EEPROM
    
    
    
    Private timer1 As Timer
    Private bc As ByteConverter
    Private Volume As UInt
    Private Num As UInt
    Private State As Boolean  'ignore
    Private answer As Byte
    Private total_files, current As Int
    Private play As Boolean = True
    Private PLoop As Boolean = False
    
    
    Private Cmd As Byte  ' ignore
    Private Arg As UInt   ' ignore
    
    Private ssid As String = "myMP3" 'ignore
    Private password As String = "8512851285128512" 'ignore
    Private channel As Int = 4 'ignore
End Sub
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    mp3Stream.Initialize(Softserial2.Stream, "mp3_NewData", "mp3_Error")
'     
    'RunNative("Mp3_set_serial", Null)
    Delay(150)
    Volume = 25
    'RunNative("Mp3_set_volume", Null) 
    Delay(150)
    'PLoop = False
    'RunNative("Mp3_single_loop", Null)
    
    answer = 1
    'RunNative("Mp3_get_tf_sum",Null)
    
    current = 0
    Log("ok")
    
'    Cmd = 0x06
'    Arg = 14
    'RunNative("Mp3_send_cmd", Null)
    'RunNative("setSoftAP", Null)
    Softserial2.Listening = False
    Softserial1.Listening = True
End Sub
Sub mp3_NewData (Buffer() As Byte)
    Log(bc.HexFromBytes(Buffer))
    
    Log("return= " , answer)
    Select answer
        Case 0
            
        Case 1
            total_files = Buffer(5) + Buffer(6)
            Log( "totalFiles=",total_files)
        Case 2
            Log("current = " , Buffer(5) + Buffer(6))
            current = Buffer(5) + Buffer(6)
    End Select
    answer = 0
End Sub
Sub mp3_Error
    Log("p2 error")
End Sub
'Sub Udp_PacketArrived (Data() As Byte, Ip() As Byte, myPort As UInt)
'       
'    Log("udpArrived ",Data)
'    Private st As String = bc.StringFromBytes(Data)
'    'Private st2() As Byte = bc.SubString2(st,0,3)
'   
'    If bc.StartsWith(Data,"<") Then  ' sensitivity, loudness etz
'        NanoStream.Write(Data)
'        Log(">Nano:",st)
'    End If   
'       
'    If bc.StartsWith(Data,"Play_") Then
'        NanoStream.Write(bc.StringToBytes("news from nano"))
'        Log("PlayOrder:",st)
'        Num = bc.StringFromBytes(bc.SubString2(Data,5, 8)) ' "Play_003"
'        State = False
'        'RunNative("Mp3_single_loop", Null)
'        'RunNative("Mp3_play_1", Null)
'        play = True
'        PLoop = False
'        current = Num
'       
'    End If
'   
'
'   
'
'    Select st
'       
'       
'       
'       
'        Case  "Volinc"
'            Volume = Volume + 5
'            'RunNative("Mp3_set_volume", Null)
'             
'        Case  "Voldec"
'            Volume = Max(Volume - 5, 0)
'            'RunNative("Mp3_set_volume", Null)
'       
'        Case  "Loop"
'            If PLoop Then
'                State = False
'                'RunNative("Mp3_single_loop", Null)
'                PLoop = False
'            Else
'                State = True
'                'RunNative("Mp3_single_loop", Null)
'                PLoop = True
'            End If
'           
'        Case  "Playpause"
'            If play Then
'                'RunNative("Mp3_pause", Null)
'                play = False
'            Else
'                'RunNative("Mp3_play", Null)
'                play = True
'            End If
'           
'        Case "Stop"
'            'RunNative("Mp3_stop", Null)
'            current = 0
'            play = False
'            PLoop = False
'           
'        Case Else  ' song number
'
'    End Select
'    'Log("current= ",current)
'
'End Sub
'
''
#If C
#include <DFPlayer_Mini_Mp3.h>
void setSoftAP(B4R::Object* o) {
   WiFi.softAP(b4r_main::_ssid->data, b4r_main::_password->data, b4r_main::_channel);
}
void Mp3_set_serial(B4R::Object* o) {
   SoftwareSerial* soft = (SoftwareSerial*)b4r_main::_softserial2->getStream()->wrappedStream;
   mp3_set_serial(*soft);
}
//void Mp3_single_loop(B4R::Object* o){
//mp3_single_loop(b4r_main::_ploop);
//}
//void Mp3_send_cmd(B4R::Object* o) {
// mp3_send_cmd(b4r_main::_cmd, b4r_main::_arg);
//}
//void Mp3_send_cmd(B4R::Object* o) {
// mp3_send_cmd(b4r_main::_cmd);
//}
void Mp3_play_1(B4R::Object* o) {
 //mp3_play_physical(b4r_main::_num);
 mp3_play(b4r_main::_num);
}
void Mp3_play_2(B4R::Object* o) {
 mp3_single_play(b4r_main::_num);
}
void Mp3_play(B4R::Object* o) {
 mp3_single_play(b4r_main::_num);
}
void Mp3_set_volume(B4R::Object* o) {
 mp3_set_volume(b4r_main::_volume);
}
void Mp3_next(B4R::Object* o) {
 mp3_next();
}
void Mp3_prev(B4R::Object* o) {
 mp3_prev();
}
void Mp3_sleep(B4R::Object* o) {
 mp3_sleep();
}
void Mp3_reset(B4R::Object* o) {
 mp3_reset();
}
void Mp3_pause(B4R::Object* o) {
 mp3_pause();
}
void Mp3_random_play(B4R::Object* o) {
 mp3_random_play();
}
void Mp3_stop(B4R::Object* o) {
 mp3_stop();
}
void Mp3_single_loop(B4R::Object* o) {
 mp3_single_loop(b4r_main::_state);
}
void Mp3_get_tf_sum(B4R::Object* o) {
 mp3_get_tf_sum();
}
void Mp3_get_tf_current(B4R::Object* o) {
 mp3_get_tf_current();
}
#End If