airwan.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. local airwan = {}
  2. dnsproxy = require("dnsproxy")
  3. dhcpsrv = require("dhcpsrv")
  4. httpplus = require("httpplus")
  5. local run_state = false
  6. local wan_state = false
  7. local wifi_net_state = "未打开"
  8. local bytes = 0
  9. local ms_duration = 0
  10. local bandwidth = 0
  11. local event = ""
  12. local function start_wan()
  13. sys.wait(500)
  14. log.info("ch390", "打开LDO供电")
  15. gpio.setup(140, 1, gpio.PULLUP) --打开ch390供电
  16. sys.wait(2000)
  17. local result = spi.setup(
  18. 1,--spi_id
  19. nil,
  20. 0,--CPHA
  21. 0,--CPOL
  22. 8,--数据宽度
  23. 25600000--,--频率
  24. -- spi.MSB,--高低位顺序 可选,默认高位在前
  25. -- spi.master,--主模式 可选,默认主
  26. -- spi.full--全双工 可选,默认全双工
  27. )
  28. log.info("main", "open",result)
  29. if result ~= 0 then--返回值为0,表示打开成功
  30. log.info("main", "spi open error",result)
  31. return
  32. end
  33. -- 初始化指定netdrv设备,
  34. -- socket.LWIP_ETH 网络适配器编号
  35. -- netdrv.CH390外挂CH390
  36. -- SPI ID 1, 片选 GPIO12
  37. netdrv.setup(socket.LWIP_ETH, netdrv.CH390, {spi=1,cs=12})
  38. netdrv.dhcp(socket.LWIP_ETH, true)
  39. while 1 do
  40. local result, ip, adapter = sys.waitUntil("IP_READY", 3000)
  41. log.info("ready?", result, ip, adapter)
  42. if adapter and adapter == socket.LWIP_ETH then
  43. break
  44. end
  45. end
  46. iperf.client(socket.LWIP_ETH, "192.168.4.1")
  47. log.info("创建完成,iperf 客户端创建完成")
  48. wifi_net_state = "创建完成,iperf 客户端创建完成"
  49. end
  50. local function stop_wan()
  51. end
  52. sys.subscribe("IPERF_REPORT", function(bytes, ms_duration, bandwidth)
  53. log.info("iperf", bytes, ms_duration, bandwidth)
  54. bytes = bytes
  55. ms_duration = ms_duration
  56. bandwidth = bandwidth
  57. end)
  58. function airwan.run()
  59. log.info("airwan.run")
  60. lcd.setFont(lcd.font_opposansm12_chinese) -- 具体取值可参考api文档的常量表
  61. run_state = true
  62. while true do
  63. sys.wait(10)
  64. -- airlink.statistics()
  65. lcd.clear(_G.bkcolor)
  66. lcd.drawStr(0,80,"以太网WAN状态:"..wifi_net_state )
  67. if wan_state then
  68. lcd.drawStr(0,120,"bytes:" .. bytes )
  69. lcd.drawStr(0,140,"ms_duration:" .. ms_duration )
  70. lcd.drawStr(0,160,"bandwidth:" .. bandwidth )
  71. lcd.drawStr(0,180,"空间 LUA:" .. rtos.meminfo() .. ",SYS:" .. rtos.meminfo("sys") )
  72. lcd.drawStr(0,200, event)
  73. end
  74. lcd.showImage(20,360,"/luadb/back.jpg")
  75. if wan_state then
  76. lcd.showImage(130,370,"/luadb/stop.jpg")
  77. else
  78. lcd.showImage(130,370,"/luadb/start.jpg")
  79. end
  80. lcd.showImage(0,448,"/luadb/Lbottom.jpg")
  81. lcd.flush()
  82. if not run_state then -- 等待结束
  83. return true
  84. end
  85. end
  86. end
  87. local function start_wan_task()
  88. wan_state = true
  89. start_wan()
  90. end
  91. local function stop_wan_task()
  92. -- stop_wan()
  93. wan_state = false
  94. end
  95. function airwan.start_wan()
  96. start_wan()
  97. end
  98. function airwan.tp_handal(x,y,event) -- 判断是否需要停止播放
  99. if x > 20 and x < 100 and y > 360 and y < 440 then
  100. run_state = false
  101. elseif x > 130 and x < 230 and y > 370 and y < 417 then
  102. if wan_state then
  103. sysplus.taskInitEx(stop_wan_task, "stop_wan_task")
  104. else
  105. sysplus.taskInitEx(start_wan_task , "start_wan_task")
  106. end
  107. end
  108. end
  109. return airwan