|
1 | 1 | """ |
2 | | -A helper module that initializes the display and buttons for the uGame |
3 | | -game console. See https://hackaday.io/project/27629-game |
| 2 | +A helper module that initializes the display and buttons for the |
| 3 | +Adafruit PyGamer game console. |
4 | 4 | """ |
5 | 5 |
|
6 | 6 | import board |
7 | | -import digitalio |
8 | 7 | import analogio |
9 | | -import gamepadshift |
10 | 8 | import stage |
11 | 9 | import displayio |
12 | 10 | import busio |
13 | 11 | import time |
| 12 | +import keypad |
14 | 13 |
|
15 | 14 |
|
16 | 15 | K_X = 0x01 |
|
47 | 46 | b"\x13\x80\x0a" # _NORON |
48 | 47 | b"\x29\x80\x64" # _DISPON |
49 | 48 | ) |
50 | | -displayio.release_displays() |
51 | | -_tft_spi = busio.SPI(clock=board.TFT_SCK, MOSI=board.TFT_MOSI) |
52 | | -_tft_spi.try_lock() |
53 | | -_tft_spi.configure(baudrate=24000000) |
54 | | -_tft_spi.unlock() |
55 | | -_fourwire = displayio.FourWire(_tft_spi, command=board.TFT_DC, |
56 | | - chip_select=board.TFT_CS) |
57 | | -_reset = digitalio.DigitalInOut(board.TFT_RST) |
58 | | -_reset.switch_to_output(value=0) |
59 | | -time.sleep(0.05) |
60 | | -_reset.value = 1 |
61 | | -time.sleep(0.05) |
62 | | -display = displayio.Display(_fourwire, _TFT_INIT, width=160, height=128, |
63 | | - rotation=0, backlight_pin=board.TFT_LITE) |
64 | | -del _TFT_INIT |
65 | | -display.auto_brightness = True |
66 | 49 |
|
67 | 50 |
|
68 | | -class Buttons: |
| 51 | +class _Buttons: |
69 | 52 | def __init__(self): |
70 | | - self.buttons = gamepadshift.GamePadShift( |
71 | | - digitalio.DigitalInOut(board.BUTTON_CLOCK), |
72 | | - digitalio.DigitalInOut(board.BUTTON_OUT), |
73 | | - digitalio.DigitalInOut(board.BUTTON_LATCH), |
74 | | - ) |
| 53 | + self.keys = keypad.ShiftRegisterKeys(clock=board.BUTTON_CLOCK, |
| 54 | + data=board.BUTTON_OUT, latch=board.BUTTON_LATCH, key_count=4, |
| 55 | + interval=0.05) |
| 56 | + self.last_state = 0 |
| 57 | + self.event = keypad.Event(0, False) |
| 58 | + self.last_z_press = None |
75 | 59 | self.joy_x = analogio.AnalogIn(board.JOYSTICK_X) |
76 | 60 | self.joy_y = analogio.AnalogIn(board.JOYSTICK_Y) |
77 | 61 |
|
78 | 62 | def get_pressed(self): |
79 | | - pressed = self.buttons.get_pressed() |
| 63 | + buttons = self.last_state |
| 64 | + events = self.keys.events |
| 65 | + while events: |
| 66 | + if events.get_into(self.event): |
| 67 | + bit = 1 << self.event.key_number |
| 68 | + if self.event.pressed: |
| 69 | + buttons |= bit |
| 70 | + self.last_state |= bit |
| 71 | + else: |
| 72 | + self.last_state &= ~bit |
| 73 | + if buttons & K_START: |
| 74 | + now = time.monotonic() |
| 75 | + if self.last_z_press: |
| 76 | + if now - self.last_z_press > 2: |
| 77 | + supervisor.reload() |
| 78 | + else: |
| 79 | + self.last_z_press = now |
| 80 | + else: |
| 81 | + self.last_z_press = None |
80 | 82 | dead = 15000 |
81 | 83 | x = self.joy_x.value - 32767 |
82 | 84 | if x < -dead: |
83 | | - pressed |= K_LEFT |
| 85 | + buttons |= K_LEFT |
84 | 86 | elif x > dead: |
85 | | - pressed |= K_RIGHT |
| 87 | + buttons |= K_RIGHT |
86 | 88 | y = self.joy_y.value - 32767 |
87 | 89 | if y < -dead: |
88 | | - pressed |= K_UP |
| 90 | + buttons |= K_UP |
89 | 91 | elif y > dead: |
90 | | - pressed |= K_DOWN |
91 | | - return pressed |
| 92 | + buttons |= K_DOWN |
| 93 | + return buttons |
92 | 94 |
|
93 | 95 |
|
94 | | -buttons = Buttons() |
| 96 | +displayio.release_displays() |
| 97 | +_tft_spi = busio.SPI(clock=board.TFT_SCK, MOSI=board.TFT_MOSI) |
| 98 | +_fourwire = displayio.FourWire(_tft_spi, command=board.TFT_DC, |
| 99 | + chip_select=board.TFT_CS, reset=board.TFT_RST) |
| 100 | +display = displayio.Display(_fourwire, _TFT_INIT, width=160, height=128, |
| 101 | + rotation=0, backlight_pin=board.TFT_LITE, |
| 102 | + auto_refresh=False, auto_brightness=True) |
| 103 | +del _TFT_INIT |
| 104 | +buttons = _Buttons() |
95 | 105 | audio = stage.Audio(board.SPEAKER, board.SPEAKER_ENABLE) |
0 commit comments