add: 添加新的阿里云物联网库-aliyun2库,及搭配的demo, 逐步完善中
[LuatOS.git] / script / libs / joystick.lua
blob26e906ac839716d40b13cc91cfa3f21fb563eadd
1 --[[
2 @module joystick
3 @summary joystick 驱动
4 @version 1.0
5 @date 2023.04.10
6 @author Dozingfiretruck
7 @usage
8 -- 用法实例
10 local joystick = require "joystick"
11 i2cid = 0
12 i2c_speed = i2c.FAST
13 sys.taskInit(function()
14 i2c.setup(i2cid,i2c_speed)
15 joystick.init(i2cid)--初始化,传入i2c_id
16 -- while 1 do
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)
24 sys.wait(1000)
25 -- end
26 end)
28 local joystick = {}
29 local sys = require "sys"
30 local i2cid
32 local JOYSTICK_ADDRESS_ADDR = 0x5A
34 ---器件所用地址
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
49 joystick.PRESS_UP = 1
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
58 joystick.BUTOON_D = 0
59 joystick.BUTOON_B = 1
60 joystick.BUTOON_A = 2
61 joystick.BUTOON_C = 3
62 joystick.BUTTON_J = 4
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")
69 return read_data
70 end
72 function joystick.init(joystick_i2c)
73 -- i2c.setup(joystick_i2c, i2c.FAST)
74 i2cid = joystick_i2c
75 end
77 function joystick.get_button_status(button)
78 local status = 0
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)
89 end
90 return status
91 end
93 function joystick.get_joystick_status(joystick_xy)
94 local status = 0
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)
99 end
100 return status
103 return joystick