joystick.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. --[[
  2. @module joystick
  3. @summary joystick 驱动
  4. @version 1.0
  5. @date 2023.04.10
  6. @author Dozingfiretruck
  7. @usage
  8. -- 用法实例
  9. local joystick = require "joystick"
  10. i2cid = 0
  11. i2c_speed = i2c.FAST
  12. sys.taskInit(function()
  13. i2c.setup(i2cid,i2c_speed)
  14. joystick.init(i2cid)--初始化,传入i2c_id
  15. -- while 1 do
  16. local button_a = joystick.get_button_status(joystick.BUTOON_A)
  17. local button_b = joystick.get_button_status(joystick.BUTOON_B)
  18. local button_select = joystick.get_button_status(joystick.BUTOON_D)
  19. local button_start = joystick.get_button_status(joystick.BUTOON_C)
  20. local joystick_x = joystick.get_joystick_status(joystick.JOYSTICK_X)
  21. local joystick_y = joystick.get_joystick_status(joystick.JOYSTICK_Y)
  22. log.info("joystick", button_a,button_b,button_select,button_start,joystick_x,joystick_y)
  23. sys.wait(1000)
  24. -- end
  25. end)
  26. ]]
  27. local joystick = {}
  28. local sys = require "sys"
  29. local i2cid
  30. local JOYSTICK_ADDRESS_ADDR = 0x5A
  31. ---器件所用地址
  32. local JOYSTICK_BASE_REG = 0x10
  33. local JOYSTICK_LEFT_X_REG = 0x10
  34. local JOYSTICK_LEFT_Y_REG = 0x11
  35. local JOYSTICK_RIGHT_X_REG = 0x12
  36. local JOYSTICK_RIGHT_Y_REG = 0x13
  37. local BUTOON_BASE = 0x20
  38. local BUTOON_LEFT_REG = 0x24
  39. local BUTOON_RIGHT_REG = 0x23
  40. local BUTOON_UP_REG = 0x22
  41. local BUTOON_DOWN_REG = 0x21
  42. local JOYSTICK_BUTTON_REG = 0x20
  43. joystick.PRESS_DOWN = 0
  44. joystick.PRESS_UP = 1
  45. joystick.PRESS_REPEAT = 2
  46. joystick.SINGLE_CLICK = 3
  47. joystick.DOUBLE_CLICK = 4
  48. joystick.LONG_PRESS_START = 5
  49. joystick.LONG_PRESS_HOLD = 6
  50. joystick.number_of_event = 7
  51. joystick.NONE_PRESS = 8
  52. joystick.BUTOON_D = 0
  53. joystick.BUTOON_B = 1
  54. joystick.BUTOON_A = 2
  55. joystick.BUTOON_C = 3
  56. joystick.BUTTON_J = 4
  57. joystick.JOYSTICK_X = 5
  58. joystick.JOYSTICK_Y = 6
  59. local function joystick_recv(...)
  60. i2c.send(i2cid, JOYSTICK_ADDRESS_ADDR, {...})
  61. local _, read_data = pack.unpack(i2c.recv(0, JOYSTICK_ADDRESS_ADDR, 1), "b")
  62. return read_data
  63. end
  64. function joystick.init(joystick_i2c)
  65. -- i2c.setup(joystick_i2c, i2c.FAST)
  66. i2cid = joystick_i2c
  67. end
  68. function joystick.get_button_status(button)
  69. local status = 0
  70. if button == joystick.BUTOON_D then
  71. status = joystick_recv(BUTOON_LEFT_REG)
  72. elseif button == joystick.BUTOON_B then
  73. status = joystick_recv(BUTOON_RIGHT_REG)
  74. elseif button == joystick.BUTOON_A then
  75. status = joystick_recv(BUTOON_UP_REG)
  76. elseif button == joystick.BUTOON_C then
  77. status = joystick_recv(BUTOON_DOWN_REG)
  78. elseif button == joystick.BUTTON_J then
  79. status = joystick_recv(JOYSTICK_BUTTON_REG)
  80. end
  81. return status
  82. end
  83. function joystick.get_joystick_status(joystick_xy)
  84. local status = 0
  85. if joystick_xy == joystick.JOYSTICK_X then
  86. status = joystick_recv(JOYSTICK_LEFT_X_REG)
  87. elseif joystick_xy == joystick.JOYSTICK_Y then
  88. status = joystick_recv(JOYSTICK_LEFT_Y_REG)
  89. end
  90. return status
  91. end
  92. return joystick