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/ports/atmel-samd/boards/pewpew10/board.c b/ports/atmel-samd/boards/pewpew10/board.c new file mode 100644 index 0000000000..d7e856d611 --- /dev/null +++ b/ports/atmel-samd/boards/pewpew10/board.c @@ -0,0 +1,37 @@ +/* + * 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..09618431b8 --- /dev/null +++ b/ports/atmel-samd/boards/pewpew10/mpconfigboard.h @@ -0,0 +1,34 @@ +#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 + +#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..983e48e21c --- /dev/null +++ b/ports/atmel-samd/boards/pewpew10/mpconfigboard.mk @@ -0,0 +1,22 @@ +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/pew-pewpew-standalone-10.x + +CIRCUITPY_PEW = 1 +CIRCUITPY_ANALOGIO = 1 +CIRCUITPY_MATH = 1 +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 new file mode 100644 index 0000000000..4a8225ad9f --- /dev/null +++ b/ports/atmel-samd/boards/pewpew10/pins.c @@ -0,0 +1,35 @@ +#include "shared-bindings/board/__init__.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) }, + { 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/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index aee082de01..8d25806b7e 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -68,9 +68,10 @@ #include "tusb.h" -#ifdef CIRCUITPY_GAMEPAD_TICKS +#if CIRCUITPY_GAMEPAD #include "shared-module/gamepad/__init__.h" #endif +#include "shared-module/_pew/PewPew.h" extern volatile bool mp_msc_enabled; @@ -222,9 +223,12 @@ void reset_port(void) { reset_gclks(); -#ifdef CIRCUITPY_GAMEPAD_TICKS +#if CIRCUITPY_GAMEPAD gamepad_reset(); #endif +#if CIRCUITPY_PEW + pew_reset(); +#endif reset_event_system(); diff --git a/ports/atmel-samd/timer_handler.c b/ports/atmel-samd/timer_handler.c index 26f984d964..059d4330ed 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,17 @@ 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: +#if CIRCUITPY_PEW + pewpew_interrupt_handler(index); +#endif + 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 2fa2e78e2a..c10edfe4e5 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 = \ @@ -318,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 6f97054854..520f7c6125 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) }, @@ -521,6 +528,7 @@ extern const struct _mp_obj_module_t ustack_module; NETWORK_MODULE \ SOCKET_MODULE \ WIZNET_MODULE \ + PEW_MODULE \ PIXELBUF_MODULE \ PULSEIO_MODULE \ RANDOM_MODULE \ @@ -562,6 +570,7 @@ extern const struct _mp_obj_module_t ustack_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 new file mode 100644 index 0000000000..50647488cf --- /dev/null +++ b/shared-bindings/_pew/PewPew.c @@ -0,0 +1,151 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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" +#include "supervisor/shared/translate.h" + + +//| .. currentmodule:: _pew +//| +//| :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, 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) { + 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 }, + { 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), + 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_ValueError(translate("")); + } + + for (size_t i = 0; i < rows_size; ++i) { + if (!MP_OBJ_IS_TYPE(rows[i], &digitalio_digitalinout_type)) { + mp_raise_TypeError(translate("")); + } + 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(translate("")); + } + digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(cols[i]); + raise_error_if_deinited( + common_hal_digitalio_digitalinout_deinited(pin)); + } + + if (!MP_OBJ_IS_TYPE(args[ARG_buttons].u_obj, + &digitalio_digitalinout_type)) { + mp_raise_TypeError(translate("")); + } + 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) { + 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->buttons = buttons; + pew->pressed = 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..f763847577 --- /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) 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 + * 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..6c5520ac58 --- /dev/null +++ b/shared-bindings/_pew/__init__.c @@ -0,0 +1,67 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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" +#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 +//| ================================== +//| +//| .. 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)}, + { 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); + +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..c1d6aa5c35 --- /dev/null +++ b/shared-module/_pew/PewPew.c @@ -0,0 +1,125 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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 "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" +#include "supervisor/shared/translate.h" +#include "timer_handler.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 = 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->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); + } + if (pewpew_tc_index == 0xff) { + // Find a spare timer. + 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); + + // 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.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) { + if (pewpew_tc_index != 0xff) { + tc_reset(tc_insts[pewpew_tc_index]); + pewpew_tc_index = 0xff; + } + MP_STATE_VM(pew_singleton) = NULL; +} diff --git a/shared-module/_pew/PewPew.h b/shared-module/_pew/PewPew.h new file mode 100644 index 0000000000..da1cae0a2c --- /dev/null +++ b/shared-module/_pew/PewPew.h @@ -0,0 +1,48 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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; + digitalio_digitalinout_obj_t *buttons; + uint8_t rows_size; + uint8_t cols_size; + uint8_t pressed; +} 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 new file mode 100644 index 0000000000..90fba39900 --- /dev/null +++ b/shared-module/_pew/__init__.c @@ -0,0 +1,85 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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) { + 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); + if (!pew) { return; } + + 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]); + uint8_t color = pew->buffer[col * (pew->rows_size) + x]; + bool value = false; + switch (color & 0x03) { + case 3: + value = true; + break; + case 2: + 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[col]); + common_hal_digitalio_digitalinout_set_value(pin, false); +} diff --git a/shared-module/_pew/__init__.h b/shared-module/_pew/__init__.h new file mode 100644 index 0000000000..4f953c6106 --- /dev/null +++ b/shared-module/_pew/__init__.h @@ -0,0 +1,32 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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); + +#endif // MICROPY_INCLUDED_PEW_H