Last active
September 28, 2019 10:01
-
-
Save kanjirz50/c413d826aa2b355435c5090301639625 to your computer and use it in GitHub Desktop.
ジョイスティックの軸をファイルに出力
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
https://mitsuru.hateblo.jp/entry/20100321/joyprint | |
上記URLのJoystick value viewerを改造し、軸をファイルに書き出すようにしています。 | |
Public Domainに感謝です。このファイルもPublic Domainです | |
出力ファイル名は ./axis.log としています。ファイルが存在すると例外が出ます。 | |
frame_no,controller, axis,valueのtsvファイルとしています。 | |
(複数ジョイスティック接続時にすべてが正しく出力されるかどうかは未確認です) | |
動作環境:Python 3.7 + Pygame 1.9.6 | |
pip install pygameでPygameライブラリをインストールしてください | |
出力サンプル | |
frame_no controller axis value | |
0 Controller (HORIPAD S) 0 0.000000 | |
0 Controller (HORIPAD S) 1 0.000000 | |
0 Controller (HORIPAD S) 2 0.000000 | |
0 Controller (HORIPAD S) 3 0.000000 | |
0 Controller (HORIPAD S) 4 0.000000 | |
1 Controller (HORIPAD S) 0 0.000000 | |
1 Controller (HORIPAD S) 1 0.000000 | |
1 Controller (HORIPAD S) 2 0.000000 | |
1 Controller (HORIPAD S) 3 0.000000 | |
1 Controller (HORIPAD S) 4 0.000000 | |
... | |
74 Controller (HORIPAD S) 0 0.000000 | |
74 Controller (HORIPAD S) 1 0.423627 | |
74 Controller (HORIPAD S) 2 0.000000 | |
74 Controller (HORIPAD S) 3 0.000000 | |
74 Controller (HORIPAD S) 4 0.000000 | |
75 Controller (HORIPAD S) 0 0.000000 | |
75 Controller (HORIPAD S) 1 0.453169 | |
75 Controller (HORIPAD S) 2 0.000000 | |
75 Controller (HORIPAD S) 3 0.000000 | |
75 Controller (HORIPAD S) 4 0.000000 | |
76 Controller (HORIPAD S) 0 0.000000 | |
76 Controller (HORIPAD S) 1 0.507401 | |
76 Controller (HORIPAD S) 2 0.000000 | |
76 Controller (HORIPAD S) 3 0.000000 | |
76 Controller (HORIPAD S) 4 0.000000 | |
""" | |
import logging | |
import pygame | |
from pygame.locals import * | |
WIDTH, HEIGHT = 512, 512 | |
COLOR = 255, 255, 255 # 文字色 | |
OFF_COLOR = 128, 128, 153 # 非アクティブ時の文字色 | |
BG_COLOR = 0, 0, 50 # 背景色 | |
FONTSIZE = 32 | |
FPS = 60 | |
# close処理が大変なので、loggerでごまかす | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.DEBUG) | |
# ファイルが存在しなければ動作 | |
handler = logging.FileHandler(filename="axis.log", mode="x") | |
handler.setLevel(logging.DEBUG) | |
handler.setFormatter(logging.Formatter("%(message)s")) | |
logger.addHandler(handler) | |
logger.info('frame_no\tcontroller\taxis\tvalue') | |
def blittext(surface, font, text, dest, color=COLOR): | |
text_s = font.render(text, True, color) | |
surface.blit(text_s, dest) | |
x, y = dest | |
return x + text_s.get_width(), y + font.get_linesize() | |
class Joy: | |
def __init__(self, joy_no): | |
self.no = joy_no | |
j = pygame.joystick.Joystick(joy_no) | |
j.init() | |
self.j = j | |
self.name = j.get_name() | |
self.axes = [0] * j.get_numaxes() | |
self.buttons = [False] * j.get_numbuttons() | |
self.hats = [(0, 0)] * j.get_numhats() | |
def event(self, e, frame_no=0): | |
if e.type == JOYAXISMOTION and e.joy == self.no: | |
self.axes[e.axis] = e.value | |
#print('{0:5d} axis {1:d}:{2: f}'.format(frame_no, e.axis, e.value)) | |
elif e.type == JOYHATMOTION and e.joy == self.no: | |
self.hats[e.hat] = e.value | |
#print('{0:5d} hat {1:d}:{2:s}'.format(frame_no, e.hat, e.value)) | |
elif e.type == JOYBUTTONUP and e.joy == self.no: | |
self.buttons[e.button] = False | |
#print('{0:5d} button {1:d} up'.format(frame_no, e.button)) | |
elif e.type == JOYBUTTONDOWN and e.joy == self.no: | |
self.buttons[e.button] = True | |
#print('{0:5d} button {1:d} doun'.format(frame_no, e.button)) | |
def blittext(self, surface, font, dest, frame_no, color=COLOR, off_color=OFF_COLOR): | |
x, y = dest | |
d, y = blittext(surface, font, self.name, (x, y)) | |
y += (font.get_linesize() // 4) | |
bx, d = blittext(surface, font, 'buttons: ', (x, y)) | |
for i, b in enumerate(self.buttons): | |
bx, d = blittext(surface, font, str(i), (bx, y), | |
(off_color, color)[b]) | |
y += font.get_linesize() | |
d, y = blittext( | |
surface, font, | |
'hats: {0:s}'.format( | |
', '.join(['_RL'[vx] + '_UD'[vy] for vx, vy in self.hats])), | |
(x, y)) | |
d, y = blittext(surface, font, 'axis:', (x, y)) | |
r = surface.get_rect() | |
for i, a in enumerate(self.axes): | |
pygame.draw.rect( | |
surface, off_color, | |
(r.centerx, y, int(WIDTH * 0.4 * a), font.get_ascent())) | |
d, y = blittext( | |
surface, font, | |
' {0:d}: {1:+f}'.format(i, a), | |
(x, y)) | |
# 軸を出力 | |
logger.info('{0:5d}\t{1}\t{2:d}\t{3: f}'.format(frame_no, self.name, i, a)) | |
y += font.get_linesize() | |
blittext(surface, font, "frame: {}".format(frame_no), (x, y)) | |
return x, y | |
def main(): | |
pygame.init() | |
screen = pygame.display.set_mode((WIDTH, HEIGHT)) | |
font = pygame.font.Font(None, FONTSIZE) | |
joysticks = [Joy(n) for n in range(pygame.joystick.get_count())] | |
pygame.display.set_caption('Joystick value') | |
clock = pygame.time.Clock() | |
clock.tick(FPS) | |
frame_no = 0 | |
while True: | |
for e in pygame.event.get(): | |
if e.type == QUIT: | |
return | |
elif e.type == KEYDOWN and e.key == K_ESCAPE: | |
return | |
else: | |
for j in joysticks: | |
j.event(e, frame_no) | |
screen.fill(BG_COLOR) | |
x, y = 5, 5 | |
for j in joysticks: | |
x, y = j.blittext(screen, font, (x, y), frame_no) | |
y += font.get_linesize() | |
clock.tick(FPS) | |
pygame.display.flip() | |
frame_no += 1 | |
if __name__ == '__main__': | |
try: | |
main() | |
finally: | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment