Skip to content

Instantly share code, notes, and snippets.

@partlyhuman
Last active February 17, 2021 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save partlyhuman/1f54a7b7576db74fe018f99bb121ec63 to your computer and use it in GitHub Desktop.
Save partlyhuman/1f54a7b7576db74fe018f99bb121ec63 to your computer and use it in GitHub Desktop.
Pico-8 Pad stuff
# coding: utf-8
"""
Standard gamepad mappings.
Pulled in to Gamepad.py directly.
"""
class NES(Gamepad):
fullName = 'Pico8 Pad'
def __init__(self, joystickNumber = 0):
Gamepad.__init__(self, joystickNumber)
self.axisNames = {
0: 'X',
1: 'Y',
}
self.buttonNames = {
0: 'B',
1: 'A',
8: 'SELECT',
9: 'START',
}
self._setupReverseMaps()
#!/usr/bin/env python
import RPi.GPIO as GPIO
import Gamepad
import time
import alsaaudio
import sys
# Gamepad settings
gamepadType = Gamepad.NES
pollInterval = 0.2
# Set some initial state
LIGHT_PIN = 7
mixer = None
pwm = None
meta = False
volume = 100
volumeStep = 5
duty = 100
dutyStep = 5
# Create some callback functions
def metaPressed():
gamepad.addAxisMovedHandler('X', onHorizontalChanged)
gamepad.addAxisMovedHandler('Y', onVerticalChanged)
def metaReleased():
gamepad.removeAxisMovedHandler('X', onHorizontalChanged)
gamepad.removeAxisMovedHandler('Y', onVerticalChanged)
def updateVolume(volume):
global mixer
#print "Volume = %d" % volume
mixer.setvolume(volume, alsaaudio.MIXER_CHANNEL_ALL)
def onHorizontalChanged(position):
global volume
if position > 0:
volume = min(volume + volumeStep, 100)
elif position < 0:
volume = max(volume - volumeStep, 0)
updateVolume(volume)
def onVerticalChanged(position):
global duty
if position < 0:
duty = min(duty + dutyStep, 100)
elif position > 0:
duty = max(0, duty - dutyStep)
pwm.ChangeDutyCycle(duty)
# Wait for a connection
if not Gamepad.available():
print('Please connect your gamepad...')
while not Gamepad.available():
time.sleep(1.0)
gamepad = gamepadType()
print('Gamepad connected')
# Start the background updating
gamepad.startBackgroundUpdates()
# Register the callback functions
gamepad.addButtonPressedHandler('SELECT', metaPressed)
gamepad.addButtonReleasedHandler('SELECT', metaReleased)
# Setup audio mixer
while mixer is None:
try:
mixer = alsaaudio.Mixer('PCM')
volume = mixer.getvolume()[0]
except alsaaudio.ALSAAudioError:
print "Mixer not found, retrying..."
time.sleep(1.0)
# Setup backlight PWM
GPIO.setmode(GPIO.BCM)
GPIO.setup(LIGHT_PIN, GPIO.OUT)
pwm = GPIO.PWM(LIGHT_PIN, 200)
pwm.start(duty)
# Keep running while joystick updates are handled by the callbacks
try:
while gamepad.isConnected():
time.sleep(pollInterval)
except KeyboardInterrupt:
print "Ended."
pwm.stop()
GPIO.output(LIGHT_PIN, 1)
gamepad.stopBackgroundUpdates()
finally:
gamepad.disconnect()
GPIO.cleanup()
sudo /usr/bin/fbcp-ili9341 &
sleep 5
sudo -u pi /opt/pico-8/pico8 -splore -width 128 -height 128 &
sudo -u pi /home/pi/gamepad/gamepad_hardware.py &
@partlyhuman
Copy link
Author

Prereqs:
sudo apt install libasound2-dev
pip install pyalsaaudio
git clone https://github.com/piborg/Gamepad.git

@partlyhuman
Copy link
Author

SPI screen:
git clone https://github.com/juj/fbcp-ili9341.git
cmake -DWAVESHARE_ST7735S_HAT=ON -DGPIO_TFT_DATA_CONTROL=24 -DGPIO_TFT_RESET_PIN=25 -DGPIO_TFT_BACKLIGHT=7 -DDISPLAY_CROPPED_INSTEAD_OF_SCALING=OFF -DSINGLE_CORE_BOARD=ON -DARMV6Z=ON -DDISPLAY_ROTATE_180_DEGREES=OFF -DSPI_BUS_CLOCK_DIVISOR=40 -D -DSTATISTICS=0 && make -j
Can play with clock divisor, probably can go much lower (faster)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment