Close

Gamepad

A project log for PewPew Standalone

A Python-based micro game console, optimized for game development workshops.

dehipudeʃhipu 03/03/2019 at 22:590 Comments

Since the new firmware is considerably smaller, it has more modules enabled by default. One of those is the usb_hid module, which lets PewPew act as a Human Interface Device over USB. That means we can pretend to be a keyboard, a mouse or a gamepad. To test this, I wrote a simple program that turns PewPew into a gamepad:

import board
import struct
import usb_hid
import pew


pew.init()
screen = pew.Pix()
for gamepad in usb_hid.devices:
    if gamepad.usage_page == 0x01 and gamepad.usage == 0x05:
        break
else:
    raise RuntimeError("Gamepad HID device not found")
report = bytearray(6)

while True:
    buttons = pew.keys()
    report_buttons = 0

    if buttons & pew.K_O:
        screen.pixel(6, 3, 3)
        report_buttons |= 0x01
    else:
        screen.pixel(6, 3, 1)

    if buttons & pew.K_X:
        screen.pixel(6, 5, 3)
        report_buttons |= 0x02
    else:
        screen.pixel(6, 5, 1)

    if buttons & pew.K_UP:
        y = -127
        screen.pixel(2, 3, 3)
        screen.pixel(2, 5, 1)
    elif buttons & pew.K_DOWN:
        y = 127
        screen.pixel(2, 3, 1)
        screen.pixel(2, 5, 3)
    else:
        y = 0
        screen.pixel(2, 3, 1)
        screen.pixel(2, 5, 1)

    if buttons & pew.K_LEFT:
        x = -127
        screen.pixel(1, 4, 3)
        screen.pixel(3, 4, 1)
    elif buttons & pew.K_RIGHT:
        x = 127
        screen.pixel(1, 4, 1)
        screen.pixel(3, 4, 3)
    else:
        x = 0
        screen.pixel(1, 4, 1)
        screen.pixel(3, 4, 1)

    struct.pack_into('<Hbbbb', report, 0, report_buttons, x, y, 0, 0)
    gamepad.send_report(report)
    pew.show(screen)
    pew.tick(1/12)

That's it. And it seems to be working! 

Discussions