_ENCRYPT_=0    --LUA脚本加密

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

function on_init()

   EN_ON_UPDATE_API = 0

    local _val = get_uint16(VT_RW, 0x2000)

    -- 没存修改过密码，掉电寄存器更新为工程界面配置的原始密码
    if _val ~= 0x0055
    then
        -- 初始化函数中，给对应内部寄存器赋密码初值
        set_uint32(VT_RW, 0x0000, 111111)
        set_uint32(VT_RW, 0x0002, 222222)
        set_uint32(VT_RW, 0x0004, 333333)
        set_uint32(VT_RW, 0x0006, 444444)
        set_uint32(VT_RW, 0x0008, 555555)

        set_pwd(0, 111111, 6)
        set_pwd(1, 222222, 6)
        set_pwd(2, 333333, 6)
        set_pwd(3, 444444, 6)
        set_pwd(4, 555555, 6)

        set_uint16(VT_RW, 0x2000, 0x0055)
        flash_sync()
    else
        set_pwd(0, get_uint32(VT_RW, 0x0000), 6)
        set_pwd(1, get_uint32(VT_RW, 0x0002), 6)
        set_pwd(2, get_uint32(VT_RW, 0x0004), 6)
        set_pwd(3, get_uint32(VT_RW, 0x0006), 6)
        set_pwd(4, get_uint32(VT_RW, 0x0008), 6)

    end
    
    
    EN_ON_UPDATE_API = 1
end

function on_run(screen)
end

function on_update(slave,vtype,addr)

    if EN_ON_UPDATE_API == 0
    then
        return
    end

    if vtype == VT_LW
    then

        if addr == 0x1000
        then
            local key = get_uint16(VT_LW, addr)
            if key == 1
            then
                set_uint32(VT_RW, 0x0000, 111111)
                set_uint32(VT_RW, 0x0002, 222222)
                set_uint32(VT_RW, 0x0004, 333333)
                set_uint32(VT_RW, 0x0006, 444444)
                set_uint32(VT_RW, 0x0008, 555555)

                set_pwd(0, get_uint32(VT_RW, 0x0000), 6)
                set_pwd(1, get_uint32(VT_RW, 0x0002), 6)
                set_pwd(2, get_uint32(VT_RW, 0x0004), 6)
                set_pwd(3, get_uint32(VT_RW, 0x0006), 6)
                set_pwd(4, get_uint32(VT_RW, 0x0008), 6)
                flash_sync()
            end
        end
    end

    if vtype == VT_RW
    then
        if addr >= 0x0000 and addr <= 0x0008
        then
            local psw = get_uint32(vtype, addr)
            local idx = (addr // 0x0002)
            set_pwd(idx, psw, 6)    -- 设置对应等级密码
            flash_sync()
        end
    end
end

function on_draw(screen_id,control_id)
end

