美文网首页Air2028266
16-GPRS(Air202)关于多个文件中的变量调用和定时器

16-GPRS(Air202)关于多个文件中的变量调用和定时器

作者: 杨奉武 | 来源:发表于2018-11-16 14:00 被阅读20次

https://www.cnblogs.com/yangfengwu/p/9968405.html

因为自己看到好多问多个文件调用的,感觉这个应该说一说

对了大家有没有知道这个是干什么的

大家有没有看这篇https://www.cnblogs.com/yangfengwu/p/8965054.html

实际上,这是lua5.几新加的功能,就是为了省掉那些复杂的程序

我又新建了一个test1

让test  调用 test1里面的一个变量

下载进去

注意哈,这样是不允许的,再用test1请求test文件,然后调用test里面的值

如果想这样做,其实可以再做一个文件哈,里面放一个变量,然后test和test1都可以请求这个文件并操作这个变量

现在看定时器

我这里就直接说啦  一次性的定时器(执行一遍就完)(不带ID)

module(...,package.seeall)--[[

函数名:print

功能  :打印接口,此文件中的所有打印都会加上test前缀

参数  :无

返回值:无

]]

local function print(...)

    _G.print("test",...)

end

local function Timer1()

    print("一次的定时器,没有ID")

end

sys.timer_start(Timer1,10000)//延时10S调用Timer1函数

下载看看

一次性的不需要关闭,因为是自己关闭

然后来个循环的

module(...,package.seeall)--[[

函数名:print

功能  :打印接口,此文件中的所有打印都会加上test前缀

参数  :无

返回值:无

]]

local function print(...)

    _G.print("test",...)

end

local function Timer1()

    print("一次的定时器,没有ID")

end

sys.timer_start(Timer1,10000)

local function Timer2()

    print("这个是循环定时器,没有ID")

end

sys.timer_loop_start(Timer2,2000)

 现在用一个变量在里面加一,加到5停止这个定时器

module(...,package.seeall)--[[

函数名:print

功能  :打印接口,此文件中的所有打印都会加上test前缀

参数  :无

返回值:无

]]

local function print(...)

    _G.print("test",...)

end

local function Timer1()

    print("一次的定时器,没有ID")

end

sys.timer_start(Timer1,10000)

local cnt=0local function Timer2()

    print("这个是循环定时器,没有ID")

cnt=cnt+1

ifcnt>=5 then

        cnt=0        sys.timer_stop(Timer2)--直接填入函数停止

print("这个是循环定时器,没有ID,现在停止")    endendsys.timer_loop_start(Timer2,

2000)

现在说带ID的,可以看一下

module(...,package.seeall)--[[

函数名:print

功能  :打印接口,此文件中的所有打印都会加上test前缀

参数  :无

返回值:无

]]

local function print(...)

    _G.print("test",...)

end

local function Timer1()

    print("一次的定时器,没有ID")

end

sys.timer_start(Timer1,10000)

local cnt=0local function Timer2()

    print("这个是循环定时器,没有ID")

    cnt=cnt+1ifcnt>=5 then

        cnt=0        sys.timer_stop(Timer2)--直接填入函数停止

        print("这个是循环定时器,没有ID,现在停止")

    end

end

sys.timer_loop_start(Timer2,2000)

local cnt1=0local function Timer3()

    print("这个是循环定时器,有ID")

    cnt1=cnt1+1ifcnt1>=5 then

        cnt1=0        sys.timer_stop(1)--直接填入函数停止

        print("这个是循环定时器,有ID,现在停止")

    end

end

sys.timer_loop_start(Timer3,2000,1)

 所以

这样可以停止

好现在控制灯每进一次定时器就反转一次,就是周期性亮灭

local function Timer4()

    pio.pin.setval(1-pio.pin.getval(pio.P0_5),pio.P0_5)

end

sys.timer_loop_start(Timer4,2000,1)

1-pio.pin.getval(pio.P0_5)

pio.pin.getval(pio.P0_5)是读取引脚的高低电平状态  1-这个(0或者1) 正好是反转得到1和0

 最终所有的源码

module(...,package.seeall)--[[

函数名:print

功能  :打印接口,此文件中的所有打印都会加上test前缀

参数  :无

返回值:无

]]

local function print(...)

    _G.print("test",...)

end

local function Timer1()

    print("一次的定时器,没有ID")

end

sys.timer_start(Timer1,10000)

local cnt=0local function Timer2()

    print("这个是循环定时器,没有ID")

    cnt=cnt+1ifcnt>=5 then

        cnt=0        sys.timer_stop(Timer2)--直接填入函数停止

        print("这个是循环定时器,没有ID,现在停止")

    end

end

sys.timer_loop_start(Timer2,2000)

local cnt1=0local function Timer3()

    print("这个是循环定时器,有ID")

    cnt1=cnt1+1ifcnt1>=5 then

        cnt1=0        sys.timer_stop(Timer3,1)

        print("这个是循环定时器,有ID,现在停止")

    end

end

sys.timer_loop_start(Timer3,2000,1)

pio.pin.setdir(pio.OUTPUT,pio.P0_5)

pio.pin.setval(0,pio.P0_5)

local function Timer4()

    pio.pin.setval(1-pio.pin.getval(pio.P0_5),pio.P0_5)

end

sys.timer_loop_start(Timer4,2000,1)

定时器就说到这里哈

资料源码链接

链接:https://pan.baidu.com/s/1-SRfsKGQ7rZVvFmp1ObHWw密码:p9qs

基础教程源码链接如果失效,请在淘宝介绍中下载,由于链接还是失效,请联系卖家,谢谢

https://item.taobao.com/item.htm?spm=a1z10.1-c-s.w4004-18540610442.6.36a74814ZSaRsu&id=569295486025

https://www.cnblogs.com/yangfengwu/p/9968883.html

相关文章

网友评论

    本文标题:16-GPRS(Air202)关于多个文件中的变量调用和定时器

    本文链接:https://www.haomeiwen.com/subject/zpkmfqtx.html