0

PyBadger Library and Button Reading
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

PyBadger Library and Button Reading

by BluesPwr1009 on Sat Jan 25, 2020 4:50 pm

So I installed the PyBadger Library on my PyBadge and eliminated most of my original button reading code. Ever since I have been having issues. These are the errors I am getting:

code.py output:
Traceback (most recent call last):
File "code.py", line 159, in <module>
File "adafruit_pybadger.py", line 131, in __init__
File "neopixel.py", line 91, in __init__
ValueError: D8 in use


Here is my code:

Code: Select all | TOGGLE FULL SIZE
"""
This is a Conference Badge type Name Tag that is intended to be displayed on
the PyBadge. Feel free to customize it to your heart's content.
"""

import time
import board
from micropython import const
import displayio
import neopixel
from adafruit_display_shapes.rect import Rect
from adafruit_display_text.label import Label
from adafruit_bitmap_font import bitmap_font
from adafruit_pybadger import PyBadger

# Button Constants
BUTTON_LEFT = const(128)
BUTTON_UP = const(64)
BUTTON_DOWN = const(32)
BUTTON_RIGHT = const(16)
BUTTON_SEL = const(8)
BUTTON_START = const(4)
BUTTON_A = const(2)
BUTTON_B = const(1)

# Customizations
HELLO_STRING = "HELLO"
MY_NAME_STRING = "MY NAME IS"
NAME_STRING = "Ian Adlon"
NAME_FONTNAME = "/fonts/Noto-18.bdf"
ORG_NAME = "North Caldwell BOE"
ORG_NAME_FONTNAME = "/fonts/Arial-12.bdf"
NEOPIXEL_COUNT = 5
BACKGROUND_COLOR = 0xFF0000
FOREGROUND_COLOR = 0xFFFFFF
BACKGROUND_TEXT_COLOR = 0xFFFFFF
FOREGROUND_TEXT_COLOR = 0x000000

# Default Values
brightness = 0.2
direction = 1
speed = 0.1

# Define the NeoPixel and Game Pad Objects
neopixels = neopixel.NeoPixel(board.NEOPIXEL, NEOPIXEL_COUNT, brightness=brightness, auto_write=False, pixel_order=neopixel.GRB)

# Make the Display Background
splash = displayio.Group(max_size=20)
board.DISPLAY.show(splash)

color_bitmap = displayio.Bitmap(160, 128, 1)
color_palette = displayio.Palette(1)
color_palette[0] = BACKGROUND_COLOR

bg_sprite = displayio.TileGrid(color_bitmap,
                               pixel_shader=color_palette,
                               x=0, y=0)
splash.append(bg_sprite)

# Draw a Foreground Rectangle where the name goes
rect = Rect(0, 50, 160, 70, fill=FOREGROUND_COLOR)
splash.append(rect)

# Load the Hello font
large_font_name = "/fonts/Verdana-Bold-18.bdf"
large_font = bitmap_font.load_font(large_font_name)
large_font.load_glyphs(HELLO_STRING.encode('utf-8'))

# Load the "My Name Is" font
small_font_name = "/fonts/Arial-12.bdf"
small_font = bitmap_font.load_font(small_font_name)
small_font.load_glyphs(MY_NAME_STRING.encode('utf-8'))

# Load the Name font
name_font_name = NAME_FONTNAME
name_font = bitmap_font.load_font(name_font_name)
name_font.load_glyphs(NAME_STRING.encode('utf-8'))

# Load the Org Name font
org_name_font_name = ORG_NAME_FONTNAME
org_name_font = bitmap_font.load_font(org_name_font_name)
org_name_font.load_glyphs(ORG_NAME.encode('utf-8'))

# Setup and Center the Hello Label
hello_label = Label(large_font, text=HELLO_STRING)
(x, y, w, h) = hello_label.bounding_box
hello_label.x = (80 - w // 2)
hello_label.y = 15
hello_label.color = BACKGROUND_TEXT_COLOR
splash.append(hello_label)

# Setup and Center the "My Name Is" Label
mni_label = Label(small_font, text=MY_NAME_STRING)
(x, y, w, h) = mni_label.bounding_box
mni_label.x = (80 - w // 2)
mni_label.y = 35
mni_label.color = BACKGROUND_TEXT_COLOR
splash.append(mni_label)

# Setup and Center the Name Label (Set name_label.y to 70 for addition of ORG_NAME)
name_label = Label(name_font, text=NAME_STRING, line_spacing=0.75)
(x, y, w, h) = name_label.bounding_box
name_label.x = (80 - w // 2)
name_label.y = 70
name_label.color = FOREGROUND_TEXT_COLOR
splash.append(name_label)


# Setup and Center the ORG Label
org_name_label = Label(org_name_font, text=ORG_NAME, line_spacing=0.75)
(x, y, w, h) = org_name_label.bounding_box
org_name_label.x = (80 - w // 2)
org_name_label.y = 95
org_name_label.color = FOREGROUND_TEXT_COLOR
splash.append(org_name_label)

# Color chase function
def color_chase(color, color2, wait):
    for i in range(NEOPIXEL_COUNT):
        if (i % 2) == 0:
            neopixels[i] = color
            time.sleep(wait)
            neopixels.show()
            neopixels.fill(0)
            time.sleep(wait)
        else:
            neopixels[i] = color2
            time.sleep(wait)
            neopixels.show()
            neopixels.fill(0)
            time.sleep(wait)
           

#Fill Neopixels
def led_fill(color, color2, wait):
    for i in range(NEOPIXEL_COUNT):
        neopixels[i] = color
        time.sleep(wait)
        neopixels.show()
        neopixels.fill(0)
        time.sleep(wait)
    for i in range(NEOPIXEL_COUNT):
        neopixels[i] = color2
        time.sleep(wait)
        neopixels.show()
        neopixels.fill(0)
        time.sleep(wait)

# Color definitions
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
BLACK = (0, 0, 0)

#Initialize PyBadger Objects
pybadger = PyBadger()

# Main Loop
while True:
        neopixels.fill(0)
        neopixels.show()
        neopixels.brightness = brightness
        # Reading buttons too fast returns 0
        pybadger.auto_dim_display(delay=10)
        if pybadger.button.a:
            color_chase(BLUE, YELLOW, 0.1)
        elif pybadger.button.b:
            led_fill(BLUE, YELLOW, 0.1)


HELP!

BluesPwr1009
 
Posts: 33
Joined: Sat Aug 22, 2015 5:52 pm

Re: PyBadger Library and Button Reading

by adafruit_support_carter on Sat Jan 25, 2020 6:24 pm

The PyBadger library takes care of the NeoPixel setup for you. So you do not need to import the neopixel library and do the setup in your code, as you are with this line:
Code: Select all | TOGGLE FULL SIZE
    neopixels = neopixel.NeoPixel(board.NEOPIXEL, NEOPIXEL_COUNT, brightness=brightness, auto_write=False, pixel_order=neopixel.GRB)

What's happening is the line above sets up the NeoPixels manually, then later when you try to create the PyBadger object:
Code: Select all | TOGGLE FULL SIZE
    pybadger = PyBadger()

it also tries to setup the NeoPixels - and you hit that error.

So just remove the neopixel import and setup from your code.py. To access the PyBadger's NeoPixels, use the pixels property.
https://circuitpython.readthedocs.io/pr ... ger.pixels.
For example, this:
Code: Select all | TOGGLE FULL SIZE
            neopixels.fill(0)
            neopixels.show()
            neopixels.brightness = brightness

would become:
Code: Select all | TOGGLE FULL SIZE
            pybadger.pixels.fill(0)
            pybadger.pixels.show()
            pybadger.pixels.brightness = brightness

adafruit_support_carter
 
Posts: 14475
Joined: Tue Nov 29, 2016 2:45 pm

Re: PyBadger Library and Button Reading

by BluesPwr1009 on Sat Jan 25, 2020 7:02 pm

Ahh, ok, thanks!

BluesPwr1009
 
Posts: 33
Joined: Sat Aug 22, 2015 5:52 pm

Please be positive and constructive with your questions and comments.