SPKT

Dependencies:   SDFileSystem_Warning_Fixed

Dependents:   DISCO-F746_WAV_PLAYER WAV

SD_PlayerSkeleton.cpp

Committer:
phungductung
Date:
2019-06-04
Revision:
18:6631cd0fbbcd
Parent:
17:abfd6af9a236

File content as of revision 18:6631cd0fbbcd:


//--------------------------------------------------------------

#include "SD_PlayerSkeleton.hpp"

namespace Mikami
{
    SD_PlayerSkeleton::SD_PlayerSkeleton(string str, bool resetButton)
        : BUFF_SIZE_(2048), sn_(BUFF_SIZE_),
          mySai_(SaiIO::OUTPUT, BUFF_SIZE_, AUDIO_FREQUENCY_44K),
          sdReader_(BUFF_SIZE_),
          fileName_(""),
          title_(214, 4, str, Label::CENTER, Font16),
          selector_(0, 22, 256, 37, sdReader_),
          menu_(BG_LEFT_, 2, BG_WIDTH_, BG_HEIGHT_, 5,
                (string[]){"SELECT", "PLAY", "PAUSE", "RESUME", "STOP"},
                0, 2, 1)
    {
        menu_.InactivateAll();
        menu_.Activate(0);

        if (!selector_.CreateTable())
            BlinkLabel errLabel(240, 100, "SD CARD ERROR", Label::CENTER);

        if (resetButton) reset_ = new ResetButton();
        else             reset_ = NULL;
    }

   // thực thi xử lý trình phát SD
    void SD_PlayerSkeleton::Execute()
    {
        bool playOk = false;
        int32_t loopCount;

        while (true)
        {
            if (playOk)             // Trong trường hợp PLAY sau PAUSE
                loopCount = SD_Open();
            else                    // Nếu không PLAY sau PAUSE
            {
                if (0 == WaitTouched()) SelectFile();
                loopCount = SD_Open();
                while (1 != WaitTouched()) {}   // Đợi cho đến khi PLAY được chạm
            }

            Display();  // Xử lý hiển thị dành riêng cho ứng dụng (chức năng ảo)
            if (reset_ != NULL) reset_->Draw();

            menu_.Inactivate(0);    // SELECT bị vô hiệu hóa
            menu_.Activate(2);      // PAUSE bị vô hiệu hóa
            menu_.Activate(4);      // STOP bị vô hiệu hóa

            playOk = false;
            bool stopOk = false;

            Clear();   // Quá trình xóa ứng dụng cụ thể (chức năng ảo)

            mySai_.PlayOut();       // Play

            // Lặp lại cho đến khi hết dữ liệu tệp
            for (int k=0; k<loopCount; k++)
            {
                int touch42 = -1;
                menu_.GetTouchedNumber(touch42);
                if (touch42 == 4) break;    // STOP
                if (touch42 == 2)           // PAUSE Xử lý khi chạm vào nút
                {
                    menu_.Inactivate(2);    // PAUSE Nút không hợp lệ
                    menu_.Activate(3);      // RESUME Đã bật nút
                    mySai_.PauseOut();

                    // PLAY Đợi cho đến khi 'RESUME' hoặc 'STOP' được chạm vào
                    switch (WaitTouched())
                    {
                        case 1: playOk = true;      // PLAY từ đầu
                                break;
                        case 3: mySai_.ResumeOut(); // Tiếp tục PLAY từ điểm PAUSE
                                menu_.Activate(2);
                                menu_.Inactivate(3);
                                menu_.TouchedColor(1);
                                break;
                        case 4: stopOk = true;      // STOP
                                break;
                    }
                }
                if (playOk || stopOk) break;

                DoIfHandled();
               // Xử lý tín hiệu cho một khối dành riêng cho ứng dụng (hàm ảo thuần túy)
                SignalProcessing();
            }
            mySai_.StopOut();
            if (!playOk) menu_.Activate(0); // SELECT hợp lệ
            menu_.Draw(1);                  // Khởi tạo màu của nút PLAY
            for (int n=2; n<5; n++)         // Các nút khác bị vô hiệu hóa
                menu_.Inactivate(n);

            sdReader_.Close();   // Đóng tệp SD
        }
    }

    // Lấy tên tệp đã chọn mà không cần phần mở rộng
    string SD_PlayerSkeleton::GetFileNameNoExt()
    {
        string fName = fileName_;
        int k = fName.rfind(".");
        if (k != string::npos)
            return fName.erase(k);
        else
            return fName;
    }

    // Mở tệp thẻ SD
    int32_t SD_PlayerSkeleton::SD_Open()
    {
        if (fileName_.empty()) SelectFile();
        sdReader_.Open(fileName_);
        sdReader_.IsWavFile();
        return sdReader_.GetSize()/BUFF_SIZE_;
    }

    // Lựa chọn tập tin
    void SD_PlayerSkeleton::SelectFile()
    {
        selector_.DisplayFileList();
        title_.Draw("Select file");
        menu_.Inactivate(0);        // SELECT vô hiệu hóa
        menu_.Inactivate(1);        // PLAY vô hiệu hóa
        do
            if (selector_.Select(fileName_))
                menu_.Activate(1);  // PLAY đã bật
        while (!menu_.Touched(1));  // PLAY Lặp lại cho đến khi nút được chạm

        selector_.Erase(0, 0, BG_LEFT_-4, 272);
        title_.Draw();
    }

    // Xử lý khi bảng điều khiển được vận hành
    void SD_PlayerSkeleton::DoIfHandled()
    {
        if (reset_ != NULL) reset_->DoIfTouched();
        Modefy();   // Ứng dụng thay đổi tham số cụ thể (chức năng ảo)
    }

    //Đợi trong khi thực hiện Do IfHandled () cho đến khi bất kỳ nút nào trên menu được chạm vào
    int SD_PlayerSkeleton::WaitTouched()
    {
        int touchNum;
        while (!menu_.GetTouchedNumber(touchNum)) DoIfHandled();
        return touchNum;
    }
}