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