6 @author Dozingfiretruck
10 local joystick = require "joystick"
13 sys.taskInit(function()
14 i2c.setup(i2cid,i2c_speed)
15 joystick.init(i2cid)--初始化,传入i2c_id
17 local button_a = joystick.get_button_status(joystick.BUTOON_A)
18 local button_b = joystick.get_button_status(joystick.BUTOON_B)
19 local button_select = joystick.get_button_status(joystick.BUTOON_D)
20 local button_start = joystick.get_button_status(joystick.BUTOON_C)
21 local joystick_x = joystick.get_joystick_status(joystick.JOYSTICK_X)
22 local joystick_y = joystick.get_joystick_status(joystick.JOYSTICK_Y)
23 log.info("joystick", button_a,button_b,button_select,button_start,joystick_x,joystick_y)
29 local sys
= require
"sys"
32 local JOYSTICK_ADDRESS_ADDR
= 0x5A
35 local JOYSTICK_BASE_REG
= 0x10
36 local JOYSTICK_LEFT_X_REG
= 0x10
37 local JOYSTICK_LEFT_Y_REG
= 0x11
38 local JOYSTICK_RIGHT_X_REG
= 0x12
39 local JOYSTICK_RIGHT_Y_REG
= 0x13
41 local BUTOON_BASE
= 0x20
42 local BUTOON_LEFT_REG
= 0x24
43 local BUTOON_RIGHT_REG
= 0x23
44 local BUTOON_UP_REG
= 0x22
45 local BUTOON_DOWN_REG
= 0x21
46 local JOYSTICK_BUTTON_REG
= 0x20
48 joystick
.PRESS_DOWN
= 0
50 joystick
.PRESS_REPEAT
= 2
51 joystick
.SINGLE_CLICK
= 3
52 joystick
.DOUBLE_CLICK
= 4
53 joystick
.LONG_PRESS_START
= 5
54 joystick
.LONG_PRESS_HOLD
= 6
55 joystick
.number_of_event
= 7
56 joystick
.NONE_PRESS
= 8
63 joystick
.JOYSTICK_X
= 5
64 joystick
.JOYSTICK_Y
= 6
66 local function joystick_recv(...)
67 i2c
.send(i2cid
, JOYSTICK_ADDRESS_ADDR
, {...})
68 local _
, read_data
= pack
.unpack(i2c
.recv(0, JOYSTICK_ADDRESS_ADDR
, 1), "b")
72 function joystick
.init(joystick_i2c
)
73 -- i2c.setup(joystick_i2c, i2c.FAST)
77 function joystick
.get_button_status(button
)
79 if button
== joystick
.BUTOON_D
then
80 status
= joystick_recv(BUTOON_LEFT_REG
)
81 elseif button
== joystick
.BUTOON_B
then
82 status
= joystick_recv(BUTOON_RIGHT_REG
)
83 elseif button
== joystick
.BUTOON_A
then
84 status
= joystick_recv(BUTOON_UP_REG
)
85 elseif button
== joystick
.BUTOON_C
then
86 status
= joystick_recv(BUTOON_DOWN_REG
)
87 elseif button
== joystick
.BUTTON_J
then
88 status
= joystick_recv(JOYSTICK_BUTTON_REG
)
93 function joystick
.get_joystick_status(joystick_xy
)
95 if joystick_xy
== joystick
.JOYSTICK_X
then
96 status
= joystick_recv(JOYSTICK_LEFT_X_REG
)
97 elseif joystick_xy
== joystick
.JOYSTICK_Y
then
98 status
= joystick_recv(JOYSTICK_LEFT_Y_REG
)