Skip to content

Commit

Permalink
Update pybadge and pygamer to use keypad module instead of gamepadshift
Browse files Browse the repository at this point in the history
  • Loading branch information
deshipu committed Aug 29, 2021
1 parent b014aa0 commit a895b3a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 69 deletions.
14 changes: 7 additions & 7 deletions pewpew_m4/pew.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
0xf8, 0xfe, 0x125b, 0xcffb, 0xe0cf, 0xffff,
0x1ff8, 0xdbff, 0xffff))

K_X = 0x01
K_DOWN = 0x02
K_LEFT = 0x04
K_RIGHT = 0x08
K_UP = 0x10
K_O = 0x20
K_X = ugame.K_X
K_DOWN = ugame.K_DOWN
K_LEFT = ugame.K_LEFT
K_RIGHT = ugame.K_RIGHT
K_UP = ugame.K_UP
K_O = ugame.K_O

_tick = None
_display = None
Expand Down Expand Up @@ -178,7 +178,7 @@ def __str__(self):


def init():
global _tick, _display, _gamepad, _bitmap, _grid, _game
global _tick, _display, _bitmap, _grid, _game

if _tick is not None:
return
Expand Down
2 changes: 1 addition & 1 deletion pewpew_m4/ugame.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ def get_pressed(self):

display = board.DISPLAY
buttons = _Buttons()
audio = stage.Audio(board.P5)
audio = stage.Audio(board.SPEAKER)
64 changes: 39 additions & 25 deletions pybadge/ugame.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
"""
A helper module that initializes the display and buttons for the uGame
game console. See https://hackaday.io/project/27629-game
"""

import board
import digitalio
import gamepadshift
import stage
import displayio
import busio
import time
import keypad


K_X = 0x01
Expand Down Expand Up @@ -46,27 +40,47 @@
b"\x13\x80\x0a" # _NORON
b"\x29\x80\x64" # _DISPON
)


class _Buttons:
def __init__(self):
self.keys = keypad.ShiftRegisterKeys(clock=board.BUTTON_CLOCK,
data=board.BUTTON_OUT, latch=board.BUTTON_LATCH, key_count=8,
interval=0.05)
self.last_state = 0
self.event = keypad.Event(0, False)
self.last_z_press = None

def get_pressed(self):
buttons = self.last_state
events = self.keys.events
while events:
if events.get_into(self.event):
bit = 1 << self.event.key_number
if self.event.pressed:
buttons |= bit
self.last_state |= bit
else:
self.last_state &= ~bit
if buttons & K_START:
now = time.monotonic()
if self.last_z_press:
if now - self.last_z_press > 2:
supervisor.reload()
else:
self.last_z_press = now
else:
self.last_z_press = None
return buttons


displayio.release_displays()
_tft_spi = busio.SPI(clock=board.TFT_SCK, MOSI=board.TFT_MOSI)
_tft_spi.try_lock()
_tft_spi.configure(baudrate=24000000)
_tft_spi.unlock()
_fourwire = displayio.FourWire(_tft_spi, command=board.TFT_DC,
chip_select=board.TFT_CS)
_reset = digitalio.DigitalInOut(board.TFT_RST)
_reset.switch_to_output(value=0)
time.sleep(0.05)
_reset.value = 1
time.sleep(0.05)
chip_select=board.TFT_CS, reset=board.TFT_RST)
display = displayio.Display(_fourwire, _TFT_INIT, width=160, height=128,
rotation=0, backlight_pin=board.TFT_LITE)
rotation=0, backlight_pin=board.TFT_LITE,
auto_refresh=False, auto_brightness=True)
del _TFT_INIT
display.auto_brightness = True

buttons = gamepadshift.GamePadShift(
digitalio.DigitalInOut(board.BUTTON_CLOCK),
digitalio.DigitalInOut(board.BUTTON_OUT),
digitalio.DigitalInOut(board.BUTTON_LATCH),
)

buttons = _Buttons()
audio = stage.Audio(board.SPEAKER, board.SPEAKER_ENABLE)
77 changes: 41 additions & 36 deletions pygamer/ugame.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
"""
A helper module that initializes the display and buttons for the uGame
game console. See https://hackaday.io/project/27629-game
"""

import board
import digitalio
import analogio
import gamepadshift
import stage
import displayio
import busio
import time
import keypad


K_X = 0x01
Expand Down Expand Up @@ -47,49 +41,60 @@
b"\x13\x80\x0a" # _NORON
b"\x29\x80\x64" # _DISPON
)
displayio.release_displays()
_tft_spi = busio.SPI(clock=board.TFT_SCK, MOSI=board.TFT_MOSI)
_tft_spi.try_lock()
_tft_spi.configure(baudrate=24000000)
_tft_spi.unlock()
_fourwire = displayio.FourWire(_tft_spi, command=board.TFT_DC,
chip_select=board.TFT_CS)
_reset = digitalio.DigitalInOut(board.TFT_RST)
_reset.switch_to_output(value=0)
time.sleep(0.05)
_reset.value = 1
time.sleep(0.05)
display = displayio.Display(_fourwire, _TFT_INIT, width=160, height=128,
rotation=0, backlight_pin=board.TFT_LITE)
del _TFT_INIT
display.auto_brightness = True


class Buttons:
class _Buttons:
def __init__(self):
self.buttons = gamepadshift.GamePadShift(
digitalio.DigitalInOut(board.BUTTON_CLOCK),
digitalio.DigitalInOut(board.BUTTON_OUT),
digitalio.DigitalInOut(board.BUTTON_LATCH),
)
self.keys = keypad.ShiftRegisterKeys(clock=board.BUTTON_CLOCK,
data=board.BUTTON_OUT, latch=board.BUTTON_LATCH, key_count=4,
interval=0.05)
self.last_state = 0
self.event = keypad.Event(0, False)
self.last_z_press = None
self.joy_x = analogio.AnalogIn(board.JOYSTICK_X)
self.joy_y = analogio.AnalogIn(board.JOYSTICK_Y)

def get_pressed(self):
pressed = self.buttons.get_pressed()
buttons = self.last_state
events = self.keys.events
while events:
if events.get_into(self.event):
bit = 1 << self.event.key_number
if self.event.pressed:
buttons |= bit
self.last_state |= bit
else:
self.last_state &= ~bit
if buttons & K_START:
now = time.monotonic()
if self.last_z_press:
if now - self.last_z_press > 2:
supervisor.reload()
else:
self.last_z_press = now
else:
self.last_z_press = None
dead = 15000
x = self.joy_x.value - 32767
if x < -dead:
pressed |= K_LEFT
buttons |= K_LEFT
elif x > dead:
pressed |= K_RIGHT
buttons |= K_RIGHT
y = self.joy_y.value - 32767
if y < -dead:
pressed |= K_UP
buttons |= K_UP
elif y > dead:
pressed |= K_DOWN
return pressed
buttons |= K_DOWN
return buttons


buttons = Buttons()
displayio.release_displays()
_tft_spi = busio.SPI(clock=board.TFT_SCK, MOSI=board.TFT_MOSI)
_fourwire = displayio.FourWire(_tft_spi, command=board.TFT_DC,
chip_select=board.TFT_CS, reset=board.TFT_RST)
display = displayio.Display(_fourwire, _TFT_INIT, width=160, height=128,
rotation=0, backlight_pin=board.TFT_LITE,
auto_refresh=False, auto_brightness=True)
del _TFT_INIT
buttons = _Buttons()
audio = stage.Audio(board.SPEAKER, board.SPEAKER_ENABLE)

0 comments on commit a895b3a

Please sign in to comment.