_ENCRYPT_=0    --LUA脚本加密

--数据类型定义
VT_LW = 1    --变量地址
VT_RW = 2    --FLASH存储
sc_music = 0

musicNameListTb = {}
music_cnt     = 0
music_curpage = 1 --页码
music_allpage = 1 --总页数
curPalyIindex = 0 --当前播放的音频索引
play_color   = 0x067F --选中播放歌曲名称颜色
noplay_color = 0x8CF1 --未选中播放

--@set_bit:设置位状态
--data:源数据
--bitmask：第几个位，如bit0，填0; bit1,填1;
--state:置0/1
function set_valbit(data, bitmask, state)

    bitmask  = (1 << bitmask) & 0xFF

    if state == 0
    then
        data = data & (~bitmask)
    elseif state == 1
    then
        data = data | bitmask
    end

    return data
end

-- 获取扩展名 
-- 自定义函数（非回调函数，可以修改）
-- @str 需要获取后缀名的字符串
function get_extension(str)

    local file_type = ''
    local token_s, token_e = string.find(str, "%.", 1)

    if token_s ~= nil
    then
        file_type = string.sub(str, (token_s + 1), string.len(str)) 
    end

    return file_type
end

--刷新音乐名称列表
function updatMisucNameColor(index)

    if index > 0
    then
        local line = index % 5
    
        if line == 0
        then
            line = 5
        end
        
        for i = 1, 5
        do
            if i == line
            then
                wgt_set_fcolor(sc_music, i, play_color)
            else
                wgt_set_fcolor(sc_music, i, noplay_color)
            end
        end
    else
        for i = 1, 5
        do
            wgt_set_fcolor(sc_music, i , noplay_color) 
        end
    end
    
end

--刷新播放歌曲名称的颜色
function updatMisucList(page)

    if music_cnt > 0 
    then
        local index = (page - 1) * 5 
        
        for i = 1, 5 
        do
            if (index + i) <= music_cnt 
            then
                local name = string.match(musicNameListTb[index + i], ".+/(.+)")
                set_string(VT_LW, 0x1100 + (i - 1)*(0x40), name)
                set_uint16(VT_LW, 0x1500 + (i - 1)*1,   1)
            else
                set_string(VT_LW, 0x1100 + (i - 1)*(0x40), '')
                set_uint16(VT_LW, 0x1500 + (i - 1)*1,   0)
            end
        end

        if curPalyIindex > 0
        then
            local cur_play_page = math.modf(curPalyIindex / 5)
            if curPalyIindex % 5 > 0
            then
                cur_play_page = cur_play_page + 1
            end

            if cur_play_page == page
            then
                updatMisucNameColor(curPalyIindex)
            else
                updatMisucNameColor(0)
            end
        end
    end
end

--刷新播放状态、检测播放完自动播放下一首
function updatMisucPlayState()

    local SysSndState     = get_uint16(VT_LW, 0x0142)    --获取播放状态
    local SysSndPlayTime  = get_uint16(VT_LW, 0x0143)    --获取当前播放的时间
    local SysSndTotalTime = get_uint16(VT_LW, 0x0144)    --获取当前播放的总时间


    set_string(VT_LW, 0x1240, string.format('%02d', (SysSndPlayTime // 60))..':'..(string.format('%02d', (SysSndPlayTime %60))))   --显示播放的当前的时间"xx:xx"
    set_string(VT_LW, 0x1250, string.format('%02d', (SysSndTotalTime // 60))..':'..(string.format('%02d', (SysSndTotalTime %60)))) --显示播放的总时间"xx:xx"

    if ((SysSndState >> 6) & 0x01) == 0x01
    then
        curPalyIindex = curPalyIindex + 1 --下一首索引+1
        if curPalyIindex > music_cnt    --大于歌曲总数,播放第1首
        then
            curPalyIindex = 1
        end

        local cur_play_page = math.modf(curPalyIindex / 5)
        if curPalyIindex % 5 > 0
        then
            cur_play_page = cur_play_page + 1
        end

        local cur_page = get_uint16(VT_LW, 0x1003)
        if cur_play_page == cur_page
        then
            updatMisucNameColor(curPalyIindex)
        else
            updatMisucNameColor(0)
        end

        play_sound(musicNameListTb[curPalyIindex])
        SysSndState = set_valbit(SysSndState, 6, 0) --bit6 置为0
        set_uint16(VT_LW, 0x0142, SysSndState)    --复位播放状态
    end
    redraw()
end

--@path    :文件路径
--@filename:文件名称
--@type    :0 文件夹，1 文件
--@fsize   :文件大小
function on_list_dir(path, filename, type, fsize)

    if type == 1
    then
        local cur_file_type = get_extension(filename)
        print('filename = '..filename)
        if cur_file_type == 'mp3'
        then
            music_cnt = music_cnt + 1
            musicNameListTb[music_cnt] = path..'/'..filename
            print(' music_cnt = '..music_cnt..' /  path = '.. musicNameListTb[music_cnt])
        end 
    end

end


function on_run(screen)
    updatMisucPlayState()
end

function on_update(slave,vtype,addr)

    if VT_LW == vtype
    then
        if addr == 0x1000 --翻页
        then
            local val      = get_uint16(VT_LW, 0x1000)
            local cur_page = get_uint16(VT_LW, 0x1003)
            local all_page = get_uint16(VT_LW, 0x1004)

            if val == 0    --上一页
            then
                cur_page = cur_page - 1
                if cur_page <= 1
                then
                    cur_page = 1
                end
                set_uint16(VT_LW, 0x1003, cur_page)
                updatMisucList(cur_page) 

            elseif val == 1    --下一页
            then
                cur_page = cur_page + 1
                if cur_page > all_page
                then
                    cur_page = all_page
                end
                set_uint16(VT_LW, 0x1003, cur_page)
                updatMisucList(cur_page) 
            end

        elseif addr == 0x1505    --点击列表播放
        then
            local val = get_uint16(VT_LW, 0x1505)
            local cur_page = get_uint16(VT_LW, 0x1003)
            local temp = curPalyIindex
            set_uint16(VT_LW, 0x1002, 1) --设置播放按钮

            curPalyIindex = (cur_page - 1) * 5 + val
            if curPalyIindex <= music_cnt
            then
                if string.len(musicNameListTb[curPalyIindex]) > 0
                then
                    print('.... curPalyIindex = '..curPalyIindex)
                    updatMisucNameColor(curPalyIindex)
                    if curPalyIindex <= music_cnt
                    then
                        stop_sound()
                        play_sound(musicNameListTb[curPalyIindex])
                    end
                end
            else
                curPalyIindex = temp
            end

        elseif addr == 0x1001
        then
            local val = get_uint16(VT_LW, 0x1001)
            set_uint16(VT_LW, 0x1002, 1) --设置播放按钮

            if val == 0    --上一首
            then
                curPalyIindex = curPalyIindex - 1
                if curPalyIindex < 1
                then
                    curPalyIindex = music_cnt
                end

            elseif val == 1    --下一首
            then
                curPalyIindex = curPalyIindex + 1
                if curPalyIindex > music_cnt
                then
                    curPalyIindex = 1
                end
            end

            --刷新当前页
            local cur_play_page = math.modf(curPalyIindex / 5)
            if curPalyIindex % 5 > 0
            then
                cur_play_page = cur_play_page + 1
            end
            set_uint16(VT_LW, 0x1003, cur_play_page)
            play_sound(musicNameListTb[curPalyIindex])
            updatMisucList(cur_play_page)
            updatMisucNameColor(curPalyIindex)

        elseif addr == 0x1002  --播放按钮
        then
            local val = get_uint16(VT_LW, 0x1002)
            if val == 0
            then
                pause_sound()
                
            elseif val == 1
            then
                resume_sound()
                --play_sound(musicNameListTb[curPalyIindex])
            end
        end
    end
end

-- 系统回调函数，插入SD卡自动回调
function on_sd_inserted(dir)

    sd_dir = dir
    list_dir(sd_dir)

    if music_cnt > 0
    then

        music_allpage = math.modf(music_cnt / 5)
        
        if music_cnt % 5 > 0
        then
            music_allpage = music_allpage + 1
        end

        set_uint16(VT_LW, 0x1003, 1)
        set_uint16(VT_LW, 0x1004, music_allpage)
        set_uint16(VT_LW, 0x1005, 1)
        updatMisucList(music_curpage)
        updatMisucNameColor(0)
    end
end

-- 系统回调函数，拔掉SD卡自动回调
function on_sd_removed()

    --复位清除相关变量
    musicNameListTb      = {}
    music_cnt     = 0
    music_curpage = 1
    music_allpage = 1
    curPalyIindex = 0

    set_uint16(VT_LW, 0x1003, 0)
    set_uint16(VT_LW, 0x1004, 0)
    set_uint16(VT_LW, 0x1005, 0)

    for i = 1, 5 
    do
        set_string(VT_LW, 0x1100 + (i - 1)*(0x40), '')
        set_uint16(VT_LW, 0x1500 + (i - 1)*1,   0)
    end

end
