main.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "HZ201P"
  3. VERSION = "1.0.4"
  4. log.info("main", PROJECT, VERSION)
  5. -- 引入必要的库文件(lua编写), 内部库不需要require
  6. sys = require "sys"
  7. sysplus = require("sysplus")
  8. pm.ioVol(pm.IOVOL_ALL_GPIO, 1800)
  9. -- mcu.hardfault(0)
  10. local function fskvInit()
  11. fskv.init()
  12. local init = fskv.get("isInit")
  13. if init then
  14. return
  15. end
  16. fskv.set("allDone", false)
  17. fskv.set("pcbaDone", false)
  18. fskv.set("isInit", true)
  19. end
  20. fskvInit()
  21. local allDone = fskv.get("allDone")
  22. if allDone then
  23. -- 运营商给的dns经常抽风,手动指定
  24. socket.setDNS(nil, 1, "223.5.5.5")
  25. socket.setDNS(nil, 2, "119.29.29.29")
  26. -- gnss的备电和gsensor的供电
  27. local vbackup = gpio.setup(24, 1)
  28. -- gnss的供电
  29. local gpsPower = gpio.setup(26, 1)
  30. -- 使用合宙iot平台时需要这个参数
  31. PRODUCT_KEY = "YXdzIDo5QawWCIRywShMAKjmJsInXtsb" -- 到 iot.openluat.com 创建项目,获取正确的项目id
  32. libfota = require "libfota"
  33. function fota_cb(ret)
  34. log.info("fota", ret)
  35. if ret == 0 then
  36. rtos.reboot()
  37. end
  38. end
  39. -- 使用合宙iot平台进行升级
  40. sys.subscribe("IP_READY", function()
  41. libfota.request(fota_cb)
  42. sys.timerLoopStart(libfota.request, 3600000, fota_cb)
  43. end)
  44. -- 云平台逻辑
  45. require "cloud"
  46. -- 获取所有参数
  47. attributes = require "attributes"
  48. attributes.initial() -- 初始化
  49. -- gnss
  50. require "gnss"
  51. -- Gsensor
  52. require "da267"
  53. sys.subscribe("STEP_COUNTER", function(step)
  54. log.info("STEP_COUNTER", step)
  55. if step > 0 then
  56. attributes.set("step", step)
  57. end
  58. end)
  59. -- LED
  60. -- 全局状态变量
  61. _G_CONNECTED = false
  62. local blueLed = gpio.setup(1, 0)
  63. local redLed = gpio.setup(16, 0, nil, nil, 4)
  64. sys.taskInit(function()
  65. while true do
  66. if attributes.get("ledControl") then
  67. blueLed(attributes.get("blueLed") and 1 or 0)
  68. redLed(attributes.get("redLed") and 1 or 0)
  69. sys.wait(500)
  70. else
  71. redLed(attributes.get("isCharging") and 1 or 0)
  72. blueLed(1)
  73. sys.wait(_G_CONNECTED and 100 or 1000)
  74. blueLed(0)
  75. sys.wait(_G_CONNECTED and 100 or 1000)
  76. end
  77. end
  78. end)
  79. -- 关机键
  80. local powerTimer
  81. local powerKey = gpio.setup(46, function()
  82. log.info("powerKey", gpio.get(46))
  83. if gpio.get(46) == 0 then
  84. sys.publish("POWERKEY_PRESSED")
  85. powerTimer = sys.timerStart(function()
  86. log.info("powerKey", "long press")
  87. -- 把灯关都掉,让用户以为已经关机了
  88. blueLed(0)
  89. redLed(0)
  90. blueLed = function()
  91. end
  92. redLed = blueLed
  93. -- 两秒后真正关机
  94. sys.timerStart(pm.shutdown, 2000)
  95. end, 3000)
  96. else
  97. if powerTimer then
  98. sys.timerStop(powerTimer)
  99. log.info("powerKey", "stop press")
  100. end
  101. end
  102. end, gpio.PULLUP)
  103. -- 电量检测与上报
  104. require "battery"
  105. -- 信号值检测与上报
  106. sys.taskInit(function()
  107. while true do
  108. attributes.set("rsrp", mobile.rsrp())
  109. attributes.set("rsrq", mobile.rsrq())
  110. sys.wait(60000)
  111. end
  112. end)
  113. -- 拨打电话与音频
  114. require "ccVolte"
  115. -- SIM DETECT
  116. local simCheck = gpio.setup(41, function()
  117. log.info("sim status", gpio.get(41))
  118. end)
  119. else
  120. require "test"
  121. end
  122. -- 用户代码已结束---------------------------------------------
  123. -- 结尾总是这一句
  124. sys.run()