Last active
February 17, 2021 16:40
-
-
Save partlyhuman/1f54a7b7576db74fe018f99bb121ec63 to your computer and use it in GitHub Desktop.
Pico-8 Pad stuff
This file contains hidden or 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
# 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() | |
This file contains hidden or 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
#!/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() |
This file contains hidden or 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
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 & |
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
Prereqs:
sudo apt install libasound2-dev
pip install pyalsaaudio
git clone https://github.com/piborg/Gamepad.git