|
1 | | -from collections import namedtuple |
2 | 1 | import board |
3 | 2 | from micropython import const |
4 | 3 | from adafruit_seesaw.seesaw import Seesaw |
5 | 4 | from adafruit_seesaw.pwmout import PWMOut |
6 | 5 | import stage |
7 | 6 | import displayio |
8 | 7 |
|
9 | | -BUTTON_RIGHT = const(7) |
10 | | -BUTTON_DOWN = const(4) |
11 | | -BUTTON_LEFT = const(3) |
12 | | -BUTTON_UP = const(2) |
13 | | -BUTTON_SEL = const(11) |
14 | | -BUTTON_A = const(10) |
15 | | -BUTTON_B = const(9) |
16 | 8 |
|
17 | | -Buttons = namedtuple("Buttons", "up down left right a b select") |
| 9 | +K_UP = const(4) |
| 10 | +K_LEFT = const(8) |
| 11 | +K_DOWN = const(16) |
| 12 | +K_RIGHT = const(128) |
| 13 | +K_X = const(512) |
| 14 | +K_O = const(1024) |
| 15 | +K_SELECT = const(2048) |
| 16 | +K_START = const(0) |
18 | 17 |
|
19 | | -button_mask = ((1 << BUTTON_RIGHT) | |
20 | | - (1 << BUTTON_DOWN) | |
21 | | - (1 << BUTTON_LEFT) | |
22 | | - (1 << BUTTON_UP) | |
23 | | - (1 << BUTTON_SEL) | |
24 | | - (1 << BUTTON_A) | |
25 | | - (1 << BUTTON_B)) |
26 | | - |
27 | | -_INIT_SEQUENCE = bytearray( |
| 18 | +_INIT_SEQUENCE = ( |
28 | 19 | b"\x01\x80\x96" # SWRESET and Delay 150ms |
29 | 20 | b"\x11\x80\xff" # SLPOUT and Delay |
30 | 21 | b"\xb1\x03\x01\x2C\x2D" # _FRMCTR1 |
|
48 | 39 | b"\x29\x80\x64" # _DISPON |
49 | 40 | ) |
50 | 41 |
|
| 42 | + |
| 43 | +class GamePadSeesaw: |
| 44 | + mask = K_RIGHT | K_DOWN | K_LEFT | K_UP | K_SELECT | K_O | K_X |
| 45 | + |
| 46 | + def __init__(self, ss): |
| 47 | + ss.pin_mode_bulk(self.mask, ss.INPUT_PULLUP) |
| 48 | + self.ss = ss |
| 49 | + |
| 50 | + def get_pressed(self): |
| 51 | + return self.ss.digital_read_bulk(self.mask) |
| 52 | + |
| 53 | + |
| 54 | +class DummyAudio: |
| 55 | + def play(self, f, loop=False): |
| 56 | + pass |
| 57 | + |
| 58 | + def stop(self): |
| 59 | + pass |
| 60 | + |
| 61 | + def mute(self, mute): |
| 62 | + pass |
| 63 | + |
| 64 | + |
51 | 65 | i2c = board.I2C() |
52 | | -spi = board.SPI() |
53 | 66 | ss = Seesaw(i2c, 0x5E) |
54 | | -backlight = PWMOut(ss, 5) |
55 | | -brightness = 1 # 0 full off, 1 full on, 0.5 half |
56 | | -backlight.duty_cycle = int(255 * min(max(1 - brightness, 0.0), 1.0)) |
57 | | -ss.pin_mode_bulk(button_mask, ss.INPUT_PULLUP) |
| 67 | +spi = board.SPI() |
58 | 68 | displayio.release_displays() |
59 | 69 | while not spi.try_lock(): |
60 | 70 | pass |
61 | 71 | spi.configure(baudrate=24000000) |
62 | 72 | spi.unlock() |
63 | 73 | ss.pin_mode(8, ss.OUTPUT) |
64 | | -ss.digital_write(8, True) # Reset the Display via Seesaw |
65 | | -display_bus = displayio.FourWire(spi, |
66 | | - command=board.D6, |
67 | | - chip_select=board.D5) |
68 | | -display = displayio.Display(display_bus, _INIT_SEQUENCE, width=160, height=80, rowstart=24) |
69 | | -ss.pin_mode_bulk(button_mask, ss.INPUT_PULLUP) |
70 | | -# clean up some RAM |
71 | | -del _INIT_SEQUENCE, brightness |
72 | | - |
73 | | -def buttons(): |
74 | | - """ |
75 | | - Return a set of buttons with current push values |
76 | | - """ |
77 | | - button_values = ss.digital_read_bulk(button_mask) |
78 | | - return Buttons(*[not button_values & (1 << button) for button in |
79 | | - (BUTTON_UP, BUTTON_DOWN, BUTTON_LEFT, BUTTON_RIGHT, |
80 | | - BUTTON_A, BUTTON_B, BUTTON_SEL)]) |
| 74 | +ss.digital_write(8, True) # reset display |
| 75 | +display_bus = displayio.FourWire(spi, command=board.D6, chip_select=board.D5) |
| 76 | +display = displayio.Display(display_bus, _INIT_SEQUENCE, width=160, height=80, |
| 77 | + rowstart=24) |
| 78 | +del _INIT_SEQUENCE |
| 79 | +buttons = GamePadSeesaw(ss) |
| 80 | +audio = DummyAudio() |
| 81 | +backlight = PWMOut(ss, 5) |
| 82 | +backlight.duty_cycle = 0 |
0 commit comments