Handle new buttons

This commit is contained in:
Radomir Dopieralski 2018-08-23 23:22:47 +02:00
parent 0a5c1c9402
commit 59f63eaef6
10 changed files with 384 additions and 9 deletions

217
frozen/pewpew10/pew.py Normal file
View File

@ -0,0 +1,217 @@
from micropython import const
import board
import time
import digitalio
import _pew
_FONT = (
b'\xff\xff\xff\xff\xff\xff\xf3\xf3\xf7\xfb\xf3\xff\xcc\xdd\xee\xff\xff'
b'\xff\xdd\x80\xdd\x80\xdd\xff\xf7\x81\xe4\xc6\xd0\xf7\xcc\xdb\xf3\xf9'
b'\xcc\xff\xf6\xcdc\xdcf\xff\xf3\xf7\xfe\xff\xff\xff\xf6\xfd\xfc\xfd'
b'\xf6\xff\xe7\xdf\xcf\xdf\xe7\xff\xff\xd9\xe2\xd9\xff\xff\xff\xf3\xc0'
b'\xf3\xff\xff\xff\xff\xff\xf3\xf7\xfe\xff\xff\x80\xff\xff\xff\xff\xff'
b'\xff\xff\xf3\xff\xcf\xdb\xf3\xf9\xfc\xff\xd2\xcd\xc8\xdc\xe1\xff\xf7'
b'\xf1\xf3\xf3\xe2\xff\xe1\xce\xe3\xfd\xc0\xff\xe1\xce\xe3\xce\xe1\xff'
b'\xf3\xf9\xdc\xc0\xcf\xff\xc0\xfc\xe4\xcf\xe1\xff\xd2\xfc\xe1\xcc\xe2'
b'\xff\xc0\xdb\xf3\xf9\xfc\xff\xe2\xcc\xe2\xcc\xe2\xff\xe2\xcc\xd2\xcf'
b'\xe1\xff\xff\xf3\xff\xf3\xff\xff\xff\xf3\xff\xf3\xf7\xfe\xcf\xf3\xfc'
b'\xf3\xcf\xff\xff\xc0\xff\xc0\xff\xff\xfc\xf3\xcf\xf3\xfc\xff\xe1\xcf'
b'\xe3\xfb\xf3\xff\xe2\xcd\xc4\xd4\xbd\xd2\xe2\xdd\xcc\xc4\xcc\xff\xe4'
b'\xcc\xe4\xcc\xe4\xff\xe2\xcd\xfc\xcd\xe2\xff\xe4\xdc\xcc\xdc\xe4\xff'
b'\xd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xff\xe4\xdc\xcc\xe4\xfc\xff\xc6\xcd\xcc\xc6\xcf\xff\xc9'
b'\xf4\xfc\xfc\xff\xff\xd2\xf8\xcb\xe1\xff\xf3\xd1\xf3\xb3\xdb\xff\xff'
b'\xcc\xcc\xcd\x82\xff\xff\xcc\xdd\xe6\xf3\xff\xff\xcc\xc8\xd1\xd9\xff'
b'\xff\xcc\xe6\xe6\xcc\xff\xff\xdc\xcd\xd2\xcf\xe1\xff\xc0\xdb\xf9\xc0'
b'\xff\xd3\xf3\xf9\xf3\xd3\xff\xf3\xf3\xf7\xf3\xf3\xff\xf1\xf3\xdb\xf3'
b'\xf1\xff\xbfr\x8d\xfe\xff\xfff\x99f\x99f\x99')
K_RIGHT = const(0x01)
K_DOWN = const(0x02)
K_LEFT = const(0x04)
K_UP = const(0x08)
K_O = const(0x40)
K_X = const(0x80)
_screen = None
def brightness(level):
pass
def show(pix):
_screen.blit(pix)
def tick(delay):
global _tick
_tick += delay
time.sleep(max(0, _tick - time.monotonic()))
class GameOver(Exception):
pass
class Pix:
def __init__(self, width=8, height=8, buffer=None):
if buffer is None:
buffer = bytearray(width * height)
self.buffer = buffer
self.width = width
self.height = height
@classmethod
def from_text(cls, string, color=None, bgcolor=0, colors=None):
pix = cls(4 * len(string), 6)
font = memoryview(_FONT)
if colors is None:
if color is None:
colors = (3, 2, 1, bgcolor)
else:
colors = (color, color, bgcolor, bgcolor)
x = 0
for c in string:
index = ord(c) - 0x20
if not 0 <= index <= 95:
continue
row = 0
for byte in font[index * 6:index * 6 + 6]:
for col in range(4):
pix.pixel(x + col, row, colors[byte & 0x03])
byte >>= 2
row += 1
x += 4
return pix
@classmethod
def from_iter(cls, lines):
pix = cls(len(lines[0]), len(lines))
y = 0
for line in lines:
x = 0
for pixel in line:
pix.pixel(x, y, pixel)
x += 1
y += 1
return pix
def pixel(self, x, y, color=None):
if not 0 <= x < self.width or not 0 <= y < self.height:
return 0
if color is None:
return self.buffer[x + y * self.width]
self.buffer[x + y * self.width] = color
def box(self, color, x=0, y=0, width=None, height=None):
x = min(max(x, 0), self.width - 1)
y = min(max(y, 0), self.height - 1)
width = max(0, min(width or self.width, self.width - x))
height = max(0, min(height or self.height, self.height - y))
for y in range(y, y + height):
xx = y * self.width + x
for i in range(width):
self.buffer[xx] = color
xx += 1
def blit(self, source, dx=0, dy=0, x=0, y=0,
width=None, height=None, key=None):
if dx < 0:
x -= dx
dx = 0
if x < 0:
dx -= x
x = 0
if dy < 0:
y -= dy
dy = 0
if y < 0:
dy -= y
y = 0
width = min(min(width or source.width, source.width - x),
self.width - dx)
height = min(min(height or source.height, source.height - y),
self.height - dy)
source_buffer = memoryview(source.buffer)
self_buffer = self.buffer
if key is None:
for row in range(height):
xx = y * source.width + x
dxx = dy * self.width + dx
self_buffer[dxx:dxx + width] = source_buffer[xx:xx + width]
y += 1
dy += 1
else:
for row in range(height):
xx = y * source.width + x
dxx = dy * self.width + dx
for col in range(width):
color = source_buffer[xx]
if color != key:
self_buffer[dxx] = color
dxx += 1
xx += 1
y += 1
dy += 1
def __str__(self):
return "\n".join(
"".join(
('.', '+', '*', '@')[self.pixel(x, y)]
for x in range(self.width)
)
for y in range(self.height)
)
def init():
global _screen, _tick, keys, _rows, _cols
if _screen is not None:
return
_screen = Pix(8, 8)
_tick = time.monotonic()
_rows = (
digitalio.DigitalInOut(board.R1),
digitalio.DigitalInOut(board.R2),
digitalio.DigitalInOut(board.R3),
digitalio.DigitalInOut(board.R4),
digitalio.DigitalInOut(board.R5),
digitalio.DigitalInOut(board.R6),
digitalio.DigitalInOut(board.R7),
digitalio.DigitalInOut(board.R8),
)
_cols = (
digitalio.DigitalInOut(board.C1),
digitalio.DigitalInOut(board.C2),
digitalio.DigitalInOut(board.C3),
digitalio.DigitalInOut(board.C4),
digitalio.DigitalInOut(board.C5),
digitalio.DigitalInOut(board.C6),
digitalio.DigitalInOut(board.C7),
digitalio.DigitalInOut(board.C8),
)
_buttons = digitalio.DigitalInOut(board.BUTTONS)
_pew.PewPew(_screen.buffer, _rows, _cols, _buttons)
keys = _pew.get_pressed

View File

@ -0,0 +1,38 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "boards/board.h"
void board_init(void)
{
}
bool board_requests_safe_mode(void) {
return false;
}
void reset_board(void) {
}

View File

@ -0,0 +1,36 @@
#define MICROPY_HW_BOARD_NAME "PewPew 10.2"
#define MICROPY_HW_MCU_NAME "samd21e18"
#define MICROPY_PORT_A (PORT_PA24 | PORT_PA25)
#define MICROPY_PORT_B (0)
#define MICROPY_PORT_C (0)
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#include "internal_flash.h"
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)
#define IGNORE_PIN_PB00 1
#define IGNORE_PIN_PB01 1
#define IGNORE_PIN_PB02 1
#define IGNORE_PIN_PB03 1
#define IGNORE_PIN_PB04 1
#define IGNORE_PIN_PB05 1
#define IGNORE_PIN_PB06 1
#define IGNORE_PIN_PB07 1
#define IGNORE_PIN_PB08 1
#define IGNORE_PIN_PB09 1
#define IGNORE_PIN_PB10 1
#define IGNORE_PIN_PB11 1
#define IGNORE_PIN_PB12 1
#define IGNORE_PIN_PB13 1
#define IGNORE_PIN_PB14 1
#define IGNORE_PIN_PB15 1
#define IGNORE_PIN_PB16 1
#define IGNORE_PIN_PB17 1
#define IGNORE_PIN_PB22 1
#define IGNORE_PIN_PB23 1
#define IGNORE_PIN_PB30 1
#define IGNORE_PIN_PB31 1

View File

@ -0,0 +1,13 @@
LD_FILE = boards/samd21x18-bootloader.ld
USB_VID = 0x239A
USB_PID = 0x801D
USB_PRODUCT = "PewPew 10.2"
USB_MANUFACTURER = "Radomir Dopieralski"
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
CHIP_VARIANT = SAMD21E18A
CHIP_FAMILY = samd21
FROZEN_MPY_DIRS += $(TOP)/frozen/pewpew10

View File

@ -0,0 +1,35 @@
#include "shared-bindings/board/__init__.h"
#include "board_busses.h"
STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_R1), MP_ROM_PTR(&pin_PA05) },
{ MP_ROM_QSTR(MP_QSTR_R2), MP_ROM_PTR(&pin_PA11) },
{ MP_ROM_QSTR(MP_QSTR_R3), MP_ROM_PTR(&pin_PA28) },
{ MP_ROM_QSTR(MP_QSTR_R4), MP_ROM_PTR(&pin_PA09) },
{ MP_ROM_QSTR(MP_QSTR_R5), MP_ROM_PTR(&pin_PA16) },
{ MP_ROM_QSTR(MP_QSTR_R6), MP_ROM_PTR(&pin_PA27) },
{ MP_ROM_QSTR(MP_QSTR_R7), MP_ROM_PTR(&pin_PA17) },
{ MP_ROM_QSTR(MP_QSTR_R8), MP_ROM_PTR(&pin_PA22) },
{ MP_ROM_QSTR(MP_QSTR_C8), MP_ROM_PTR(&pin_PA10) },
{ MP_ROM_QSTR(MP_QSTR_C7), MP_ROM_PTR(&pin_PA18) },
{ MP_ROM_QSTR(MP_QSTR_C6), MP_ROM_PTR(&pin_PA19) },
{ MP_ROM_QSTR(MP_QSTR_C5), MP_ROM_PTR(&pin_PA06) },
{ MP_ROM_QSTR(MP_QSTR_C4), MP_ROM_PTR(&pin_PA23) },
{ MP_ROM_QSTR(MP_QSTR_C3), MP_ROM_PTR(&pin_PA07) },
{ MP_ROM_QSTR(MP_QSTR_C2), MP_ROM_PTR(&pin_PA14) },
{ MP_ROM_QSTR(MP_QSTR_C1), MP_ROM_PTR(&pin_PA15) },
{ MP_ROM_QSTR(MP_QSTR_P1), MP_ROM_PTR(&pin_PA30) },
{ MP_ROM_QSTR(MP_QSTR_P2), MP_ROM_PTR(&pin_PA31) },
{ MP_ROM_QSTR(MP_QSTR_P3), MP_ROM_PTR(&pin_PA00) },
{ MP_ROM_QSTR(MP_QSTR_P4), MP_ROM_PTR(&pin_PA01) },
{ MP_ROM_QSTR(MP_QSTR_P5), MP_ROM_PTR(&pin_PA02) },
{ MP_ROM_QSTR(MP_QSTR_P6), MP_ROM_PTR(&pin_PA03) },
{ MP_ROM_QSTR(MP_QSTR_P7), MP_ROM_PTR(&pin_PA04) },
{ MP_ROM_QSTR(MP_QSTR_BUTTONS), MP_ROM_PTR(&pin_PA08) },
};
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

View File

@ -50,14 +50,15 @@
//|
STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args,
size_t n_kw, const mp_obj_t *pos_args) {
mp_arg_check_num(n_args, n_kw, 3, 3, true);
mp_arg_check_num(n_args, n_kw, 4, 4, true);
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
enum { ARG_buffer, ARG_rows, ARG_cols };
enum { ARG_buffer, ARG_rows, ARG_cols, ARG_buttons };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_rows, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_cols, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_buttons, MP_ARG_OBJ | MP_ARG_REQUIRED },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
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,
common_hal_digitalio_digitalinout_deinited(pin));
}
if (!MP_OBJ_IS_TYPE(args[ARG_buttons].u_obj,
&digitalio_digitalinout_type)) {
mp_raise_TypeError("expected a DigitalInOut");
}
digitalio_digitalinout_obj_t *buttons = MP_OBJ_TO_PTR(
args[ARG_buttons].u_obj);
raise_error_if_deinited(
common_hal_digitalio_digitalinout_deinited(buttons));
pew_obj_t *pew = MP_STATE_VM(pew_singleton);
if (!pew) {
@ -110,6 +119,8 @@ STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args,
pew->rows_size = rows_size;
pew->cols = cols;
pew->cols_size = cols_size;
pew->buttons = buttons;
pew->pressed = 0;
pew_init();
return MP_OBJ_FROM_PTR(pew);

View File

@ -27,6 +27,18 @@
#include "py/runtime.h"
#include "py/mphal.h"
#include "PewPew.h"
#include "shared-module/_pew/PewPew.h"
STATIC mp_obj_t get_pressed(void) {
pew_obj_t *pew = MP_STATE_VM(pew_singleton);
if (!pew) {
return mp_const_none;
}
uint8_t pressed = pew->pressed;
pew->pressed = 0;
return mp_obj_new_int(pressed);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(get_pressed_obj, get_pressed);
//| :mod:`_pew` --- LED matrix driver
@ -44,6 +56,7 @@
STATIC const mp_rom_map_elem_t pew_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__pew) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PewPew), MP_ROM_PTR(&pewpew_type)},
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&get_pressed_obj)},
};
STATIC MP_DEFINE_CONST_DICT(pew_module_globals,
pew_module_globals_table);

View File

@ -52,16 +52,17 @@ void pewpew_interrupt_handler(uint8_t index) {
}
void pew_init() {
pew_obj_t* pew_singleton = MP_STATE_VM(pew_singleton);
for (size_t i = 0; i < pew_singleton->rows_size; ++i) {
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(
pew_singleton->rows[i]);
pew_obj_t* pew = MP_STATE_VM(pew_singleton);
common_hal_digitalio_digitalinout_switch_to_input(pew->buttons, PULL_UP);
for (size_t i = 0; i < pew->rows_size; ++i) {
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pew->rows[i]);
common_hal_digitalio_digitalinout_switch_to_output(pin, false,
DRIVE_MODE_PUSH_PULL);
}
for (size_t i = 0; i < pew_singleton->cols_size; ++i) {
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(
pew_singleton->cols[i]);
for (size_t i = 0; i < pew->cols_size; ++i) {
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pew->cols[i]);
common_hal_digitalio_digitalinout_switch_to_output(pin, true,
DRIVE_MODE_OPEN_DRAIN);
}

View File

@ -28,14 +28,17 @@
#define MICROPY_INCLUDED_PEW_PEWPEW_H
#include <stdint.h>
#include "shared-bindings/digitalio/DigitalInOut.h"
typedef struct {
mp_obj_base_t base;
uint8_t* buffer;
mp_obj_t* rows;
mp_obj_t* cols;
digitalio_digitalinout_obj_t *buttons;
uint8_t rows_size;
uint8_t cols_size;
uint8_t pressed;
} pew_obj_t;
void pew_init(void);

View File

@ -36,6 +36,8 @@
void pew_tick(void) {
static uint8_t col = 0;
static uint8_t turn = 0;
static uint8_t pressed = 0;
static uint8_t last_pressed = 0;
digitalio_digitalinout_obj_t *pin;
pew_obj_t* pew = MP_STATE_VM(pew_singleton);
@ -44,12 +46,18 @@ void pew_tick(void) {
pin = MP_OBJ_TO_PTR(pew->cols[col]);
++col;
if (col >= pew->cols_size) {
pew->pressed |= last_pressed & pressed;
last_pressed = pressed;
pressed = 0;
col = 0;
++turn;
if (turn >= 8) {
turn = 0;
}
}
if (!common_hal_digitalio_digitalinout_get_value(pew->buttons)) {
pressed |= 1 << col;
}
common_hal_digitalio_digitalinout_set_value(pin, true);
for (size_t x = 0; x < pew->rows_size; ++x) {
pin = MP_OBJ_TO_PTR(pew->rows[x]);