Add a _pew module

This commit is contained in:
Radomir Dopieralski 2018-08-01 15:29:26 +02:00
parent 7ac11ed8e1
commit 88e40193ae
11 changed files with 443 additions and 0 deletions

View File

@ -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();

View File

@ -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() {

View File

@ -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 = \

View File

@ -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) },

View File

@ -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,
};

View File

@ -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

View File

@ -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,
};

View File

@ -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 <stdbool.h>
#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);
}
}

View File

@ -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 <stdint.h>
#include "shared-bindings/digitalio/DigitalInOut.h"
typedef struct {
mp_obj_base_t base;
uint8_t* buffer;
mp_obj_t* rows;
mp_obj_t* cols;
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

View File

@ -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 <stdbool.h>
#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;
}

View File

@ -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