gt911.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. --[[
  2. @module gt911
  3. @summary gt911 驱动
  4. @version 1.0
  5. @date 2022.05.26
  6. @author Dozingfiretruck
  7. @usage
  8. --注意:因使用了sys.wait()所有api需要在协程中使用
  9. -- 用法实例
  10. local gt911 = require "gt911"
  11. local i2cid = 0
  12. local gt911_res = pin.PA07
  13. local gt911_int = pin.PA00
  14. i2c_speed = i2c.FAST
  15. sys.taskInit(function()
  16. i2c.setup(i2cid,i2c_speed)
  17. gt911.init(i2cid,gt911_res,gt911_int)
  18. while 1 do
  19. sys.wait(1000)
  20. end
  21. end)
  22. local function gt911CallBack(press_sta,i,x,y)
  23. print(press_sta,i,x,y)
  24. end
  25. sys.subscribe("GT911",gt911CallBack)
  26. ]]
  27. local gt911 = {}
  28. local sys = require "sys"
  29. local i2cid
  30. local gt911_id
  31. local gt911_id_2 = 0x14
  32. local gt911_id_1 = 0x5D
  33. local function gt911_send(...)
  34. i2c.send(i2cid, gt911_id, {...})
  35. end
  36. local function gt911_recv(...)
  37. i2c.send(i2cid, gt911_id, {...})
  38. local _, read_data = pack.unpack(i2c.recv(0, gt911_id, 1), "b")
  39. return read_data
  40. end
  41. local press_sta = false
  42. local point = {{x=0,y=0},{x=0,y=0},{x=0,y=0},{x=0,y=0},{x=0,y=0}}
  43. --器件ID检测
  44. local function chip_check()
  45. local ret = i2c.send(i2cid, gt911_id_1,string.char(0x00, 0x00))
  46. if ret then
  47. gt911_id = gt911_id_1
  48. else
  49. ret = i2c.send(i2cid, gt911_id_2,string.char(0x00, 0x00))
  50. if ret then
  51. gt911_id = gt911_id_2
  52. else
  53. log.info("gt911", "Can't find device")
  54. return false
  55. end
  56. end
  57. log.info("gt911", "find device",gt911_id)
  58. return true
  59. end
  60. --[[
  61. gt911初始化
  62. @api gt911.init(gt911_i2c,gt911_res,gt911_int)
  63. @number gt911_i2c i2c_id
  64. @number gt911_res 复位引脚
  65. @number gt911_int 中断引脚
  66. @return bool 成功返回true
  67. @usage
  68. gt911.init(0)
  69. ]]
  70. function gt911.init(gt911_i2c,gt911_res,gt911_int)
  71. i2cid = gt911_i2c
  72. gpio.setup(gt911_int, 0)
  73. gpio.setup(gt911_res, 0)
  74. gpio.set(gt911_int, 0)
  75. gpio.set(gt911_res, 1)
  76. gpio.set(gt911_res, 0)
  77. sys.wait(10)
  78. gpio.set(gt911_res, 1)
  79. sys.wait(10)
  80. if chip_check()~=true then
  81. return false
  82. end
  83. gpio.setup(gt911_int,
  84. function(val)
  85. local count
  86. local data = gt911_recv(0x81, 0x4E)
  87. if data ~=0 then
  88. press_sta = true
  89. press_times = 0
  90. count = data-128
  91. -- print("触控点数",count)
  92. if press_sta == true and count==0 then
  93. press_sta = false
  94. -- print("抬起")
  95. sys.publish("GT911",press_sta,1,point[1].x,point[1].y)
  96. end
  97. for i=1,data-128 do
  98. point[i].x=gt911_recv(0x81, 0x50+(i-1)*8)+gt911_recv(0x81, 0x51+(i-1)*8)*256
  99. point[i].y=gt911_recv(0x81, 0x52+(i-1)*8)+gt911_recv(0x81, 0x53+(i-1)*8)*256
  100. -- print(i,point[i].x,point[i].y)
  101. sys.publish("GT911",press_sta,i,point[i].x,point[i].y)
  102. end
  103. gt911_send(0x81, 0x4E, 0x00)
  104. else
  105. end
  106. end,nil,gpio.RISING)
  107. gt911_send(0x80, 0x40, 0x02)
  108. gt911_send(0x80, 0x40, 0x00)
  109. local touchic_id = gt911_recv(0x81, 0x40)
  110. print("touchic_id",touchic_id)
  111. return true
  112. end
  113. return gt911