Friday, December 4, 2020

Simple pybadge circuitpython code example

I just got a pybadge, and I was trying to find an example that used the display but only used the core libraries. I couldn't find one, so here it is. I grant this code to the public domain.

import time

import board
import displayio
import digitalio

import vectorio
import gamepadshift

BUTTON_LEFT = 128
BUTTON_UP = 64
BUTTON_DOWN = 32
BUTTON_RIGHT = 16
BUTTON_SELECT = 8
BUTTON_START = 4
BUTTON_A = 2
BUTTON_B = 1

print('About to begin.')
for i in range(3,0,-1):
    print(i)
    time.sleep(0.2)

circle = vectorio.Circle(10)
circle2 = vectorio.Circle(6)

palette = displayio.Palette(2)
palette[0] = 0x000000
palette[1] = 0xFFFFFF
palette.make_transparent(0)
palette2 = displayio.Palette(2)
palette2[0] = 0xFF0000
palette2[1] = 0x0000FF
palette2.make_transparent(0)

vect = vectorio.VectorShape(shape=circle, pixel_shader=palette)
vect2 = vectorio.VectorShape(shape=circle2, pixel_shader=palette2, x=5, y=-5)

group = displayio.Group()
group.append(vect)
group.append(vect2)
display = board.DISPLAY

display.show(group)

group.x = 20
group.y = 20

pad = gamepadshift.GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
                                digitalio.DigitalInOut(board.BUTTON_OUT),
                                digitalio.DigitalInOut(board.BUTTON_LATCH))
button = 0
while button != BUTTON_START:
    button = pad.get_pressed()
    if button == BUTTON_UP:
        group.y += -1
    elif button == BUTTON_DOWN:
        group.y += 1
    elif button == BUTTON_LEFT:
        group.x += -1
    elif button == BUTTON_RIGHT:
        group.x += 1
    time.sleep(0.01)
    group.x %= display.width
    group.y %= display.height

  

No comments:

Post a Comment