Paste the reading helper
A SPIKE App Python project is a single file and cannot import your own .py, so paste the whole "reading helper" at the very top (its first lines say so: copy it as is). Without it the hub does not know GamepadApp and stops with NameError.
- Open the LEGO SPIKE App (Education, yellow SPIKE Prime hub), create a project and choose "Python".
- Paste the program marked ① on the right at the top (only the first helper line shows; press "Show helper" to see it all, and "Copy program" always copies everything). The helper alone: MBC_gamepad_App_helper.py (open it in a text editor and copy all; the file has Chinese and English comments).
- Plug the receiver into any port A–F; the hub powers it. The receiver needs the SPIKE firmware. PS2 and PS4 receivers are written the same way (the helper is also in the PS4 download folder: PS4 download, same file); switch PS2 / PS4 under the simulated pad.
The helper splits the numbers the App reads for you (the same conversion as the SPIKE App blocks page):
| What the App reads | What it holds | Helper function |
|---|---|---|
color_sensor.reflection() (reflection) | D-pad + shape buttons: up 1, right 2, down 4, left 8, △ 16, ○ 32, ✕ 64, □ 128, added up when held together | pressed('UP')…, low 8 bits of buttons() |
color_sensor.color() (color) | shoulder + function buttons: L2 1, R2 2, L1 4, R1 8, SELECT (PS4: SHARE) 16, L3 32, R3 64, START (PS4: OPTIONS) −128 | pressed('L1')…, high 8 bits of buttons() |
color_sensor.rgbi() item 1 (raw red) | left stick X×10 + Y (each 1–9, 55 when released) | stick('LX') (whole part of ÷ 10), stick('LY') (remainder) |
rgbi() item 2 (raw green) | right stick X×10 + Y | stick('RX'), stick('RY') |
rgbi() item 3 (raw blue) | the 16 buttons in another layout (PSX) | not used (same as the SPIKE App blocks page; see "Values the SPIKE App cannot read") |