|
| 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/terminalio/Terminal.h" |
| 30 | +#include "shared-bindings/util.h" |
| 31 | + |
| 32 | +#include "py/ioctl.h" |
| 33 | +#include "py/objproperty.h" |
| 34 | +#include "py/objstr.h" |
| 35 | +#include "py/runtime.h" |
| 36 | +#include "py/stream.h" |
| 37 | +#include "supervisor/shared/translate.h" |
| 38 | + |
| 39 | + |
| 40 | +//| .. currentmodule:: terminalio |
| 41 | +//| |
| 42 | +//| :class:`Terminal` -- manage a |
| 43 | +//| ============================================================== |
| 44 | +//| |
| 45 | +//| .. class:: Terminal(tilegrid, *, unicode_characters="") |
| 46 | +//| |
| 47 | +//| Terminal manages tile indices and cursor position based on VT100 commands. Visible ASCII |
| 48 | +//| characters are mapped to the first 94 tile indices by substracting 0x20 from characters value. |
| 49 | +//| Unicode characters are mapped based on unicode_characters starting at index 94. |
| 50 | +//| |
| 51 | + |
| 52 | +STATIC mp_obj_t terminalio_terminal_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 53 | + enum { ARG_tilegrid, ARG_unicode_characters }; |
| 54 | + static const mp_arg_t allowed_args[] = { |
| 55 | + { MP_QSTR_tilegrid, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 56 | + { MP_QSTR_unicode_characters, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} }, |
| 57 | + }; |
| 58 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 59 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 60 | + |
| 61 | + mp_obj_t tilegrid = args[ARG_tilegrid].u_obj; |
| 62 | + if (!MP_OBJ_IS_TYPE(tilegrid, &displayio_tilegrid_type)) { |
| 63 | + mp_raise_TypeError_varg(translate("Expected a %q"), displayio_tilegrid_type.name); |
| 64 | + } |
| 65 | + |
| 66 | + mp_obj_t unicode_characters_obj = args[ARG_unicode_characters].u_obj; |
| 67 | + if (MP_OBJ_IS_STR(unicode_characters_obj)) { |
| 68 | + mp_raise_TypeError(translate("unicode_characters must be a string")); |
| 69 | + } |
| 70 | + |
| 71 | + GET_STR_DATA_LEN(unicode_characters_obj, unicode_characters, unicode_characters_len); |
| 72 | + terminalio_terminal_obj_t *self = m_new_obj(terminalio_terminal_obj_t); |
| 73 | + self->base.type = &terminalio_terminal_type; |
| 74 | + common_hal_terminalio_terminal_construct(self, MP_OBJ_TO_PTR(tilegrid), unicode_characters, unicode_characters_len); |
| 75 | + return MP_OBJ_FROM_PTR(self); |
| 76 | +} |
| 77 | + |
| 78 | +// These are standard stream methods. Code is in py/stream.c. |
| 79 | +// |
| 80 | +//| .. method:: write(buf) |
| 81 | +//| |
| 82 | +//| Write the buffer of bytes to the bus. |
| 83 | +//| |
| 84 | +//| :return: the number of bytes written |
| 85 | +//| :rtype: int or None |
| 86 | +//| |
| 87 | +STATIC mp_uint_t terminalio_terminal_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { |
| 88 | + terminalio_terminal_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 89 | + const byte *buf = buf_in; |
| 90 | + |
| 91 | + return common_hal_terminalio_terminal_write(self, buf, size, errcode); |
| 92 | +} |
| 93 | + |
| 94 | +STATIC mp_uint_t terminalio_terminal_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { |
| 95 | + terminalio_terminal_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 96 | + mp_uint_t ret; |
| 97 | + if (request == MP_IOCTL_POLL) { |
| 98 | + mp_uint_t flags = arg; |
| 99 | + ret = 0; |
| 100 | + if ((flags & MP_IOCTL_POLL_WR) && common_hal_terminalio_terminal_ready_to_tx(self)) { |
| 101 | + ret |= MP_IOCTL_POLL_WR; |
| 102 | + } |
| 103 | + } else { |
| 104 | + *errcode = MP_EINVAL; |
| 105 | + ret = MP_STREAM_ERROR; |
| 106 | + } |
| 107 | + return ret; |
| 108 | +} |
| 109 | + |
| 110 | +STATIC const mp_rom_map_elem_t terminalio_terminal_locals_dict_table[] = { |
| 111 | + // Standard stream methods. |
| 112 | + { MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, |
| 113 | +}; |
| 114 | +STATIC MP_DEFINE_CONST_DICT(terminalio_terminal_locals_dict, terminalio_terminal_locals_dict_table); |
| 115 | + |
| 116 | +STATIC const mp_stream_p_t terminalio_terminal_stream_p = { |
| 117 | + .read = NULL, |
| 118 | + .write = terminalio_terminal_write, |
| 119 | + .ioctl = terminalio_terminal_ioctl, |
| 120 | + .is_text = true, |
| 121 | +}; |
| 122 | + |
| 123 | +const mp_obj_type_t terminalio_terminal_type = { |
| 124 | + { &mp_type_type }, |
| 125 | + .name = MP_QSTR_Terminal, |
| 126 | + .make_new = terminalio_terminal_make_new, |
| 127 | + .getiter = mp_identity_getiter, |
| 128 | + .iternext = mp_stream_unbuffered_iter, |
| 129 | + .protocol = &terminalio_terminal_stream_p, |
| 130 | + .locals_dict = (mp_obj_dict_t*)&terminalio_terminal_locals_dict, |
| 131 | +}; |
0 commit comments