Skip to content

Commit fb0970e

Browse files
committed
Add terminalio
1 parent 73cf490 commit fb0970e

13 files changed

Lines changed: 475 additions & 1 deletion

File tree

ports/atmel-samd/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ SRC_SHARED_MODULE = \
399399
os/__init__.c \
400400
random/__init__.c \
401401
struct/__init__.c \
402+
terminalio/__init__.c \
403+
terminalio/Terminal.c \
402404
uheap/__init__.c \
403405
ustack/__init__.c \
404406
usb_hid/__init__.c \

ports/atmel-samd/mpconfigport.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ extern const struct _mp_obj_module_t ustack_module;
235235
extern const struct _mp_obj_module_t supervisor_module;
236236
extern const struct _mp_obj_module_t gamepad_module;
237237
extern const struct _mp_obj_module_t stage_module;
238+
extern const struct _mp_obj_module_t terminalio_module;
238239
extern const struct _mp_obj_module_t touchio_module;
239240
extern const struct _mp_obj_module_t usb_hid_module;
240241
extern const struct _mp_obj_module_t usb_midi_module;
@@ -286,7 +287,8 @@ extern const struct _mp_obj_module_t pixelbuf_module;
286287
#if !defined(CIRCUITPY_DISPLAYIO) || CIRCUITPY_DISPLAYIO
287288
#define CIRCUITPY_DISPLAYIO (1)
288289
#define CIRCUITPY_DISPLAY_LIMIT (3)
289-
#define DISPLAYIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_displayio), (mp_obj_t)&displayio_module },
290+
#define DISPLAYIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_displayio), (mp_obj_t)&displayio_module }, \
291+
{ MP_OBJ_NEW_QSTR(MP_QSTR_terminalio), (mp_obj_t)&terminalio_module },
290292
#else
291293
#define CIRCUITPY_DISPLAYIO (0)
292294
#define CIRCUITPY_DISPLAY_LIMIT (0)

shared-bindings/displayio/TileGrid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ void common_hal_displayio_tilegrid_set_position(displayio_tilegrid_t *self, int1
4141
mp_obj_t common_hal_displayio_tilegrid_get_pixel_shader(displayio_tilegrid_t *self);
4242
void common_hal_displayio_tilegrid_set_pixel_shader(displayio_tilegrid_t *self, mp_obj_t pixel_shader);
4343

44+
void common_hal_displayio_textgrid_set_tile(displayio_tilegrid_t *self, uint16_t x, uint16_t y, uint8_t tile_index);
45+
4446
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_TILEGRID_H
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_TERMINALIO_TERMINAL_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_TERMINALIO_TERMINAL_H
29+
30+
#include "shared-module/terminalio/Terminal.h"
31+
32+
#include "shared-bindings/displayio/TileGrid.h"
33+
34+
extern const mp_obj_type_t terminalio_terminal_type;
35+
36+
extern void common_hal_terminalio_terminal_construct(terminalio_terminal_obj_t *self,
37+
displayio_tilegrid_t* tilegrid, const byte* unicode_characters, size_t unicode_characters_len);
38+
39+
// Write characters. len is in characters NOT bytes!
40+
extern size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self,
41+
const uint8_t *data, size_t len, int *errcode);
42+
43+
extern bool common_hal_terminalio_terminal_ready_to_tx(terminalio_terminal_obj_t *self);
44+
45+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_TERMINALIO_TERMINAL_H
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* This file is part of the MicroPython 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 "py/obj.h"
30+
#include "py/runtime.h"
31+
32+
#include "shared-bindings/terminalio/__init__.h"
33+
#include "shared-bindings/terminalio/Terminal.h"
34+
35+
#include "py/runtime.h"
36+
37+
//| :mod:`terminalio` --- MIDI over USB
38+
//| =================================================
39+
//|
40+
//| .. module:: terminalio
41+
//| :synopsis: MIDI over USB
42+
//|
43+
//| The `terminalio` module contains classes to transmit and receive MIDI messages over USB
44+
//|
45+
//| Libraries
46+
//|
47+
//| .. toctree::
48+
//| :maxdepth: 3
49+
//|
50+
//| Terminal
51+
//|
52+
//|
53+
STATIC const mp_rom_map_elem_t terminalio_module_globals_table[] = {
54+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_terminalio) },
55+
{ MP_ROM_QSTR(MP_QSTR_Terminal), MP_OBJ_FROM_PTR(&terminalio_terminal_type) },
56+
};
57+
58+
59+
STATIC MP_DEFINE_CONST_DICT(terminalio_module_globals, terminalio_module_globals_table);
60+
61+
const mp_obj_module_t terminalio_module = {
62+
.base = { &mp_type_module },
63+
.globals = (mp_obj_dict_t*)&terminalio_module_globals,
64+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft
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_SHARED_BINDINGS_USB_MIDI___INIT___H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_USB_MIDI___INIT___H
29+
30+
#include "py/obj.h"
31+
32+
extern mp_obj_dict_t usb_midi_module_globals;
33+
34+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_MIDI___INIT___H

shared-module/displayio/TileGrid.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void common_hal_displayio_tilegrid_construct(displayio_tilegrid_t *self, mp_obj_
4545
self->inline_tiles = false;
4646
}
4747
self->width_in_tiles = width;
48+
self->height_in_tiles = width;
4849
self->total_width = width * tile_width;
4950
self->total_height = height * tile_height;
5051
self->tile_width = tile_width;
@@ -112,6 +113,14 @@ bool displayio_tilegrid_get_pixel(displayio_tilegrid_t *self, int16_t x, int16_t
112113
return false;
113114
}
114115

116+
void common_hal_displayio_textgrid_set_tile(displayio_tilegrid_t *self, uint16_t x, uint16_t y, uint8_t tile_index) {
117+
uint8_t* tiles = self->tiles;
118+
if (self->inline_tiles) {
119+
tiles = (uint8_t*) &self->tiles;
120+
}
121+
tiles[y * self->width_in_tiles + x] = tile_index;
122+
}
123+
115124
bool displayio_tilegrid_needs_refresh(displayio_tilegrid_t *self) {
116125
return self->needs_refresh || displayio_palette_needs_refresh(self->pixel_shader);
117126
}

shared-module/displayio/TileGrid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef struct {
3939
uint16_t x;
4040
uint16_t y;
4141
uint16_t width_in_tiles;
42+
uint16_t height_in_tiles;
4243
uint16_t total_width;
4344
uint16_t total_height;
4445
uint16_t tile_width;

0 commit comments

Comments
 (0)