task_control.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. -- 业务逻辑触发方式配置
  2. -- 配置说明:
  3. -- 1. 设置为true: 开机自动创建ap热点,初始化sd卡,创建http 文件服务器
  4. -- 2. 设置为false: 默认不创建ap热点,不初始化sd卡,不创建http 文件服务器;通过短按boot按键控制开关
  5. -- 导入exremotefile库
  6. local exremotefile = require "exremotefile"
  7. local AUTO_START = false -- 默认使用boot按键控制方式
  8. -- 系统状态变量
  9. local is_running = false -- 标记系统是否正在运行
  10. -- 启动系统服务
  11. local function start_services()
  12. if not is_running then
  13. log.info("main", "启动系统服务")
  14. -- 自定义参数启动(使用8000开发板)
  15. -- 启动后连接默认AP热点,访问日志中的地址"http://192.168.4.1:80/explorer.html"来访问文件管理服务器。
  16. exremotefile.open(nil, {is_8000_development_board = true})
  17. is_running = true
  18. log.info("main", "系统服务启动完成")
  19. end
  20. end
  21. -- 停止系统服务
  22. local function stop_services()
  23. if is_running then
  24. log.info("main", "停止系统服务")
  25. -- 关闭远程文件管理系统
  26. exremotefile.close()
  27. is_running = false
  28. log.info("main", "系统服务已停止")
  29. end
  30. end
  31. -- 初始化按键,这里选取boot键作为功能键
  32. local function press_key()
  33. log.info("boot press")
  34. sys.publish("PRESS", true)
  35. end
  36. gpio.setup(0, press_key, gpio.PULLDOWN, gpio.RISING)
  37. gpio.debounce(0, 100, 1)
  38. local function config_services()
  39. -- 根据配置决定是否自动启动服务
  40. if AUTO_START then
  41. start_services()
  42. else
  43. log.info("main", "系统已就绪,等待boot按键触发")
  44. end
  45. while 1 do
  46. sys.waitUntil("PRESS")
  47. -- 切换系统状态
  48. if is_running then
  49. stop_services()
  50. else
  51. start_services()
  52. end
  53. end
  54. end
  55. sys.taskInit(config_services)