luat_lib_sys_doc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. @module sys
  3. @summary sys库
  4. @version 1.0
  5. @date 2019.11.23
  6. @video https://www.bilibili.com/video/BV1194y1o7q2
  7. @tag LUAT_USE_GPIO
  8. */
  9. /*
  10. Task协程等待指定时长
  11. @api sys.wait(timeout)
  12. @int 等待时长,单位毫秒,必须大于0,否则无效
  13. @return any 通常为nil,除非主动被唤醒(通常不会)
  14. @usage
  15. sys.taskInit(function()
  16. while 1 do
  17. sys.wait(500)
  18. end
  19. end)
  20. */
  21. void doc_sys_wait(void){};
  22. /*
  23. Task协程等待指定时长或者特定的topic
  24. @api sys.waitUntil(topic, timeout)
  25. @string 事件topic
  26. @int 等待时长,单位毫秒,必须大于0,否则无效
  27. @return boolean 如果是超时,返回false,否则返回true
  28. @return any 对应topic的内容
  29. @usage
  30. sys.taskInit(function()
  31. // do something
  32. local result, data = sys.waitUntil("NET_READY", 30000)
  33. // do something else
  34. end)
  35. */
  36. void doc_sys_waitUntil(void){};
  37. /*
  38. 创建一个Task协程
  39. @api sys.taskInit(func, arg1, arg2, argN)
  40. @function 待执行的函数,可以是匿名函数, 也可以是local或全局函数
  41. @any 需要传递的参数1,可选
  42. @any 需要传递的参数2,可选
  43. @any 需要传递的参数N,可选
  44. @return task 协程对象
  45. @usage
  46. sys.taskInit(function(a, b, c)
  47. log.info("task", a, b, c) -- 打印 task A B C
  48. end, "A", "B", "N")
  49. */
  50. void doc_sys_taskInit(void){};
  51. /*
  52. 创建一个定时器.非Task,函数里不能直接sys.waitXXX
  53. @api sys.timerStart(func, timeout, arg1, arg2, argN)
  54. @function 待执行的函数,可以是匿名函数, 也可以是local或全局函数
  55. @int 延时时长,单位毫秒
  56. @any 需要传递的参数1,可选
  57. @any 需要传递的参数2,可选
  58. @any 需要传递的参数N,可选
  59. @return int 定时器id
  60. @usage
  61. sys.timerStart(function(a, b, c)
  62. log.info("task", a, b, c) -- 1000毫秒后才会执行, 打印 task A B C
  63. end, 1000, "A", "B", "N")
  64. */
  65. void doc_sys_timerStart(void){};
  66. /*
  67. 创建一个循环定时器.非Task,函数里不能直接sys.waitXXX
  68. @api sys.timerLoopStart(func, timeout, arg1, arg2, argN)
  69. @function 待执行的函数,可以是匿名函数, 也可以是local或全局函数
  70. @int 延时时长,单位毫秒
  71. @any 需要传递的参数1,可选
  72. @any 需要传递的参数2,可选
  73. @any 需要传递的参数N,可选
  74. @return int 定时器id
  75. @usage
  76. sys.timerLoopStart(function(a, b, c)
  77. log.info("task", a, b, c) -- 1000毫秒后才会执行, 打印 task A B C
  78. end, 1000, "A", "B", "N")
  79. */
  80. void doc_sys_timerLoopStart(void){};
  81. /*
  82. 关闭一个定时器.
  83. @api sys.timerStop(id)
  84. @int 定时器id
  85. @return nil 无返回值
  86. @usage
  87. local tcount = 0
  88. local tid
  89. tid = sys.timerLoopStart(function(a, b, c)
  90. log.info("task", a, b, c) -- 1000毫秒后才会执行, 打印 task A B C
  91. if tcount > 10 then
  92. sys.timerStop(tid)
  93. end
  94. tcount = tcount + 1
  95. end, 1000, "A", "B", "N")
  96. */
  97. void doc_sys_timerStop(void){};
  98. /*
  99. 关闭同一回调函数的所有定时器.
  100. @api sys.timerStopAll(fnc)
  101. @function fnc回调的函数
  102. @return nil 无返回值
  103. @usage
  104. -- 关闭回调函数为publicTimerCbFnc的所有定时器
  105. local function publicTimerCbFnc(tag)
  106. log.info("publicTimerCbFnc",tag)
  107. end
  108. sys.timerStart(publicTimerCbFnc,8000,"first")
  109. sys.timerStart(publicTimerCbFnc,8000,"second")
  110. sys.timerStart(publicTimerCbFnc,8000,"third")
  111. sys.timerStopAll(publicTimerCbFnc)
  112. */
  113. void doc_sys_timerStopAll(void){};
  114. /*
  115. 往特定topic通道发布一个消息
  116. @api sys.publish(topic, arg1, agr2, argN)
  117. @string topic的值
  118. @any 附带的参数1
  119. @any 附带的参数2
  120. @any 附带的参数N
  121. @return nil 无返回值
  122. @usage
  123. sys.publish("BT_READY", false)
  124. */
  125. void doc_sys_publish(void){};
  126. /*
  127. 订阅一个topic通道
  128. @api sys.subscribe(topic, func)
  129. @string topic的值
  130. @function 回调函数, 注意, 不能直接使用sys.waitXXX
  131. @return nil 无返回值
  132. @usage
  133. local function bt_cb(state)
  134. log.info("bt", state)
  135. end
  136. sys.subscribe("BT_READY", bt_cb)
  137. sys.subscribe("BT_READY", function(state)
  138. log.info("sys", "Got BT_READY", state)
  139. end)
  140. */
  141. void doc_sys_subscribe(void){};
  142. /*
  143. 取消订阅topic通道
  144. @api sys.unsubscribe(topic, func)
  145. @string topic的值
  146. @function 回调函数, 注意, 不能直接使用sys.waitXXX
  147. @return nil 无返回值
  148. @usage
  149. local function bt_cb(state)
  150. log.info("bt", state)
  151. end
  152. sys.unsubscribe("BT_READY", bt_cb)
  153. */
  154. void doc_sys_unsubscribe(void){};
  155. /*
  156. sys库主循环方法,仅允许在main.lua的末尾调用
  157. @api sys.run()
  158. @return nil 无返回值. 这个方法几乎不可能返回.
  159. @usage
  160. -- 总是main.lua的结尾一句,将来也许会简化掉
  161. sys.run()
  162. -- 之后的代码不会被执行
  163. */
  164. void doc_sys_run(void){};
  165. /*
  166. 等待接收一个目标消息
  167. @api sys.waitMsg(taskName, target, timeout)
  168. @string 任务名称,用于唤醒任务的id
  169. @string 目标消息,如果为nil,则表示接收到任意消息都会退出
  170. @int 超时时间,如果为nil,则表示无超时,永远等待
  171. @return table 成功返回table型的msg,超时返回false
  172. @usage
  173. -- 等待任务
  174. sys.waitMsg('a', 'b', 1000)
  175. -- 注意, 本函数会自动注册成全局函数 sys_wait
  176. */
  177. void doc_sysplus_wait(void){};
  178. /*
  179. 向目标任务发送一个消息
  180. @api sys.sendMsg(taskName, target, arg2, arg3, arg4)
  181. @string 任务名称,用于唤醒任务的id
  182. @any 消息中的参数1,同时也是waitMsg里的target
  183. @any 消息中的参数2
  184. @any 消息中的参数3
  185. @any 消息中的参数4
  186. @return bool 成功返回true, 否则返回false
  187. @usage
  188. -- 向任务a,目标b发送消息
  189. sys.sendMsg('a', 'b')
  190. -- 注意, 本函数会自动注册成全局函数 sys_send
  191. */
  192. void doc_sysplus_send(void){};
  193. /*
  194. 创建一个任务线程,在模块最末行调用该函数并注册模块中的任务函数,main.lua导入该模块即可
  195. @api sys.taskInitEx(fun, taskName, cbFun, ...)
  196. @function 任务函数名,用于resume唤醒时调用
  197. @string 任务名称,用于唤醒任务的id
  198. @function 接收到非目标消息时的回调函数
  199. @any ... 任务函数fun的可变参数
  200. @return number 返回该任务的线程号
  201. @usage
  202. sys.taskInitEx(task1,'a',callback)
  203. */
  204. void doc_sysplus_taskInitEx(void){};
  205. /*
  206. 删除由taskInitEx创建的任务线程
  207. @api sys.taskDel(taskName)
  208. @string 任务名称,用于唤醒任务的id
  209. @usage
  210. sys.taskDel('a')
  211. */
  212. void doc_sysplus_taskDel(void){};
  213. /*
  214. 清除指定task的消息队列
  215. @api sys.cleanMsg(taskName)
  216. @string 任务名称
  217. @usage
  218. sys.cleanMsg('a')
  219. */
  220. void doc_sysplus_cleanMsg(void){};