circuitpython/shared-bindings/_pew/PewPew.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

139 lines
5.2 KiB
C
Raw Normal View History

2018-08-01 09:29:26 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Radomir Dopieralski
2018-08-01 09:29:26 -04:00
*
* 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 "common-hal/_pew/PewPew.h"
2022-05-27 15:59:54 -04:00
#include "supervisor/shared/translate/translate.h"
2018-08-01 09:29:26 -04:00
2020-05-08 16:03:39 -04:00
//| class PewPew:
//| """This is an internal module to be used by the ``pew.py`` library from
2020-05-08 16:03:39 -04:00
//| https://github.com/pewpew-game/pew-pewpew-standalone-10.x to handle the
//| LED matrix display and buttons on the ``pewpew10`` board.
2018-08-01 09:29:26 -04:00
//|
2020-05-08 16:03:39 -04:00
//| Usage::
2018-08-01 09:29:26 -04:00
//|
2020-05-08 16:03:39 -04:00
//| This singleton class is instantiated by the ``pew`` library, and
//| used internally by it. All user-visible interactions are done through
//| that library."""
2018-08-01 09:29:26 -04:00
//|
2020-07-23 14:22:41 -04:00
//| def __init__(
//| self,
//| buffer: ReadableBuffer,
2020-08-03 00:35:43 -04:00
//| rows: List[digitalio.DigitalInOut],
//| cols: List[digitalio.DigitalInOut],
2020-07-23 14:22:41 -04:00
//| buttons: digitalio.DigitalInOut,
//| ) -> None:
2020-05-08 16:03:39 -04:00
//| """Initializes matrix scanning routines.
2018-08-01 09:29:26 -04:00
//|
2020-05-08 16:03:39 -04:00
//| 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, size_t n_kw, const mp_obj_t *all_args) {
2018-08-23 17:22:47 -04:00
enum { ARG_buffer, ARG_rows, ARG_cols, ARG_buttons };
2018-08-01 09:29:26 -04:00
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 },
2018-08-23 17:22:47 -04:00
{ MP_QSTR_buttons, MP_ARG_OBJ | MP_ARG_REQUIRED },
2018-08-01 09:29:26 -04:00
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args),
2021-03-15 09:57:36 -04:00
allowed_args, args);
2018-08-01 09:29:26 -04:00
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("Incorrect buffer size"));
2018-08-01 09:29:26 -04:00
}
for (size_t i = 0; i < rows_size; ++i) {
digitalio_digitalinout_obj_t *pin = mp_arg_validate_type(rows[i], &digitalio_digitalinout_type, MP_QSTR_rows);
if (common_hal_digitalio_digitalinout_deinited(pin)) {
raise_deinited_error();
}
2018-08-01 09:29:26 -04:00
}
for (size_t i = 0; i < cols_size; ++i) {
digitalio_digitalinout_obj_t *pin = mp_arg_validate_type(cols[i], &digitalio_digitalinout_type, MP_QSTR_cols);
if (common_hal_digitalio_digitalinout_deinited(pin)) {
raise_deinited_error();
}
2018-08-01 09:29:26 -04:00
}
digitalio_digitalinout_obj_t *buttons = mp_arg_validate_type(args[ARG_buttons].u_obj, &digitalio_digitalinout_type, MP_QSTR_buttons);
if (common_hal_digitalio_digitalinout_deinited(buttons)) {
raise_deinited_error();
}
2018-08-01 09:29:26 -04:00
pew_obj_t *pew = MP_STATE_VM(pew_singleton);
if (!pew) {
pew = mp_obj_malloc(pew_obj_t, &pewpew_type);
2018-08-01 09:29:26 -04:00
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;
2018-08-23 17:22:47 -04:00
pew->buttons = buttons;
pew->pressed = 0;
2018-08-01 09:29:26 -04:00
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,
2021-03-15 09:57:36 -04:00
.locals_dict = (mp_obj_dict_t *)&pewpew_locals_dict,
2018-08-01 09:29:26 -04:00
};