Skip to content

Commit f9264dd

Browse files
authored
Merge pull request #23 from dherrada/master
Made #14 pass
2 parents 97e487d + 28cd1c2 commit f9264dd

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

adafruit_framebuf.py

100644100755
Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,26 @@ def scroll(self, delta_x, delta_y):
305305
x += dt_x
306306
y += dt_y
307307

308+
# pylint: disable=too-many-arguments
308309
def text(self, string, x, y, color, *,
309-
font_name="font5x8.bin"):
310-
"""text is not yet implemented"""
311-
if not self._font or self._font.font_name != font_name:
312-
# load the font!
313-
self._font = BitmapFont()
314-
w = self._font.font_width
315-
for i, char in enumerate(string):
316-
self._font.draw_char(char,
317-
x + (i * (w + 1)),
318-
y, self, color)
310+
font_name="font5x8.bin", size=1):
311+
"""Place text on the screen in variables sizes. Breaks on \n to next line.
312+
313+
Does not break on line going off screen.
314+
"""
315+
for chunk in string.split('\n'):
316+
if not self._font or self._font.font_name != font_name:
317+
# load the font!
318+
self._font = BitmapFont()
319+
w = self._font.font_width
320+
for i, char in enumerate(chunk):
321+
self._font.draw_char(char,
322+
x + (i * (w + 1))*size,
323+
y, self, color, size=size)
324+
y += self._font.font_height*size
325+
# pylint: enable=too-many-arguments
326+
327+
319328

320329
def image(self, img):
321330
"""Set buffer to value of Python Imaging Library image. The image should
@@ -382,9 +391,9 @@ def __exit__(self, exception_type, exception_value, traceback):
382391
"""cleanup on exit"""
383392
self.deinit()
384393

385-
def draw_char(self, char, x, y, framebuffer, color):
386-
# pylint: disable=too-many-arguments
394+
def draw_char(self, char, x, y, framebuffer, color, size=1): # pylint: disable=too-many-arguments
387395
"""Draw one character at position (x,y) to a framebuffer in a given color"""
396+
size = max(size, 1)
388397
# Don't draw the character if it will be clipped off the visible area.
389398
#if x < -self.font_width or x >= framebuffer.width or \
390399
# y < -self.font_height or y >= framebuffer.height:
@@ -401,7 +410,7 @@ def draw_char(self, char, x, y, framebuffer, color):
401410
for char_y in range(self.font_height):
402411
# Draw a pixel for each bit that's flipped on.
403412
if (line >> char_y) & 0x1:
404-
framebuffer.pixel(x + char_x, y + char_y, color)
413+
framebuffer.fill_rect(x + char_x*size, y + char_y*size, size, size, color)
405414

406415
def width(self, text):
407416
"""Return the pixel width of the specified text message."""

examples/framebuf_simpletest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def print_buffer(the_fb):
2222
print(".")
2323
print("." * (the_fb.width+2))
2424

25+
# Small function to clear the buffer
26+
def clear_buffer():
27+
for i, _ in enumerate(buffer):
28+
buffer[i] = 0
29+
30+
2531
print("Shapes test: ")
2632
fb.pixel(3, 5, True)
2733
fb.rect(0, 0, fb.width, fb.height, True)
@@ -32,6 +38,12 @@ def print_buffer(the_fb):
3238
print("Text test: ")
3339
# empty
3440
fb.fill_rect(0, 0, WIDTH, HEIGHT, False)
41+
3542
# write some text
3643
fb.text("hello", 0, 0, True)
3744
print_buffer(fb)
45+
clear_buffer()
46+
47+
# write some larger text
48+
fb.text("hello", 8, 0, True, size = 2)
49+
print_buffer(fb)

0 commit comments

Comments
 (0)