|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Scott Shawcroft 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 <stdint.h> |
| 28 | + |
| 29 | +#include "shared-bindings/usb_midi/PortIn.h" |
| 30 | +#include "shared-bindings/util.h" |
| 31 | + |
| 32 | +#include "py/ioctl.h" |
| 33 | +#include "py/objproperty.h" |
| 34 | +#include "py/runtime.h" |
| 35 | +#include "py/stream.h" |
| 36 | +#include "supervisor/shared/translate.h" |
| 37 | + |
| 38 | + |
| 39 | +//| .. currentmodule:: usb_midi |
| 40 | +//| |
| 41 | +//| :class:`PortIn` -- receives midi commands over USB |
| 42 | +//| =================================================== |
| 43 | +//| |
| 44 | +//| .. class:: PortIn() |
| 45 | +//| |
| 46 | +//| Not currently dynamically supported. |
| 47 | +//| |
| 48 | + |
| 49 | +STATIC mp_obj_t usb_midi_portin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { |
| 50 | + return mp_const_none; |
| 51 | +} |
| 52 | + |
| 53 | +// These are standard stream methods. Code is in py/stream.c. |
| 54 | +// |
| 55 | +//| .. method:: read(nbytes=None) |
| 56 | +//| |
| 57 | +//| Read characters. If ``nbytes`` is specified then read at most that many |
| 58 | +//| bytes. Otherwise, read everything that arrives until the connection |
| 59 | +//| times out. Providing the number of bytes expected is highly recommended |
| 60 | +//| because it will be faster. |
| 61 | +//| |
| 62 | +//| :return: Data read |
| 63 | +//| :rtype: bytes or None |
| 64 | +//| |
| 65 | +//| .. method:: readinto(buf, nbytes=None) |
| 66 | +//| |
| 67 | +//| Read bytes into the ``buf``. If ``nbytes`` is specified then read at most |
| 68 | +//| that many bytes. Otherwise, read at most ``len(buf)`` bytes. |
| 69 | +//| |
| 70 | +//| :return: number of bytes read and stored into ``buf`` |
| 71 | +//| :rtype: bytes or None |
| 72 | +//| |
| 73 | + |
| 74 | +// These three methods are used by the shared stream methods. |
| 75 | +STATIC mp_uint_t usb_midi_portin_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { |
| 76 | + usb_midi_portin_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 77 | + byte *buf = buf_in; |
| 78 | + |
| 79 | + // make sure we want at least 1 char |
| 80 | + if (size == 0) { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + return common_hal_usb_midi_portin_read(self, buf, size, errcode); |
| 85 | +} |
| 86 | + |
| 87 | +STATIC mp_uint_t usb_midi_portin_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { |
| 88 | + usb_midi_portin_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 89 | + mp_uint_t ret; |
| 90 | + if (request == MP_IOCTL_POLL) { |
| 91 | + mp_uint_t flags = arg; |
| 92 | + ret = 0; |
| 93 | + if ((flags & MP_IOCTL_POLL_RD) && common_hal_usb_midi_portin_bytes_available(self) > 0) { |
| 94 | + ret |= MP_IOCTL_POLL_RD; |
| 95 | + } |
| 96 | + } else { |
| 97 | + *errcode = MP_EINVAL; |
| 98 | + ret = MP_STREAM_ERROR; |
| 99 | + } |
| 100 | + return ret; |
| 101 | +} |
| 102 | + |
| 103 | +STATIC const mp_rom_map_elem_t usb_midi_portin_locals_dict_table[] = { |
| 104 | + // Standard stream methods. |
| 105 | + { MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, |
| 106 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, |
| 107 | +}; |
| 108 | +STATIC MP_DEFINE_CONST_DICT(usb_midi_portin_locals_dict, usb_midi_portin_locals_dict_table); |
| 109 | + |
| 110 | +STATIC const mp_stream_p_t usb_midi_portin_stream_p = { |
| 111 | + .read = usb_midi_portin_read, |
| 112 | + .write = NULL, |
| 113 | + .ioctl = usb_midi_portin_ioctl, |
| 114 | + .is_text = false, |
| 115 | +}; |
| 116 | + |
| 117 | +const mp_obj_type_t usb_midi_portin_type = { |
| 118 | + { &mp_type_type }, |
| 119 | + .name = MP_QSTR_PortIn, |
| 120 | + .make_new = usb_midi_portin_make_new, |
| 121 | + .getiter = mp_identity_getiter, |
| 122 | + .iternext = mp_stream_unbuffered_iter, |
| 123 | + .protocol = &usb_midi_portin_stream_p, |
| 124 | + .locals_dict = (mp_obj_dict_t*)&usb_midi_portin_locals_dict, |
| 125 | +}; |
0 commit comments