_ENCRYPT_=0    --LUA脚本加密

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

_TIMER_CNT_    = 0

function on_init()
    set_notify(0)
    set_string(VT_LW, 0x1010, '00:00:00')
    set_notify(1)
end

function on_run(screen)

end

function on_timer(timer_id)

    if timer_id == 0
    then
        _TIMER_CNT_ = _TIMER_CNT_ + 1--累计回调次数
        local remaining = _TIMER_REPEAT_ - _TIMER_CNT_--剩余次数

        set_notify(0)
        set_string(VT_LW, 0x1010, string.format('%02d', (remaining // (60*60))) ..':'.. string.format('%02d', ((remaining // (60)) % 60)) ..':'.. string.format('%02d', (remaining % 60))) --显示倒计时

        if _TIMER_CNT_ == _TIMER_REPEAT_
        then

            set_uint16(VT_LW, 0x1002, 0x00)--复位定时器按钮
            set_notify(1)
            set_uint16(VT_LW, 0x1003, 0x01)--发指令通知主板
        end
    end

end

function on_update(slave,vtype,addr)

    if vtype == VT_LW
    then
        if addr == 0x1002
        then
            local val = get_uint16(vtype,addr)
            local hour = get_uint16(vtype,0x1000)
            local minu = get_uint16(vtype,0x1001)
            _TIMER_REPEAT_ = hour*60*60 + minu*60  --计算倒计时,单位s

            set_notify(0) --设置倒计时，不发出指令通知
            set_string(VT_LW, 0x1010, string.format('%02d', (_TIMER_REPEAT_ // (60*60))) ..':'.. string.format('%02d', ((_TIMER_REPEAT_ // (60)) % 60)) ..':'.. string.format('%02d', (_TIMER_REPEAT_ % 60)))

            if val == 1
            then
                if _TIMER_REPEAT_ > 0 --倒计时大于1才执行
                then
                    _TIMER_CNT_ = 0
                    stop_timer(0)
                    start_timer(0, 1000, 0, _TIMER_REPEAT_)
                end
            else
                stop_timer(0)
            end
        end
    end

end

function on_draw(screen_id,control_id)
end

