GT911.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. module(..., package.seeall)
  2. ----GT911
  3. require "misc"
  4. require "utils"
  5. require "pins"
  6. local GT911addr = 0x14
  7. local i2cid = 2
  8. local rst = pins.setup(23, 1)
  9. local int = pins.setup(19)
  10. local function init()
  11. if i2c.setup(i2cid, i2c.SLOW) ~= i2c.SLOW then
  12. print("i2c.init fail")
  13. return
  14. end
  15. -------------------------初始化-------------------------
  16. rst(0)
  17. int(1)
  18. sys.wait(10)
  19. rst(1)
  20. sys.wait(10)
  21. int(0)
  22. sys.wait(55)
  23. sys.wait(50)
  24. end
  25. sys.taskInit(init)
  26. local ispress = false
  27. local last_x, last_y
  28. x = 0
  29. y = 0
  30. function get()
  31. i2c.send(i2cid, GT911addr, string.char(0x81, 0x4e))
  32. pressed = i2c.recv(i2cid, GT911addr, 1)
  33. if pressed:byte()==nil then
  34. return false, false, -1, -1
  35. end
  36. pressed = bit.band(pressed:byte(), 0x0f)
  37. i2c.send(i2cid, GT911addr, string.char(0x81, 0x4e, 0x00, 0x00))
  38. if pressed == 0 then
  39. if ispress == false then
  40. -- _G.iCool_standByTimeoutSleepScreen()
  41. return false, false, -1, -1
  42. end
  43. ispress = false
  44. -- log.info("ispress x,y ", ispress, x, y)
  45. return true, ispress, x, y
  46. end
  47. i2c.send(i2cid, GT911addr, string.char(0x81, 0x51))
  48. xh = i2c.recv(i2cid, GT911addr, 1):byte()
  49. i2c.send(i2cid, GT911addr, string.char(0x81, 0x50))
  50. xl = i2c.recv(i2cid, GT911addr, 1):byte()
  51. i2c.send(i2cid, GT911addr, string.char(0x81, 0x53))
  52. yh = i2c.recv(i2cid, GT911addr, 1):byte()
  53. i2c.send(i2cid, GT911addr, string.char(0x81, 0x52))
  54. yl = i2c.recv(i2cid, GT911addr, 1):byte()
  55. x = xl + (xh * 256)
  56. y = yl + (yh * 256)
  57. if ispress == true and last_x == x and last_y == y then
  58. return false, false, -1, -1
  59. end
  60. ispress = true
  61. last_x = x
  62. last_y = y
  63. -- log.info("ispress x,y ", ispress, x, y)
  64. return true, ispress, x, y
  65. end