Przeglądaj źródła

Add PicoInput to get buttons on Interstate75 W.

Thomas Buck 1 rok temu
rodzic
commit
4d2eb03483
7 zmienionych plików z 68 dodań i 24 usunięć
  1. 3
    2
      camp_pico.py
  2. 1
    2
      camp_small.py
  3. 12
    11
      manager.py
  4. 29
    1
      pico.py
  5. 2
    4
      snake.py
  6. 1
    3
      tetris.py
  7. 20
    1
      util.py

+ 3
- 2
camp_pico.py Wyświetl plik

@@ -27,7 +27,8 @@ from pico import PicoBatt
27 27
 url = "http://www.xythobuz.de"
28 28
 
29 29
 import util
30
-t = util.getTarget()
30
+i = util.getInput()
31
+t = util.getTarget(i)
31 32
 
32 33
 # Loading fonts and graphics takes a while.
33 34
 # So show a splash screen while the user waits.
@@ -60,7 +61,7 @@ d.success(success)
60 61
 d.fail(fail)
61 62
 
62 63
 # Main "Menu"
63
-m = Manager(t)
64
+m = Manager(t, i)
64 65
 m.add(QRScreen(t, img_data, 15.0))
65 66
 m.add(Solid(t, 1.0))
66 67
 m.add(d) # HTTP Check, either "success" or "fail"

+ 1
- 2
camp_small.py Wyświetl plik

@@ -28,8 +28,7 @@ url = "http://www.xythobuz.de"
28 28
 scroll_speed = 15
29 29
 
30 30
 # Need to import InputWrapper before initializing RGB Matrix on Pi
31
-i = InputWrapper()
32
-
31
+i = util.getInput()
33 32
 t = util.getTarget(i)
34 33
 
35 34
 # Loading fonts and graphics takes a while.

+ 12
- 11
manager.py Wyświetl plik

@@ -109,23 +109,15 @@ if __name__ == "__main__":
109 109
     from life import GameOfLife
110 110
 
111 111
     import util
112
-    t = util.getTarget()
112
+    i = util.getInput()
113
+    t = util.getTarget(i)
113 114
 
114 115
     splash = SplashScreen(t)
115 116
     t.loop_start()
116 117
     splash.draw()
117 118
     t.loop_end()
118 119
 
119
-    m = Manager(t)
120
-
121
-    m.add(ScrollText(t, "This appears once", "ib8x8u"))
122
-    m.add(Solid(t, 1.0))
123
-
124
-    m.add(ScrollText(t, "And this twice...", "ib8x8u", 2))
125
-    m.add(Solid(t, 1.0))
126
-
127
-    m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), 20.0, True))
128
-    m.add(Solid(t, 1.0))
120
+    m = Manager(t, i)
129 121
 
130 122
     sub = Manager(t)
131 123
     sub.add(ScrollText(t, "Hello", "ib8x8u"))
@@ -135,5 +127,14 @@ if __name__ == "__main__":
135 127
     m.add(sub)
136 128
     m.add(Solid(t, 1.0))
137 129
 
130
+    m.add(ScrollText(t, "This appears once", "ib8x8u"))
131
+    m.add(Solid(t, 1.0))
132
+
133
+    m.add(ScrollText(t, "And this twice...", "ib8x8u", 2))
134
+    m.add(Solid(t, 1.0))
135
+
136
+    m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), 5.0, True))
137
+    m.add(Solid(t, 1.0))
138
+
138 139
     m.restart()
139 140
     util.loop(t, m.draw)

+ 29
- 1
pico.py Wyświetl plik

@@ -47,7 +47,7 @@ def batt_to_color(batt):
47 47
     return r, g, b
48 48
 
49 49
 class PicoMatrix:
50
-    def __init__(self, w = 32, h = 32):
50
+    def __init__(self, input = None, w = 32, h = 32):
51 51
         self.width = w # x-axis
52 52
         self.height = h # y-axis
53 53
 
@@ -60,6 +60,10 @@ class PicoMatrix:
60 60
         if (w != 32) or (h != 32):
61 61
             raise RuntimeError("TODO not yet supported")
62 62
 
63
+        self.input = input
64
+        if self.input != None:
65
+            self.input.gui = self
66
+
63 67
         mode = interstate75.DISPLAY_INTERSTATE75_32X32
64 68
         self.matrix = interstate75.Interstate75(display = mode, panel_type = hub75.PANEL_FM6126A)
65 69
 
@@ -284,6 +288,30 @@ class PicoBatt:
284 288
         else:
285 289
             self.drawText(refresh)
286 290
 
291
+class PicoInput:
292
+    def __init__(self):
293
+        self.gui = None
294
+        self.keys = {
295
+            "left": False,
296
+            "right": False,
297
+            "up": False,
298
+            "down": False,
299
+            "a": False,
300
+            "b": False,
301
+            "x": False,
302
+            "y": False,
303
+            "l": False,
304
+            "r": False,
305
+            "start": False,
306
+            "select": False,
307
+        }
308
+
309
+    def get(self):
310
+        if self.gui != None:
311
+            self.keys["l"] = self.gui.matrix.switch_pressed(interstate75.SWITCH_A)
312
+            self.keys["r"] = self.gui.matrix.switch_pressed(interstate75.SWITCH_B)
313
+        return self.keys
314
+
287 315
 if __name__ == "__main__":
288 316
     import time
289 317
     import util

+ 2
- 4
snake.py Wyświetl plik

@@ -178,11 +178,9 @@ class Snake:
178 178
                 self.gui.set_pixel(x, y, self.colors[self.data[x][y]])
179 179
 
180 180
 if __name__ == "__main__":
181
-    # Need to import InputWrapper before initializing RGB Matrix on Pi
182
-    from gamepad import InputWrapper
183
-    i = InputWrapper()
184
-
185 181
     import util
182
+    # Need to import InputWrapper before initializing RGB Matrix on Pi
183
+    i = util.getInput()
186 184
     t = util.getTarget(i)
187 185
 
188 186
     d = Snake(t, i)

+ 1
- 3
tetris.py Wyświetl plik

@@ -422,9 +422,7 @@ class Tetris:
422 422
 
423 423
 if __name__ == "__main__":
424 424
     # Need to import InputWrapper before initializing RGB Matrix on Pi
425
-    from gamepad import InputWrapper
426
-    i = InputWrapper()
427
-
425
+    i = util.getInput()
428 426
     t = util.getTarget(i)
429 427
 
430 428
     # show splash screen while initializing

+ 20
- 1
util.py Wyświetl plik

@@ -65,7 +65,7 @@ def getTarget(i = None):
65 65
         try:
66 66
             # Next we try the Pico Interstate75 interface
67 67
             from pico import PicoMatrix
68
-            pico = PicoMatrix()
68
+            pico = PicoMatrix(i)
69 69
 
70 70
             # TODO hard-coded adjustments
71 71
             from mapper import MapperReduceBrightness
@@ -225,6 +225,25 @@ def getTextDrawer():
225 225
 
226 226
     return None
227 227
 
228
+def getInput():
229
+    try:
230
+        # try evdev library
231
+        from gamepad import InputWrapper
232
+        return InputWrapper()
233
+    except Exception as e:
234
+        print()
235
+        if hasattr(sys, "print_exception"):
236
+            sys.print_exception(e)
237
+        else:
238
+            print(e)
239
+        print()
240
+
241
+        # fall back to the Pico Interstate75 implementation
242
+        from pico import PicoInput
243
+        return PicoInput()
244
+
245
+    return None
246
+
228 247
 def loop(gui, func = None):
229 248
     while True:
230 249
         if gui.loop_start():

Ładowanie…
Anuluj
Zapisz