|
| 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 "shared-module/gamepad/GamePad.h" |
| 30 | +#include "GamePad.h" |
| 31 | + |
| 32 | + |
| 33 | +gamepad_obj_t* gamepad_singleton = NULL; |
| 34 | + |
| 35 | +//| .. currentmodule:: gamepad |
| 36 | +//| |
| 37 | +//| :class:`GamePad` -- Scan buttons for presses |
| 38 | +//| ============================================ |
| 39 | +//| |
| 40 | +//| Usage:: |
| 41 | +//| |
| 42 | +//| import board |
| 43 | +//| import digitalio |
| 44 | +//| import gamepad |
| 45 | +//| import time |
| 46 | +//| |
| 47 | +//| B_UP = 1 << 0 |
| 48 | +//| B_DOWN = 1 << 1 |
| 49 | +//| |
| 50 | +//| |
| 51 | +//| pad = gamepad.GamePad( |
| 52 | +//| digitalio.DigitalInOut(board.D0), |
| 53 | +//| digitalio.DigitalInOut(board.D1), |
| 54 | +//| ) |
| 55 | +//| |
| 56 | +//| y = 0 |
| 57 | +//| while True: |
| 58 | +//| buttons = pad.get_pressed() |
| 59 | +//| if buttons & B_UP: |
| 60 | +//| y -= 1 |
| 61 | +//| print(y) |
| 62 | +//| elif buttons & B_DOWN: |
| 63 | +//| y += 1 |
| 64 | +//| print(y) |
| 65 | +//| time.sleep(0.1) |
| 66 | +//| while pad.get_pressed(): |
| 67 | +//| # Wait for all buttons to be released. |
| 68 | +//| time.sleep(0.1) |
| 69 | +//| |
| 70 | + |
| 71 | +//| .. class:: GamePad([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]]) |
| 72 | +//| |
| 73 | +//| Initializes button scanning routines. |
| 74 | +//| |
| 75 | +//| The ``b1``-``b8`` parameters are ``DigitalInOut`` objects, which |
| 76 | +//| immediately get switched to input with a pull-up, and then scanned |
| 77 | +//| regularly for button presses. The order is the same as the order of |
| 78 | +//| bits returned by the ``get_pressed`` function. You can re-initialize |
| 79 | +//| it with different keys, then the new object will replace the previous |
| 80 | +//| one. |
| 81 | +//| |
| 82 | +//| The basic feature required here is the ability to poll the keys at |
| 83 | +//| regular intervals (so that de-bouncing is consistent) and fast enough |
| 84 | +//| (so that we don't miss short button presses) while at the same time |
| 85 | +//| letting the user code run normally, call blocking functions and wait |
| 86 | +//| on delays. |
| 87 | +//| |
| 88 | +//| They button presses are accumulated, until the ``get_pressed`` method |
| 89 | +//| is called, at which point the button state is cleared, and the new |
| 90 | +//| button presses start to be recorded. |
| 91 | +//| |
| 92 | +STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, |
| 93 | + size_t n_kw, const mp_obj_t *args) { |
| 94 | + if (!gamepad_singleton) { |
| 95 | + gamepad_singleton = m_new_obj(gamepad_obj_t); |
| 96 | + gamepad_singleton->base.type = &gamepad_type; |
| 97 | + } |
| 98 | + gamepad_init(n_args, args); |
| 99 | + return MP_OBJ_FROM_PTR(gamepad_singleton); |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +//| .. method:: get_pressed() |
| 104 | +//| |
| 105 | +//| Get the status of buttons pressed since the last call and clear it. |
| 106 | +//| |
| 107 | +//| Returns an 8-bit number, with bits that correspond to buttons, |
| 108 | +//| which have been pressed (or held down) since the last call to this |
| 109 | +//| function set to 1, and the remaining bits set to 0. Then it clears |
| 110 | +//| the button state, so that new button presses (or buttons that are |
| 111 | +//| held down) can be recorded for the next call. |
| 112 | +//| |
| 113 | +STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) { |
| 114 | + gamepad_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 115 | + mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(self->pressed); |
| 116 | + self->pressed = 0; |
| 117 | + return gamepad; |
| 118 | +} |
| 119 | +MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed); |
| 120 | + |
| 121 | + |
| 122 | +//| .. method:: deinit() |
| 123 | +//| |
| 124 | +//| Disable button scanning. |
| 125 | +//| |
| 126 | +STATIC mp_obj_t gamepad_deinit(mp_obj_t self_in) { |
| 127 | + gamepad_singleton = NULL; |
| 128 | + return mp_const_none; |
| 129 | +} |
| 130 | +MP_DEFINE_CONST_FUN_OBJ_1(gamepad_deinit_obj, gamepad_deinit); |
| 131 | + |
| 132 | + |
| 133 | +STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, |
| 134 | + size_t n_kw, const mp_obj_t *args); |
| 135 | +STATIC const mp_rom_map_elem_t gamepad_locals_dict_table[] = { |
| 136 | + { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)}, |
| 137 | + { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepad_deinit_obj)}, |
| 138 | +}; |
| 139 | +STATIC MP_DEFINE_CONST_DICT(gamepad_locals_dict, gamepad_locals_dict_table); |
| 140 | +const mp_obj_type_t gamepad_type = { |
| 141 | + { &mp_type_type }, |
| 142 | + .name = MP_QSTR_GamePad, |
| 143 | + .make_new = gamepad_make_new, |
| 144 | + .locals_dict = (mp_obj_dict_t*)&gamepad_locals_dict, |
| 145 | +}; |
| 146 | + |
0 commit comments