From a41ea275965af3e9bc43b285a5ee1080a6676d74 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 31 Jul 2018 12:32:37 +0200 Subject: [PATCH 01/20] Add pewpew70 board --- ports/atmel-samd/boards/pewpew70/board.c | 38 +++++++++++++++++++ .../boards/pewpew70/mpconfigboard.h | 12 ++++++ .../boards/pewpew70/mpconfigboard.mk | 11 ++++++ ports/atmel-samd/boards/pewpew70/pins.c | 31 +++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 ports/atmel-samd/boards/pewpew70/board.c create mode 100644 ports/atmel-samd/boards/pewpew70/mpconfigboard.h create mode 100644 ports/atmel-samd/boards/pewpew70/mpconfigboard.mk create mode 100644 ports/atmel-samd/boards/pewpew70/pins.c diff --git a/ports/atmel-samd/boards/pewpew70/board.c b/ports/atmel-samd/boards/pewpew70/board.c new file mode 100644 index 0000000000..c8e20206a1 --- /dev/null +++ b/ports/atmel-samd/boards/pewpew70/board.c @@ -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) { +} diff --git a/ports/atmel-samd/boards/pewpew70/mpconfigboard.h b/ports/atmel-samd/boards/pewpew70/mpconfigboard.h new file mode 100644 index 0000000000..e839bc5ea9 --- /dev/null +++ b/ports/atmel-samd/boards/pewpew70/mpconfigboard.h @@ -0,0 +1,12 @@ +#define MICROPY_HW_BOARD_NAME "PewPew 7.0" +#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) diff --git a/ports/atmel-samd/boards/pewpew70/mpconfigboard.mk b/ports/atmel-samd/boards/pewpew70/mpconfigboard.mk new file mode 100644 index 0000000000..1b5d1ff824 --- /dev/null +++ b/ports/atmel-samd/boards/pewpew70/mpconfigboard.mk @@ -0,0 +1,11 @@ +LD_FILE = boards/samd21x18-bootloader.ld +USB_VID = 0x239A +USB_PID = 0x801D +USB_PRODUCT = "PewPew 7.0" +USB_MANUFACTURER = "Radomir Dopieralski" + +INTERNAL_FLASH_FILESYSTEM = 1 +LONGINT_IMPL = NONE + +CHIP_VARIANT = SAMD21E18A +CHIP_FAMILY = samd21 diff --git a/ports/atmel-samd/boards/pewpew70/pins.c b/ports/atmel-samd/boards/pewpew70/pins.c new file mode 100644 index 0000000000..7102ff2336 --- /dev/null +++ b/ports/atmel-samd/boards/pewpew70/pins.c @@ -0,0 +1,31 @@ +#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_PA08) }, + { MP_ROM_QSTR(MP_QSTR_R2), MP_ROM_PTR(&pin_PA15) }, + { MP_ROM_QSTR(MP_QSTR_R3), MP_ROM_PTR(&pin_PA01) }, + { MP_ROM_QSTR(MP_QSTR_R4), MP_ROM_PTR(&pin_PA11) }, + { MP_ROM_QSTR(MP_QSTR_R5), MP_ROM_PTR(&pin_PA27) }, + { MP_ROM_QSTR(MP_QSTR_R6), MP_ROM_PTR(&pin_PA00) }, + { MP_ROM_QSTR(MP_QSTR_R7), MP_ROM_PTR(&pin_PA28) }, + { MP_ROM_QSTR(MP_QSTR_R8), MP_ROM_PTR(&pin_PA19) }, + + { MP_ROM_QSTR(MP_QSTR_C1), MP_ROM_PTR(&pin_PA14) }, + { MP_ROM_QSTR(MP_QSTR_C2), MP_ROM_PTR(&pin_PA23) }, + { MP_ROM_QSTR(MP_QSTR_C3), MP_ROM_PTR(&pin_PA22) }, + { MP_ROM_QSTR(MP_QSTR_C4), MP_ROM_PTR(&pin_PA09) }, + { MP_ROM_QSTR(MP_QSTR_C5), MP_ROM_PTR(&pin_PA18) }, + { MP_ROM_QSTR(MP_QSTR_C6), MP_ROM_PTR(&pin_PA10) }, + { MP_ROM_QSTR(MP_QSTR_C7), MP_ROM_PTR(&pin_PA16) }, + { MP_ROM_QSTR(MP_QSTR_C8), MP_ROM_PTR(&pin_PA17) }, + + { MP_ROM_QSTR(MP_QSTR_UP), MP_ROM_PTR(&pin_PA04) }, + { MP_ROM_QSTR(MP_QSTR_DOWN), MP_ROM_PTR(&pin_PA03) }, + { MP_ROM_QSTR(MP_QSTR_LEFT), MP_ROM_PTR(&pin_PA02) }, + { MP_ROM_QSTR(MP_QSTR_RIGH), MP_ROM_PTR(&pin_PA05) }, + { MP_ROM_QSTR(MP_QSTR_O), MP_ROM_PTR(&pin_PA07) }, + { MP_ROM_QSTR(MP_QSTR_X), MP_ROM_PTR(&pin_PA06) }, +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From 5a1e69f5b842fe091210a898f52712dfaceec3c6 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 31 Jul 2018 13:24:15 +0200 Subject: [PATCH 02/20] Correct a typo in the pin name --- ports/atmel-samd/boards/pewpew70/pins.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/atmel-samd/boards/pewpew70/pins.c b/ports/atmel-samd/boards/pewpew70/pins.c index 7102ff2336..9fca3c4ad9 100644 --- a/ports/atmel-samd/boards/pewpew70/pins.c +++ b/ports/atmel-samd/boards/pewpew70/pins.c @@ -24,7 +24,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_UP), MP_ROM_PTR(&pin_PA04) }, { MP_ROM_QSTR(MP_QSTR_DOWN), MP_ROM_PTR(&pin_PA03) }, { MP_ROM_QSTR(MP_QSTR_LEFT), MP_ROM_PTR(&pin_PA02) }, - { MP_ROM_QSTR(MP_QSTR_RIGH), MP_ROM_PTR(&pin_PA05) }, + { MP_ROM_QSTR(MP_QSTR_RIGHT), MP_ROM_PTR(&pin_PA05) }, { MP_ROM_QSTR(MP_QSTR_O), MP_ROM_PTR(&pin_PA07) }, { MP_ROM_QSTR(MP_QSTR_X), MP_ROM_PTR(&pin_PA06) }, }; From 7ac11ed8e1da94c5408842cffd98a7265327f0df Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 31 Jul 2018 16:04:55 +0200 Subject: [PATCH 03/20] Enlarge the usb disk --- ports/atmel-samd/boards/pewpew70/mpconfigboard.h | 2 +- ports/atmel-samd/boards/samd21x18-bootloader-crystalless.ld | 2 +- ports/atmel-samd/supervisor/internal_flash.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/atmel-samd/boards/pewpew70/mpconfigboard.h b/ports/atmel-samd/boards/pewpew70/mpconfigboard.h index e839bc5ea9..92662bd551 100644 --- a/ports/atmel-samd/boards/pewpew70/mpconfigboard.h +++ b/ports/atmel-samd/boards/pewpew70/mpconfigboard.h @@ -9,4 +9,4 @@ #include "internal_flash.h" -#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) +#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x014000) diff --git a/ports/atmel-samd/boards/samd21x18-bootloader-crystalless.ld b/ports/atmel-samd/boards/samd21x18-bootloader-crystalless.ld index 2adf4fa909..bc4b53ae9c 100644 --- a/ports/atmel-samd/boards/samd21x18-bootloader-crystalless.ld +++ b/ports/atmel-samd/boards/samd21x18-bootloader-crystalless.ld @@ -6,7 +6,7 @@ MEMORY { /* Leave 8KiB for the bootloader, 256b for persistent config (clock), 64k for the flash file system and 256b for the user config. */ - FLASH (rx) : ORIGIN = 0x00000000 + 8K, LENGTH = 256K - 8K - 256 - 64K - 256 + FLASH (rx) : ORIGIN = 0x00000000 + 8K, LENGTH = 256K - 8K - 256 - 80K - 256 RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K } diff --git a/ports/atmel-samd/supervisor/internal_flash.h b/ports/atmel-samd/supervisor/internal_flash.h index 0939a34548..d1037012d1 100644 --- a/ports/atmel-samd/supervisor/internal_flash.h +++ b/ports/atmel-samd/supervisor/internal_flash.h @@ -37,7 +37,7 @@ #endif #ifdef SAMD21 -#define TOTAL_INTERNAL_FLASH_SIZE 0x010000 +#define TOTAL_INTERNAL_FLASH_SIZE 0x014000 #endif #define INTERNAL_FLASH_MEM_SEG1_START_ADDR (FLASH_SIZE - TOTAL_INTERNAL_FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE) From 88e40193ae92a390ff0cc8872ec2b7cd953c83cb Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Wed, 1 Aug 2018 15:29:26 +0200 Subject: [PATCH 04/20] Add a _pew module --- ports/atmel-samd/supervisor/port.c | 6 ++ ports/atmel-samd/tick.c | 6 ++ py/circuitpy_defns.mk | 3 + py/circuitpy_mpconfig.h | 7 ++ shared-bindings/_pew/PewPew.c | 130 +++++++++++++++++++++++++++++ shared-bindings/_pew/PewPew.h | 33 ++++++++ shared-bindings/_pew/__init__.c | 54 ++++++++++++ shared-module/_pew/PewPew.c | 52 ++++++++++++ shared-module/_pew/PewPew.h | 47 +++++++++++ shared-module/_pew/__init__.c | 72 ++++++++++++++++ shared-module/_pew/__init__.h | 33 ++++++++ 11 files changed, 443 insertions(+) create mode 100644 shared-bindings/_pew/PewPew.c create mode 100644 shared-bindings/_pew/PewPew.h create mode 100644 shared-bindings/_pew/__init__.c create mode 100644 shared-module/_pew/PewPew.c create mode 100644 shared-module/_pew/PewPew.h create mode 100644 shared-module/_pew/__init__.c create mode 100644 shared-module/_pew/__init__.h diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index aee082de01..9b5de88e66 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -71,6 +71,9 @@ #ifdef CIRCUITPY_GAMEPAD_TICKS #include "shared-module/gamepad/__init__.h" #endif +#ifdef CIRCUITPY_PEWPEW_TICKS +#include "shared-module/_pew/__init__.h" +#endif extern volatile bool mp_msc_enabled; @@ -225,6 +228,9 @@ void reset_port(void) { #ifdef CIRCUITPY_GAMEPAD_TICKS gamepad_reset(); #endif +#ifdef CIRCUITPY_PEWPEW_TICKS + pew_reset(); +#endif reset_event_system(); diff --git a/ports/atmel-samd/tick.c b/ports/atmel-samd/tick.c index 1493477933..e1f9c180b6 100644 --- a/ports/atmel-samd/tick.c +++ b/ports/atmel-samd/tick.c @@ -30,6 +30,7 @@ #include "supervisor/shared/autoreload.h" #include "shared-module/gamepad/__init__.h" +#include "shared-module/_pew/__init__.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Processor.h" @@ -54,6 +55,11 @@ void SysTick_Handler(void) { gamepad_tick(); } #endif + #ifdef CIRCUITPY_PEWPEW_TICKS + if (!(ticks_ms & CIRCUITPY_PEWPEW_TICKS)) { + pew_tick(); + } + #endif } void tick_init() { diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 2fa2e78e2a..080263487e 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -198,6 +198,9 @@ endif ifeq ($(CIRCUITPY_USTACK),1) SRC_PATTERNS += ustack/% endif +ifeq ($(CIRCUITPY_PEW),1) +SRC_PATTERNS += _pew/% +endif # All possible sources are listed here, and are filtered by SRC_PATTERNS. SRC_COMMON_HAL = \ diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 6f97054854..c20c0fb662 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -463,6 +463,13 @@ extern const struct _mp_obj_module_t ustack_module; #define USTACK_MODULE #endif +#if CIRCUITPY_PEW +extern const struct _mp_obj_module_t pew_module; +#define PEW_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__pew),(mp_obj_t)&pew_module }, +#else +#define PEW_MODULE +#endif + // These modules are not yet in shared-bindings, but we prefer the non-uxxx names. #if MICROPY_PY_UERRNO #define ERRNO_MODULE { MP_ROM_QSTR(MP_QSTR_errno), MP_ROM_PTR(&mp_module_uerrno) }, diff --git a/shared-bindings/_pew/PewPew.c b/shared-bindings/_pew/PewPew.c new file mode 100644 index 0000000000..c819dd8616 --- /dev/null +++ b/shared-bindings/_pew/PewPew.c @@ -0,0 +1,130 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski 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 "py/obj.h" +#include "py/runtime.h" +#include "py/mphal.h" +#include "py/gc.h" +#include "py/mpstate.h" +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/util.h" +#include "PewPew.h" +#include "shared-module/_pew/PewPew.h" + + +//| .. currentmodule:: _pew +//| +//| :class:`PewPew` -- LED Matrix driver +//| ==================================== +//| +//| Usage:: +//| +//| + +//| .. class:: PewPew(buffer, rows, cols) +//| +//| Initializes matrix scanning routines. +//| +//| +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_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); + enum { ARG_buffer, ARG_rows, ARG_cols }; + 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_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), + allowed_args, args); + + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); + + size_t rows_size = 0; + mp_obj_t *rows; + mp_obj_get_array(args[ARG_rows].u_obj, &rows_size, &rows); + + size_t cols_size = 0; + mp_obj_t *cols; + mp_obj_get_array(args[ARG_cols].u_obj, &cols_size, &cols); + + if (bufinfo.len != rows_size * cols_size) { + mp_raise_TypeError("wrong buffer size"); + } + + for (size_t i = 0; i < rows_size; ++i) { + if (!MP_OBJ_IS_TYPE(rows[i], &digitalio_digitalinout_type)) { + mp_raise_TypeError("expected a DigitalInOut"); + } + digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(rows[i]); + raise_error_if_deinited( + common_hal_digitalio_digitalinout_deinited(pin)); + } + + for (size_t i = 0; i < cols_size; ++i) { + if (!MP_OBJ_IS_TYPE(cols[i], &digitalio_digitalinout_type)) { + mp_raise_TypeError("expected a DigitalInOut"); + } + digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(cols[i]); + raise_error_if_deinited( + common_hal_digitalio_digitalinout_deinited(pin)); + } + + + pew_obj_t *pew = MP_STATE_VM(pew_singleton); + if (!pew) { + pew = m_new_obj(pew_obj_t); + pew->base.type = &pewpew_type; + pew = gc_make_long_lived(pew); + MP_STATE_VM(pew_singleton) = pew; + } + + pew->buffer = bufinfo.buf; + pew->rows = rows; + pew->rows_size = rows_size; + pew->cols = cols; + pew->cols_size = cols_size; + pew->col = 0; + pew->turn = 0; + pew_init(); + + return MP_OBJ_FROM_PTR(pew); +} + + +STATIC const mp_rom_map_elem_t pewpew_locals_dict_table[] = { +}; +STATIC MP_DEFINE_CONST_DICT(pewpew_locals_dict, pewpew_locals_dict_table); +const mp_obj_type_t pewpew_type = { + { &mp_type_type }, + .name = MP_QSTR_PewPew, + .make_new = pewpew_make_new, + .locals_dict = (mp_obj_dict_t*)&pewpew_locals_dict, +}; + diff --git a/shared-bindings/_pew/PewPew.h b/shared-bindings/_pew/PewPew.h new file mode 100644 index 0000000000..13633ef934 --- /dev/null +++ b/shared-bindings/_pew/PewPew.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski 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. + */ + + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_PEW_PEWPEW_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_PEW_PEWPEW_H + +extern const mp_obj_type_t pewpew_type; + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_PEW_PEWPEW_H diff --git a/shared-bindings/_pew/__init__.c b/shared-bindings/_pew/__init__.c new file mode 100644 index 0000000000..39a3bc885f --- /dev/null +++ b/shared-bindings/_pew/__init__.c @@ -0,0 +1,54 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski 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 "py/obj.h" +#include "py/runtime.h" +#include "py/mphal.h" +#include "PewPew.h" + + +//| :mod:`_pew` --- LED matrix driver +//| ================================== +//| +//| .. module:: _pew +//| :synopsis: LED matrix driver +//| :platform: SAMD21 +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| PewPew +//| +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)}, +}; +STATIC MP_DEFINE_CONST_DICT(pew_module_globals, + pew_module_globals_table); + +const mp_obj_module_t pew_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&pew_module_globals, +}; diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c new file mode 100644 index 0000000000..ede8ed9bb9 --- /dev/null +++ b/shared-module/_pew/PewPew.c @@ -0,0 +1,52 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski 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 + +#include "py/mpstate.h" +#include "__init__.h" +#include "PewPew.h" + +#include "shared-bindings/digitalio/Pull.h" +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/util.h" + + +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]); + 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]); + common_hal_digitalio_digitalinout_switch_to_output(pin, true, + DRIVE_MODE_OPEN_DRAIN); + } +} diff --git a/shared-module/_pew/PewPew.h b/shared-module/_pew/PewPew.h new file mode 100644 index 0000000000..b21ff01372 --- /dev/null +++ b/shared-module/_pew/PewPew.h @@ -0,0 +1,47 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski 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. + */ + +#ifndef MICROPY_INCLUDED_PEW_PEWPEW_H +#define MICROPY_INCLUDED_PEW_PEWPEW_H + +#include + +#include "shared-bindings/digitalio/DigitalInOut.h" + +typedef struct { + mp_obj_base_t base; + uint8_t* buffer; + mp_obj_t* rows; + mp_obj_t* cols; + size_t rows_size; + size_t cols_size; + volatile uint8_t col; + volatile uint8_t turn; +} pew_obj_t; + +void pew_init(void); + +#endif // MICROPY_INCLUDED_PEW_PEWPEW_H diff --git a/shared-module/_pew/__init__.c b/shared-module/_pew/__init__.c new file mode 100644 index 0000000000..92637a04dd --- /dev/null +++ b/shared-module/_pew/__init__.c @@ -0,0 +1,72 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski 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 + +#include "py/mpstate.h" +#include "__init__.h" +#include "PewPew.h" + +#include "shared-bindings/digitalio/DigitalInOut.h" + + +void pew_tick(void) { + digitalio_digitalinout_obj_t *pin; + pew_obj_t* pew = MP_STATE_VM(pew_singleton); + if (!pew) { return; } + + pin = MP_OBJ_TO_PTR(pew->cols[pew->col]); + common_hal_digitalio_digitalinout_set_value(pin, true); + pew->col += 1; + if (pew->col >= pew->cols_size) { + pew->col = 0; + pew->turn += 1; + if (pew->turn >= 4) { + pew->turn = 0; + } + } + for (size_t x = 0; x < pew->rows_size; ++x) { + pin = MP_OBJ_TO_PTR(pew->rows[x]); + uint8_t color = pew->buffer[(pew->col) * (pew->rows_size) + x]; + bool value = true; + switch (pew->turn) { + case 0: + if (color & 0x03) { value = true; } + break; + case 1: + case 2: + if (color & 0x02) { value = true; } + break; + } + common_hal_digitalio_digitalinout_set_value(pin, value); + } + pin = MP_OBJ_TO_PTR(pew->cols[pew->col]); + common_hal_digitalio_digitalinout_set_value(pin, false); +} + +void pew_reset(void) { + MP_STATE_VM(pew_singleton) = NULL; +} diff --git a/shared-module/_pew/__init__.h b/shared-module/_pew/__init__.h new file mode 100644 index 0000000000..30c9c8c930 --- /dev/null +++ b/shared-module/_pew/__init__.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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. + */ + +#ifndef MICROPY_INCLUDED_PEW_H +#define MICROPY_INCLUDED_PEW_H + +void pew_tick(void); +void pew_reset(void); + +#endif // MICROPY_INCLUDED_PEW_H From 55b511a5d887c5e02031d40c661b31fce336c488 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Thu, 9 Aug 2018 22:35:26 +0200 Subject: [PATCH 05/20] Use a dedicated timer --- .../boards/pewpew70/mpconfigboard.h | 2 +- .../boards/pewpew70/mpconfigboard.mk | 2 + .../samd21x18-bootloader-crystalless.ld | 2 +- ports/atmel-samd/supervisor/internal_flash.h | 2 +- ports/atmel-samd/supervisor/port.c | 2 +- ports/atmel-samd/tick.c | 5 -- shared-bindings/_pew/PewPew.c | 2 - shared-module/_pew/PewPew.c | 69 +++++++++++++++++++ shared-module/_pew/PewPew.h | 10 ++- shared-module/_pew/__init__.c | 45 ++++++------ shared-module/_pew/__init__.h | 1 - 11 files changed, 104 insertions(+), 38 deletions(-) diff --git a/ports/atmel-samd/boards/pewpew70/mpconfigboard.h b/ports/atmel-samd/boards/pewpew70/mpconfigboard.h index 92662bd551..e839bc5ea9 100644 --- a/ports/atmel-samd/boards/pewpew70/mpconfigboard.h +++ b/ports/atmel-samd/boards/pewpew70/mpconfigboard.h @@ -9,4 +9,4 @@ #include "internal_flash.h" -#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x014000) +#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) diff --git a/ports/atmel-samd/boards/pewpew70/mpconfigboard.mk b/ports/atmel-samd/boards/pewpew70/mpconfigboard.mk index 1b5d1ff824..f316bfb2ee 100644 --- a/ports/atmel-samd/boards/pewpew70/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pewpew70/mpconfigboard.mk @@ -9,3 +9,5 @@ LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 + +FROZEN_MPY_DIRS += $(TOP)/frozen/pewpew70 diff --git a/ports/atmel-samd/boards/samd21x18-bootloader-crystalless.ld b/ports/atmel-samd/boards/samd21x18-bootloader-crystalless.ld index bc4b53ae9c..2adf4fa909 100644 --- a/ports/atmel-samd/boards/samd21x18-bootloader-crystalless.ld +++ b/ports/atmel-samd/boards/samd21x18-bootloader-crystalless.ld @@ -6,7 +6,7 @@ MEMORY { /* Leave 8KiB for the bootloader, 256b for persistent config (clock), 64k for the flash file system and 256b for the user config. */ - FLASH (rx) : ORIGIN = 0x00000000 + 8K, LENGTH = 256K - 8K - 256 - 80K - 256 + FLASH (rx) : ORIGIN = 0x00000000 + 8K, LENGTH = 256K - 8K - 256 - 64K - 256 RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K } diff --git a/ports/atmel-samd/supervisor/internal_flash.h b/ports/atmel-samd/supervisor/internal_flash.h index d1037012d1..0939a34548 100644 --- a/ports/atmel-samd/supervisor/internal_flash.h +++ b/ports/atmel-samd/supervisor/internal_flash.h @@ -37,7 +37,7 @@ #endif #ifdef SAMD21 -#define TOTAL_INTERNAL_FLASH_SIZE 0x014000 +#define TOTAL_INTERNAL_FLASH_SIZE 0x010000 #endif #define INTERNAL_FLASH_MEM_SEG1_START_ADDR (FLASH_SIZE - TOTAL_INTERNAL_FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE) diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index 9b5de88e66..d1e0ae5936 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -72,7 +72,7 @@ #include "shared-module/gamepad/__init__.h" #endif #ifdef CIRCUITPY_PEWPEW_TICKS -#include "shared-module/_pew/__init__.h" +#include "shared-module/_pew/PewPew.h" #endif extern volatile bool mp_msc_enabled; diff --git a/ports/atmel-samd/tick.c b/ports/atmel-samd/tick.c index e1f9c180b6..44b631838e 100644 --- a/ports/atmel-samd/tick.c +++ b/ports/atmel-samd/tick.c @@ -55,11 +55,6 @@ void SysTick_Handler(void) { gamepad_tick(); } #endif - #ifdef CIRCUITPY_PEWPEW_TICKS - if (!(ticks_ms & CIRCUITPY_PEWPEW_TICKS)) { - pew_tick(); - } - #endif } void tick_init() { diff --git a/shared-bindings/_pew/PewPew.c b/shared-bindings/_pew/PewPew.c index c819dd8616..c9322100fa 100644 --- a/shared-bindings/_pew/PewPew.c +++ b/shared-bindings/_pew/PewPew.c @@ -110,8 +110,6 @@ 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->col = 0; - pew->turn = 0; pew_init(); return MP_OBJ_FROM_PTR(pew); diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c index ede8ed9bb9..de1224a37f 100644 --- a/shared-module/_pew/PewPew.c +++ b/shared-module/_pew/PewPew.c @@ -27,14 +27,30 @@ #include #include "py/mpstate.h" +#include "py/runtime.h" #include "__init__.h" #include "PewPew.h" #include "shared-bindings/digitalio/Pull.h" #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/util.h" +#include "samd/timers.h" +static uint8_t pewpew_tc_index = 0xff; + + +void pewpew_interrupt_handler(uint8_t index) { + if (index != pewpew_tc_index) return; + Tc* tc = tc_insts[index]; + if (!tc->COUNT16.INTFLAG.bit.MC0) return; + + pew_tick(); + + // Clear the interrupt bit. + tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0; +} + void pew_init() { pew_obj_t* pew_singleton = MP_STATE_VM(pew_singleton); for (size_t i = 0; i < pew_singleton->rows_size; ++i) { @@ -49,4 +65,57 @@ void pew_init() { common_hal_digitalio_digitalinout_switch_to_output(pin, true, DRIVE_MODE_OPEN_DRAIN); } + if (pewpew_tc_index == 0xff) { + // Find a spare timer. + Tc *tc = NULL; + int8_t index = TC_INST_NUM - 1; + for (; index >= 0; index--) { + if (tc_insts[index]->COUNT16.CTRLA.bit.ENABLE == 0) { + tc = tc_insts[index]; + break; + } + } + if (tc == NULL) { + mp_raise_RuntimeError("All timers in use"); + } + + pewpew_tc_index = index; + + // We use GCLK0 for SAMD21 and GCLK1 for SAMD51 because they both run + // at 48mhz making our math the same across the boards. + #ifdef SAMD21 + turn_on_clocks(true, index, 0); + #endif + #ifdef SAMD51 + turn_on_clocks(true, index, 1); + #endif + + + #ifdef SAMD21 + tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 | + TC_CTRLA_PRESCALER_DIV64 | + TC_CTRLA_WAVEGEN_MFRQ; + #endif + #ifdef SAMD51 + tc_reset(tc); + tc_set_enable(tc, false); + tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 + | TC_CTRLA_PRESCALER_DIV64; + tc->COUNT16.WAVE.reg = TC_WAVE_WAVEGEN_MFRQ; + #endif + + tc_set_enable(tc, true); + //tc->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_STOP; + tc->COUNT16.CC[0].reg = 160; + + // Clear our interrupt in case it was set earlier + tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0; + tc->COUNT16.INTENSET.reg = TC_INTENSET_MC0; + tc_enable_interrupts(pewpew_tc_index); + } +} + +void pew_reset(void) { + MP_STATE_VM(pew_singleton) = NULL; + pewpew_tc_index = 0xff; } diff --git a/shared-module/_pew/PewPew.h b/shared-module/_pew/PewPew.h index b21ff01372..2f25f9ce2d 100644 --- a/shared-module/_pew/PewPew.h +++ b/shared-module/_pew/PewPew.h @@ -29,19 +29,17 @@ #include -#include "shared-bindings/digitalio/DigitalInOut.h" - typedef struct { mp_obj_base_t base; uint8_t* buffer; mp_obj_t* rows; mp_obj_t* cols; - size_t rows_size; - size_t cols_size; - volatile uint8_t col; - volatile uint8_t turn; + uint8_t rows_size; + uint8_t cols_size; } pew_obj_t; void pew_init(void); +void pewpew_interrupt_handler(uint8_t index); +void pew_reset(void); #endif // MICROPY_INCLUDED_PEW_PEWPEW_H diff --git a/shared-module/_pew/__init__.c b/shared-module/_pew/__init__.c index 92637a04dd..2e09932f3a 100644 --- a/shared-module/_pew/__init__.c +++ b/shared-module/_pew/__init__.c @@ -34,39 +34,44 @@ void pew_tick(void) { + static uint8_t col = 0; + static uint8_t turn = 0; digitalio_digitalinout_obj_t *pin; + pew_obj_t* pew = MP_STATE_VM(pew_singleton); if (!pew) { return; } - pin = MP_OBJ_TO_PTR(pew->cols[pew->col]); - common_hal_digitalio_digitalinout_set_value(pin, true); - pew->col += 1; - if (pew->col >= pew->cols_size) { - pew->col = 0; - pew->turn += 1; - if (pew->turn >= 4) { - pew->turn = 0; + pin = MP_OBJ_TO_PTR(pew->cols[col]); + ++col; + if (col >= pew->cols_size) { + col = 0; + ++turn; + if (turn >= 8) { + turn = 0; } } + 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]); - uint8_t color = pew->buffer[(pew->col) * (pew->rows_size) + x]; - bool value = true; - switch (pew->turn) { - case 0: - if (color & 0x03) { value = true; } + uint8_t color = pew->buffer[col * (pew->rows_size) + x]; + bool value = false; + switch (color & 0x03) { + case 3: + value = true; break; - case 1: case 2: - if (color & 0x02) { value = true; } + if (turn == 2 || turn == 4 || turn == 6) { + value = true; + } + case 1: + if (turn == 0) { + value = true; + } + case 0: break; } common_hal_digitalio_digitalinout_set_value(pin, value); } - pin = MP_OBJ_TO_PTR(pew->cols[pew->col]); + pin = MP_OBJ_TO_PTR(pew->cols[col]); common_hal_digitalio_digitalinout_set_value(pin, false); } - -void pew_reset(void) { - MP_STATE_VM(pew_singleton) = NULL; -} diff --git a/shared-module/_pew/__init__.h b/shared-module/_pew/__init__.h index 30c9c8c930..f85dec7491 100644 --- a/shared-module/_pew/__init__.h +++ b/shared-module/_pew/__init__.h @@ -28,6 +28,5 @@ #define MICROPY_INCLUDED_PEW_H void pew_tick(void); -void pew_reset(void); #endif // MICROPY_INCLUDED_PEW_H From 0a5c1c94025a1d36762d14b48995337b0f09ce97 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sun, 12 Aug 2018 14:36:38 +0200 Subject: [PATCH 06/20] Some cleanup --- ports/atmel-samd/supervisor/port.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index d1e0ae5936..4aa07a6dbc 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -71,9 +71,7 @@ #ifdef CIRCUITPY_GAMEPAD_TICKS #include "shared-module/gamepad/__init__.h" #endif -#ifdef CIRCUITPY_PEWPEW_TICKS #include "shared-module/_pew/PewPew.h" -#endif extern volatile bool mp_msc_enabled; @@ -228,9 +226,7 @@ void reset_port(void) { #ifdef CIRCUITPY_GAMEPAD_TICKS gamepad_reset(); #endif -#ifdef CIRCUITPY_PEWPEW_TICKS pew_reset(); -#endif reset_event_system(); From 59f63eaef6d1024163864c9b30f879efc9dfd682 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Thu, 23 Aug 2018 23:22:47 +0200 Subject: [PATCH 07/20] Handle new buttons --- frozen/pewpew10/pew.py | 217 ++++++++++++++++++ ports/atmel-samd/boards/pewpew10/board.c | 38 +++ .../boards/pewpew10/mpconfigboard.h | 36 +++ .../boards/pewpew10/mpconfigboard.mk | 13 ++ ports/atmel-samd/boards/pewpew10/pins.c | 35 +++ shared-bindings/_pew/PewPew.c | 15 +- shared-bindings/_pew/__init__.c | 13 ++ shared-module/_pew/PewPew.c | 15 +- shared-module/_pew/PewPew.h | 3 + shared-module/_pew/__init__.c | 8 + 10 files changed, 384 insertions(+), 9 deletions(-) create mode 100644 frozen/pewpew10/pew.py create mode 100644 ports/atmel-samd/boards/pewpew10/board.c create mode 100644 ports/atmel-samd/boards/pewpew10/mpconfigboard.h create mode 100644 ports/atmel-samd/boards/pewpew10/mpconfigboard.mk create mode 100644 ports/atmel-samd/boards/pewpew10/pins.c diff --git a/frozen/pewpew10/pew.py b/frozen/pewpew10/pew.py new file mode 100644 index 0000000000..8d60d0ec6d --- /dev/null +++ b/frozen/pewpew10/pew.py @@ -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'\xd0\xfc\xf4\xfc\xd0\xff\xd0\xfc\xfc\xf4\xfc\xff\xd2\xfd\xfc\x8d\xd2' + b'\xff\xcc\xcc\xc4\xcc\xcc\xff\xd1\xf3\xf3\xf3\xd1\xff\xcb\xcf\xcf\xdc' + b'\xe2\xff\xdc\xcc\xd8\xf4\xc8\xff\xfc\xfc\xfc\xec\xc0\xff\xdd\xc4\xc0' + b'\xc8\xcc\xff\xcd\xd4\xd1\xc5\xdc\xff\xe2\xdd\xcc\xdd\xe2\xff\xe4\xcc' + b'\xcc\xe4\xfc\xff\xe2\xcc\xcc\xc8\xd2\xcf\xe4\xcc\xcc\xe0\xcc\xff\xd2' + b'\xec\xe2\xce\xe1\xff\xc0\xe2\xf3\xf3\xf3\xff\xcc\xcc\xcc\xdd\xe2\xff' + b'\xcc\xcc\xdd\xe6\xf3\xff\xcc\xc8\xc4\xc0\xd9\xff\xcc\xd9\xe2\xd9\xcc' + b'\xff\xcc\xdd\xe6\xf3\xf3\xff\xc0\xde\xf7\xed\xc0\xff\xd0\xfc\xfc\xfc' + b'\xd0\xff\xfc\xf9\xf3\xdb\xcf\xff\xc1\xcf\xcf\xcf\xc1\xff\xf3\xd9\xee' + b'\xff\xff\xff\xff\xff\xff\xff\x80\xff\xfc\xf7\xef\xff\xff\xff\xff\xd2' + b'\xcd\xcc\x86\xff\xfc\xe4\xdc\xcc\xe4\xff\xff\xd2\xfd\xbc\xc6\xff\xcf' + b'\xc6\xcd\xcc\x86\xff\xff\xd6\xcd\xb1\xd2\xff\xcb\xb7\xc1\xf3\xf3\xf6' + b'\xff\xe2\xcc\xd2\xdf\xe1\xfc\xe4\xdc\xcc\xcc\xff\xf3\xfb\xf1\xb3\xdb' + b'\xff\xcf\xef\xc7\xcf\xdd\xe2\xfd\xec\xd8\xf4\xcc\xff\xf6\xf3\xf3\xf3' + b'\xdb\xff\xff\xd9\xc4\xc8\xcc\xff\xff\xe4\xdd\xcc\xcc\xff\xff\xe2\xcc' + b'\xcc\xe2\xff\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 diff --git a/ports/atmel-samd/boards/pewpew10/board.c b/ports/atmel-samd/boards/pewpew10/board.c new file mode 100644 index 0000000000..c8e20206a1 --- /dev/null +++ b/ports/atmel-samd/boards/pewpew10/board.c @@ -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) { +} diff --git a/ports/atmel-samd/boards/pewpew10/mpconfigboard.h b/ports/atmel-samd/boards/pewpew10/mpconfigboard.h new file mode 100644 index 0000000000..3fd1cbce18 --- /dev/null +++ b/ports/atmel-samd/boards/pewpew10/mpconfigboard.h @@ -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 diff --git a/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk b/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk new file mode 100644 index 0000000000..cf3004a4da --- /dev/null +++ b/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk @@ -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 diff --git a/ports/atmel-samd/boards/pewpew10/pins.c b/ports/atmel-samd/boards/pewpew10/pins.c new file mode 100644 index 0000000000..499e6270b8 --- /dev/null +++ b/ports/atmel-samd/boards/pewpew10/pins.c @@ -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); diff --git a/shared-bindings/_pew/PewPew.c b/shared-bindings/_pew/PewPew.c index c9322100fa..69c9ff4fc9 100644 --- a/shared-bindings/_pew/PewPew.c +++ b/shared-bindings/_pew/PewPew.c @@ -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); diff --git a/shared-bindings/_pew/__init__.c b/shared-bindings/_pew/__init__.c index 39a3bc885f..652d95f1b5 100644 --- a/shared-bindings/_pew/__init__.c +++ b/shared-bindings/_pew/__init__.c @@ -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); diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c index de1224a37f..27c2d1c997 100644 --- a/shared-module/_pew/PewPew.c +++ b/shared-module/_pew/PewPew.c @@ -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); } diff --git a/shared-module/_pew/PewPew.h b/shared-module/_pew/PewPew.h index 2f25f9ce2d..57b25c3a0c 100644 --- a/shared-module/_pew/PewPew.h +++ b/shared-module/_pew/PewPew.h @@ -28,14 +28,17 @@ #define MICROPY_INCLUDED_PEW_PEWPEW_H #include +#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); diff --git a/shared-module/_pew/__init__.c b/shared-module/_pew/__init__.c index 2e09932f3a..e44a4c0e39 100644 --- a/shared-module/_pew/__init__.c +++ b/shared-module/_pew/__init__.c @@ -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]); From 48f0a5163e6a3a5e44e6df231867b9bc90ce933b Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sat, 29 Sep 2018 20:51:32 +0200 Subject: [PATCH 08/20] Reset timer when releasing _pew --- shared-module/_pew/PewPew.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c index 27c2d1c997..cbe7164bcb 100644 --- a/shared-module/_pew/PewPew.c +++ b/shared-module/_pew/PewPew.c @@ -117,6 +117,7 @@ void pew_init() { } void pew_reset(void) { - MP_STATE_VM(pew_singleton) = NULL; + tc_insts[pewpew_tc_index]->COUNT16.CTRLA.bit.ENABLE = 0; pewpew_tc_index = 0xff; + MP_STATE_VM(pew_singleton) = NULL; } From 45fea86554d42d07360ec16574908bc96c5e37ec Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 14:59:21 +0100 Subject: [PATCH 09/20] Rebase on top of CircuitPython 4.x --- .../atmel-samd/boards/pewpew10/mpconfigboard.h | 2 -- .../atmel-samd/boards/pewpew10/mpconfigboard.mk | 9 +++++++++ ports/atmel-samd/boards/pewpew10/pins.c | 2 +- ports/atmel-samd/timer_handler.c | 12 ++++++++++-- ports/atmel-samd/timer_handler.h | 1 + py/circuitpy_defns.mk | 2 ++ py/circuitpy_mpconfig.h | 2 ++ py/circuitpy_mpconfig.mk | 5 +++++ shared-bindings/_pew/PewPew.c | 17 ++++++++--------- shared-module/_pew/PewPew.c | 12 ++++++++---- 10 files changed, 46 insertions(+), 18 deletions(-) diff --git a/ports/atmel-samd/boards/pewpew10/mpconfigboard.h b/ports/atmel-samd/boards/pewpew10/mpconfigboard.h index 3fd1cbce18..09618431b8 100644 --- a/ports/atmel-samd/boards/pewpew10/mpconfigboard.h +++ b/ports/atmel-samd/boards/pewpew10/mpconfigboard.h @@ -7,8 +7,6 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 -#include "internal_flash.h" - #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000) diff --git a/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk b/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk index cf3004a4da..eeb5632ef9 100644 --- a/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk @@ -11,3 +11,12 @@ CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 FROZEN_MPY_DIRS += $(TOP)/frozen/pewpew10 + +CIRCUITPY_PEW = 1 +CIRCUITPY_ANALOGIO = 1 +CIRCUITPY_MATH = 0 +CIRCUITPY_NEOPIXEL_WRITE = 1 +CIRCUITPY_RTC = 0 +CIRCUITPY_SAMD = 0 +CIRCUITPY_USB_MIDI = 0 +CIRCUITPY_SMALL_BUILD = 1 diff --git a/ports/atmel-samd/boards/pewpew10/pins.c b/ports/atmel-samd/boards/pewpew10/pins.c index 499e6270b8..082394d17a 100644 --- a/ports/atmel-samd/boards/pewpew10/pins.c +++ b/ports/atmel-samd/boards/pewpew10/pins.c @@ -1,6 +1,6 @@ #include "shared-bindings/board/__init__.h" -#include "board_busses.h" +#include "supervisor/shared/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) }, diff --git a/ports/atmel-samd/timer_handler.c b/ports/atmel-samd/timer_handler.c index 26f984d964..ac162329ed 100644 --- a/ports/atmel-samd/timer_handler.c +++ b/ports/atmel-samd/timer_handler.c @@ -30,6 +30,7 @@ #include "timer_handler.h" #include "common-hal/pulseio/PulseOut.h" +#include "shared-module/_pew/PewPew.h" static uint8_t tc_handler[TC_INST_NUM]; @@ -44,8 +45,15 @@ void shared_timer_handler(bool is_tc, uint8_t index) { // Make sure to add the handler #define to timer_handler.h if (is_tc) { uint8_t handler = tc_handler[index]; - if (handler == TC_HANDLER_PULSEOUT) { - pulseout_interrupt_handler(index); + switch(handler) { + case TC_HANDLER_PULSEOUT: + pulseout_interrupt_handler(index); + break; + case TC_HANDLER_PEW: + pewpew_interrupt_handler(index); + break; + default: + break; } } } diff --git a/ports/atmel-samd/timer_handler.h b/ports/atmel-samd/timer_handler.h index f7a6e6e0ed..e249fca363 100644 --- a/ports/atmel-samd/timer_handler.h +++ b/ports/atmel-samd/timer_handler.h @@ -28,6 +28,7 @@ #define TC_HANDLER_NO_INTERRUPT 0x0 #define TC_HANDLER_PULSEOUT 0x1 +#define TC_HANDLER_PEW 0x2 void set_timer_handler(bool is_tc, uint8_t index, uint8_t timer_handler); void shared_timer_handler(bool is_tc, uint8_t index); diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 080263487e..c10edfe4e5 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -321,6 +321,8 @@ $(filter $(SRC_PATTERNS), \ terminalio/__init__.c \ uheap/__init__.c \ ustack/__init__.c \ + _pew/__init__.c \ + _pew/PewPew.c \ ) ifeq ($(INTERNAL_LIBM),1) diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index c20c0fb662..25aabc292a 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -543,6 +543,7 @@ extern const struct _mp_obj_module_t pew_module; USB_HID_MODULE \ USB_MIDI_MODULE \ USTACK_MODULE \ + PEW_MODULE \ // If weak links are enabled, just include strong links in the main list of modules, // and also include the underscore alternate names. @@ -569,6 +570,7 @@ extern const struct _mp_obj_module_t pew_module; vstr_t *repl_line; \ mp_obj_t rtc_time_source; \ mp_obj_t gamepad_singleton; \ + mp_obj_t pew_singleton; \ mp_obj_t terminal_tilegrid_tiles; \ FLASH_ROOT_POINTERS \ NETWORK_ROOT_POINTERS \ diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index a4a59170f0..54a38dfeec 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -213,6 +213,11 @@ CIRCUITPY_USB_MIDI = 1 endif CFLAGS += -DCIRCUITPY_USB_MIDI=$(CIRCUITPY_USB_MIDI) +ifndef CIRCUITPY_PEW +CIRCUITPY_PEW = 0 +endif +CFLAGS += -DCIRCUITPY_PEW=$(CIRCUITPY_PEW) + # For debugging. ifndef CIRCUITPY_USTACK CIRCUITPY_USTACK = 0 diff --git a/shared-bindings/_pew/PewPew.c b/shared-bindings/_pew/PewPew.c index 69c9ff4fc9..19eb00736c 100644 --- a/shared-bindings/_pew/PewPew.c +++ b/shared-bindings/_pew/PewPew.c @@ -32,6 +32,7 @@ #include "shared-bindings/util.h" #include "PewPew.h" #include "shared-module/_pew/PewPew.h" +#include "supervisor/shared/translate.h" //| .. currentmodule:: _pew @@ -49,10 +50,8 @@ //| //| 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, 4, 4, true); - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); + const mp_obj_t *pos_args, mp_map_t *kw_args) { + mp_arg_check_num(n_args, kw_args, 4, 4, true); 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 }, @@ -61,7 +60,7 @@ STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args, { 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), + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_buffer_info_t bufinfo; @@ -76,12 +75,12 @@ STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args, mp_obj_get_array(args[ARG_cols].u_obj, &cols_size, &cols); if (bufinfo.len != rows_size * cols_size) { - mp_raise_TypeError("wrong buffer size"); + mp_raise_TypeError(translate("")); } for (size_t i = 0; i < rows_size; ++i) { if (!MP_OBJ_IS_TYPE(rows[i], &digitalio_digitalinout_type)) { - mp_raise_TypeError("expected a DigitalInOut"); + mp_raise_TypeError(translate("")); } digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(rows[i]); raise_error_if_deinited( @@ -90,7 +89,7 @@ STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args, for (size_t i = 0; i < cols_size; ++i) { if (!MP_OBJ_IS_TYPE(cols[i], &digitalio_digitalinout_type)) { - mp_raise_TypeError("expected a DigitalInOut"); + mp_raise_TypeError(translate("")); } digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(cols[i]); raise_error_if_deinited( @@ -99,7 +98,7 @@ STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args, if (!MP_OBJ_IS_TYPE(args[ARG_buttons].u_obj, &digitalio_digitalinout_type)) { - mp_raise_TypeError("expected a DigitalInOut"); + mp_raise_TypeError(translate("")); } digitalio_digitalinout_obj_t *buttons = MP_OBJ_TO_PTR( args[ARG_buttons].u_obj); diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c index cbe7164bcb..2900f2ac7a 100644 --- a/shared-module/_pew/PewPew.c +++ b/shared-module/_pew/PewPew.c @@ -35,6 +35,8 @@ #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/util.h" #include "samd/timers.h" +#include "supervisor/shared/translate.h" +#include "timer_handler.h" static uint8_t pewpew_tc_index = 0xff; @@ -77,10 +79,11 @@ void pew_init() { } } if (tc == NULL) { - mp_raise_RuntimeError("All timers in use"); + mp_raise_RuntimeError(translate("")); } pewpew_tc_index = index; + set_timer_handler(true, index, TC_HANDLER_PEW); // We use GCLK0 for SAMD21 and GCLK1 for SAMD51 because they both run // at 48mhz making our math the same across the boards. @@ -106,7 +109,6 @@ void pew_init() { #endif tc_set_enable(tc, true); - //tc->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_STOP; tc->COUNT16.CC[0].reg = 160; // Clear our interrupt in case it was set earlier @@ -117,7 +119,9 @@ void pew_init() { } void pew_reset(void) { - tc_insts[pewpew_tc_index]->COUNT16.CTRLA.bit.ENABLE = 0; - pewpew_tc_index = 0xff; + if (pewpew_tc_index != 0xff) { + tc_reset(tc_insts[pewpew_tc_index]); + pewpew_tc_index = 0xff; + } MP_STATE_VM(pew_singleton) = NULL; } From 18870255ea4c18eddcbd91ffd36517c728e34907 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 15:06:24 +0100 Subject: [PATCH 10/20] Remove the pewpew70 board definition, leave only pewpew10 --- ports/atmel-samd/boards/pewpew70/board.c | 38 ------------------- .../boards/pewpew70/mpconfigboard.h | 12 ------ .../boards/pewpew70/mpconfigboard.mk | 13 ------- ports/atmel-samd/boards/pewpew70/pins.c | 31 --------------- 4 files changed, 94 deletions(-) delete mode 100644 ports/atmel-samd/boards/pewpew70/board.c delete mode 100644 ports/atmel-samd/boards/pewpew70/mpconfigboard.h delete mode 100644 ports/atmel-samd/boards/pewpew70/mpconfigboard.mk delete mode 100644 ports/atmel-samd/boards/pewpew70/pins.c diff --git a/ports/atmel-samd/boards/pewpew70/board.c b/ports/atmel-samd/boards/pewpew70/board.c deleted file mode 100644 index c8e20206a1..0000000000 --- a/ports/atmel-samd/boards/pewpew70/board.c +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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) { -} diff --git a/ports/atmel-samd/boards/pewpew70/mpconfigboard.h b/ports/atmel-samd/boards/pewpew70/mpconfigboard.h deleted file mode 100644 index e839bc5ea9..0000000000 --- a/ports/atmel-samd/boards/pewpew70/mpconfigboard.h +++ /dev/null @@ -1,12 +0,0 @@ -#define MICROPY_HW_BOARD_NAME "PewPew 7.0" -#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) diff --git a/ports/atmel-samd/boards/pewpew70/mpconfigboard.mk b/ports/atmel-samd/boards/pewpew70/mpconfigboard.mk deleted file mode 100644 index f316bfb2ee..0000000000 --- a/ports/atmel-samd/boards/pewpew70/mpconfigboard.mk +++ /dev/null @@ -1,13 +0,0 @@ -LD_FILE = boards/samd21x18-bootloader.ld -USB_VID = 0x239A -USB_PID = 0x801D -USB_PRODUCT = "PewPew 7.0" -USB_MANUFACTURER = "Radomir Dopieralski" - -INTERNAL_FLASH_FILESYSTEM = 1 -LONGINT_IMPL = NONE - -CHIP_VARIANT = SAMD21E18A -CHIP_FAMILY = samd21 - -FROZEN_MPY_DIRS += $(TOP)/frozen/pewpew70 diff --git a/ports/atmel-samd/boards/pewpew70/pins.c b/ports/atmel-samd/boards/pewpew70/pins.c deleted file mode 100644 index 9fca3c4ad9..0000000000 --- a/ports/atmel-samd/boards/pewpew70/pins.c +++ /dev/null @@ -1,31 +0,0 @@ -#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_PA08) }, - { MP_ROM_QSTR(MP_QSTR_R2), MP_ROM_PTR(&pin_PA15) }, - { MP_ROM_QSTR(MP_QSTR_R3), MP_ROM_PTR(&pin_PA01) }, - { MP_ROM_QSTR(MP_QSTR_R4), MP_ROM_PTR(&pin_PA11) }, - { MP_ROM_QSTR(MP_QSTR_R5), MP_ROM_PTR(&pin_PA27) }, - { MP_ROM_QSTR(MP_QSTR_R6), MP_ROM_PTR(&pin_PA00) }, - { MP_ROM_QSTR(MP_QSTR_R7), MP_ROM_PTR(&pin_PA28) }, - { MP_ROM_QSTR(MP_QSTR_R8), MP_ROM_PTR(&pin_PA19) }, - - { MP_ROM_QSTR(MP_QSTR_C1), MP_ROM_PTR(&pin_PA14) }, - { MP_ROM_QSTR(MP_QSTR_C2), MP_ROM_PTR(&pin_PA23) }, - { MP_ROM_QSTR(MP_QSTR_C3), MP_ROM_PTR(&pin_PA22) }, - { MP_ROM_QSTR(MP_QSTR_C4), MP_ROM_PTR(&pin_PA09) }, - { MP_ROM_QSTR(MP_QSTR_C5), MP_ROM_PTR(&pin_PA18) }, - { MP_ROM_QSTR(MP_QSTR_C6), MP_ROM_PTR(&pin_PA10) }, - { MP_ROM_QSTR(MP_QSTR_C7), MP_ROM_PTR(&pin_PA16) }, - { MP_ROM_QSTR(MP_QSTR_C8), MP_ROM_PTR(&pin_PA17) }, - - { MP_ROM_QSTR(MP_QSTR_UP), MP_ROM_PTR(&pin_PA04) }, - { MP_ROM_QSTR(MP_QSTR_DOWN), MP_ROM_PTR(&pin_PA03) }, - { MP_ROM_QSTR(MP_QSTR_LEFT), MP_ROM_PTR(&pin_PA02) }, - { MP_ROM_QSTR(MP_QSTR_RIGHT), MP_ROM_PTR(&pin_PA05) }, - { MP_ROM_QSTR(MP_QSTR_O), MP_ROM_PTR(&pin_PA07) }, - { MP_ROM_QSTR(MP_QSTR_X), MP_ROM_PTR(&pin_PA06) }, -}; -MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From f5f493222067a0be2123de1c6cea90ac4f06aef6 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 15:09:00 +0100 Subject: [PATCH 11/20] Clean unused include from tick.c --- ports/atmel-samd/tick.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/atmel-samd/tick.c b/ports/atmel-samd/tick.c index 44b631838e..1493477933 100644 --- a/ports/atmel-samd/tick.c +++ b/ports/atmel-samd/tick.c @@ -30,7 +30,6 @@ #include "supervisor/shared/autoreload.h" #include "shared-module/gamepad/__init__.h" -#include "shared-module/_pew/__init__.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Processor.h" From 3826ca41941922d820c1c2a24c3a335876254619 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 15:10:43 +0100 Subject: [PATCH 12/20] Rename internal pins to add underscore in front --- frozen/pewpew10/pew.py | 34 ++++++++++++------------- ports/atmel-samd/boards/pewpew10/pins.c | 34 ++++++++++++------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/frozen/pewpew10/pew.py b/frozen/pewpew10/pew.py index 8d60d0ec6d..99819cd5ae 100644 --- a/frozen/pewpew10/pew.py +++ b/frozen/pewpew10/pew.py @@ -192,26 +192,26 @@ def init(): _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), + 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), + 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) + _buttons = digitalio.DigitalInOut(board._BUTTONS) _pew.PewPew(_screen.buffer, _rows, _cols, _buttons) keys = _pew.get_pressed diff --git a/ports/atmel-samd/boards/pewpew10/pins.c b/ports/atmel-samd/boards/pewpew10/pins.c index 082394d17a..4a8225ad9f 100644 --- a/ports/atmel-samd/boards/pewpew10/pins.c +++ b/ports/atmel-samd/boards/pewpew10/pins.c @@ -3,23 +3,23 @@ #include "supervisor/shared/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__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__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) }, @@ -29,7 +29,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { 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_ROM_QSTR(MP_QSTR__BUTTONS), MP_ROM_PTR(&pin_PA08) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); From 8e50aeb06f24a264b474d62b52c7f59220db7195 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 15:54:16 +0100 Subject: [PATCH 13/20] Fix pew.tick() to not accumulate error over time --- frozen/pewpew10/pew.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frozen/pewpew10/pew.py b/frozen/pewpew10/pew.py index 99819cd5ae..333b193d5b 100644 --- a/frozen/pewpew10/pew.py +++ b/frozen/pewpew10/pew.py @@ -63,8 +63,12 @@ def show(pix): def tick(delay): global _tick + now = time.monotonic() _tick += delay - time.sleep(max(0, _tick - time.monotonic())) + if _tick < now: + _tick = now + else: + time.sleep(_tick - now) class GameOver(Exception): From 89b2788d119c811327b366e061481fbcc96cbc65 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 16:05:15 +0100 Subject: [PATCH 14/20] Apply review fixes: * fix formatting * fix copyrights * fix CIRCUITPYTHON_GAMEPAD guards * add CIRCUITPYTHON_PEW guards to reset * fix module list order --- ports/atmel-samd/boards/pewpew10/board.c | 3 +-- ports/atmel-samd/supervisor/port.c | 6 ++++-- py/circuitpy_mpconfig.h | 2 +- shared-bindings/_pew/PewPew.c | 2 +- shared-bindings/_pew/PewPew.h | 2 +- shared-bindings/_pew/__init__.c | 2 +- shared-module/_pew/PewPew.c | 2 +- shared-module/_pew/PewPew.h | 2 +- shared-module/_pew/__init__.c | 2 +- shared-module/_pew/__init__.h | 2 +- 10 files changed, 13 insertions(+), 12 deletions(-) diff --git a/ports/atmel-samd/boards/pewpew10/board.c b/ports/atmel-samd/boards/pewpew10/board.c index c8e20206a1..d7e856d611 100644 --- a/ports/atmel-samd/boards/pewpew10/board.c +++ b/ports/atmel-samd/boards/pewpew10/board.c @@ -26,8 +26,7 @@ #include "boards/board.h" -void board_init(void) -{ +void board_init(void) { } bool board_requests_safe_mode(void) { diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index 4aa07a6dbc..b3f4a2e89a 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -68,7 +68,7 @@ #include "tusb.h" -#ifdef CIRCUITPY_GAMEPAD_TICKS +#if CIRCUITPY_GAMEPAD #include "shared-module/gamepad/__init__.h" #endif #include "shared-module/_pew/PewPew.h" @@ -223,10 +223,12 @@ void reset_port(void) { reset_gclks(); -#ifdef CIRCUITPY_GAMEPAD_TICKS +#if CIRCUITPY_GAMEPAD gamepad_reset(); #endif +#ifdef CIRCUITPY_PEW pew_reset(); +#endif reset_event_system(); diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 25aabc292a..520f7c6125 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -528,6 +528,7 @@ extern const struct _mp_obj_module_t pew_module; NETWORK_MODULE \ SOCKET_MODULE \ WIZNET_MODULE \ + PEW_MODULE \ PIXELBUF_MODULE \ PULSEIO_MODULE \ RANDOM_MODULE \ @@ -543,7 +544,6 @@ extern const struct _mp_obj_module_t pew_module; USB_HID_MODULE \ USB_MIDI_MODULE \ USTACK_MODULE \ - PEW_MODULE \ // If weak links are enabled, just include strong links in the main list of modules, // and also include the underscore alternate names. diff --git a/shared-bindings/_pew/PewPew.c b/shared-bindings/_pew/PewPew.c index 19eb00736c..d0803e0a9d 100644 --- a/shared-bindings/_pew/PewPew.c +++ b/shared-bindings/_pew/PewPew.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * Copyright (c) 2019 Radomir Dopieralski * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/shared-bindings/_pew/PewPew.h b/shared-bindings/_pew/PewPew.h index 13633ef934..f763847577 100644 --- a/shared-bindings/_pew/PewPew.h +++ b/shared-bindings/_pew/PewPew.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * Copyright (c) 2019 Radomir Dopieralski * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/shared-bindings/_pew/__init__.c b/shared-bindings/_pew/__init__.c index 652d95f1b5..6c5520ac58 100644 --- a/shared-bindings/_pew/__init__.c +++ b/shared-bindings/_pew/__init__.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * Copyright (c) 2019 Radomir Dopieralski * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c index 2900f2ac7a..3fea689a39 100644 --- a/shared-module/_pew/PewPew.c +++ b/shared-module/_pew/PewPew.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * Copyright (c) 2019 Radomir Dopieralski * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/shared-module/_pew/PewPew.h b/shared-module/_pew/PewPew.h index 57b25c3a0c..da1cae0a2c 100644 --- a/shared-module/_pew/PewPew.h +++ b/shared-module/_pew/PewPew.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * Copyright (c) 2019 Radomir Dopieralski * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/shared-module/_pew/__init__.c b/shared-module/_pew/__init__.c index e44a4c0e39..90fba39900 100644 --- a/shared-module/_pew/__init__.c +++ b/shared-module/_pew/__init__.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * Copyright (c) 2019 Radomir Dopieralski * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/shared-module/_pew/__init__.h b/shared-module/_pew/__init__.h index f85dec7491..4f953c6106 100644 --- a/shared-module/_pew/__init__.h +++ b/shared-module/_pew/__init__.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2019 Radomir Dopieralski * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal From a9074f7bd10c102925510229bfc43e550c989cb9 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 16:11:22 +0100 Subject: [PATCH 15/20] More style fixes --- shared-module/_pew/PewPew.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c index 3fea689a39..755b883945 100644 --- a/shared-module/_pew/PewPew.c +++ b/shared-module/_pew/PewPew.c @@ -43,9 +43,13 @@ static uint8_t pewpew_tc_index = 0xff; void pewpew_interrupt_handler(uint8_t index) { - if (index != pewpew_tc_index) return; + if (index != pewpew_tc_index) { + return; + } Tc* tc = tc_insts[index]; - if (!tc->COUNT16.INTFLAG.bit.MC0) return; + if (!tc->COUNT16.INTFLAG.bit.MC0) { + return; + } pew_tick(); From 5d85d5402613d2ec376dde1a7f3b9c77f54bd1e4 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 16:24:22 +0100 Subject: [PATCH 16/20] Move the frozen pew.py into a submodule --- .gitmodules | 3 + frozen/pew-pewpew-standalone-10.x | 1 + frozen/pewpew10/pew.py | 221 ------------------ .../boards/pewpew10/mpconfigboard.mk | 2 +- shared-bindings/_pew/PewPew.c | 2 +- 5 files changed, 6 insertions(+), 223 deletions(-) create mode 160000 frozen/pew-pewpew-standalone-10.x delete mode 100644 frozen/pewpew10/pew.py diff --git a/.gitmodules b/.gitmodules index b36ad6203b..1f7a28d142 100644 --- a/.gitmodules +++ b/.gitmodules @@ -92,3 +92,6 @@ [submodule "tools/Tecate-bitmap-fonts"] path = tools/Tecate-bitmap-fonts url = https://github.com/Tecate/bitmap-fonts.git +[submodule "frozen/pew-pewpew-standalone-10.x"] + path = frozen/pew-pewpew-standalone-10.x + url = https://github.com/pewpew-game/pew-pewpew-standalone-10.x.git diff --git a/frozen/pew-pewpew-standalone-10.x b/frozen/pew-pewpew-standalone-10.x new file mode 160000 index 0000000000..87755e0881 --- /dev/null +++ b/frozen/pew-pewpew-standalone-10.x @@ -0,0 +1 @@ +Subproject commit 87755e088150cc9bce42f4104cbd74d91b923c6f diff --git a/frozen/pewpew10/pew.py b/frozen/pewpew10/pew.py deleted file mode 100644 index 333b193d5b..0000000000 --- a/frozen/pewpew10/pew.py +++ /dev/null @@ -1,221 +0,0 @@ -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'\xd0\xfc\xf4\xfc\xd0\xff\xd0\xfc\xfc\xf4\xfc\xff\xd2\xfd\xfc\x8d\xd2' - b'\xff\xcc\xcc\xc4\xcc\xcc\xff\xd1\xf3\xf3\xf3\xd1\xff\xcb\xcf\xcf\xdc' - b'\xe2\xff\xdc\xcc\xd8\xf4\xc8\xff\xfc\xfc\xfc\xec\xc0\xff\xdd\xc4\xc0' - b'\xc8\xcc\xff\xcd\xd4\xd1\xc5\xdc\xff\xe2\xdd\xcc\xdd\xe2\xff\xe4\xcc' - b'\xcc\xe4\xfc\xff\xe2\xcc\xcc\xc8\xd2\xcf\xe4\xcc\xcc\xe0\xcc\xff\xd2' - b'\xec\xe2\xce\xe1\xff\xc0\xe2\xf3\xf3\xf3\xff\xcc\xcc\xcc\xdd\xe2\xff' - b'\xcc\xcc\xdd\xe6\xf3\xff\xcc\xc8\xc4\xc0\xd9\xff\xcc\xd9\xe2\xd9\xcc' - b'\xff\xcc\xdd\xe6\xf3\xf3\xff\xc0\xde\xf7\xed\xc0\xff\xd0\xfc\xfc\xfc' - b'\xd0\xff\xfc\xf9\xf3\xdb\xcf\xff\xc1\xcf\xcf\xcf\xc1\xff\xf3\xd9\xee' - b'\xff\xff\xff\xff\xff\xff\xff\x80\xff\xfc\xf7\xef\xff\xff\xff\xff\xd2' - b'\xcd\xcc\x86\xff\xfc\xe4\xdc\xcc\xe4\xff\xff\xd2\xfd\xbc\xc6\xff\xcf' - b'\xc6\xcd\xcc\x86\xff\xff\xd6\xcd\xb1\xd2\xff\xcb\xb7\xc1\xf3\xf3\xf6' - b'\xff\xe2\xcc\xd2\xdf\xe1\xfc\xe4\xdc\xcc\xcc\xff\xf3\xfb\xf1\xb3\xdb' - b'\xff\xcf\xef\xc7\xcf\xdd\xe2\xfd\xec\xd8\xf4\xcc\xff\xf6\xf3\xf3\xf3' - b'\xdb\xff\xff\xd9\xc4\xc8\xcc\xff\xff\xe4\xdd\xcc\xcc\xff\xff\xe2\xcc' - b'\xcc\xe2\xff\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 - - now = time.monotonic() - _tick += delay - if _tick < now: - _tick = now - else: - time.sleep(_tick - now) - - -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 diff --git a/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk b/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk index eeb5632ef9..0c20b4010e 100644 --- a/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk @@ -10,7 +10,7 @@ LONGINT_IMPL = NONE CHIP_VARIANT = SAMD21E18A CHIP_FAMILY = samd21 -FROZEN_MPY_DIRS += $(TOP)/frozen/pewpew10 +FROZEN_MPY_DIRS += $(TOP)/frozen/pew-pewpew-standalone-10.x CIRCUITPY_PEW = 1 CIRCUITPY_ANALOGIO = 1 diff --git a/shared-bindings/_pew/PewPew.c b/shared-bindings/_pew/PewPew.c index d0803e0a9d..26b0601886 100644 --- a/shared-bindings/_pew/PewPew.c +++ b/shared-bindings/_pew/PewPew.c @@ -75,7 +75,7 @@ STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args, mp_obj_get_array(args[ARG_cols].u_obj, &cols_size, &cols); if (bufinfo.len != rows_size * cols_size) { - mp_raise_TypeError(translate("")); + mp_raise_ValueError(translate("")); } for (size_t i = 0; i < rows_size; ++i) { From ea78417f7fe80cc870fd6bef24a4938bddfa4f37 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 16:36:29 +0100 Subject: [PATCH 17/20] Improve documentation --- shared-bindings/_pew/PewPew.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/shared-bindings/_pew/PewPew.c b/shared-bindings/_pew/PewPew.c index 26b0601886..50647488cf 100644 --- a/shared-bindings/_pew/PewPew.c +++ b/shared-bindings/_pew/PewPew.c @@ -37,17 +37,30 @@ //| .. currentmodule:: _pew //| -//| :class:`PewPew` -- LED Matrix driver -//| ==================================== +//| :class:`PewPew` -- LED matrix and button driver +//| =============================================== +//| +//| This is an internal module to be used by the ``pew.py`` library from +//| https://github.com/pewpew-game/pew-pewpew-standalone-10.x to handle the +//| LED matrix display and buttons on the ``pewpew10`` board. //| //| Usage:: //| +//| This singleton class is instantiated by the ``pew`` library, and +//| used internally by it. All user-visible interactions are done through +//| that library. //| -//| .. class:: PewPew(buffer, rows, cols) +//| .. class:: PewPew(buffer, rows, cols, buttons) //| //| Initializes matrix scanning routines. //| +//| The ``buffer`` is a 64 byte long ``bytearray`` that stores what should +//| be displayed on the matrix. ``rows`` and ``cols`` are both lists of +//| eight ``DigitalInputOutput`` objects that are connected to the matrix +//| rows and columns. ``buttons`` is a ``DigitalInputOutput`` object that +//| is connected to the common side of all buttons (the other sides of the +//| buttons are connected to rows of the matrix). //| STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { From 7afbfc70038201fdea5fa2601be4d3235ffca7d6 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 16:46:59 +0100 Subject: [PATCH 18/20] Use find_free_timer() --- shared-module/_pew/PewPew.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c index 755b883945..c1d6aa5c35 100644 --- a/shared-module/_pew/PewPew.c +++ b/shared-module/_pew/PewPew.c @@ -74,17 +74,11 @@ void pew_init() { } if (pewpew_tc_index == 0xff) { // Find a spare timer. - Tc *tc = NULL; - int8_t index = TC_INST_NUM - 1; - for (; index >= 0; index--) { - if (tc_insts[index]->COUNT16.CTRLA.bit.ENABLE == 0) { - tc = tc_insts[index]; - break; - } - } - if (tc == NULL) { + uint8_t index = find_free_timer(); + if (index == 0xff) { mp_raise_RuntimeError(translate("")); } + Tc *tc = tc_insts[index]; pewpew_tc_index = index; set_timer_handler(true, index, TC_HANDLER_PEW); From 2bfafd1fc953a874e86aba0e55e89174449ab3ca Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 16:55:54 +0100 Subject: [PATCH 19/20] Enable math for pewpew10 --- ports/atmel-samd/boards/pewpew10/mpconfigboard.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk b/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk index 0c20b4010e..983e48e21c 100644 --- a/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk @@ -14,7 +14,7 @@ FROZEN_MPY_DIRS += $(TOP)/frozen/pew-pewpew-standalone-10.x CIRCUITPY_PEW = 1 CIRCUITPY_ANALOGIO = 1 -CIRCUITPY_MATH = 0 +CIRCUITPY_MATH = 1 CIRCUITPY_NEOPIXEL_WRITE = 1 CIRCUITPY_RTC = 0 CIRCUITPY_SAMD = 0 From 263134dcd3e1d0151be081a2ba04b358f732035e Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 1 Mar 2019 18:50:00 +0100 Subject: [PATCH 20/20] Add more guards for CIRCUITPYTHON_PEW --- ports/atmel-samd/supervisor/port.c | 2 +- ports/atmel-samd/timer_handler.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index b3f4a2e89a..8d25806b7e 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -226,7 +226,7 @@ void reset_port(void) { #if CIRCUITPY_GAMEPAD gamepad_reset(); #endif -#ifdef CIRCUITPY_PEW +#if CIRCUITPY_PEW pew_reset(); #endif diff --git a/ports/atmel-samd/timer_handler.c b/ports/atmel-samd/timer_handler.c index ac162329ed..059d4330ed 100644 --- a/ports/atmel-samd/timer_handler.c +++ b/ports/atmel-samd/timer_handler.c @@ -50,7 +50,9 @@ void shared_timer_handler(bool is_tc, uint8_t index) { pulseout_interrupt_handler(index); break; case TC_HANDLER_PEW: +#if CIRCUITPY_PEW pewpew_interrupt_handler(index); +#endif break; default: break;