Last active
February 5, 2024 19:54
-
-
Save todbot/43c3b20f4e216ef13547d073395cc689 to your computer and use it in GitHub Desktop.
Draw a funciton on the screen, like an oscilloscope, building on @jedgarpark Parsec on circle drawing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# builds on John Park's Parsec on drawing circle https://www.youtube.com/watch?v=nI55N6q0sUo | |
import math | |
import time | |
import random | |
# Set up the display | |
display = board.DISPLAY | |
# Create a blank displayio group | |
group = displayio.Group() | |
display.root_group = group # put group on screen | |
palette = displayio.Palette(1) | |
palette[0] = 0x00ff00 # green | |
bitmap = displayio.Bitmap(5, 5, 1) # 5x5 box, 1 color | |
tile = displayio.TileGrid(bitmap, pixel_shader=palette) # stick bitmap in tilegrid | |
group.append(tile) # add tilegrid to group | |
# Draws random values | |
def my_function_random(x): | |
return random.randint(0, display.height-1) | |
# Draws a sine wave | |
def my_function_sine(x): | |
amplitude = 50 | |
angle = (x / display.height) * math.pi * 2 # convert x to radians | |
y = display.height/2 + (amplitude * math.sin(angle)) | |
return y | |
x = 0 # Where we currently are on the screen | |
x_speed = 5 # speed across the screen | |
while True: | |
# Compute new x position | |
x = (x + x_speed) % display.width | |
# Compute new y position | |
y = my_function_sine(x) | |
# Update the box position | |
tile.x = int(x) | |
tile.y = int(y) | |
# Update the display | |
display.refresh() | |
# Pause for a short time to control the speed of the animation | |
time.sleep(0.05) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IMG_3444.mov