Skip to content

Commit 59f63ea

Browse files
committed
Handle new buttons
1 parent 0a5c1c9 commit 59f63ea

10 files changed

Lines changed: 384 additions & 9 deletions

File tree

frozen/pewpew10/pew.py

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
from micropython import const
2+
import board
3+
import time
4+
import digitalio
5+
import _pew
6+
7+
8+
_FONT = (
9+
b'\xff\xff\xff\xff\xff\xff\xf3\xf3\xf7\xfb\xf3\xff\xcc\xdd\xee\xff\xff'
10+
b'\xff\xdd\x80\xdd\x80\xdd\xff\xf7\x81\xe4\xc6\xd0\xf7\xcc\xdb\xf3\xf9'
11+
b'\xcc\xff\xf6\xcdc\xdcf\xff\xf3\xf7\xfe\xff\xff\xff\xf6\xfd\xfc\xfd'
12+
b'\xf6\xff\xe7\xdf\xcf\xdf\xe7\xff\xff\xd9\xe2\xd9\xff\xff\xff\xf3\xc0'
13+
b'\xf3\xff\xff\xff\xff\xff\xf3\xf7\xfe\xff\xff\x80\xff\xff\xff\xff\xff'
14+
b'\xff\xff\xf3\xff\xcf\xdb\xf3\xf9\xfc\xff\xd2\xcd\xc8\xdc\xe1\xff\xf7'
15+
b'\xf1\xf3\xf3\xe2\xff\xe1\xce\xe3\xfd\xc0\xff\xe1\xce\xe3\xce\xe1\xff'
16+
b'\xf3\xf9\xdc\xc0\xcf\xff\xc0\xfc\xe4\xcf\xe1\xff\xd2\xfc\xe1\xcc\xe2'
17+
b'\xff\xc0\xdb\xf3\xf9\xfc\xff\xe2\xcc\xe2\xcc\xe2\xff\xe2\xcc\xd2\xcf'
18+
b'\xe1\xff\xff\xf3\xff\xf3\xff\xff\xff\xf3\xff\xf3\xf7\xfe\xcf\xf3\xfc'
19+
b'\xf3\xcf\xff\xff\xc0\xff\xc0\xff\xff\xfc\xf3\xcf\xf3\xfc\xff\xe1\xcf'
20+
b'\xe3\xfb\xf3\xff\xe2\xcd\xc4\xd4\xbd\xd2\xe2\xdd\xcc\xc4\xc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f\xdd\xe2\xfd\xec\xd8\xf4\xcc\xff\xf6\xf3\xf3\xf3'
36+
b'\xdb\xff\xff\xd9\xc4\xc8\xcc\xff\xff\xe4\xdd\xcc\xcc\xff\xff\xe2\xcc'
37+
b'\xcc\xe2\xff\xff\xe4\xdc\xcc\xe4\xfc\xff\xc6\xcd\xcc\xc6\xcf\xff\xc9'
38+
b'\xf4\xfc\xfc\xff\xff\xd2\xf8\xcb\xe1\xff\xf3\xd1\xf3\xb3\xdb\xff\xff'
39+
b'\xcc\xcc\xcd\x82\xff\xff\xcc\xdd\xe6\xf3\xff\xff\xcc\xc8\xd1\xd9\xff'
40+
b'\xff\xcc\xe6\xe6\xcc\xff\xff\xdc\xcd\xd2\xcf\xe1\xff\xc0\xdb\xf9\xc0'
41+
b'\xff\xd3\xf3\xf9\xf3\xd3\xff\xf3\xf3\xf7\xf3\xf3\xff\xf1\xf3\xdb\xf3'
42+
b'\xf1\xff\xbfr\x8d\xfe\xff\xfff\x99f\x99f\x99')
43+
44+
45+
K_RIGHT = const(0x01)
46+
K_DOWN = const(0x02)
47+
K_LEFT = const(0x04)
48+
K_UP = const(0x08)
49+
K_O = const(0x40)
50+
K_X = const(0x80)
51+
52+
_screen = None
53+
54+
55+
def brightness(level):
56+
pass
57+
58+
59+
def show(pix):
60+
_screen.blit(pix)
61+
62+
63+
def tick(delay):
64+
global _tick
65+
66+
_tick += delay
67+
time.sleep(max(0, _tick - time.monotonic()))
68+
69+
70+
class GameOver(Exception):
71+
pass
72+
73+
74+
class Pix:
75+
def __init__(self, width=8, height=8, buffer=None):
76+
if buffer is None:
77+
buffer = bytearray(width * height)
78+
self.buffer = buffer
79+
self.width = width
80+
self.height = height
81+
82+
@classmethod
83+
def from_text(cls, string, color=None, bgcolor=0, colors=None):
84+
pix = cls(4 * len(string), 6)
85+
font = memoryview(_FONT)
86+
if colors is None:
87+
if color is None:
88+
colors = (3, 2, 1, bgcolor)
89+
else:
90+
colors = (color, color, bgcolor, bgcolor)
91+
x = 0
92+
for c in string:
93+
index = ord(c) - 0x20
94+
if not 0 <= index <= 95:
95+
continue
96+
row = 0
97+
for byte in font[index * 6:index * 6 + 6]:
98+
for col in range(4):
99+
pix.pixel(x + col, row, colors[byte & 0x03])
100+
byte >>= 2
101+
row += 1
102+
x += 4
103+
return pix
104+
105+
@classmethod
106+
def from_iter(cls, lines):
107+
pix = cls(len(lines[0]), len(lines))
108+
y = 0
109+
for line in lines:
110+
x = 0
111+
for pixel in line:
112+
pix.pixel(x, y, pixel)
113+
x += 1
114+
y += 1
115+
return pix
116+
117+
def pixel(self, x, y, color=None):
118+
if not 0 <= x < self.width or not 0 <= y < self.height:
119+
return 0
120+
if color is None:
121+
return self.buffer[x + y * self.width]
122+
self.buffer[x + y * self.width] = color
123+
124+
def box(self, color, x=0, y=0, width=None, height=None):
125+
x = min(max(x, 0), self.width - 1)
126+
y = min(max(y, 0), self.height - 1)
127+
width = max(0, min(width or self.width, self.width - x))
128+
height = max(0, min(height or self.height, self.height - y))
129+
for y in range(y, y + height):
130+
xx = y * self.width + x
131+
for i in range(width):
132+
self.buffer[xx] = color
133+
xx += 1
134+
135+
def blit(self, source, dx=0, dy=0, x=0, y=0,
136+
width=None, height=None, key=None):
137+
if dx < 0:
138+
x -= dx
139+
dx = 0
140+
if x < 0:
141+
dx -= x
142+
x = 0
143+
if dy < 0:
144+
y -= dy
145+
dy = 0
146+
if y < 0:
147+
dy -= y
148+
y = 0
149+
width = min(min(width or source.width, source.width - x),
150+
self.width - dx)
151+
height = min(min(height or source.height, source.height - y),
152+
self.height - dy)
153+
source_buffer = memoryview(source.buffer)
154+
self_buffer = self.buffer
155+
if key is None:
156+
for row in range(height):
157+
xx = y * source.width + x
158+
dxx = dy * self.width + dx
159+
self_buffer[dxx:dxx + width] = source_buffer[xx:xx + width]
160+
y += 1
161+
dy += 1
162+
else:
163+
for row in range(height):
164+
xx = y * source.width + x
165+
dxx = dy * self.width + dx
166+
for col in range(width):
167+
color = source_buffer[xx]
168+
if color != key:
169+
self_buffer[dxx] = color
170+
dxx += 1
171+
xx += 1
172+
y += 1
173+
dy += 1
174+
175+
def __str__(self):
176+
return "\n".join(
177+
"".join(
178+
('.', '+', '*', '@')[self.pixel(x, y)]
179+
for x in range(self.width)
180+
)
181+
for y in range(self.height)
182+
)
183+
184+
185+
def init():
186+
global _screen, _tick, keys, _rows, _cols
187+
188+
if _screen is not None:
189+
return
190+
191+
_screen = Pix(8, 8)
192+
_tick = time.monotonic()
193+
194+
_rows = (
195+
digitalio.DigitalInOut(board.R1),
196+
digitalio.DigitalInOut(board.R2),
197+
digitalio.DigitalInOut(board.R3),
198+
digitalio.DigitalInOut(board.R4),
199+
digitalio.DigitalInOut(board.R5),
200+
digitalio.DigitalInOut(board.R6),
201+
digitalio.DigitalInOut(board.R7),
202+
digitalio.DigitalInOut(board.R8),
203+
)
204+
205+
_cols = (
206+
digitalio.DigitalInOut(board.C1),
207+
digitalio.DigitalInOut(board.C2),
208+
digitalio.DigitalInOut(board.C3),
209+
digitalio.DigitalInOut(board.C4),
210+
digitalio.DigitalInOut(board.C5),
211+
digitalio.DigitalInOut(board.C6),
212+
digitalio.DigitalInOut(board.C7),
213+
digitalio.DigitalInOut(board.C8),
214+
)
215+
_buttons = digitalio.DigitalInOut(board.BUTTONS)
216+
_pew.PewPew(_screen.buffer, _rows, _cols, _buttons)
217+
keys = _pew.get_pressed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "boards/board.h"
28+
29+
void board_init(void)
30+
{
31+
}
32+
33+
bool board_requests_safe_mode(void) {
34+
return false;
35+
}
36+
37+
void reset_board(void) {
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#define MICROPY_HW_BOARD_NAME "PewPew 10.2"
2+
#define MICROPY_HW_MCU_NAME "samd21e18"
3+
4+
#define MICROPY_PORT_A (PORT_PA24 | PORT_PA25)
5+
#define MICROPY_PORT_B (0)
6+
#define MICROPY_PORT_C (0)
7+
8+
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
9+
10+
#include "internal_flash.h"
11+
12+
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)
13+
14+
15+
#define IGNORE_PIN_PB00 1
16+
#define IGNORE_PIN_PB01 1
17+
#define IGNORE_PIN_PB02 1
18+
#define IGNORE_PIN_PB03 1
19+
#define IGNORE_PIN_PB04 1
20+
#define IGNORE_PIN_PB05 1
21+
#define IGNORE_PIN_PB06 1
22+
#define IGNORE_PIN_PB07 1
23+
#define IGNORE_PIN_PB08 1
24+
#define IGNORE_PIN_PB09 1
25+
#define IGNORE_PIN_PB10 1
26+
#define IGNORE_PIN_PB11 1
27+
#define IGNORE_PIN_PB12 1
28+
#define IGNORE_PIN_PB13 1
29+
#define IGNORE_PIN_PB14 1
30+
#define IGNORE_PIN_PB15 1
31+
#define IGNORE_PIN_PB16 1
32+
#define IGNORE_PIN_PB17 1
33+
#define IGNORE_PIN_PB22 1
34+
#define IGNORE_PIN_PB23 1
35+
#define IGNORE_PIN_PB30 1
36+
#define IGNORE_PIN_PB31 1
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
LD_FILE = boards/samd21x18-bootloader.ld
2+
USB_VID = 0x239A
3+
USB_PID = 0x801D
4+
USB_PRODUCT = "PewPew 10.2"
5+
USB_MANUFACTURER = "Radomir Dopieralski"
6+
7+
INTERNAL_FLASH_FILESYSTEM = 1
8+
LONGINT_IMPL = NONE
9+
10+
CHIP_VARIANT = SAMD21E18A
11+
CHIP_FAMILY = samd21
12+
13+
FROZEN_MPY_DIRS += $(TOP)/frozen/pewpew10
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "shared-bindings/board/__init__.h"
2+
3+
#include "board_busses.h"
4+
5+
STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
6+
{ MP_ROM_QSTR(MP_QSTR_R1), MP_ROM_PTR(&pin_PA05) },
7+
{ MP_ROM_QSTR(MP_QSTR_R2), MP_ROM_PTR(&pin_PA11) },
8+
{ MP_ROM_QSTR(MP_QSTR_R3), MP_ROM_PTR(&pin_PA28) },
9+
{ MP_ROM_QSTR(MP_QSTR_R4), MP_ROM_PTR(&pin_PA09) },
10+
{ MP_ROM_QSTR(MP_QSTR_R5), MP_ROM_PTR(&pin_PA16) },
11+
{ MP_ROM_QSTR(MP_QSTR_R6), MP_ROM_PTR(&pin_PA27) },
12+
{ MP_ROM_QSTR(MP_QSTR_R7), MP_ROM_PTR(&pin_PA17) },
13+
{ MP_ROM_QSTR(MP_QSTR_R8), MP_ROM_PTR(&pin_PA22) },
14+
15+
{ MP_ROM_QSTR(MP_QSTR_C8), MP_ROM_PTR(&pin_PA10) },
16+
{ MP_ROM_QSTR(MP_QSTR_C7), MP_ROM_PTR(&pin_PA18) },
17+
{ MP_ROM_QSTR(MP_QSTR_C6), MP_ROM_PTR(&pin_PA19) },
18+
{ MP_ROM_QSTR(MP_QSTR_C5), MP_ROM_PTR(&pin_PA06) },
19+
{ MP_ROM_QSTR(MP_QSTR_C4), MP_ROM_PTR(&pin_PA23) },
20+
{ MP_ROM_QSTR(MP_QSTR_C3), MP_ROM_PTR(&pin_PA07) },
21+
{ MP_ROM_QSTR(MP_QSTR_C2), MP_ROM_PTR(&pin_PA14) },
22+
{ MP_ROM_QSTR(MP_QSTR_C1), MP_ROM_PTR(&pin_PA15) },
23+
24+
{ MP_ROM_QSTR(MP_QSTR_P1), MP_ROM_PTR(&pin_PA30) },
25+
{ MP_ROM_QSTR(MP_QSTR_P2), MP_ROM_PTR(&pin_PA31) },
26+
{ MP_ROM_QSTR(MP_QSTR_P3), MP_ROM_PTR(&pin_PA00) },
27+
{ MP_ROM_QSTR(MP_QSTR_P4), MP_ROM_PTR(&pin_PA01) },
28+
{ MP_ROM_QSTR(MP_QSTR_P5), MP_ROM_PTR(&pin_PA02) },
29+
{ MP_ROM_QSTR(MP_QSTR_P6), MP_ROM_PTR(&pin_PA03) },
30+
{ MP_ROM_QSTR(MP_QSTR_P7), MP_ROM_PTR(&pin_PA04) },
31+
32+
{ MP_ROM_QSTR(MP_QSTR_BUTTONS), MP_ROM_PTR(&pin_PA08) },
33+
34+
};
35+
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

shared-bindings/_pew/PewPew.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@
5050
//|
5151
STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args,
5252
size_t n_kw, const mp_obj_t *pos_args) {
53-
mp_arg_check_num(n_args, n_kw, 3, 3, true);
53+
mp_arg_check_num(n_args, n_kw, 4, 4, true);
5454
mp_map_t kw_args;
5555
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
56-
enum { ARG_buffer, ARG_rows, ARG_cols };
56+
enum { ARG_buffer, ARG_rows, ARG_cols, ARG_buttons };
5757
static const mp_arg_t allowed_args[] = {
5858
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED },
5959
{ MP_QSTR_rows, MP_ARG_OBJ | MP_ARG_REQUIRED },
6060
{ MP_QSTR_cols, MP_ARG_OBJ | MP_ARG_REQUIRED },
61+
{ MP_QSTR_buttons, MP_ARG_OBJ | MP_ARG_REQUIRED },
6162
};
6263
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
6364
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args),
@@ -96,6 +97,14 @@ STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args,
9697
common_hal_digitalio_digitalinout_deinited(pin));
9798
}
9899

100+
if (!MP_OBJ_IS_TYPE(args[ARG_buttons].u_obj,
101+
&digitalio_digitalinout_type)) {
102+
mp_raise_TypeError("expected a DigitalInOut");
103+
}
104+
digitalio_digitalinout_obj_t *buttons = MP_OBJ_TO_PTR(
105+
args[ARG_buttons].u_obj);
106+
raise_error_if_deinited(
107+
common_hal_digitalio_digitalinout_deinited(buttons));
99108

100109
pew_obj_t *pew = MP_STATE_VM(pew_singleton);
101110
if (!pew) {
@@ -110,6 +119,8 @@ STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args,
110119
pew->rows_size = rows_size;
111120
pew->cols = cols;
112121
pew->cols_size = cols_size;
122+
pew->buttons = buttons;
123+
pew->pressed = 0;
113124
pew_init();
114125

115126
return MP_OBJ_FROM_PTR(pew);

0 commit comments

Comments
 (0)