Skip to content
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

Set a single pixel #125

Open
harwalan opened this issue Oct 20, 2022 · 1 comment
Open

Set a single pixel #125

harwalan opened this issue Oct 20, 2022 · 1 comment

Comments

@harwalan
Copy link

I'd love way to set a single pixel. Is there an existing way to do this?

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Oct 20, 2022

There isn't anything built in to this library that is specifically for drawing individual pixels, but you can add a Bitmap to the pyportal.graphics.splash group in order to have it shown on the display. Then you can manipulate the pixels inside of that Bitmap object. This Learn Guide Page shows any example of that technique (albeit without the pyportal library specifically).

This is one of the existing examples modified to add a Bitmap and set some pixels inside of it:

import board
from adafruit_pyportal.graphics import Graphics
import displayio

# Set display to show
display = board.DISPLAY

# Background Information
base = Graphics(default_bg=0x990099, debug=True)

# WebPage to show in the QR
webpage = "http://www.adafruit.com"

# QR size Information
qr_size = 9  # Pixels
scale = 3

# Create a barcode
base.qrcode(
    webpage,
    qr_size=scale,
    x=display.width // 2 - qr_size * scale,
    y=display.height // 2 - qr_size * scale,
)

# Create a bitmap with two colors
bitmap = displayio.Bitmap(display.width, display.height, 2)

# Create a two color palette
palette = displayio.Palette(2)
palette[0] = 0x000000
palette[1] = 0x00ff00
palette.make_transparent(0)

# Create a TileGrid using the Bitmap and Palette
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)


# Draw a pixel
bitmap[20, 20] = 1

# Draw even more pixels
for x in range(50, 70):
    for y in range(10, 20):
        bitmap[x, y] = 1

base.splash.append(tile_grid)

while True:
    pass

@harwalan also note that the new Bitmap will get put in front of the other things drawn. Unless you're intending to cover everything up, you'll probably want to set one or more of the colors in the palette to be transparent. Index 0 is set to transparent in this example, then pixels are drawn with index 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants