From 281d73f47ff1800c3125282bdc30452db94b66de Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 29 Sep 2017 01:56:07 +0200 Subject: [PATCH 01/13] Initial proof of concept of the buttons module --- atmel-samd/Makefile | 1 + atmel-samd/bindings/samd/__init__.c | 15 +++++++++ atmel-samd/buttons.c | 49 +++++++++++++++++++++++++++++ atmel-samd/buttons.h | 46 +++++++++++++++++++++++++++ atmel-samd/tick.c | 5 +++ 5 files changed, 116 insertions(+) create mode 100644 atmel-samd/buttons.c create mode 100644 atmel-samd/buttons.h diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 1e5fe110d0f62..5d3d65178d1f9 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -192,6 +192,7 @@ SRC_ASF = $(addprefix asf/sam0/,\ SRC_C = \ access_vfs.c \ autoreload.c \ + buttons.c \ background.c \ fatfs_port.c \ flash_api.c \ diff --git a/atmel-samd/bindings/samd/__init__.c b/atmel-samd/bindings/samd/__init__.c index 1634d8daaedbb..37cf8b17dabf7 100644 --- a/atmel-samd/bindings/samd/__init__.c +++ b/atmel-samd/bindings/samd/__init__.c @@ -26,8 +26,21 @@ #include "py/obj.h" #include "py/runtime.h" #include "autoreload.h" + #include "buttons.h" #include "rgb_led_status.h" +STATIC mp_obj_t samd_get_buttons(void) { + return MP_OBJ_NEW_SMALL_INT(buttons_pressed); +} +MP_DEFINE_CONST_FUN_OBJ_0(samd_get_buttons_obj, samd_get_buttons); + +STATIC mp_obj_t samd_setup_buttons(size_t n_args, const mp_obj_t *args) { + buttons_setup(args[0], args[1], args[2], args[3], args[4], args[5]); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(samd_setup_buttons_obj, 6, 6, + samd_setup_buttons); + //| :mod:`samd` --- SAMD implementation settings //| ================================================= //| @@ -78,6 +91,8 @@ STATIC const mp_rom_map_elem_t samd_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&samd_enable_autoreload_obj)}, { MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&samd_disable_autoreload_obj)}, { MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&samd_set_rgb_status_brightness_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_get_buttons), MP_ROM_PTR(&samd_get_buttons_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_setup_buttons), MP_ROM_PTR(&samd_setup_buttons_obj)}, }; STATIC MP_DEFINE_CONST_DICT(samd_module_globals, samd_module_globals_table); diff --git a/atmel-samd/buttons.c b/atmel-samd/buttons.c new file mode 100644 index 0000000000000..f7d681c947021 --- /dev/null +++ b/atmel-samd/buttons.c @@ -0,0 +1,49 @@ + +#include + +#include "buttons.h" + +#include "shared-bindings/digitalio/Pull.h" +#include "shared-bindings/digitalio/DigitalInOut.h" + + +volatile uint8_t buttons_pressed; +static digitalio_digitalinout_obj_t* button_pins[6]; + + +void buttons_tick(void) { + static uint8_t buttons_last = 0; + + if (!button_pins[0]) { + return; // Not configured yet. + } + uint8_t buttons_current = 0; + uint8_t bit = 1; + for (int i=0; i<6; ++i) { + if (common_hal_digitalio_digitalinout_get_value(button_pins[i])) { + buttons_current |= bit; + } + bit <<= 1; + } + buttons_pressed = buttons_last & buttons_current; + buttons_last = buttons_current; +} + +void buttons_setup( + digitalio_digitalinout_obj_t* pin_up, + digitalio_digitalinout_obj_t* pin_down, + digitalio_digitalinout_obj_t* pin_left, + digitalio_digitalinout_obj_t* pin_right, + digitalio_digitalinout_obj_t* pin_o, + digitalio_digitalinout_obj_t* pin_x) { + button_pins[0] = pin_up; + button_pins[1] = pin_down; + button_pins[2] = pin_left; + button_pins[3] = pin_right; + button_pins[4] = pin_o; + button_pins[5] = pin_x; + for (int i=0; i<6; ++i) { + common_hal_digitalio_digitalinout_switch_to_input( + button_pins[i], PULL_UP); + } +} diff --git a/atmel-samd/buttons.h b/atmel-samd/buttons.h new file mode 100644 index 0000000000000..95cfdc752853a --- /dev/null +++ b/atmel-samd/buttons.h @@ -0,0 +1,46 @@ +/* + * 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_ATMEL_SAMD_BUTTONS_H +#define MICROPY_INCLUDED_ATMEL_SAMD_BUTTONS_H + +#include + +#include "shared-bindings/digitalio/DigitalInOut.h" + + +extern volatile uint8_t buttons_pressed; + +void buttons_tick(void); +void buttons_setup( + digitalio_digitalinout_obj_t* pin_up, + digitalio_digitalinout_obj_t* pin_down, + digitalio_digitalinout_obj_t* pin_left, + digitalio_digitalinout_obj_t* pin_right, + digitalio_digitalinout_obj_t* pin_o, + digitalio_digitalinout_obj_t* pin_x); + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_BUTTONS_H diff --git a/atmel-samd/tick.c b/atmel-samd/tick.c index cbe72abc6e197..0c1458a9db80c 100644 --- a/atmel-samd/tick.c +++ b/atmel-samd/tick.c @@ -1,4 +1,5 @@ #include "autoreload.h" +#include "buttons.h" #include "tick.h" @@ -17,6 +18,10 @@ static void ms_tick(struct tc_module *const module_inst) { #ifdef CIRCUITPY_AUTORELOAD_DELAY_MS autoreload_tick(); #endif + if (ticks_ms & 0x0f) { + // Every 16 ticks. + buttons_tick(); + } } void tick_init() { From 4d18b2f2c51f00911cefbc34710ed4c268953f0d Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 29 Sep 2017 12:24:11 +0200 Subject: [PATCH 02/13] Fix the behavior of the get_buttons and setup_buttons --- atmel-samd/bindings/samd/__init__.c | 8 ++++--- atmel-samd/buttons.c | 36 +++++++++++------------------ atmel-samd/buttons.h | 8 +------ atmel-samd/tick.c | 4 ++-- 4 files changed, 22 insertions(+), 34 deletions(-) diff --git a/atmel-samd/bindings/samd/__init__.c b/atmel-samd/bindings/samd/__init__.c index 37cf8b17dabf7..d307485bef9db 100644 --- a/atmel-samd/bindings/samd/__init__.c +++ b/atmel-samd/bindings/samd/__init__.c @@ -30,15 +30,17 @@ #include "rgb_led_status.h" STATIC mp_obj_t samd_get_buttons(void) { - return MP_OBJ_NEW_SMALL_INT(buttons_pressed); + mp_obj_t buttons = MP_OBJ_NEW_SMALL_INT(buttons_pressed); + buttons_pressed = 0; + return buttons; } MP_DEFINE_CONST_FUN_OBJ_0(samd_get_buttons_obj, samd_get_buttons); STATIC mp_obj_t samd_setup_buttons(size_t n_args, const mp_obj_t *args) { - buttons_setup(args[0], args[1], args[2], args[3], args[4], args[5]); + buttons_setup(n_args, args); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(samd_setup_buttons_obj, 6, 6, +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(samd_setup_buttons_obj, 0, 8, samd_setup_buttons); //| :mod:`samd` --- SAMD implementation settings diff --git a/atmel-samd/buttons.c b/atmel-samd/buttons.c index f7d681c947021..e25f492230e1e 100644 --- a/atmel-samd/buttons.c +++ b/atmel-samd/buttons.c @@ -8,42 +8,34 @@ volatile uint8_t buttons_pressed; -static digitalio_digitalinout_obj_t* button_pins[6]; +static digitalio_digitalinout_obj_t* button_pins[8]; void buttons_tick(void) { static uint8_t buttons_last = 0; - if (!button_pins[0]) { - return; // Not configured yet. - } uint8_t buttons_current = 0; uint8_t bit = 1; for (int i=0; i<6; ++i) { - if (common_hal_digitalio_digitalinout_get_value(button_pins[i])) { + if (!button_pins[i]) { + break; + } + if (!common_hal_digitalio_digitalinout_get_value(button_pins[i])) { buttons_current |= bit; } bit <<= 1; } - buttons_pressed = buttons_last & buttons_current; + buttons_pressed |= buttons_last & buttons_current; buttons_last = buttons_current; } -void buttons_setup( - digitalio_digitalinout_obj_t* pin_up, - digitalio_digitalinout_obj_t* pin_down, - digitalio_digitalinout_obj_t* pin_left, - digitalio_digitalinout_obj_t* pin_right, - digitalio_digitalinout_obj_t* pin_o, - digitalio_digitalinout_obj_t* pin_x) { - button_pins[0] = pin_up; - button_pins[1] = pin_down; - button_pins[2] = pin_left; - button_pins[3] = pin_right; - button_pins[4] = pin_o; - button_pins[5] = pin_x; - for (int i=0; i<6; ++i) { - common_hal_digitalio_digitalinout_switch_to_input( - button_pins[i], PULL_UP); +void buttons_setup(size_t n_pins, const mp_obj_t* pins) { + for (size_t i=0; i<8; ++i) { + button_pins[i] = NULL; + } + for (size_t i=0; i Date: Fri, 29 Sep 2017 17:07:09 +0200 Subject: [PATCH 03/13] Support up to 8 buttons --- atmel-samd/buttons.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atmel-samd/buttons.c b/atmel-samd/buttons.c index e25f492230e1e..5f049b747b8ce 100644 --- a/atmel-samd/buttons.c +++ b/atmel-samd/buttons.c @@ -16,7 +16,7 @@ void buttons_tick(void) { uint8_t buttons_current = 0; uint8_t bit = 1; - for (int i=0; i<6; ++i) { + for (int i=0; i<8; ++i) { if (!button_pins[i]) { break; } From 1f5e0fb6b308bf6590f8af2f14fdedf7093648f3 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 29 Sep 2017 22:12:55 +0200 Subject: [PATCH 04/13] Move buttons to a separate module --- atmel-samd/Makefile | 1 + atmel-samd/bindings/buttons/__init__.c | 54 ++++++++++++++++++++++++++ atmel-samd/bindings/samd/__init__.c | 17 -------- atmel-samd/buttons.c | 4 +- atmel-samd/buttons.h | 2 +- atmel-samd/mpconfigport.h | 2 + 6 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 atmel-samd/bindings/buttons/__init__.c diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 5d3d65178d1f9..449256c65df47 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -205,6 +205,7 @@ SRC_C = \ tick.c \ $(FLASH_IMPL) \ bindings/samd/__init__.c \ + bindings/buttons/__init__.c \ asf/common/services/sleepmgr/samd/sleepmgr.c \ asf/common/services/storage/ctrl_access/ctrl_access.c \ asf/common/services/usb/class/cdc/device/udi_cdc.c \ diff --git a/atmel-samd/bindings/buttons/__init__.c b/atmel-samd/bindings/buttons/__init__.c new file mode 100644 index 0000000000000..813c288faaeac --- /dev/null +++ b/atmel-samd/bindings/buttons/__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 "buttons.h" + +STATIC mp_obj_t buttons_get_pressed(void) { + mp_obj_t buttons = MP_OBJ_NEW_SMALL_INT(buttons_pressed); + buttons_pressed = 0; + return buttons; +} +MP_DEFINE_CONST_FUN_OBJ_0(buttons_get_pressed_obj, buttons_get_pressed); + +STATIC mp_obj_t buttons_setup(size_t n_args, const mp_obj_t *args) { + buttons_init(n_args, args); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(buttons_setup_obj, 0, 8, buttons_setup); + +STATIC const mp_rom_map_elem_t buttons_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_buttons) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&buttons_get_pressed_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_setup), MP_ROM_PTR(&buttons_setup_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(buttons_module_globals, buttons_module_globals_table); + +const mp_obj_module_t buttons_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&buttons_module_globals, +}; diff --git a/atmel-samd/bindings/samd/__init__.c b/atmel-samd/bindings/samd/__init__.c index d307485bef9db..1634d8daaedbb 100644 --- a/atmel-samd/bindings/samd/__init__.c +++ b/atmel-samd/bindings/samd/__init__.c @@ -26,23 +26,8 @@ #include "py/obj.h" #include "py/runtime.h" #include "autoreload.h" - #include "buttons.h" #include "rgb_led_status.h" -STATIC mp_obj_t samd_get_buttons(void) { - mp_obj_t buttons = MP_OBJ_NEW_SMALL_INT(buttons_pressed); - buttons_pressed = 0; - return buttons; -} -MP_DEFINE_CONST_FUN_OBJ_0(samd_get_buttons_obj, samd_get_buttons); - -STATIC mp_obj_t samd_setup_buttons(size_t n_args, const mp_obj_t *args) { - buttons_setup(n_args, args); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(samd_setup_buttons_obj, 0, 8, - samd_setup_buttons); - //| :mod:`samd` --- SAMD implementation settings //| ================================================= //| @@ -93,8 +78,6 @@ STATIC const mp_rom_map_elem_t samd_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&samd_enable_autoreload_obj)}, { MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&samd_disable_autoreload_obj)}, { MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&samd_set_rgb_status_brightness_obj)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_get_buttons), MP_ROM_PTR(&samd_get_buttons_obj)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_setup_buttons), MP_ROM_PTR(&samd_setup_buttons_obj)}, }; STATIC MP_DEFINE_CONST_DICT(samd_module_globals, samd_module_globals_table); diff --git a/atmel-samd/buttons.c b/atmel-samd/buttons.c index 5f049b747b8ce..8a7bdc895f43b 100644 --- a/atmel-samd/buttons.c +++ b/atmel-samd/buttons.c @@ -8,7 +8,7 @@ volatile uint8_t buttons_pressed; -static digitalio_digitalinout_obj_t* button_pins[8]; +static digitalio_digitalinout_obj_t* button_pins[8] = {}; void buttons_tick(void) { @@ -29,7 +29,7 @@ void buttons_tick(void) { buttons_last = buttons_current; } -void buttons_setup(size_t n_pins, const mp_obj_t* pins) { +void buttons_init(size_t n_pins, const mp_obj_t* pins) { for (size_t i=0; i<8; ++i) { button_pins[i] = NULL; } diff --git a/atmel-samd/buttons.h b/atmel-samd/buttons.h index db7689143e29e..f761d6aa36387 100644 --- a/atmel-samd/buttons.h +++ b/atmel-samd/buttons.h @@ -35,6 +35,6 @@ extern volatile uint8_t buttons_pressed; void buttons_tick(void); -void buttons_setup(size_t n_pins, const mp_obj_t* pins); +void buttons_init(size_t n_pins, const mp_obj_t* pins); #endif // MICROPY_INCLUDED_ATMEL_SAMD_BUTTONS_H diff --git a/atmel-samd/mpconfigport.h b/atmel-samd/mpconfigport.h index e99d190be5ca5..6a820c8ade4fa 100644 --- a/atmel-samd/mpconfigport.h +++ b/atmel-samd/mpconfigport.h @@ -152,6 +152,7 @@ extern const struct _mp_obj_module_t neopixel_write_module; extern const struct _mp_obj_module_t uheap_module; extern const struct _mp_obj_module_t ustack_module; extern const struct _mp_obj_module_t samd_module; +extern const struct _mp_obj_module_t buttons_module; extern const struct _mp_obj_module_t touchio_module; extern const struct _mp_obj_module_t usb_hid_module; @@ -202,6 +203,7 @@ extern const struct _mp_obj_module_t usb_hid_module; { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_samd),(mp_obj_t)&samd_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_samd),(mp_obj_t)&buttons_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, \ EXTRA_BUILTIN_MODULES From 1ca36857fa8ecd09fc1cff5ccbe6340b4ac4a644 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 29 Sep 2017 22:21:36 +0200 Subject: [PATCH 05/13] Add documentation for the buttons module --- atmel-samd/bindings/buttons/__init__.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/atmel-samd/bindings/buttons/__init__.c b/atmel-samd/bindings/buttons/__init__.c index 813c288faaeac..56c2afebbb467 100644 --- a/atmel-samd/bindings/buttons/__init__.c +++ b/atmel-samd/bindings/buttons/__init__.c @@ -27,6 +27,22 @@ #include "py/runtime.h" #include "buttons.h" +//| :mod:`buttons` --- Button handling +// ================================== +//| +//| .. module:: buttons +//| :synopsis: Button handling +//| :platform: SAMD21 +//| + +//| ..function:: get_pressed() +//| +//| Get the status of buttons pressed since the last call. +//| +//| Returns an 8-bit number, with bits that correspond to buttons, which +//| have been pressed (or held down) since the last call to this function +//| set to 1, and the remaining bits set to 0. +//| STATIC mp_obj_t buttons_get_pressed(void) { mp_obj_t buttons = MP_OBJ_NEW_SMALL_INT(buttons_pressed); buttons_pressed = 0; @@ -34,6 +50,16 @@ STATIC mp_obj_t buttons_get_pressed(void) { } MP_DEFINE_CONST_FUN_OBJ_0(buttons_get_pressed_obj, buttons_get_pressed); +//| ..function:: setup([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]]) +//| +//| Initializes button scanning routines. +//| +//| The ``b1``-``b8`` parameters are ``DigitalInOut`` objects, which +//| immediately get switched to input with a pull-up, and then scanned +//| regularly for button presses. The order is the same as the order of +//| bits returned by the ``get_pressed`` function. To disable button +//| scanning, call this without any arguments. +//| STATIC mp_obj_t buttons_setup(size_t n_args, const mp_obj_t *args) { buttons_init(n_args, args); return mp_const_none; From 958875db6a4418dbc7befe56be214d41588664d2 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sat, 30 Sep 2017 01:30:39 +0200 Subject: [PATCH 06/13] Rename buttons to gamepad and move it to shared bindings --- atmel-samd/Makefile | 3 +- atmel-samd/mpconfigport.h | 4 +- atmel-samd/tick.c | 6 ++- .../gamepad}/__init__.c | 40 +++++++++---------- .../gamepad/__init__.c | 18 ++++----- .../gamepad/__init__.h | 12 +++--- 6 files changed, 42 insertions(+), 41 deletions(-) rename {atmel-samd/bindings/buttons => shared-bindings/gamepad}/__init__.c (67%) rename atmel-samd/buttons.c => shared-module/gamepad/__init__.c (67%) rename atmel-samd/buttons.h => shared-module/gamepad/__init__.h (83%) diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 449256c65df47..4b1e7cc0560a3 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -192,7 +192,6 @@ SRC_ASF = $(addprefix asf/sam0/,\ SRC_C = \ access_vfs.c \ autoreload.c \ - buttons.c \ background.c \ fatfs_port.c \ flash_api.c \ @@ -205,7 +204,6 @@ SRC_C = \ tick.c \ $(FLASH_IMPL) \ bindings/samd/__init__.c \ - bindings/buttons/__init__.c \ asf/common/services/sleepmgr/samd/sleepmgr.c \ asf/common/services/storage/ctrl_access/ctrl_access.c \ asf/common/services/usb/class/cdc/device/udi_cdc.c \ @@ -286,6 +284,7 @@ SRC_SHARED_MODULE = \ bitbangio/OneWire.c \ bitbangio/SPI.c \ busio/OneWire.c \ + gamepad/__init__.c \ os/__init__.c \ random/__init__.c \ storage/__init__.c \ diff --git a/atmel-samd/mpconfigport.h b/atmel-samd/mpconfigport.h index 6a820c8ade4fa..471a548beea0b 100644 --- a/atmel-samd/mpconfigport.h +++ b/atmel-samd/mpconfigport.h @@ -152,7 +152,7 @@ extern const struct _mp_obj_module_t neopixel_write_module; extern const struct _mp_obj_module_t uheap_module; extern const struct _mp_obj_module_t ustack_module; extern const struct _mp_obj_module_t samd_module; -extern const struct _mp_obj_module_t buttons_module; +extern const struct _mp_obj_module_t gamepad_module; extern const struct _mp_obj_module_t touchio_module; extern const struct _mp_obj_module_t usb_hid_module; @@ -197,13 +197,13 @@ extern const struct _mp_obj_module_t usb_hid_module; { MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_samd),(mp_obj_t)&samd_module }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_samd),(mp_obj_t)&buttons_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, \ EXTRA_BUILTIN_MODULES diff --git a/atmel-samd/tick.c b/atmel-samd/tick.c index 1271a3bdafc56..f5a6458a965d7 100644 --- a/atmel-samd/tick.c +++ b/atmel-samd/tick.c @@ -1,5 +1,5 @@ #include "autoreload.h" -#include "buttons.h" +#include "shared-module/gamepad/__init__.h" #include "tick.h" @@ -18,10 +18,12 @@ static void ms_tick(struct tc_module *const module_inst) { #ifdef CIRCUITPY_AUTORELOAD_DELAY_MS autoreload_tick(); #endif + #ifdef CIRCUITPY_GAMEPAD_MODULE_ENABLED if (!(ticks_ms & 0x1f)) { // Every 32 ticks. - buttons_tick(); + gamepad_tick(); } + #endif } void tick_init() { diff --git a/atmel-samd/bindings/buttons/__init__.c b/shared-bindings/gamepad/__init__.c similarity index 67% rename from atmel-samd/bindings/buttons/__init__.c rename to shared-bindings/gamepad/__init__.c index 56c2afebbb467..22b6cb1201378 100644 --- a/atmel-samd/bindings/buttons/__init__.c +++ b/shared-bindings/gamepad/__init__.c @@ -25,30 +25,30 @@ */ #include "py/obj.h" #include "py/runtime.h" - #include "buttons.h" + #include "shared-module/gamepad/__init__.h" -//| :mod:`buttons` --- Button handling +//| :mod:`gamepad` --- Button handling // ================================== //| -//| .. module:: buttons +//| .. module:: gamepad //| :synopsis: Button handling //| :platform: SAMD21 //| //| ..function:: get_pressed() //| -//| Get the status of buttons pressed since the last call. +//| Get the status of gamepad pressed since the last call. //| -//| Returns an 8-bit number, with bits that correspond to buttons, which +//| Returns an 8-bit number, with bits that correspond to gamepad, which //| have been pressed (or held down) since the last call to this function //| set to 1, and the remaining bits set to 0. //| -STATIC mp_obj_t buttons_get_pressed(void) { - mp_obj_t buttons = MP_OBJ_NEW_SMALL_INT(buttons_pressed); - buttons_pressed = 0; - return buttons; +STATIC mp_obj_t gamepad_get_pressed(void) { + mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(gamepad_pressed); + gamepad_pressed = 0; + return gamepad; } -MP_DEFINE_CONST_FUN_OBJ_0(buttons_get_pressed_obj, buttons_get_pressed); +MP_DEFINE_CONST_FUN_OBJ_0(gamepad_get_pressed_obj, gamepad_get_pressed); //| ..function:: setup([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]]) //| @@ -60,21 +60,21 @@ MP_DEFINE_CONST_FUN_OBJ_0(buttons_get_pressed_obj, buttons_get_pressed); //| bits returned by the ``get_pressed`` function. To disable button //| scanning, call this without any arguments. //| -STATIC mp_obj_t buttons_setup(size_t n_args, const mp_obj_t *args) { - buttons_init(n_args, args); +STATIC mp_obj_t gamepad_setup(size_t n_args, const mp_obj_t *args) { + gamepad_init(n_args, args); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(buttons_setup_obj, 0, 8, buttons_setup); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gamepad_setup_obj, 0, 8, gamepad_setup); -STATIC const mp_rom_map_elem_t buttons_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_buttons) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&buttons_get_pressed_obj)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_setup), MP_ROM_PTR(&buttons_setup_obj)}, +STATIC const mp_rom_map_elem_t gamepad_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gamepad) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_setup), MP_ROM_PTR(&gamepad_setup_obj)}, }; -STATIC MP_DEFINE_CONST_DICT(buttons_module_globals, buttons_module_globals_table); +STATIC MP_DEFINE_CONST_DICT(gamepad_module_globals, gamepad_module_globals_table); -const mp_obj_module_t buttons_module = { +const mp_obj_module_t gamepad_module = { .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&buttons_module_globals, + .globals = (mp_obj_dict_t*)&gamepad_module_globals, }; diff --git a/atmel-samd/buttons.c b/shared-module/gamepad/__init__.c similarity index 67% rename from atmel-samd/buttons.c rename to shared-module/gamepad/__init__.c index 8a7bdc895f43b..2a685489ec489 100644 --- a/atmel-samd/buttons.c +++ b/shared-module/gamepad/__init__.c @@ -1,35 +1,35 @@ #include -#include "buttons.h" +#include "__init__.h" #include "shared-bindings/digitalio/Pull.h" #include "shared-bindings/digitalio/DigitalInOut.h" -volatile uint8_t buttons_pressed; +volatile uint8_t gamepad_pressed; static digitalio_digitalinout_obj_t* button_pins[8] = {}; -void buttons_tick(void) { - static uint8_t buttons_last = 0; +void gamepad_tick(void) { + static uint8_t gamepad_last = 0; - uint8_t buttons_current = 0; + uint8_t gamepad_current = 0; uint8_t bit = 1; for (int i=0; i<8; ++i) { if (!button_pins[i]) { break; } if (!common_hal_digitalio_digitalinout_get_value(button_pins[i])) { - buttons_current |= bit; + gamepad_current |= bit; } bit <<= 1; } - buttons_pressed |= buttons_last & buttons_current; - buttons_last = buttons_current; + gamepad_pressed |= gamepad_last & gamepad_current; + gamepad_last = gamepad_current; } -void buttons_init(size_t n_pins, const mp_obj_t* pins) { +void gamepad_init(size_t n_pins, const mp_obj_t* pins) { for (size_t i=0; i<8; ++i) { button_pins[i] = NULL; } diff --git a/atmel-samd/buttons.h b/shared-module/gamepad/__init__.h similarity index 83% rename from atmel-samd/buttons.h rename to shared-module/gamepad/__init__.h index f761d6aa36387..df5ff033554c6 100644 --- a/atmel-samd/buttons.h +++ b/shared-module/gamepad/__init__.h @@ -24,17 +24,17 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BUTTONS_H -#define MICROPY_INCLUDED_ATMEL_SAMD_BUTTONS_H +#ifndef MICROPY_INCLUDED_GAMEPAD_H +#define MICROPY_INCLUDED_GAMEPAD_H #include #include "shared-bindings/digitalio/DigitalInOut.h" -extern volatile uint8_t buttons_pressed; +extern volatile uint8_t gamepad_pressed; -void buttons_tick(void); -void buttons_init(size_t n_pins, const mp_obj_t* pins); +void gamepad_tick(void); +void gamepad_init(size_t n_pins, const mp_obj_t* pins); -#endif // MICROPY_INCLUDED_ATMEL_SAMD_BUTTONS_H +#endif // MICROPY_INCLUDED_GAMEPAD_H From 12fc5e99ef22226f0651108dc0565070d8678ae8 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sat, 30 Sep 2017 02:17:17 +0200 Subject: [PATCH 07/13] Finish gamepad rename and make gamepad ticks configurable --- atmel-samd/mpconfigport.h | 2 ++ atmel-samd/tick.c | 5 ++--- shared-module/gamepad/__init__.c | 15 ++++++--------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/atmel-samd/mpconfigport.h b/atmel-samd/mpconfigport.h index 471a548beea0b..c0e753cd83d79 100644 --- a/atmel-samd/mpconfigport.h +++ b/atmel-samd/mpconfigport.h @@ -247,5 +247,7 @@ void run_background_tasks(void); #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 #define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt" +// Scan gamepad every 32ms +#define CIRCUITPY_GAMEPAD_TICKS 0x1f #endif // __INCLUDED_MPCONFIGPORT_H diff --git a/atmel-samd/tick.c b/atmel-samd/tick.c index f5a6458a965d7..e97da07303fcd 100644 --- a/atmel-samd/tick.c +++ b/atmel-samd/tick.c @@ -18,9 +18,8 @@ static void ms_tick(struct tc_module *const module_inst) { #ifdef CIRCUITPY_AUTORELOAD_DELAY_MS autoreload_tick(); #endif - #ifdef CIRCUITPY_GAMEPAD_MODULE_ENABLED - if (!(ticks_ms & 0x1f)) { - // Every 32 ticks. + #ifdef CIRCUITPY_GAMEPAD_TICKS + if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) { gamepad_tick(); } #endif diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index 2a685489ec489..b1e91b0e8e722 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -1,4 +1,3 @@ - #include #include "__init__.h" @@ -8,22 +7,20 @@ volatile uint8_t gamepad_pressed; -static digitalio_digitalinout_obj_t* button_pins[8] = {}; +static digitalio_digitalinout_obj_t* gamepad_pins[8] = {}; void gamepad_tick(void) { static uint8_t gamepad_last = 0; uint8_t gamepad_current = 0; - uint8_t bit = 1; for (int i=0; i<8; ++i) { - if (!button_pins[i]) { + if (!gamepad_pins[i]) { break; } - if (!common_hal_digitalio_digitalinout_get_value(button_pins[i])) { - gamepad_current |= bit; + if (!common_hal_digitalio_digitalinout_get_value(gamepad_pins[i])) { + gamepad_current |= 1< Date: Sat, 30 Sep 2017 18:19:57 +0200 Subject: [PATCH 08/13] Add gamepad module to the support matrix --- shared-bindings/index.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/shared-bindings/index.rst b/shared-bindings/index.rst index 49b6ce5bc3ab4..556626268bbce 100644 --- a/shared-bindings/index.rst +++ b/shared-bindings/index.rst @@ -21,9 +21,10 @@ Module / Port SAMD21 SAMD21 Express ESP8266 `board` **Yes** **Yes** **Yes** `busio` **Yes** **Yes** **Yes** `digitalio` **Yes** **Yes** **Yes** -`microcontroller` **Yes** **Yes** **Yes** +`gamepad` No **Yes** No +`microcontroller` **Yes** **Yes** **Yes** `multiterminal` No No **Yes** -`neopixel_write` **Yes** **Yes** **Yes** +`neopixel_write` **Yes** **Yes** **Yes** `nvm` No **Yes** No `os` **Yes** **Yes** **Yes** `pulseio` No **Yes** No From 624ecb899a82496276e1916b2e847a2abb28e3c4 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sat, 30 Sep 2017 18:21:04 +0200 Subject: [PATCH 09/13] Move gamepad to extra building modules --- atmel-samd/mpconfigport.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atmel-samd/mpconfigport.h b/atmel-samd/mpconfigport.h index c0e753cd83d79..b51bf5095ef88 100644 --- a/atmel-samd/mpconfigport.h +++ b/atmel-samd/mpconfigport.h @@ -176,6 +176,7 @@ extern const struct _mp_obj_module_t usb_hid_module; #define EXTRA_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_audioio), (mp_obj_t)&audioio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_audiobusio), (mp_obj_t)&audiobusio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_nvm), (mp_obj_t)&cpy_nvm_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module } @@ -197,7 +198,6 @@ extern const struct _mp_obj_module_t usb_hid_module; { MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, \ From cb41776d52f253d1236fda63207b6dd220af4071 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sun, 1 Oct 2017 01:27:59 +0200 Subject: [PATCH 10/13] Make a GamePad object in gamepad module --- shared-bindings/gamepad/__init__.c | 53 +++++++++++++++++++++--------- shared-module/gamepad/__init__.c | 20 ++++++----- shared-module/gamepad/__init__.h | 8 ++++- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/shared-bindings/gamepad/__init__.c b/shared-bindings/gamepad/__init__.c index 22b6cb1201378..50cf87c78ccb2 100644 --- a/shared-bindings/gamepad/__init__.c +++ b/shared-bindings/gamepad/__init__.c @@ -23,9 +23,15 @@ * 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 "shared-module/gamepad/__init__.h" +#include "py/obj.h" +#include "py/runtime.h" +#include "py/mphal.h" +#include "shared-module/gamepad/__init__.h" + + +gamepad_obj_t* gamepad_singleton = NULL; + + //| :mod:`gamepad` --- Button handling // ================================== @@ -35,7 +41,7 @@ //| :platform: SAMD21 //| -//| ..function:: get_pressed() +//| ..method:: GamePad.get_pressed() //| //| Get the status of gamepad pressed since the last call. //| @@ -43,14 +49,28 @@ //| have been pressed (or held down) since the last call to this function //| set to 1, and the remaining bits set to 0. //| -STATIC mp_obj_t gamepad_get_pressed(void) { - mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(gamepad_pressed); - gamepad_pressed = 0; +STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) { + gamepad_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(self->pressed); + self->pressed = 0; return gamepad; } -MP_DEFINE_CONST_FUN_OBJ_0(gamepad_get_pressed_obj, gamepad_get_pressed); +MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed); -//| ..function:: setup([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]]) +STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *args); +STATIC const mp_rom_map_elem_t gamepad_locals_dict_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)}, +}; +STATIC MP_DEFINE_CONST_DICT(gamepad_locals_dict, gamepad_locals_dict_table); +const mp_obj_type_t gamepad_type = { + { &mp_type_type }, + .name = MP_QSTR_GamePad, + .make_new = gamepad_make_new, + .locals_dict = (mp_obj_dict_t*)&gamepad_locals_dict, +}; + +//| ..class:: GamePad([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]]) //| //| Initializes button scanning routines. //| @@ -60,20 +80,21 @@ MP_DEFINE_CONST_FUN_OBJ_0(gamepad_get_pressed_obj, gamepad_get_pressed); //| bits returned by the ``get_pressed`` function. To disable button //| scanning, call this without any arguments. //| -STATIC mp_obj_t gamepad_setup(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *args) { + if (!gamepad_singleton) { + gamepad_singleton = m_new_obj(gamepad_obj_t); + gamepad_singleton->base.type = &gamepad_type; + } gamepad_init(n_args, args); - return mp_const_none; + return MP_OBJ_FROM_PTR(gamepad_singleton); } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gamepad_setup_obj, 0, 8, gamepad_setup); STATIC const mp_rom_map_elem_t gamepad_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gamepad) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_setup), MP_ROM_PTR(&gamepad_setup_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_GamePad), MP_ROM_PTR(&gamepad_type)}, }; - STATIC MP_DEFINE_CONST_DICT(gamepad_module_globals, gamepad_module_globals_table); - const mp_obj_module_t gamepad_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)&gamepad_module_globals, diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index b1e91b0e8e722..9da896baab05e 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -7,32 +7,34 @@ volatile uint8_t gamepad_pressed; -static digitalio_digitalinout_obj_t* gamepad_pins[8] = {}; void gamepad_tick(void) { - static uint8_t gamepad_last = 0; - + if (!gamepad_singleton) { + return; + } uint8_t gamepad_current = 0; for (int i=0; i<8; ++i) { - if (!gamepad_pins[i]) { + digitalio_digitalinout_obj_t* pin = gamepad_singleton->pins[i]; + if (!pin) { break; } - if (!common_hal_digitalio_digitalinout_get_value(gamepad_pins[i])) { + if (!common_hal_digitalio_digitalinout_get_value(pin)) { gamepad_current |= 1<pressed |= gamepad_singleton->last & gamepad_current; + gamepad_singleton->last = gamepad_current; } void gamepad_init(size_t n_pins, const mp_obj_t* pins) { for (size_t i=0; i<8; ++i) { - gamepad_pins[i] = NULL; + gamepad_singleton->pins[i] = NULL; } for (size_t i=0; ipins[i] = pin; common_hal_digitalio_digitalinout_switch_to_input(pin, PULL_UP); } + gamepad_singleton->last = 0; } diff --git a/shared-module/gamepad/__init__.h b/shared-module/gamepad/__init__.h index df5ff033554c6..3b43b9f1c3fcc 100644 --- a/shared-module/gamepad/__init__.h +++ b/shared-module/gamepad/__init__.h @@ -31,8 +31,14 @@ #include "shared-bindings/digitalio/DigitalInOut.h" +typedef struct { + mp_obj_base_t base; + digitalio_digitalinout_obj_t* pins[8]; + volatile uint8_t last; + volatile uint8_t pressed; +} gamepad_obj_t; -extern volatile uint8_t gamepad_pressed; +extern gamepad_obj_t* gamepad_singleton; void gamepad_tick(void); void gamepad_init(size_t n_pins, const mp_obj_t* pins); From 3701139f2e6f3a1b2754777cdafaf4218df71ec6 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 3 Oct 2017 10:15:11 +0200 Subject: [PATCH 11/13] Reorganize gamepad code --- atmel-samd/Makefile | 1 + shared-bindings/gamepad/GamePad.c | 111 +++++++++++++++++++++++++++++ shared-bindings/gamepad/GamePad.h | 33 +++++++++ shared-bindings/gamepad/__init__.c | 67 ++--------------- shared-module/gamepad/GamePad.c | 46 ++++++++++++ shared-module/gamepad/GamePad.h | 45 ++++++++++++ shared-module/gamepad/__init__.c | 43 ++++++----- shared-module/gamepad/__init__.h | 14 ---- 8 files changed, 267 insertions(+), 93 deletions(-) create mode 100644 shared-bindings/gamepad/GamePad.c create mode 100644 shared-bindings/gamepad/GamePad.h create mode 100644 shared-module/gamepad/GamePad.c create mode 100644 shared-module/gamepad/GamePad.h diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 4b1e7cc0560a3..c8f3bc690c78a 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -285,6 +285,7 @@ SRC_SHARED_MODULE = \ bitbangio/SPI.c \ busio/OneWire.c \ gamepad/__init__.c \ + gamepad/GamePad.c \ os/__init__.c \ random/__init__.c \ storage/__init__.c \ diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c new file mode 100644 index 0000000000000..86a7e78ad4ac8 --- /dev/null +++ b/shared-bindings/gamepad/GamePad.c @@ -0,0 +1,111 @@ +/* + * 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 "shared-module/gamepad/GamePad.h" +#include "GamePad.h" + + +gamepad_obj_t* gamepad_singleton = NULL; + +//| :mod:`gamepad` --- Button handling +// ================================== +//| +//| .. module:: gamepad +//| :synopsis: Button handling +//| :platform: SAMD21 +//| + +//| ..class:: GamePad([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]]) +//| +//| Initializes button scanning routines. +//| +//| The ``b1``-``b8`` parameters are ``DigitalInOut`` objects, which +//| immediately get switched to input with a pull-up, and then scanned +//| regularly for button presses. The order is the same as the order of +//| bits returned by the ``get_pressed`` function. You can re-initialize +//| it with different keys, then the new object will replace the previous +//| one. +//| +//| The basic feature required here is the ability to poll the keys at regular +//| intervals (so that de-bouncing is consistent) and fast enough (so that we +//| don't miss short button presses) while at the same time letting the user +//| code run normally, call blocking functions and wait on delays. +//| +STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *args) { + if (!gamepad_singleton) { + gamepad_singleton = m_new_obj(gamepad_obj_t); + gamepad_singleton->base.type = &gamepad_type; + } + gamepad_init(n_args, args); + return MP_OBJ_FROM_PTR(gamepad_singleton); +} + + +//| ..method:: get_pressed() +//| +//| Get the status of gamepad pressed since the last call and clear it. +//| +//| Returns an 8-bit number, with bits that correspond to buttons, which +//| have been pressed (or held down) since the last call to this function +//| set to 1, and the remaining bits set to 0. +//| +STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) { + gamepad_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(self->pressed); + self->pressed = 0; + return gamepad; +} +MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed); + + +//| ..method:: deinit() +//| +//| Disable scanning. +//| +STATIC mp_obj_t gamepad_deinit(mp_obj_t self_in) { + gamepad_singleton = NULL; + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(gamepad_deinit_obj, gamepad_deinit); + + +STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *args); +STATIC const mp_rom_map_elem_t gamepad_locals_dict_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepad_deinit_obj)}, +}; +STATIC MP_DEFINE_CONST_DICT(gamepad_locals_dict, gamepad_locals_dict_table); +const mp_obj_type_t gamepad_type = { + { &mp_type_type }, + .name = MP_QSTR_GamePad, + .make_new = gamepad_make_new, + .locals_dict = (mp_obj_dict_t*)&gamepad_locals_dict, +}; + diff --git a/shared-bindings/gamepad/GamePad.h b/shared-bindings/gamepad/GamePad.h new file mode 100644 index 0000000000000..172c95ace85ff --- /dev/null +++ b/shared-bindings/gamepad/GamePad.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_GAMEPAD_GAMEPAD_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H + +extern const mp_obj_type_t gamepad_type; + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H diff --git a/shared-bindings/gamepad/__init__.c b/shared-bindings/gamepad/__init__.c index 50cf87c78ccb2..f28d2ae059750 100644 --- a/shared-bindings/gamepad/__init__.c +++ b/shared-bindings/gamepad/__init__.c @@ -26,75 +26,16 @@ #include "py/obj.h" #include "py/runtime.h" #include "py/mphal.h" -#include "shared-module/gamepad/__init__.h" +#include "GamePad.h" -gamepad_obj_t* gamepad_singleton = NULL; - - - -//| :mod:`gamepad` --- Button handling -// ================================== -//| -//| .. module:: gamepad -//| :synopsis: Button handling -//| :platform: SAMD21 -//| - -//| ..method:: GamePad.get_pressed() -//| -//| Get the status of gamepad pressed since the last call. -//| -//| Returns an 8-bit number, with bits that correspond to gamepad, which -//| have been pressed (or held down) since the last call to this function -//| set to 1, and the remaining bits set to 0. -//| -STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) { - gamepad_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(self->pressed); - self->pressed = 0; - return gamepad; -} -MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed); - -STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, - size_t n_kw, const mp_obj_t *args); -STATIC const mp_rom_map_elem_t gamepad_locals_dict_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)}, -}; -STATIC MP_DEFINE_CONST_DICT(gamepad_locals_dict, gamepad_locals_dict_table); -const mp_obj_type_t gamepad_type = { - { &mp_type_type }, - .name = MP_QSTR_GamePad, - .make_new = gamepad_make_new, - .locals_dict = (mp_obj_dict_t*)&gamepad_locals_dict, -}; - -//| ..class:: GamePad([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]]) -//| -//| Initializes button scanning routines. -//| -//| The ``b1``-``b8`` parameters are ``DigitalInOut`` objects, which -//| immediately get switched to input with a pull-up, and then scanned -//| regularly for button presses. The order is the same as the order of -//| bits returned by the ``get_pressed`` function. To disable button -//| scanning, call this without any arguments. -//| -STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, - size_t n_kw, const mp_obj_t *args) { - if (!gamepad_singleton) { - gamepad_singleton = m_new_obj(gamepad_obj_t); - gamepad_singleton->base.type = &gamepad_type; - } - gamepad_init(n_args, args); - return MP_OBJ_FROM_PTR(gamepad_singleton); -} - STATIC const mp_rom_map_elem_t gamepad_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gamepad) }, { MP_OBJ_NEW_QSTR(MP_QSTR_GamePad), MP_ROM_PTR(&gamepad_type)}, }; -STATIC MP_DEFINE_CONST_DICT(gamepad_module_globals, gamepad_module_globals_table); +STATIC MP_DEFINE_CONST_DICT(gamepad_module_globals, + gamepad_module_globals_table); + const mp_obj_module_t gamepad_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)&gamepad_module_globals, diff --git a/shared-module/gamepad/GamePad.c b/shared-module/gamepad/GamePad.c new file mode 100644 index 0000000000000..3a2b8a3fe9264 --- /dev/null +++ b/shared-module/gamepad/GamePad.c @@ -0,0 +1,46 @@ +/* + * 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 "__init__.h" +#include "GamePad.h" + +#include "shared-bindings/digitalio/Pull.h" +#include "shared-bindings/digitalio/DigitalInOut.h" + + +void gamepad_init(size_t n_pins, const mp_obj_t* pins) { + for (size_t i=0; i<8; ++i) { + gamepad_singleton->pins[i] = NULL; + } + for (size_t i=0; ipins[i] = pin; + common_hal_digitalio_digitalinout_switch_to_input(pin, PULL_UP); + } + gamepad_singleton->last = 0; +} diff --git a/shared-module/gamepad/GamePad.h b/shared-module/gamepad/GamePad.h new file mode 100644 index 0000000000000..e5f709134de1d --- /dev/null +++ b/shared-module/gamepad/GamePad.h @@ -0,0 +1,45 @@ +/* + * 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_GAMEPAD_GAMEPAD_H +#define MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H + +#include + +#include "shared-bindings/digitalio/DigitalInOut.h" + +typedef struct { + mp_obj_base_t base; + digitalio_digitalinout_obj_t* pins[8]; + volatile uint8_t last; + volatile uint8_t pressed; +} gamepad_obj_t; + +extern gamepad_obj_t* gamepad_singleton; + +void gamepad_init(size_t n_pins, const mp_obj_t* pins); + +#endif // MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index 9da896baab05e..1aebf611d3935 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -1,14 +1,37 @@ +/* + * 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 "__init__.h" +#include "GamePad.h" -#include "shared-bindings/digitalio/Pull.h" #include "shared-bindings/digitalio/DigitalInOut.h" -volatile uint8_t gamepad_pressed; - - void gamepad_tick(void) { if (!gamepad_singleton) { return; @@ -26,15 +49,3 @@ void gamepad_tick(void) { gamepad_singleton->pressed |= gamepad_singleton->last & gamepad_current; gamepad_singleton->last = gamepad_current; } - -void gamepad_init(size_t n_pins, const mp_obj_t* pins) { - for (size_t i=0; i<8; ++i) { - gamepad_singleton->pins[i] = NULL; - } - for (size_t i=0; ipins[i] = pin; - common_hal_digitalio_digitalinout_switch_to_input(pin, PULL_UP); - } - gamepad_singleton->last = 0; -} diff --git a/shared-module/gamepad/__init__.h b/shared-module/gamepad/__init__.h index 3b43b9f1c3fcc..eacd7236699b0 100644 --- a/shared-module/gamepad/__init__.h +++ b/shared-module/gamepad/__init__.h @@ -27,20 +27,6 @@ #ifndef MICROPY_INCLUDED_GAMEPAD_H #define MICROPY_INCLUDED_GAMEPAD_H -#include - -#include "shared-bindings/digitalio/DigitalInOut.h" - -typedef struct { - mp_obj_base_t base; - digitalio_digitalinout_obj_t* pins[8]; - volatile uint8_t last; - volatile uint8_t pressed; -} gamepad_obj_t; - -extern gamepad_obj_t* gamepad_singleton; - void gamepad_tick(void); -void gamepad_init(size_t n_pins, const mp_obj_t* pins); #endif // MICROPY_INCLUDED_GAMEPAD_H From 1f178f5725d2f1f402bde61171af1775e852fb83 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 3 Oct 2017 21:57:11 +0200 Subject: [PATCH 12/13] Update docs for gamepad module --- shared-bindings/gamepad/GamePad.c | 83 +++++++++++++++++++++--------- shared-bindings/gamepad/__init__.c | 12 +++++ 2 files changed, 71 insertions(+), 24 deletions(-) diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c index 86a7e78ad4ac8..bdc3359fb698a 100644 --- a/shared-bindings/gamepad/GamePad.c +++ b/shared-bindings/gamepad/GamePad.c @@ -32,29 +32,62 @@ gamepad_obj_t* gamepad_singleton = NULL; -//| :mod:`gamepad` --- Button handling -// ================================== +//| .. currentmodule:: gamepad //| -//| .. module:: gamepad -//| :synopsis: Button handling -//| :platform: SAMD21 +//| :class:`GamePad` -- Scan buttons for presses +//| ============================================ +//| +//| Usage:: +//| +//| import board +//| import digitalio +//| import gamepad +//| import time +//| +//| B_UP = 1 << 0 +//| B_DOWN = 1 << 1 +//| +//| +//| pad = gamepad.GamePad( +//| digitalio.DigitalInOut(board.D0), +//| digitalio.DigitalInOut(board.D1), +//| ) +//| +//| y = 0 +//| while True: +//| buttons = pad.get_pressed() +//| if buttons & B_UP: +//| y -= 1 +//| print(y) +//| elif buttons & B_DOWN: +//| y += 1 +//| print(y) +//| time.sleep(0.1) +//| while pad.get_pressed(): +//| # Wait for all buttons to be released. +//| time.sleep(0.1) //| -//| ..class:: GamePad([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]]) +//| .. class:: GamePad([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]]) +//| +//| Initializes button scanning routines. //| -//| Initializes button scanning routines. +//| The ``b1``-``b8`` parameters are ``DigitalInOut`` objects, which +//| immediately get switched to input with a pull-up, and then scanned +//| regularly for button presses. The order is the same as the order of +//| bits returned by the ``get_pressed`` function. You can re-initialize +//| it with different keys, then the new object will replace the previous +//| one. //| -//| The ``b1``-``b8`` parameters are ``DigitalInOut`` objects, which -//| immediately get switched to input with a pull-up, and then scanned -//| regularly for button presses. The order is the same as the order of -//| bits returned by the ``get_pressed`` function. You can re-initialize -//| it with different keys, then the new object will replace the previous -//| one. +//| The basic feature required here is the ability to poll the keys at +//| regular intervals (so that de-bouncing is consistent) and fast enough +//| (so that we don't miss short button presses) while at the same time +//| letting the user code run normally, call blocking functions and wait +//| on delays. //| -//| The basic feature required here is the ability to poll the keys at regular -//| intervals (so that de-bouncing is consistent) and fast enough (so that we -//| don't miss short button presses) while at the same time letting the user -//| code run normally, call blocking functions and wait on delays. +//| They button presses are accumulated, until the ``get_pressed`` method +//| is called, at which point the button state is cleared, and the new +//| button presses start to be recorded. //| STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { @@ -67,13 +100,15 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, } -//| ..method:: get_pressed() +//| .. method:: get_pressed() //| -//| Get the status of gamepad pressed since the last call and clear it. +//| Get the status of buttons pressed since the last call and clear it. //| -//| Returns an 8-bit number, with bits that correspond to buttons, which -//| have been pressed (or held down) since the last call to this function -//| set to 1, and the remaining bits set to 0. +//| Returns an 8-bit number, with bits that correspond to buttons, +//| which have been pressed (or held down) since the last call to this +//| function set to 1, and the remaining bits set to 0. Then it clears +//| the button state, so that new button presses (or buttons that are +//| held down) can be recorded for the next call. //| STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) { gamepad_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -84,9 +119,9 @@ STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) { MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed); -//| ..method:: deinit() +//| .. method:: deinit() //| -//| Disable scanning. +//| Disable button scanning. //| STATIC mp_obj_t gamepad_deinit(mp_obj_t self_in) { gamepad_singleton = NULL; diff --git a/shared-bindings/gamepad/__init__.c b/shared-bindings/gamepad/__init__.c index f28d2ae059750..0c99d0d52bea8 100644 --- a/shared-bindings/gamepad/__init__.c +++ b/shared-bindings/gamepad/__init__.c @@ -29,6 +29,18 @@ #include "GamePad.h" +//| :mod:`gamepad` --- Button handling +//| ================================== +//| +//| .. module:: gamepad +//| :synopsis: Button handling +//| :platform: SAMD21 +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| GamePad +//| STATIC const mp_rom_map_elem_t gamepad_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gamepad) }, { MP_OBJ_NEW_QSTR(MP_QSTR_GamePad), MP_ROM_PTR(&gamepad_type)}, From 97fab02a13f6fa73856bf15b34cee4f59efe2d86 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 3 Oct 2017 22:00:27 +0200 Subject: [PATCH 13/13] Only call gamepad_tick when the module is enabled --- atmel-samd/mpconfigport.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/atmel-samd/mpconfigport.h b/atmel-samd/mpconfigport.h index b51bf5095ef88..fc061d1cb896c 100644 --- a/atmel-samd/mpconfigport.h +++ b/atmel-samd/mpconfigport.h @@ -172,6 +172,8 @@ extern const struct _mp_obj_module_t usb_hid_module; #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) #define MICROPY_PY_SYS_MAXSIZE (1) #define MICROPY_CPYTHON_COMPAT (1) + // Scan gamepad every 32ms + #define CIRCUITPY_GAMEPAD_TICKS 0x1f #define EXTRA_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_audioio), (mp_obj_t)&audioio_module }, \ @@ -247,7 +249,5 @@ void run_background_tasks(void); #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 #define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt" -// Scan gamepad every 32ms -#define CIRCUITPY_GAMEPAD_TICKS 0x1f #endif // __INCLUDED_MPCONFIGPORT_H