main.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "HZ201P"
  3. VERSION = "1.0.5"
  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 gnssEnvPower = gpio.setup(26, 1)
  30. local gpsPower = gpio.setup(2, 1)
  31. -- 使用合宙iot平台时需要这个参数
  32. PRODUCT_KEY = "YXdzIDo5QawWCIRywShMAKjmJsInXtsb" -- 到 iot.openluat.com 创建项目,获取正确的项目id
  33. libfota = require "libfota"
  34. function fota_cb(ret)
  35. log.info("fota", ret)
  36. if ret == 0 then
  37. rtos.reboot()
  38. end
  39. end
  40. -- 使用合宙iot平台进行升级
  41. sys.subscribe("IP_READY", function()
  42. libfota.request(fota_cb)
  43. sys.timerLoopStart(libfota.request, 3600000, fota_cb)
  44. end)
  45. -- 云平台逻辑
  46. require "cloud"
  47. -- 获取所有参数
  48. attributes = require "attributes"
  49. attributes.initial() -- 初始化
  50. -- gnss
  51. require "gnss"
  52. -- Gsensor
  53. require "da267"
  54. sys.subscribe("STEP_COUNTER", function(step)
  55. log.info("STEP_COUNTER", step)
  56. if step > 0 then
  57. attributes.set("step", step)
  58. end
  59. end)
  60. -- LED
  61. -- 全局状态变量
  62. _G_CONNECTED = false
  63. local blueLed = gpio.setup(1, 0)
  64. local redLed = gpio.setup(16, 0, nil, nil, 4)
  65. sys.taskInit(function()
  66. while true do
  67. if attributes.get("ledControl") then
  68. blueLed(attributes.get("blueLed") and 1 or 0)
  69. redLed(attributes.get("redLed") and 1 or 0)
  70. sys.wait(500)
  71. else
  72. redLed(attributes.get("isCharging") and 1 or 0)
  73. blueLed(1)
  74. sys.wait(_G_CONNECTED and 100 or 1000)
  75. blueLed(0)
  76. sys.wait(_G_CONNECTED and 100 or 1000)
  77. end
  78. end
  79. end)
  80. -- 关机键
  81. local powerTimer
  82. local powerKey = gpio.setup(46, function()
  83. log.info("powerKey", gpio.get(46))
  84. if gpio.get(46) == 0 then
  85. sys.publish("POWERKEY_PRESSED")
  86. powerTimer = sys.timerStart(function()
  87. log.info("powerKey", "long press")
  88. -- 把灯关都掉,让用户以为已经关机了
  89. blueLed(0)
  90. redLed(0)
  91. blueLed = function()
  92. end
  93. redLed = blueLed
  94. -- 两秒后真正关机
  95. sys.timerStart(pm.shutdown, 2000)
  96. end, 3000)
  97. else
  98. if powerTimer then
  99. sys.timerStop(powerTimer)
  100. log.info("powerKey", "stop press")
  101. end
  102. end
  103. end, gpio.PULLUP)
  104. -- 电量检测与上报
  105. require "battery"
  106. -- 信号值检测与上报
  107. sys.taskInit(function()
  108. while true do
  109. attributes.set("rsrp", mobile.rsrp())
  110. attributes.set("rsrq", mobile.rsrq())
  111. sys.wait(60000)
  112. end
  113. end)
  114. -- 拨打电话与音频
  115. require "ccVolte"
  116. -- SIM DETECT
  117. local simCheck = gpio.setup(41, function()
  118. log.info("sim status", gpio.get(41))
  119. end)
  120. else
  121. require "test"
  122. end
  123. -- 用户代码已结束---------------------------------------------
  124. -- 结尾总是这一句
  125. sys.run()