Thursday, 4 August 2016

Custom Display Clear

MicroPython has a simple built-in command to clear the display: display.clear(). The results are equally simple - it instantly turns off every LED on the display. We don't need to be limited to such a simplistic way of clearing the screen though, we can write our own exciting fades, wipes and dissolves.

The program code below demonstrates a few different ways of clearing the screen. Press A to try each one in turn. Pressing B will cycle through a few different images so you can see how the clear looks with different source images. Each clear is written in it's own function, so hopefully you should be able to understand them without too much trouble. Try to use these as a basis for your own custom clear functions.

 Custom Display Clear

 Demonstrate some different ways of clearing the micro:bit display. Pressing A will cycle through 
 the different methods. Pressing B will change the image to be cleared.

 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 
 International License.

from microbit import *
import random

def clear_fade():
    Fade the display out
    # We need to do nine complete passes to ensure that everything is faded to 0
    for i in range(9):
        # Go through all pixels on the display
        for x in range(5):
            for y in range(5):
                # Get current pixel value
                value = display.get_pixel(x, y)
                # Fade it if it's not already 0
                display.set_pixel(x, y, max(value - 1, 0))
        # Slow down the fade

def clear_wipe_down():
    Wipe the image clear from top to bottom
    # Go through each line
    for y in range(5):
        # Set the entire line to 0
        for x in range(5):
            display.set_pixel(x, y, 0)
        # Slow down the wipe

def clear_slide_down():
    Slide the image off the bottom of the display
    # We need to do five complete passes to slide the whole image off the bottom 
    for i in range(5):
        # Process all rows
        for x in range(5):
            # Process the bottom four pixels of each column
            for y in range(4, 0, -1):
                # Copy the pixel above into this pixel
                value = display.get_pixel(x, y - 1)
                display.set_pixel(x, y, value)
            # Set the top pixel to 0
            display.set_pixel(x, 0, 0)
        # Slow down the wipe

def clear_dissolve():
    Dissolve the display
    # Make a list of all pixels
    pixels = list(range(25))
    # Randomise the order by swapping random pairs of items lots of time
    for i in range(50):
        a = random.randint(0, len(pixels) - 1)
        b = random.randint(0, len(pixels) - 1)
        swap = pixels[a]
        pixels[a] = pixels[b]
        pixels[b] = swap

    # Go through all of the pixels, one at a time, and turn them off
    for pixel in pixels:
        # Turn pixel off
        display.set_pixel(int(pixel / 5), int(pixel % 5), 0)
        # Slow down the dissolve

# All of the clear functions
clear_functions = [
clear_function_index = 0

# The test images
test_images = [
test_image_index = 0

# Main loop
while True:[test_image_index])
    if  button_a.was_pressed():
        clear_function_index = (clear_function_index + 1) % len(clear_functions)
    elif button_b.was_pressed():
        test_image_index = (test_image_index + 1) % len(test_images)

No comments:

Post a comment