1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

連射が上手くいかない方のためのテストコード

1
Last updated at Posted at 2021-02-24

テスト用プログラム1

以下のコードを/home/pi下にファイル名test.pyとして保存し
sudo python3 test.pyとコマンドラインに打ってください。
このプログラム実行中にSwitchの画面の電池マークの上に「USB」と表示されるかどうかを確認してください。

test.py
# !/usr/bin/env python3

import os
import threading
import time

# Re-connect USB Gadget device
os.system('echo > /sys/kernel/config/usb_gadget/procon/UDC')
os.system('ls /sys/class/udc > /sys/kernel/config/usb_gadget/procon/UDC')

time.sleep(0.5)

gadget = os.open('/dev/hidg0', os.O_RDWR | os.O_NONBLOCK)
procon = os.open('/dev/hidraw6', os.O_RDWR | os.O_NONBLOCK)


def procon_input():
    while True:
        try:
            input_data = os.read(gadget, 128)
            os.write(procon, input_data)
        except BlockingIOError:
            pass
        except:
            os._exit(1)

def procon_output():
    while True:
        try:
            output_data = os.read(procon, 128)
            os.write(gadget, output_data)#output_data
        except BlockingIOError:
            pass
        except Exception as g:
            print(type(g))
            print(g)
            os._exit(1)

threading.Thread(target=procon_input).start()
threading.Thread(target=procon_output).start()

テスト用プログラム2

rensha.pyに存在する無駄なコードを省いたシンプルな連射用プログラムです。
以下のコードを/home/pi下にファイル名test2.pyとして保存し
sudo python3 test2.pyとコマンドラインに打ってください。
これで連射できるかどうか確認してください。

test2.py

# !/usr/bin/env python3

import os
import threading
import time

# Re-connect USB Gadget device
os.system('echo > /sys/kernel/config/usb_gadget/procon/UDC')
os.system('ls /sys/class/udc > /sys/kernel/config/usb_gadget/procon/UDC')

time.sleep(0.5)

gadget = os.open('/dev/hidg0', os.O_RDWR | os.O_NONBLOCK)
procon = os.open('/dev/hidraw4', os.O_RDWR | os.O_NONBLOCK)

def replace_mouse(output_data):
    global ti
    ri_btn = 0
    le_btn = 0
    zr_btn = 0b10000000
    l_btn  = 0b01000000

    if (output_data[3] & zr_btn) and time.time() > ti + 0.02:
        ri_btn = 0b10000000 | output_data[3]
        ti = time.time()
    else:
        ri_btn = output_data[3] & 0b01111111
        
    if (output_data[5] & l_btn):#Lbutton ga 0x40 nara ZR wo osu
        ri_btn = output_data[3] | 0b10000000
    ri_btn = ri_btn.to_bytes(1, byteorder='little')

    a = output_data[0:3] + ri_btn + output_data[4:5] + output_data[5:6] + output_data[6:13]
    e = a+output_data[13:63]#mouse no add wo kesita mono
    return e


def procon_input():
    while True:
        try:
            input_data = os.read(gadget, 128)
            os.write(procon, input_data)
        except BlockingIOError:
            pass
        except:
            os._exit(1)

def procon_output():
    while True:
        try:
            output_data = os.read(procon, 128)
            e = replace_mouse(output_data)
            os.write(gadget, e)#output_data
        except BlockingIOError:
            pass
        except Exception as g:
            print(type(g))
            print(g)
            os._exit(1)
            
ti = time.time()
threading.Thread(target=procon_input).start()
threading.Thread(target=procon_output).start()

テスト用プログラム3

テスト用プログラム1のtest.pyからロジックを変更しました。
以下のコードを/home/pi下にファイル名test3.pyとして保存し
sudo python3 test3.pyとコマンドラインに打ってください。
このプログラム実行中にSwitchの画面の電池マークの上に「USB」と表示されるかどうかを確認してください。

test3.py
# !/usr/bin/env python3

import os
import threading
import time

# Re-connect USB Gadget device
os.system('echo > /sys/kernel/config/usb_gadget/procon/UDC')
os.system('ls /sys/class/udc > /sys/kernel/config/usb_gadget/procon/UDC')

time.sleep(0.5)

gadget = os.open('/dev/hidg0', os.O_RDWR | os.O_NONBLOCK)
procon = os.open('/dev/hidraw4', os.O_RDWR | os.O_NONBLOCK)



while True:
    try:
        input_data = os.read(gadget, 128)
        os.write(procon, input_data)
    except BlockingIOError:
        pass
    except:
        os._exit(1)



    try:
        output_data = os.read(procon, 128)
        os.write(gadget, output_data)#output_data
    except BlockingIOError:
        pass
    except Exception as g:
        print(type(g))
        print(g)
        os._exit(1)

テスト用プログラム4

テスト用プログラム2のtest2.pyからロジックを変更したものです。
よりシンプルになったかもしれません。
体感的にプログラムの安定感も増した気がします。
test2.pyよりプログラムが途中で強制終了することが少なくなった気がします。
以下のコードを/home/pi下にファイル名test4.pyとして保存し
sudo python3 test4.pyとコマンドラインに打ってください。
これで連射できるかどうか確認してください。

test4.py
# !/usr/bin/env python3

import os
import threading
import time

# Re-connect USB Gadget device
os.system('echo > /sys/kernel/config/usb_gadget/procon/UDC')
os.system('ls /sys/class/udc > /sys/kernel/config/usb_gadget/procon/UDC')

time.sleep(0.5)

gadget = os.open('/dev/hidg0', os.O_RDWR | os.O_NONBLOCK)
procon = os.open('/dev/hidraw4', os.O_RDWR | os.O_NONBLOCK)

def replace_mouse(output_data):
    global ti
    ri_btn = 0
    le_btn = 0
    zr_btn = 0b10000000
    l_btn  = 0b01000000

    if (output_data[3] & zr_btn) and time.time() > ti + 0.02:
        ri_btn = 0b10000000 | output_data[3]
        ti = time.time()
    else:
        ri_btn = output_data[3] & 0b01111111

    if (output_data[5] & l_btn):#Lbutton ga 0x40 nara ZR wo osu
        ri_btn = output_data[3] | 0b10000000
    ri_btn = ri_btn.to_bytes(1, byteorder='little')

    a = output_data[0:3] + ri_btn + output_data[4:5] + output_data[5:6] + output_data[6:13]
    e = a+output_data[13:63]#mouse no add wo kesita mono
    return e

ti = time.time()
while True:


    try:
        input_data = os.read(gadget, 128)
        os.write(procon, input_data)
    except BlockingIOError:
        pass
    except:
        os._exit(1)



    try:
        output_data = os.read(procon, 128)
        e = replace_mouse(output_data)
        os.write(gadget, e)#output_data
    except BlockingIOError:
        pass
    except Exception as g:
        print(type(g))
        print(g)
        os._exit(1)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?