Skip to content

Made #14 pass #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions adafruit_framebuf.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,26 @@ def scroll(self, delta_x, delta_y):
x += dt_x
y += dt_y

# pylint: disable=too-many-arguments
def text(self, string, x, y, color, *,
font_name="font5x8.bin"):
"""text is not yet implemented"""
if not self._font or self._font.font_name != font_name:
# load the font!
self._font = BitmapFont()
w = self._font.font_width
for i, char in enumerate(string):
self._font.draw_char(char,
x + (i * (w + 1)),
y, self, color)
font_name="font5x8.bin", size=1):
"""Place text on the screen in variables sizes. Breaks on \n to next line.

Does not break on line going off screen.
"""
for chunk in string.split('\n'):
if not self._font or self._font.font_name != font_name:
# load the font!
self._font = BitmapFont()
w = self._font.font_width
for i, char in enumerate(chunk):
self._font.draw_char(char,
x + (i * (w + 1))*size,
y, self, color, size=size)
y += self._font.font_height*size
# pylint: enable=too-many-arguments



def image(self, img):
"""Set buffer to value of Python Imaging Library image. The image should
Expand Down Expand Up @@ -352,9 +361,9 @@ def __exit__(self, exception_type, exception_value, traceback):
"""cleanup on exit"""
self.deinit()

def draw_char(self, char, x, y, framebuffer, color):
# pylint: disable=too-many-arguments
def draw_char(self, char, x, y, framebuffer, color, size=1): # pylint: disable=too-many-arguments
"""Draw one character at position (x,y) to a framebuffer in a given color"""
size = max(size, 1)
# Don't draw the character if it will be clipped off the visible area.
if x < -self.font_width or x >= framebuffer.width or \
y < -self.font_height or y >= framebuffer.height:
Expand All @@ -368,7 +377,7 @@ def draw_char(self, char, x, y, framebuffer, color):
for char_y in range(self.font_height):
# Draw a pixel for each bit that's flipped on.
if (line >> char_y) & 0x1:
framebuffer.pixel(x + char_x, y + char_y, color)
framebuffer.fill_rect(x + char_x*size, y + char_y*size, size, size, color)

def width(self, text):
"""Return the pixel width of the specified text message."""
Expand Down
12 changes: 12 additions & 0 deletions examples/framebuf_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def print_buffer(the_fb):
print(".")
print("." * (the_fb.width+2))

# Small function to clear the buffer
def clear_buffer():
for i, _ in enumerate(buffer):
buffer[i] = 0


print("Shapes test: ")
fb.pixel(3, 5, True)
fb.rect(0, 0, fb.width, fb.height, True)
Expand All @@ -32,6 +38,12 @@ def print_buffer(the_fb):
print("Text test: ")
# empty
fb.fill_rect(0, 0, WIDTH, HEIGHT, False)

# write some text
fb.text("hello", 0, 0, True)
print_buffer(fb)
clear_buffer()

# write some larger text
fb.text("hello", 8, 0, True, size = 2)
print_buffer(fb)