Skip to content

Commit 187bcae

Browse files
authored
Merge pull request #2 from tannewt/subclass_group
Subclass Group and use new accessors.
2 parents 2ff5676 + 5fc08a9 commit 187bcae

File tree

7 files changed

+79
-97
lines changed

7 files changed

+79
-97
lines changed

.travis.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,28 @@ cache:
1212
pip: true
1313

1414
deploy:
15-
provider: releases
16-
api_key: $GITHUB_TOKEN
17-
file_glob: true
18-
file: $TRAVIS_BUILD_DIR/bundles/*
19-
skip_cleanup: true
20-
overwrite: true
21-
on:
22-
tags: true
15+
- provider: releases
16+
api_key: "$GITHUB_TOKEN"
17+
file_glob: true
18+
file: "$TRAVIS_BUILD_DIR/bundles/*"
19+
skip_cleanup: true
20+
overwrite: true
21+
on:
22+
tags: true
23+
- provider: pypi
24+
user: adafruit-travis
25+
password:
26+
secure: "P8vHzlHSkY7aLsGMD12+43hG4eHcxjqFWaKYhvtoUlbTjOtHD+ZlzFG8rSXooUEdJZWYy9SDFShGfNMvws1I5opNFV0HA2Cs14jBeYa9hFi0p/7Box0tAHZbi6aGGcho/WbwQqUgaOkH0QJnsYgZedu65S1SXVkl7zfcORMRw3dXBM5UY5Q7KP8X/62CC78P1dPC/2127vyn98qHVEiT7rtCkBgXUM3WuPz2VadaLodvAD/E0xwqJXqZmDIM2stc223m9n9plXPahUHl9grT9oH8KhJP9Wr6uaRjAOhbFKimKFWTEcc1ugHjH8U+UFRk+OVTDNf5etgiQMs82x2Ssfoz+yi6z/HMQN2uc9TM4kAGrPUZIcZTVaniCtGSfa7HZSj60uxDkTMKuOt5B+ey5bGjpRAsNxTRJtgT8taIWURO4YU9Il3orPr9ByIp1OqjRmHwGF0PXK+U5G6UEP+JP9R8OvS9Q71nOrDZmFAD0krw9ZxO4p4T93bgV4ELkoI8RLbudqh68FG6XYwg2hr+VPT/9eLuYeOGdHB7vNAc1qumOX+e3bbC0BdpexxYyBtgaWsbGZEP9F2uYi20wUWGnM1agFXpua1JiWAqKf14ML5ysOTsyWKJ8+IXNtQIyY9z9gD5TCiNgkyiHbDEg2JA2ATOuWz42UzhdxntXBCuV00="
27+
on:
28+
tags: true
2329

2430
install:
2531
- pip install -r requirements.txt
2632
- pip install circuitpython-build-tools Sphinx sphinx-rtd-theme
2733
- pip install --force-reinstall pylint==1.9.2
2834

2935
script:
30-
- pylint adafruit_display_text.py
36+
- pylint adafruit_display_text/*.py
3137
- ([[ ! -d "examples" ]] || pylint --disable=missing-docstring,invalid-name,bad-whitespace examples/*.py)
3238
- circuitpython-build-bundles --filename_prefix adafruit-circuitpython-display_text --library_location .
3339
- cd docs && sphinx-build -E -W -b html . _build/html && cd ..

README.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,17 @@ This is easily achieved by downloading
2828
Usage Example
2929
=============
3030

31+
For a board with a built-in display.
32+
3133
.. code:: python
3234
35+
import board
36+
import terminalio
37+
from adafruit_display_text import text_area
38+
39+
text = "Hello world"
40+
text_area = text_area.TextArea(terminalio.FONT, text=text, width=len(text))
41+
board.DISPLAY.show(text_area)
3342
3443
3544
Contributing

adafruit_display_text/text_area.py

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_display_text`
23+
`adafruit_display_text.text_area`
2424
====================================================
2525
2626
Displays text using CircuitPython's displayio.
@@ -44,31 +44,31 @@
4444
__version__ = "0.0.0-auto.0"
4545
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
4646

47+
class TextArea(displayio.Group):
48+
"""An area displaying a string of textself.
4749
48-
class TextArea:
49-
def __init__(self, font, *, text=None, width=None, color=0x0, height=1):
50+
:param Font font: A font class that has ``get_bounding_box`` and ``get_glyph``
51+
:param str text: Text to display
52+
:param int width: Area width in characters
53+
:param int height: Area height in characters
54+
:param int color: Color of all text in RGB hex"""
55+
def __init__(self, font, *, text=None, width=None, height=1, color=0xffffff):
5056
if not width and not text:
5157
raise RuntimeError("Please provide a width")
5258
if not width:
5359
width = len(text)
60+
super().__init__(max_size=width * height)
5461
self.width = width
5562
self.font = font
5663
self._text = None
5764

58-
self.p = displayio.Palette(2)
59-
self.p.make_transparent(0)
60-
self.p[1] = color
61-
62-
self.group = displayio.Group(max_size=width * height)
65+
self.palette = displayio.Palette(2)
66+
self.palette.make_transparent(0)
67+
self.palette[1] = color
6368

6469
bounds = self.font.get_bounding_box()
6570
self.height = bounds[1]
6671

67-
self.sprites = [None] * (width * height)
68-
69-
self._x = 0
70-
self._y = 0
71-
7272
if text:
7373
self._update_text(text)
7474

@@ -77,79 +77,56 @@ def _update_text(self, new_text):
7777
x = 0
7878
y = 0
7979
i = 0
80-
first_different = self._text is not None
81-
for c in new_text:
82-
if chr(ord(c)) == '\n':
80+
old_c = 0
81+
for character in new_text:
82+
if character == '\n':
8383
y += int(self.height * 1.25)
8484
x = 0
8585
continue
86-
glyph = self.font.get_glyph(ord(c))
86+
glyph = self.font.get_glyph(ord(character))
8787
if not glyph:
8888
continue
89-
# Remove any characters that are different
90-
if first_different and c != self._text[i]:
91-
# TODO(tannewt): Make this smarter when we can remove and add things into the middle
92-
# of a group.
93-
for _ in range(len(self.sprites) - i):
94-
try:
95-
self.group.pop()
96-
except IndexError:
97-
break
98-
first_different = False
99-
if not first_different:
100-
position = (self._x + x, self._y + y + self.height - glyph["bounds"][1] - glyph["bounds"][3])
101-
try:
102-
face = displayio.TileGrid(glyph["bitmap"], pixel_shader=self.p, position=position)
103-
except:
104-
face = displayio.Sprite(glyph["bitmap"], pixel_shader=self.p, position=position)
105-
self.group.append(face)
106-
self.sprites[i] = face
107-
x += glyph["shift"][0]
89+
position_y = y + self.height - glyph.height - glyph.dy
90+
if not self._text or old_c >= len(self._text) or character != self._text[old_c]:
91+
face = displayio.TileGrid(glyph.bitmap, pixel_shader=self.palette,
92+
default_tile=glyph.tile_index,
93+
tile_width=glyph.width, tile_height=glyph.height,
94+
position=(x, position_y))
95+
if i < len(self):
96+
self[i] = face
97+
else:
98+
self.append(face)
99+
elif self._text and character == self._text[old_c]:
100+
self[i].position = (x, position_y)
101+
102+
x += glyph.shift_x
108103

109104
# TODO skip this for control sequences or non-printables.
110105
i += 1
111-
112-
# TODO: support multiple lines by adjusting y
106+
old_c += 1
107+
# skip all non-prinables in the old string
108+
while (self._text and old_c < len(self._text) and
109+
(self._text[old_c] == '\n' or not self.font.get_glyph(ord(self._text[old_c])))):
110+
old_c += 1
111+
# Remove the rest
112+
while len(self) > i:
113+
self.pop()
113114
self._text = new_text
114115

115116
@property
116117
def color(self):
117-
return self.p[1]
118+
"""Color of the text as an RGB hex number."""
119+
return self.palette[1]
118120

119121
@color.setter
120-
def color(self, c):
121-
self.p[1] = c
122+
def color(self, new_color):
123+
self.palette[1] = new_color
122124

123125
@property
124-
def text(self, t):
125-
self._text = t
126+
def text(self):
127+
"""Text to display."""
128+
return self._text
126129

127130
@text.setter
128-
def text(self, t):
129-
self._update_text(t)
130-
131-
@property
132-
def y(self):
133-
return self._y
134-
135-
@y.setter
136-
def y(self, new_y):
137-
for sprite in self.sprites:
138-
if not sprite:
139-
continue
140-
pos = sprite.position
141-
sprite.position = (pos[0], (pos[1] - self._y) + new_y)
142-
self._y = new_y
143-
144-
@property
145-
def x(self):
146-
return self._x
147-
148-
@x.setter
149-
def x(self, new_x):
150-
for sprite in self.sprites:
151-
if not sprite:
152-
continue
153-
pos = sprite.position
154-
sprite.position = ((pos[0] - self._x) + new_x, pos[1])
155-
self._x = new_x
131+
def text(self, new_text):
132+
self._update_text(new_text)

docs/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
55
.. use this format as the module name: "adafruit_foo.foo"
66
7-
.. automodule:: adafruit_display_text
7+
.. automodule:: adafruit_display_text.text_area
88
:members:

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# Uncomment the below if you use native CircuitPython modules such as
2121
# digitalio, micropython and busio. List the modules you use. Without it, the
2222
# autodoc module docs will fail to generate with a warning.
23-
# autodoc_mock_imports = ["digitalio", "busio"]
23+
autodoc_mock_imports = ["displayio"]
2424

2525

2626
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

docs/index.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,9 @@ Table of Contents
2323
.. toctree::
2424
:caption: Tutorials
2525

26-
.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave
27-
the toctree above for use later.
28-
2926
.. toctree::
3027
:caption: Related Products
3128

32-
.. todo:: Add any product links here. If there are none, then simply delete this todo and leave
33-
the toctree above for use later.
34-
3529
.. toctree::
3630
:caption: Other Links
3731

examples/pyportal.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import board
2-
import displayio
31
import os
4-
import gc
5-
import pulseio
6-
import random
72
import time
3+
import board
4+
import pulseio
85
import microcontroller
6+
import displayio
97

108
from adafruit_bitmap_font import bitmap_font
119
from adafruit_display_text.text_area import TextArea
1210

13-
backlight = pulseio.PWMOut(microcontroller.pin.PB21)
11+
backlight = pulseio.PWMOut(microcontroller.pin.PB21) #pylint: disable=no-member
1412

1513
max_brightness = 2 ** 15
1614

@@ -35,14 +33,12 @@
3533
print("Font load {}".format(font.name))
3634
area = TextArea(font, text=demo_text)
3735
area.y = y
38-
splash.append(area.group)
36+
splash.append(area)
3937

4038
y += area.height
4139

4240
# Wait for the image to load.
4341
board.DISPLAY.wait_for_frame()
44-
gc.collect()
45-
print("mem free:", gc.mem_free())
4642

4743
# Wait forever
4844
time.sleep(600)

0 commit comments

Comments
 (0)