Share My Creation simulator using the DFPlayer Mini Mp3 Player

Hi everyone, how are you?
I want to share my engine simulator using the DFPlayer Mini Mp3 Player module in B4R.



B4R:
Sub Process_Globals
    Public Serial1 As Serial
    Public MP3 As SoftwareSerial
    Public AStream As AsyncStreams
    Private bc As ByteConverter
    Public Rx As Byte = 16     ' RX do DFPlayer
    Public Tx As Byte = 17     ' TX do DFPlayer

    Public BtnOnOff As Pin
    Public BtnMais As Pin
    Public BtnMenos As Pin

    Public motorLigado As Boolean = False
    Public nivelSom As Byte = 0
    Public somAtual As Byte = 0
    
    
    ' Comandos MP3 — Pasta 1
    Private Cmd1 () As Byte = Array As Byte(0x7E,0xFF,0x06,0x0F,0x00,0x01,0x01,0xEF) 'Ligar Motor
    Private Cmd2 () As Byte = Array As Byte(0x7E,0xFF,0x06,0x0F,0x00,0x02,0x02,0xEF) 'Desligar Motor
    Private Cmd3 () As Byte = Array As Byte(0x7E,0xFF,0x06,0x0F,0x00,0x03,0x03,0xEF)
    Private Cmd4 () As Byte = Array As Byte(0x7E,0xFF,0x06,0x0F,0x00,0x04,0x04,0xEF)
    Private Cmd5 () As Byte = Array As Byte(0x7E,0xFF,0x06,0x0F,0x00,0x05,0x05,0xEF)
    Private Cmd6 () As Byte = Array As Byte(0x7E,0xFF,0x06,0x0F,0x00,0x06,0x06,0xEF)
    Private Cmd7 () As Byte = Array As Byte(0x7E,0xFF,0x06,0x0F,0x00,0x07,0x07,0xEF)
    Private Cmd8 () As Byte = Array As Byte(0x7E,0xFF,0x06,0x0F,0x00,0x08,0x08,0xEF)
    Public timerSom As Timer
    Public TempoPartida As Int = 10000 ' tempo em ms para o som de partida (Cmd1)
    
    Public NivelAtual As Byte = 0
    Public UltimoNivel As Byte = 0
    Public NivelAtual As Byte = 3   ' marcha lenta inicial
    Public UltimoNivel As Byte = 255 ' valor inválido pra forçar atualização na 1ª vez
    
End Sub

Sub AppStart
    Serial1.Initialize(115200)
   'Inicializa a primeira SoftwareSerial
    MP3.Initialize(9600, Rx, Tx)
    AStream.Initialize(MP3.Stream, "AstreamCom_NewData", Null)

    BtnOnOff.Initialize(13, BtnOnOff.MODE_INPUT_PULLUP)
    BtnOnOff.AddListener("Ligar_Desligar")
    
    BtnMais.Initialize(12, BtnMais.MODE_INPUT_PULLUP)
    BtnMais.AddListener("Acelerar_Mas")
    
    BtnMenos.Initialize(11, BtnMenos.MODE_INPUT_PULLUP)
    BtnMenos.AddListener("Desacelerar_Menos")
    
    ' Inicializa o timer que mantém o som da marcha lenta
    timerSom.Initialize("timerSom_Tick", 2000) ' repete a cada 3 segundos
    timerSom.Enabled = False
    
    Log("Sistema de som do motor iniciado.")
End Sub

Sub AstreamCom_NewData (Buffer() As Byte)
    Dim Recebido As String = bc.StringFromBytes(Buffer)
    Log("Comando Recebido: ", Recebido)
End Sub
'---------------------------------------------------
' Botão Liga / Desliga
'---------------------------------------------------
Sub Ligar_Desligar(State As Boolean)
    If BtnOnOff.DigitalRead = False Then
        If motorLigado = False Then
            LigarMotor
        Else
            DesligarMotor
        End If
        Delay(400)
    End If
End Sub
'---------------------------------------------------
' Liga o motor
'---------------------------------------------------
Sub LigarMotor
    AStream.Write(Cmd1)
    Log("Som de partida (Cmd1)")
    Delay(TempoPartida)
    motorLigado = True
    nivelSom = 1
    TocarNivel
    timerSom.Enabled = True
    Log("Motor ligado - marcha lenta CMD3")
End Sub
'---------------------------------------------------
' Desliga o motor
'---------------------------------------------------
Sub DesligarMotor
    AStream.Write(Cmd2)
    Log("Motor desligado - Cmd2")
    motorLigado = False
    nivelSom = 0
    timerSom.Enabled = False
End Sub
'---------------------------------------------------
' Mantém o som da marcha lenta em loop
'---------------------------------------------------
Sub timerSom_Tick
    If motorLigado = True And nivelSom = 1 Then
        AStream.Write(Cmd3)
        Log("Loop marcha lenta (Cmd3)")
    End If
End Sub
'---------------------------------------------------
' Botão Acelerar
'---------------------------------------------------
Sub Acelerar_Mas(State As Boolean)
    If BtnMais.DigitalRead = False And motorLigado = True Then
        If nivelSom < 6 Then
            nivelSom = nivelSom + 1
            timerSom.Enabled = False
            TocarNivel
            Log("Acelerando - nível: ", nivelSom)
        End If
        Delay(400)
    End If
End Sub
'---------------------------------------------------
' Botão Desacelerar
'---------------------------------------------------
Sub Desacelerar_Menos(State As Boolean)
    If BtnMenos.DigitalRead = False And motorLigado = True Then
        If nivelSom > 1 Then
            nivelSom = nivelSom - 1
            timerSom.Enabled = False
            TocarNivel
            Log("Desacelerando - nível: ", nivelSom)
            If nivelSom = 1 Then
                timerSom.Enabled = True ' volta pra marcha lenta
            End If
        End If
        Delay(400)
    End If
End Sub
'---------------------------------------------------
' Reproduz o som conforme o nível atual
'---------------------------------------------------
Sub TocarNivel
    Select nivelSom
        Case 1
            AStream.Write(Cmd3)
            Delay(200)
        Case 2
            AStream.Write(Cmd4)
            Delay(200)
        Case 3
            AStream.Write(Cmd5)
            Delay(200)
        Case 4
            AStream.Write(Cmd6)
            Delay(200)
        Case 5
            AStream.Write(Cmd7)
            Delay(200)
        Case 6
            AStream.Write(Cmd8)
            Delay(200)
    End Select
    Log("Tocando nível: ", nivelSom)
End Sub
 
Top