Skip to content

Business card update #3

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 6 commits into from
Jul 6, 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
34 changes: 18 additions & 16 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,24 @@ To install in a virtual environment in your current project:
Usage Example
=============

from adafruit_pybadger import PyBadger

pybadger = PyBadger()

while True:
pybadger.show_badge(hello_scale=2, my_name_is_scale=2, name_scale=3)
pybadger.auto_dim_display()

if pybadger.button.a:
pybadger.show_business_card(image_name="Blinka.bmp")
elif pybadger.button.b:
print("b B")
elif pybadger.button.start:
print("b start")
elif pybadger.button.select:
pybadger.show_qr_code()
.. code-block:: python

from adafruit_pybadger import PyBadger

pybadger = PyBadger()

pybadger.show_badge(name_string="Blinka", hello_scale=2, my_name_is_scale=2, name_scale=3)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend moving show_badge and auto_dim out of the loop and then allowing something like pressing the start button to switch back to badge mode.


while True:
pybadger.auto_dim_display(delay=10)
if pybadger.button.a:
pybadger.show_business_card(image_name="Blinka.bmp", name_string="Blinka", name_scale=2,
email_string_one="blinka@", email_string_two="adafruit.com")
elif pybadger.button.b:
pybadger.show_qr_code(data="https://circuitpython.org")
elif pybadger.button.start:
pybadger.show_badge(name_string="Blinka", hello_scale=2, my_name_is_scale=2, name_scale=3)


Contributing
============
Expand Down
71 changes: 55 additions & 16 deletions adafruit_pybadger.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,31 @@ def brightness(self):
def brightness(self, value):
self.display.brightness = value

def show_business_card(self, image_name=None, dwell=20):
# pylint: disable=too-many-locals
def show_business_card(self, *, image_name=None, name_string=None, name_scale=1,
name_font=terminalio.FONT, email_string_one=None,
email_scale_one=1, email_font_one=terminalio.FONT,
email_string_two=None, email_scale_two=1,
email_font_two=terminalio.FONT):
"""Display a bitmap image and a text string, such as a personal image and email address.
CURRENTLY ONLY DISPLAYS BITMAP IMAGE. Text string to be added.

:param str image_name: The name of the bitmap image including .bmp, e.g. ``"Blinka.bmp"``.
:param int dwell: The amount of time in seconds to display the business card.
:param str image_name: REQUIRED. The name of the bitmap image including .bmp, e.g.
``"Blinka.bmp"``.
:param str name_string: A name string to display along the bottom of the display, e.g.
``"Blinka"``.
:param int name_scale: The scale of ``name_string``. Defaults to 1.
:param name_font: The font for the name string. Defaults to ``terminalio.FONT``.
:param str email_string_one: A string to display along the bottom of the display, e.g.
``"blinka@adafruit.com"``.
:param int email_scale_one: The scale of ``email_string_one``. Defaults to 1.
:param email_font_one: The font for the first email string. Defaults to ``terminalio.FONT``.
:param str email_string_two: A second string to display along the bottom of the display.
Use if your email address is longer than one line or to add
more space between the name and email address,
e.g. (blinka@) ``"adafruit.com"``.
:param int email_scale_two: The scale of ``email_string_two``. Defaults to 1.
:param email_font_two: The font for the second email string. Defaults to
``terminalio.FONT``.

"""
business_card_splash = displayio.Group(max_size=30)
Expand All @@ -227,9 +246,36 @@ def show_business_card(self, image_name=None, dwell=20):
on_disk_bitmap = displayio.OnDiskBitmap(file_name)
face_image = displayio.TileGrid(on_disk_bitmap, pixel_shader=displayio.ColorConverter())
business_card_splash.append(face_image)
# Wait for the image to load.
self.display.wait_for_frame()
time.sleep(dwell)
if name_string:
name_group = displayio.Group(scale=name_scale)
name_label = Label(name_font, text=name_string)
(_, _, width, height) = name_label.bounding_box
name_label.x = ((self.display.width // (2 * name_scale)) - width // 2)
name_label.y = int(height // (0.15 * name_scale))
name_label.color = 0xFFFFFF
name_group.append(name_label)
business_card_splash.append(name_group)
if email_string_one:
email_group_one = displayio.Group(scale=email_scale_one)
email_label_one = Label(email_font_one, text=email_string_one)
(_, _, width, height) = email_label_one.bounding_box
email_label_one.width = self.display.width
email_label_one.x = ((self.display.width // (2 * email_scale_one)) - width // 2)
email_label_one.y = int(height // (0.13 * email_scale_one))
email_label_one.color = 0xFFFFFF
email_group_one.append(email_label_one)
business_card_splash.append(email_group_one)
if email_string_two:
email_group_two = displayio.Group(scale=email_scale_two)
email_label_two = Label(email_font_two, text=email_string_two)
(_, _, width, height) = email_label_two.bounding_box
email_label_two.width = self.display.width
email_label_two.x = ((self.display.width // (2 * email_scale_two)) - width // 2)
email_label_two.y = int(height // (0.12 * email_scale_two))
email_label_two.color = 0xFFFFFF
email_group_two.append(email_label_two)
business_card_splash.append(email_group_two)

# pylint: disable=too-many-locals
def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF,
Expand Down Expand Up @@ -258,7 +304,6 @@ def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF,
"Blinka".

"""
# Make the Display Background
splash = displayio.Group(max_size=20)

color_bitmap = displayio.Bitmap(self.display.width, self.display.height, 1)
Expand All @@ -270,15 +315,12 @@ def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF,
x=0, y=0)
splash.append(bg_sprite)

# Draw a Foreground Rectangle where the name goes
# x, y, width, height
rect = Rect(0, (int(self.display.height * 0.4)), self.display.width,
(int(self.display.height * 0.5)), fill=foreground_color)
splash.append(rect)

hello_scale = hello_scale
hello_group = displayio.Group(scale=hello_scale)
# Setup and Center the Hello Label
hello_label = Label(font=hello_font, text=hello_string)
(_, _, width, height) = hello_label.bounding_box
hello_label.x = ((self.display.width // (2 * hello_scale)) - width // 2)
Expand All @@ -288,7 +330,6 @@ def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF,

my_name_is_scale = my_name_is_scale
my_name_is_group = displayio.Group(scale=my_name_is_scale)
# Setup and Center the "My Name Is" Label
my_name_is_label = Label(font=my_name_is_font, text=my_name_is_string)
(_, _, width, height) = my_name_is_label.bounding_box
my_name_is_label.x = ((self.display.width // (2 * my_name_is_scale)) - width // 2)
Expand All @@ -298,7 +339,6 @@ def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF,

name_scale = name_scale
name_group = displayio.Group(scale=name_scale)
# Setup and Center the Name Label
name_label = Label(font=name_font, text=name_string)
(_, _, width, height) = name_label.bounding_box
name_label.x = ((self.display.width // (2 * name_scale)) - width // 2)
Expand Down Expand Up @@ -327,15 +367,15 @@ def bitmap_qr(matrix):
bitmap[x + border_pixels, y + border_pixels] = 0
return bitmap

def show_qr_code(self, data=b'https://circuitpython.org', dwell=20):
def show_qr_code(self, *, data="https://circuitpython.org"):
"""Generate a QR code and display it for ``dwell`` seconds.

:param bytearray data: A bytearray of data for the QR code
:param string data: A string of data for the QR code
:param int dwell: The amount of time in seconds to display the QR code

"""
qr_code = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.L)
qr_code.add_data(data)
qr_code.add_data(bytearray(data))
qr_code.make()
qr_bitmap = self.bitmap_qr(qr_code.matrix)
palette = displayio.Palette(2)
Expand All @@ -350,7 +390,6 @@ def show_qr_code(self, data=b'https://circuitpython.org', dwell=20):
qr_code = displayio.Group(scale=qr_code_scale)
qr_code.append(qr_img)
self.display.show(qr_code)
time.sleep(dwell)

@staticmethod
def _sine_sample(length):
Expand Down
Binary file removed examples/Blinka.bmp
Binary file not shown.
15 changes: 7 additions & 8 deletions examples/pybadger_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

pybadger = PyBadger()

while True:
pybadger.show_badge(hello_scale=2, my_name_is_scale=2, name_scale=3)
pybadger.auto_dim_display()
pybadger.show_badge(name_string="Blinka", hello_scale=2, my_name_is_scale=2, name_scale=3)

while True:
pybadger.auto_dim_display(delay=10)
if pybadger.button.a:
pybadger.show_business_card(image_name="Blinka.bmp")
pybadger.show_business_card(image_name="Blinka.bmp", name_string="Blinka", name_scale=2,
email_string_one="blinka@", email_string_two="adafruit.com")
elif pybadger.button.b:
print("b B")
pybadger.show_qr_code(data="https://circuitpython.org")
elif pybadger.button.start:
print("b start")
elif pybadger.button.select:
pybadger.show_qr_code()
pybadger.show_badge(name_string="Blinka", hello_scale=2, my_name_is_scale=2, name_scale=3)