Skip to content

Commit 88e4019

Browse files
committed
Add a _pew module
1 parent 7ac11ed commit 88e4019

11 files changed

Lines changed: 443 additions & 0 deletions

File tree

ports/atmel-samd/supervisor/port.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
#ifdef CIRCUITPY_GAMEPAD_TICKS
7272
#include "shared-module/gamepad/__init__.h"
7373
#endif
74+
#ifdef CIRCUITPY_PEWPEW_TICKS
75+
#include "shared-module/_pew/__init__.h"
76+
#endif
7477

7578
extern volatile bool mp_msc_enabled;
7679

@@ -225,6 +228,9 @@ void reset_port(void) {
225228
#ifdef CIRCUITPY_GAMEPAD_TICKS
226229
gamepad_reset();
227230
#endif
231+
#ifdef CIRCUITPY_PEWPEW_TICKS
232+
pew_reset();
233+
#endif
228234

229235
reset_event_system();
230236

ports/atmel-samd/tick.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "supervisor/shared/autoreload.h"
3232
#include "shared-module/gamepad/__init__.h"
33+
#include "shared-module/_pew/__init__.h"
3334
#include "shared-bindings/microcontroller/__init__.h"
3435
#include "shared-bindings/microcontroller/Processor.h"
3536

@@ -54,6 +55,11 @@ void SysTick_Handler(void) {
5455
gamepad_tick();
5556
}
5657
#endif
58+
#ifdef CIRCUITPY_PEWPEW_TICKS
59+
if (!(ticks_ms & CIRCUITPY_PEWPEW_TICKS)) {
60+
pew_tick();
61+
}
62+
#endif
5763
}
5864

5965
void tick_init() {

py/circuitpy_defns.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ endif
198198
ifeq ($(CIRCUITPY_USTACK),1)
199199
SRC_PATTERNS += ustack/%
200200
endif
201+
ifeq ($(CIRCUITPY_PEW),1)
202+
SRC_PATTERNS += _pew/%
203+
endif
201204

202205
# All possible sources are listed here, and are filtered by SRC_PATTERNS.
203206
SRC_COMMON_HAL = \

py/circuitpy_mpconfig.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ extern const struct _mp_obj_module_t ustack_module;
463463
#define USTACK_MODULE
464464
#endif
465465

466+
#if CIRCUITPY_PEW
467+
extern const struct _mp_obj_module_t pew_module;
468+
#define PEW_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__pew),(mp_obj_t)&pew_module },
469+
#else
470+
#define PEW_MODULE
471+
#endif
472+
466473
// These modules are not yet in shared-bindings, but we prefer the non-uxxx names.
467474
#if MICROPY_PY_UERRNO
468475
#define ERRNO_MODULE { MP_ROM_QSTR(MP_QSTR_errno), MP_ROM_PTR(&mp_module_uerrno) },

shared-bindings/_pew/PewPew.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#include "py/obj.h"
27+
#include "py/runtime.h"
28+
#include "py/mphal.h"
29+
#include "py/gc.h"
30+
#include "py/mpstate.h"
31+
#include "shared-bindings/digitalio/DigitalInOut.h"
32+
#include "shared-bindings/util.h"
33+
#include "PewPew.h"
34+
#include "shared-module/_pew/PewPew.h"
35+
36+
37+
//| .. currentmodule:: _pew
38+
//|
39+
//| :class:`PewPew` -- LED Matrix driver
40+
//| ====================================
41+
//|
42+
//| Usage::
43+
//|
44+
//|
45+
46+
//| .. class:: PewPew(buffer, rows, cols)
47+
//|
48+
//| Initializes matrix scanning routines.
49+
//|
50+
//|
51+
STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args,
52+
size_t n_kw, const mp_obj_t *pos_args) {
53+
mp_arg_check_num(n_args, n_kw, 3, 3, true);
54+
mp_map_t kw_args;
55+
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
56+
enum { ARG_buffer, ARG_rows, ARG_cols };
57+
static const mp_arg_t allowed_args[] = {
58+
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED },
59+
{ MP_QSTR_rows, MP_ARG_OBJ | MP_ARG_REQUIRED },
60+
{ MP_QSTR_cols, MP_ARG_OBJ | MP_ARG_REQUIRED },
61+
};
62+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
63+
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args),
64+
allowed_args, args);
65+
66+
mp_buffer_info_t bufinfo;
67+
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
68+
69+
size_t rows_size = 0;
70+
mp_obj_t *rows;
71+
mp_obj_get_array(args[ARG_rows].u_obj, &rows_size, &rows);
72+
73+
size_t cols_size = 0;
74+
mp_obj_t *cols;
75+
mp_obj_get_array(args[ARG_cols].u_obj, &cols_size, &cols);
76+
77+
if (bufinfo.len != rows_size * cols_size) {
78+
mp_raise_TypeError("wrong buffer size");
79+
}
80+
81+
for (size_t i = 0; i < rows_size; ++i) {
82+
if (!MP_OBJ_IS_TYPE(rows[i], &digitalio_digitalinout_type)) {
83+
mp_raise_TypeError("expected a DigitalInOut");
84+
}
85+
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(rows[i]);
86+
raise_error_if_deinited(
87+
common_hal_digitalio_digitalinout_deinited(pin));
88+
}
89+
90+
for (size_t i = 0; i < cols_size; ++i) {
91+
if (!MP_OBJ_IS_TYPE(cols[i], &digitalio_digitalinout_type)) {
92+
mp_raise_TypeError("expected a DigitalInOut");
93+
}
94+
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(cols[i]);
95+
raise_error_if_deinited(
96+
common_hal_digitalio_digitalinout_deinited(pin));
97+
}
98+
99+
100+
pew_obj_t *pew = MP_STATE_VM(pew_singleton);
101+
if (!pew) {
102+
pew = m_new_obj(pew_obj_t);
103+
pew->base.type = &pewpew_type;
104+
pew = gc_make_long_lived(pew);
105+
MP_STATE_VM(pew_singleton) = pew;
106+
}
107+
108+
pew->buffer = bufinfo.buf;
109+
pew->rows = rows;
110+
pew->rows_size = rows_size;
111+
pew->cols = cols;
112+
pew->cols_size = cols_size;
113+
pew->col = 0;
114+
pew->turn = 0;
115+
pew_init();
116+
117+
return MP_OBJ_FROM_PTR(pew);
118+
}
119+
120+
121+
STATIC const mp_rom_map_elem_t pewpew_locals_dict_table[] = {
122+
};
123+
STATIC MP_DEFINE_CONST_DICT(pewpew_locals_dict, pewpew_locals_dict_table);
124+
const mp_obj_type_t pewpew_type = {
125+
{ &mp_type_type },
126+
.name = MP_QSTR_PewPew,
127+
.make_new = pewpew_make_new,
128+
.locals_dict = (mp_obj_dict_t*)&pewpew_locals_dict,
129+
};
130+

shared-bindings/_pew/PewPew.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
28+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_PEW_PEWPEW_H
29+
#define MICROPY_INCLUDED_SHARED_BINDINGS_PEW_PEWPEW_H
30+
31+
extern const mp_obj_type_t pewpew_type;
32+
33+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_PEW_PEWPEW_H

shared-bindings/_pew/__init__.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#include "py/obj.h"
27+
#include "py/runtime.h"
28+
#include "py/mphal.h"
29+
#include "PewPew.h"
30+
31+
32+
//| :mod:`_pew` --- LED matrix driver
33+
//| ==================================
34+
//|
35+
//| .. module:: _pew
36+
//| :synopsis: LED matrix driver
37+
//| :platform: SAMD21
38+
//|
39+
//| .. toctree::
40+
//| :maxdepth: 3
41+
//|
42+
//| PewPew
43+
//|
44+
STATIC const mp_rom_map_elem_t pew_module_globals_table[] = {
45+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__pew) },
46+
{ MP_OBJ_NEW_QSTR(MP_QSTR_PewPew), MP_ROM_PTR(&pewpew_type)},
47+
};
48+
STATIC MP_DEFINE_CONST_DICT(pew_module_globals,
49+
pew_module_globals_table);
50+
51+
const mp_obj_module_t pew_module = {
52+
.base = { &mp_type_module },
53+
.globals = (mp_obj_dict_t*)&pew_module_globals,
54+
};

shared-module/_pew/PewPew.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdbool.h>
28+
29+
#include "py/mpstate.h"
30+
#include "__init__.h"
31+
#include "PewPew.h"
32+
33+
#include "shared-bindings/digitalio/Pull.h"
34+
#include "shared-bindings/digitalio/DigitalInOut.h"
35+
#include "shared-bindings/util.h"
36+
37+
38+
void pew_init() {
39+
pew_obj_t* pew_singleton = MP_STATE_VM(pew_singleton);
40+
for (size_t i = 0; i < pew_singleton->rows_size; ++i) {
41+
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(
42+
pew_singleton->rows[i]);
43+
common_hal_digitalio_digitalinout_switch_to_output(pin, false,
44+
DRIVE_MODE_PUSH_PULL);
45+
}
46+
for (size_t i = 0; i < pew_singleton->cols_size; ++i) {
47+
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(
48+
pew_singleton->cols[i]);
49+
common_hal_digitalio_digitalinout_switch_to_output(pin, true,
50+
DRIVE_MODE_OPEN_DRAIN);
51+
}
52+
}

shared-module/_pew/PewPew.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_PEW_PEWPEW_H
28+
#define MICROPY_INCLUDED_PEW_PEWPEW_H
29+
30+
#include <stdint.h>
31+
32+
#include "shared-bindings/digitalio/DigitalInOut.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
uint8_t* buffer;
37+
mp_obj_t* rows;
38+
mp_obj_t* cols;
39+
size_t rows_size;
40+
size_t cols_size;
41+
volatile uint8_t col;
42+
volatile uint8_t turn;
43+
} pew_obj_t;
44+
45+
void pew_init(void);
46+
47+
#endif // MICROPY_INCLUDED_PEW_PEWPEW_H

0 commit comments

Comments
 (0)