clib.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. --- 模块功能:完善luat的c库接口
  2. -- @module clib
  3. -- @author openLuat
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2017.9.20
  7. local uartReceiveCallbacks = {}
  8. local uartSentCallbacks = {}
  9. --- 注册串口事件的处理函数
  10. -- @number id 串口ID,1表示串口1,2表示串口2,uart.ATC表示虚拟AT口
  11. -- @string event 串口事件:
  12. -- "recieve"表示串口收到数据,注意:使用uart.setup配置串口时,第6个参数设置为nil或者0,收到数据时,才会产生"receive"事件
  13. -- "sent"表示串口数据发送完成,注意:使用uart.setup配置串口时,第7个参数设置为1,调用uart.write接口发送数据之后,才会产生"sent"事件
  14. -- @function[opt=nil] callback 串口事件的处理函数
  15. -- @return nil
  16. -- @usage
  17. -- uart.on(1,"receive",rcvFnc)
  18. -- uart.on(1,"sent",sentFnc)
  19. uart.on = function(id, event, callback)
  20. if event == "receive" then
  21. uartReceiveCallbacks[id] = callback
  22. elseif event == "sent" then
  23. uartSentCallbacks[id] = callback
  24. end
  25. end
  26. rtos.on(rtos.MSG_UART_RXDATA, function(id, length)
  27. if uartReceiveCallbacks[id] then
  28. uartReceiveCallbacks[id](id, length)
  29. end
  30. end)
  31. rtos.on(rtos.MSG_UART_TX_DONE, function(id)
  32. if uartSentCallbacks[id] then
  33. uartSentCallbacks[id](id)
  34. end
  35. end)