Now that you have everything installed, we can get started with making a custom badge for CLUE!

Save the following example code as code.py on your CIRCUITPY drive.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Custom badge example for Adafruit CLUE."""
from adafruit_pybadger import pybadger

pybadger.badge_background(
    background_color=pybadger.WHITE,
    rectangle_color=pybadger.PURPLE,
    rectangle_drop=0.2,
    rectangle_height=0.6,
)

pybadger.badge_line(
    text="@circuitpython", color=pybadger.BLINKA_PURPLE, scale=2, padding_above=2
)
pybadger.badge_line(text="Blinka", color=pybadger.WHITE, scale=5, padding_above=3)
pybadger.badge_line(
    text="CircuitPythonista", color=pybadger.WHITE, scale=2, padding_above=2
)
pybadger.badge_line(
    text="she/her", color=pybadger.BLINKA_PINK, scale=4, padding_above=4
)

pybadger.show_custom_badge()

while True:
    if pybadger.button.a:
        pybadger.show_qr_code("https://circuitpython.org")

    if pybadger.button.b:
        pybadger.show_custom_badge()

Let's take a look at the code.

First, we import the PyBadger library.

from adafruit_pybadger import pybadger

Then we create the color-block badge background.

We set the background to white. Then we create a purple rectangle that is 60% of the display in height, and is located centered vertically, with the background being 20% of the display above and below the rectangle.

pybadger.badge_background(background_color=pybadger.WHITE,
                          rectangle_color=pybadger.PURPLE,
                          rectangle_drop=0.2, rectangle_height=0.6)

Next, we'll add the lines of text. We're going to include Blinka's Twitter handle, name, job title and preferred pronoun. You can include any info you like!

pybadger.badge_line(text="@circuitpython", color=pybadger.BLINKA_PURPLE, scale=2, padding_above=2)
pybadger.badge_line(text="Blinka", color=pybadger.WHITE, scale=5, padding_above=3)
pybadger.badge_line(text="CircuitPythonista", color=pybadger.WHITE, scale=2, padding_above=2)
pybadger.badge_line(text="she/her", color=pybadger.BLINKA_PINK, scale=4, padding_above=4)

Then we call show_custom_badge() to display the badge background and badge lines we set up.

pybadger.show_custom_badge()

We call it before the loop so it displays on startup and we can display other things inside the loop.

Inside our loop, we're looking for button presses on button A and button B.

First, we check button A. If button A is pressed, we show a customisable QR code.

while True:
    if pybadger.button.a:
        pybadger.show_qr_code("https://circuitpython.org")

To change the target of the QR code, include a URL as a string (e.g. in quotation marks: "https://circuitpython.org").

Finally, we check button B. If button B is pressed, we show the badge again.

[...]
    if pybadger.button.b:
        pybadger.show_custom_badge()

That's all there is to using creating a custom badge with a color-block background!

Now that you've seen how it works, let's take a more detailed look at the custom badge features of PyBadger.

This guide was first published on Mar 04, 2020. It was last updated on Mar 04, 2020.

This page (CLUE Badge) was last updated on Oct 02, 2021.

Text editor powered by tinymce.