Skip to content

Commit cf55921

Browse files
authored
Merge pull request #3 from kattni/business-card-update
Business card update
2 parents 17950a0 + 0ac4255 commit cf55921

File tree

4 files changed

+80
-40
lines changed

4 files changed

+80
-40
lines changed

README.rst

+18-16
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,24 @@ To install in a virtual environment in your current project:
5656
Usage Example
5757
=============
5858

59-
from adafruit_pybadger import PyBadger
60-
61-
pybadger = PyBadger()
62-
63-
while True:
64-
pybadger.show_badge(hello_scale=2, my_name_is_scale=2, name_scale=3)
65-
pybadger.auto_dim_display()
66-
67-
if pybadger.button.a:
68-
pybadger.show_business_card(image_name="Blinka.bmp")
69-
elif pybadger.button.b:
70-
print("b B")
71-
elif pybadger.button.start:
72-
print("b start")
73-
elif pybadger.button.select:
74-
pybadger.show_qr_code()
59+
.. code-block:: python
60+
61+
from adafruit_pybadger import PyBadger
62+
63+
pybadger = PyBadger()
64+
65+
pybadger.show_badge(name_string="Blinka", hello_scale=2, my_name_is_scale=2, name_scale=3)
66+
67+
while True:
68+
pybadger.auto_dim_display(delay=10)
69+
if pybadger.button.a:
70+
pybadger.show_business_card(image_name="Blinka.bmp", name_string="Blinka", name_scale=2,
71+
email_string_one="blinka@", email_string_two="adafruit.com")
72+
elif pybadger.button.b:
73+
pybadger.show_qr_code(data="https://circuitpython.org")
74+
elif pybadger.button.start:
75+
pybadger.show_badge(name_string="Blinka", hello_scale=2, my_name_is_scale=2, name_scale=3)
76+
7577
7678
Contributing
7779
============

adafruit_pybadger.py

+55-16
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,31 @@ def brightness(self):
213213
def brightness(self, value):
214214
self.display.brightness = value
215215

216-
def show_business_card(self, image_name=None, dwell=20):
216+
# pylint: disable=too-many-locals
217+
def show_business_card(self, *, image_name=None, name_string=None, name_scale=1,
218+
name_font=terminalio.FONT, email_string_one=None,
219+
email_scale_one=1, email_font_one=terminalio.FONT,
220+
email_string_two=None, email_scale_two=1,
221+
email_font_two=terminalio.FONT):
217222
"""Display a bitmap image and a text string, such as a personal image and email address.
218-
CURRENTLY ONLY DISPLAYS BITMAP IMAGE. Text string to be added.
219223
220-
:param str image_name: The name of the bitmap image including .bmp, e.g. ``"Blinka.bmp"``.
221-
:param int dwell: The amount of time in seconds to display the business card.
224+
:param str image_name: REQUIRED. The name of the bitmap image including .bmp, e.g.
225+
``"Blinka.bmp"``.
226+
:param str name_string: A name string to display along the bottom of the display, e.g.
227+
``"Blinka"``.
228+
:param int name_scale: The scale of ``name_string``. Defaults to 1.
229+
:param name_font: The font for the name string. Defaults to ``terminalio.FONT``.
230+
:param str email_string_one: A string to display along the bottom of the display, e.g.
231+
232+
:param int email_scale_one: The scale of ``email_string_one``. Defaults to 1.
233+
:param email_font_one: The font for the first email string. Defaults to ``terminalio.FONT``.
234+
:param str email_string_two: A second string to display along the bottom of the display.
235+
Use if your email address is longer than one line or to add
236+
more space between the name and email address,
237+
e.g. (blinka@) ``"adafruit.com"``.
238+
:param int email_scale_two: The scale of ``email_string_two``. Defaults to 1.
239+
:param email_font_two: The font for the second email string. Defaults to
240+
``terminalio.FONT``.
222241
223242
"""
224243
business_card_splash = displayio.Group(max_size=30)
@@ -227,9 +246,36 @@ def show_business_card(self, image_name=None, dwell=20):
227246
on_disk_bitmap = displayio.OnDiskBitmap(file_name)
228247
face_image = displayio.TileGrid(on_disk_bitmap, pixel_shader=displayio.ColorConverter())
229248
business_card_splash.append(face_image)
230-
# Wait for the image to load.
231249
self.display.wait_for_frame()
232-
time.sleep(dwell)
250+
if name_string:
251+
name_group = displayio.Group(scale=name_scale)
252+
name_label = Label(name_font, text=name_string)
253+
(_, _, width, height) = name_label.bounding_box
254+
name_label.x = ((self.display.width // (2 * name_scale)) - width // 2)
255+
name_label.y = int(height // (0.15 * name_scale))
256+
name_label.color = 0xFFFFFF
257+
name_group.append(name_label)
258+
business_card_splash.append(name_group)
259+
if email_string_one:
260+
email_group_one = displayio.Group(scale=email_scale_one)
261+
email_label_one = Label(email_font_one, text=email_string_one)
262+
(_, _, width, height) = email_label_one.bounding_box
263+
email_label_one.width = self.display.width
264+
email_label_one.x = ((self.display.width // (2 * email_scale_one)) - width // 2)
265+
email_label_one.y = int(height // (0.13 * email_scale_one))
266+
email_label_one.color = 0xFFFFFF
267+
email_group_one.append(email_label_one)
268+
business_card_splash.append(email_group_one)
269+
if email_string_two:
270+
email_group_two = displayio.Group(scale=email_scale_two)
271+
email_label_two = Label(email_font_two, text=email_string_two)
272+
(_, _, width, height) = email_label_two.bounding_box
273+
email_label_two.width = self.display.width
274+
email_label_two.x = ((self.display.width // (2 * email_scale_two)) - width // 2)
275+
email_label_two.y = int(height // (0.12 * email_scale_two))
276+
email_label_two.color = 0xFFFFFF
277+
email_group_two.append(email_label_two)
278+
business_card_splash.append(email_group_two)
233279

234280
# pylint: disable=too-many-locals
235281
def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF,
@@ -258,7 +304,6 @@ def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF,
258304
"Blinka".
259305
260306
"""
261-
# Make the Display Background
262307
splash = displayio.Group(max_size=20)
263308

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

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

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

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

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

330-
def show_qr_code(self, data=b'https://circuitpython.org', dwell=20):
370+
def show_qr_code(self, *, data="https://circuitpython.org"):
331371
"""Generate a QR code and display it for ``dwell`` seconds.
332372
333-
:param bytearray data: A bytearray of data for the QR code
373+
:param string data: A string of data for the QR code
334374
:param int dwell: The amount of time in seconds to display the QR code
335375
336376
"""
337377
qr_code = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.L)
338-
qr_code.add_data(data)
378+
qr_code.add_data(bytearray(data))
339379
qr_code.make()
340380
qr_bitmap = self.bitmap_qr(qr_code.matrix)
341381
palette = displayio.Palette(2)
@@ -350,7 +390,6 @@ def show_qr_code(self, data=b'https://circuitpython.org', dwell=20):
350390
qr_code = displayio.Group(scale=qr_code_scale)
351391
qr_code.append(qr_img)
352392
self.display.show(qr_code)
353-
time.sleep(dwell)
354393

355394
@staticmethod
356395
def _sine_sample(length):

examples/Blinka.bmp

-60.1 KB
Binary file not shown.

examples/pybadger_simpletest.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
pybadger = PyBadger()
44

5-
while True:
6-
pybadger.show_badge(hello_scale=2, my_name_is_scale=2, name_scale=3)
7-
pybadger.auto_dim_display()
5+
pybadger.show_badge(name_string="Blinka", hello_scale=2, my_name_is_scale=2, name_scale=3)
86

7+
while True:
8+
pybadger.auto_dim_display(delay=10)
99
if pybadger.button.a:
10-
pybadger.show_business_card(image_name="Blinka.bmp")
10+
pybadger.show_business_card(image_name="Blinka.bmp", name_string="Blinka", name_scale=2,
11+
email_string_one="blinka@", email_string_two="adafruit.com")
1112
elif pybadger.button.b:
12-
print("b B")
13+
pybadger.show_qr_code(data="https://circuitpython.org")
1314
elif pybadger.button.start:
14-
print("b start")
15-
elif pybadger.button.select:
16-
pybadger.show_qr_code()
15+
pybadger.show_badge(name_string="Blinka", hello_scale=2, my_name_is_scale=2, name_scale=3)

0 commit comments

Comments
 (0)