Python读取罗技G29数据

"""
罗技G29数据读取
各个轴和按钮的序号请根据官方的demo查看或者根据此程序尝试
各个轴的取值范围是【-1,1】
按钮按下即为1.否则为0
"""
import pygame


class JSManager:
    def __init__(self):
        pygame.init()
        # pygame.joystick.init()
        try:
            self.js = pygame.joystick.Joystick(0)
        except:
            print('could not find joystick')
            exit()
        self.js.init()
        self.axes = self.js.get_numaxes()
        self.buttons = self.js.get_numbuttons()

    def update_single_axes(self, a):
        axis = self.js.get_axis(a)
        return axis

    def update_axes(self):
        # get joystick values
        # pygame.event.get is needed to update axis values
        pygame.event.get()
        axes_values = []
        # self.js.init()
        for a in range(self.axes):
            axis = self.js.get_axis(a)
            axes_values.append(axis)
        return axes_values

    def update_single_button(self, b):
        button = self.js.get_button(b)
        return button

    def update_buttons(self):
        # get button presses
        pygame.event.get()
        button_presses = []
        for b in range(self.buttons):
            button = self.js.get_button(b)
            button_presses.append(button)
        return button_presses


if __name__ == "__main__":
    '''testing code'''
    js = JSManager()
    while True:
        vals = js.update_axes()
        buts = js.update_buttons()
        print(*vals)
        print(*buts)
        '''
        ax = js.update_single_axes(0)
        but = js.update_single_button(14)
        print(ax)
        print(but)
        '''
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值