_ENCRYPT_=0    --LUA脚本加密

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

penColor = {0xF800, 0x001F}
draw_type = 0  --对应的绘制类型
surface = {0, 0} --绘图指针，surface[1] ,jpg  surface[2] ,png 
CUR_COLOR = 0xF800 --初始颜色，

mode = {  --绘制类型表
    line          = 1,
    rect          = 2,
    rect_alpha    = 3,
    circle        = 4,
    ellipse       = 5,
    imageId       = 6,
    text          = 7,
    surface_path3 = 8,
    destroyOne    = 9,
    destroyAll    = 10,
    getSurface    = 11,
    videoShoot    = 12,
    screenShoot   = 13,

    surface_pathsd  = 20,
    surface_pathusb = 21
    
}

function on_init()
end

function on_run(screen)
end

function on_update(slave,vtype,addr)

    if addr == 0x6100
    then
        CUR_COLOR = penColor[get_uint16(VT_LW, addr) + 1]

    elseif addr == 0x6101
    then
        draw_type = get_uint16(VT_LW, addr)
        
        local msg = ''
        if draw_type == mode.surface_path3    --屏内路径
        then
            if surface[1] > 0
            then
                destroy_surface(surface[1])
            end
            surface[1] = load_surface('3:/test.jpg')

            if surface[2] > 0
            then
                destroy_surface(surface[2])
            end
            surface[2] = load_surface('3:/test.png')

        elseif draw_type == mode.surface_pathsd    --SD根目录路径
        then
            if surface[1] > 0
            then
                destroy_surface(surface[1])
            end
            surface[1] = load_surface('1:/test.jpg')

            if surface[2] > 0
            then
                destroy_surface(surface[2])
            end
            surface[2] = load_surface('1:/test.png')

        elseif draw_type == mode.surface_pathusb --U盘根目录路径
        then
            if surface[1] > 0
            then
                destroy_surface(surface[1])
            end
            surface[1] = load_surface('2:/test.jpg')

            if surface[2] > 0
            then
                destroy_surface(surface[2])
            end
            surface[2] = load_surface('2:/test.png')
        end

    elseif addr == 0x6102    --销毁第2个绘图指针
    then
        local _destroy = get_uint16(VT_LW, addr)
        local msg = ''

        if _destroy == mode.destroyOne
        then
            if surface[2] > 0
            then
                destroy_surface(surface[2])
                msg = msg..' destroy pic2 '
            else
                msg = msg..'pic2 is nil ！！！ / '
            end
        elseif _destroy == mode.destroyAll    --销毁所有绘图指针
        then 
            destroy_all_surface()
        elseif _destroy == mode.getSurface    --获取绘制的图片分辨率
        then
            local cur_w1,cur_h1,cur_w2,cur_h2 = 0,0,0,0

            if surface[1] > 0
            then
                cur_w1,cur_h1 = get_surface_size(surface[1])
                msg = msg..'pic1 size = '..math.ceil(cur_w1)..' * '..math.ceil(cur_h1)..' / '
            else
                msg = msg..'pic1 size is nil ！！！ / '
            end
            
            if surface[2] > 0
            then
                cur_w2,cur_h2 = get_surface_size(surface[2])
                msg = msg..'pic2 size = '..math.ceil(cur_w2)..' * '..math.ceil(cur_h2)..' / '
            else
                msg = msg..'pic2 size is nil ！！！'
            end

            set_string(VT_LW, 0x6103, msg)
        elseif _destroy == mode.videoShoot
        then
            --video_shoot('1:/video_shoot.jpg', 480, 320)--AV视频截图,需要定制特殊固件支持
        elseif _destroy == mode.screenShoot    --截屏显示
        then
            screen_shoot('1:/screen_shoot.jpg')
        end
    end

    redraw()
end

function on_draw(screen_id,control_id)

    set_pen_color(CUR_COLOR)

    local switch = {
        [mode.line] = function(control)
            draw_line(225, 253, 405, 253)
            draw_line(508, 128, 508, 378, 5)
        end,

        [mode.rect] = function(control)
            draw_rect(225, 128, (225+180), (128+250), 0)
            draw_rect(418, 163, (418+180), (128+180), 1)
        end,

        [mode.rect_alpha] = function(control)
            draw_rect_alpha(225, 128, (225+180), (128+250), 30)
            draw_rect_alpha(418, 163, (418+180), (128+180), 60)
        end,

        [mode.circle] = function(control)
            draw_circle(300, 253, 100, 0)
            draw_circle(450, 253, 150, 1)
        end,

        [mode.ellipse] = function(control)
            draw_ellipse(225, 128, (225+180), (128+250), 0)
            draw_ellipse(418, 163, (418+180), (128+180), 1)
        end,

        [mode.imageId] = function(control)
            draw_image(1, 0, 225, 128, (225+180), (128+250), 0, 0)
            draw_image(2, 0, 418, 163, (418+180), (128+180), 0, 0)
        end,

        [mode.text] = function(control)
            draw_text('Hello VisualHMI\nCopyright? gz-dc.com ', 195, 96, 408 ,318, 0, 32, 0xF800, 5, 1) 
        end,
        
        [mode.surface_path3] = function(control)
            if screen_id == 0 and control_id == 1
            then
                draw_surface(surface[1], 293, 88, 222, 353, 0, 0)  --裁剪显示
            elseif screen_id == 0 and control_id == 2
            then
                draw_surface(surface[2], 314, 158, 180, 250, 0, 0) --裁剪显示
            end

        end,

        [mode.surface_pathsd] = function(control)
            draw_surface(surface[1], 175, 60, 222, 353, 0, 0)  --裁剪显示
            draw_surface(surface[2], 418, 128, 180, 250, 0, 0) --裁剪显示
        end,

        [mode.surface_pathusb] = function(control)
            draw_surface(surface[1], 175, 60, 222, 353, 0, 0)  --裁剪显示
            draw_surface(surface[2], 418, 128, 180, 250, 0, 0) --裁剪显示
        end
    }

    if switch[draw_type]
    then
        switch[draw_type](control)
    end
end

