def detect_fail():
    # An unconnected chain should report IOError
    try:
        FrameBuffer.detect()
    except IOError:
        return True
    return False
def detect_fail():
    # An unconnected chain should report IOError
    try:
        FrameBuffer.detect()
    except IOError:
        return True
    return False
def brightness():
    '''All matrices show displays all LEDs lit, from min to max brightness over about 1 second.
    Should be left at max brightness.
    '''
    fb = FrameBuffer(matrix_layout=[(0,0,90)])
    for i in range(16):
        fb.erase(i)
        fb.show()
        time.sleep(0.1)
def diagonal_wave_animation():
    '''Displays a diagonal wave animation over ~3 seconds.  Movement should
    appear in the direction from the botton right to the top left.
    '''
    fb = FrameBuffer(matrix_layout=[(0,0,90)])
    colors = list(range(16)) + list(reversed(range(16)))
    for i in range(80):
        for x in range(16):
            fb.line((x-8,0),(x,fb.height), color=colors[x])
        fb.show()
        # rotate colors
        colors = colors[1:] + colors[:1]
        time.sleep(0.04)
def rect3():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.rect((2, 1), (3, 5), fill=True)
    expected_fb = '''
        00000000
        00000000
        00FFF000
        00FFF000
        00FFF000
        00FFF000
        00FFF000
        00000000
        '''
    return arrays_equal(expected_fb, fb)
def rect2():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.rect((0, 0), (8, 8), fill=True)
    expected_fb = '''
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        '''
    return arrays_equal(expected_fb, fb)
def line5():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.line((1, 3), (6, 5))
    expected_fb = '''
        00000000
        00000000
        00000FF0
        000FF000
        0FF00000
        00000000
        00000000
        00000000
        '''
    return arrays_equal(expected_fb, fb)
def init_two():
    fb = FrameBuffer(matrix_layout=[(0,0,0), (8,0,0)])
    fb.line((0,0),(fb.width, fb.height), color=0xa)
    expected_fb = '''
        00000000000000aa
        000000000000aa00
        0000000000aa0000
        00000000aa000000
        000000aa00000000
        0000aa0000000000
        00aa000000000000
        aa00000000000000
        '''
    return arrays_equal(expected_fb, fb)
def line3():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.line((0, 0), (7, 7))
    expected_fb = '''
        0000000F
        000000F0
        00000F00
        0000F000
        000F0000
        00F00000
        0F000000
        F0000000
        '''
    return arrays_equal(expected_fb, fb)
def point4():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.point(7, 7, color=10)
    expected_fb = '''
        0000000A
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        '''
    return arrays_equal(expected_fb, fb)
def point3():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.point(1, 2, color=3)
    expected_fb = '''
        00000000
        00000000
        00000000
        00000000
        00000000
        03000000
        00000000
        00000000
        '''
    return arrays_equal(expected_fb, fb)
def point2():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.point((0, 0), color=1)
    expected_fb = '''
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        10000000
        '''
    return arrays_equal(expected_fb, fb)
def point1():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.point(0,0)
    expected_fb = '''
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        F0000000
        '''
    return arrays_equal(expected_fb, fb)
def point1():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.point(0, 0)
    expected_fb = '''
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        F0000000
        '''
    return arrays_equal(expected_fb, fb)
def point3():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.point(1,2,color=3)
    expected_fb = '''
        00000000
        00000000
        00000000
        00000000
        00000000
        03000000
        00000000
        00000000
        '''
    return arrays_equal(expected_fb, fb)
def point2():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.point((0,0), color=1)
    expected_fb = '''
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        10000000
        '''
    return arrays_equal(expected_fb, fb)
def line3():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.line((0,0),(7,7))
    expected_fb = '''
        0000000F
        000000F0
        00000F00
        0000F000
        000F0000
        00F00000
        0F000000
        F0000000
        '''
    return arrays_equal(expected_fb, fb)
def point4():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.point(7,7,color=10)
    expected_fb = '''
        0000000A
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        00000000
        '''
    return arrays_equal(expected_fb, fb)
def rect2():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.rect((0,0),(8,8), fill=True)
    expected_fb = '''
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        FFFFFFFF
        '''
    return arrays_equal(expected_fb, fb)
def line5():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.line((1,3),(6,5))
    expected_fb = '''
        00000000
        00000000
        00000FF0
        000FF000
        0FF00000
        00000000
        00000000
        00000000
        '''
    return arrays_equal(expected_fb, fb)
def init_two():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0), (8, 0, 0)])
    fb.line((0, 0), (fb.width, fb.height), color=0xa)
    expected_fb = '''
        00000000000000aa
        000000000000aa00
        0000000000aa0000
        00000000aa000000
        000000aa00000000
        0000aa0000000000
        00aa000000000000
        aa00000000000000
        '''
    return arrays_equal(expected_fb, fb)
def rect3():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.rect((2,1),(3,5), fill=True)
    expected_fb = '''
        00000000
        00000000
        00FFF000
        00FFF000
        00FFF000
        00FFF000
        00FFF000
        00000000
        '''
    return arrays_equal(expected_fb, fb)
def sprite_draw():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.erase(0xE)
    s = copy.deepcopy(default_sprite)
    fb.draw(s, origin=(2,3))
    expected_fb = '''
        EEEEEEEE
        EE123EEE
        EE456EEE
        EE789EEE
        EEABCEEE
        EEEEEEEE
        EEEEEEEE
        EEEEEEEE
        '''
    return arrays_equal(expected_fb, fb)
def rect4():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.rect((0,0),(4,5), color=1)
    fb.rect((2,2),(6,6), color=2)
    fb.rect((5,1),(3,3), color=3)
    fb.rect((1,5),(4,2), color=4)
    expected_fb = '''
        00222222
        04444002
        04444002
        11210002
        10210333
        10222323
        10010333
        11110000
        '''
    return arrays_equal(expected_fb, fb)
def sprite_time_large_bitmap_draw_and_show():
    # This is currently quite slow, as it is all done in Python via a 2D array.
    # Speed not needed right now, but may in future move to numpy or CPython
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    s = Sprite((('5' * 16) + '\n') * 16)

    def draw():
        fb.draw(s)
        fb.show()

    return timeit(partial(draw), loops=100) > 50
def char_on_each_matrix():
    '''Verify that the numbers 0-9, A-Z are in the vertical chain of LEDs,
    lowest number at the bottom.
    '''
    chars = [Text(chr(ord('0')+i)) for i in range(10)]
    chars += [Text(chr(ord('A')+i)) for i in range(26)]

    num = FrameBuffer.detect()
    matrix_layout = [(0,y*8,90) for y in reversed(range(num))]
    fb = FrameBuffer(matrix_layout=matrix_layout)

    fb.erase()
    for i in range(num):
        fb.draw(chars[i], (2,8*i))
    fb.show()
def brightness():
    '''All matrices show displays all LEDs lit, from min to max brightness over about 1 second.
    Should be left at max brightness.
    '''
    fb = FrameBuffer(matrix_layout=[(0, 0, 90)])
    for i in range(16):
        fb.erase(i)
        fb.show()
        time.sleep(0.1)
def line4():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.line((0, 0), (7, 7))
    fb.line((7, 0), (0, 7), color=1)
    fb.line((2, 0), (2, 7), color=2)
    fb.line((0, 2), (7, 2), color=3)
    expected_fb = '''
        1020000F
        012000F0
        00200F00
        0021F000
        002F1000
        33333333
        0F200010
        F0200001
        '''
    return arrays_equal(expected_fb, fb)
def diagonal_wave_animation():
    '''Displays a diagonal wave animation over ~3 seconds.  Movement should
    appear in the direction from the botton right to the top left.
    '''
    fb = FrameBuffer(matrix_layout=[(0, 0, 90)])
    colors = list(range(16)) + list(reversed(range(16)))
    for i in range(80):
        for x in range(16):
            fb.line((x - 8, 0), (x, fb.height), color=colors[x])
        fb.show()
        # rotate colors
        colors = colors[1:] + colors[:1]
        time.sleep(0.04)
def line4():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.line((0,0),(7,7))
    fb.line((7,0),(0,7), color=1)
    fb.line((2,0),(2,7), color=2)
    fb.line((0,2),(7,2), color=3)
    expected_fb = '''
        1020000F
        012000F0
        00200F00
        0021F000
        002F1000
        33333333
        0F200010
        F0200001
        '''
    return arrays_equal(expected_fb, fb)
def sprite_draw():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.erase(0xE)
    s = copy.deepcopy(default_sprite)
    fb.draw(s, origin=(2, 3))
    expected_fb = '''
        EEEEEEEE
        EE123EEE
        EE456EEE
        EE789EEE
        EEABCEEE
        EEEEEEEE
        EEEEEEEE
        EEEEEEEE
        '''
    return arrays_equal(expected_fb, fb)
def rect4():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.rect((0, 0), (4, 5), color=1)
    fb.rect((2, 2), (6, 6), color=2)
    fb.rect((5, 1), (3, 3), color=3)
    fb.rect((1, 5), (4, 2), color=4)
    expected_fb = '''
        00222222
        04444002
        04444002
        11210002
        10210333
        10222323
        10010333
        11110000
        '''
    return arrays_equal(expected_fb, fb)
from rstem.accel import Accel
from rstem.led_matrix import FrameBuffer
import time

fb = FrameBuffer()
accel = Accel()

TILT_FORCE = 0.1
STEP = 0.1
x, y = (3, 3)
while True:
    x_force, y_force, z_force = accel.forces()
    
    if x_force > TILT_FORCE:
        x -= STEP
    elif x_force < -TILT_FORCE:
        x += STEP

    fb.erase()
    fb.point(round(x), round(y))
    fb.show()
    
    time.sleep(.1)


def default_erased():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    return fb._framebuffer() == erased_fb()
def time_show():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    return timeit(partial(fb.show), loops=200) > 300
from rstem.accel import Accel
from rstem.led_matrix import FrameBuffer
import time

fb = FrameBuffer()
accel = Accel()

spaceship_position = fb.width / 2

alien_columns = [0, 1, 2, 3]
alien_row = fb.height - 1

TILT_FORCE = 0.1
SPACESHIP_STEP = 0.1

while True:
    # ########################################
    # Get inputs
    # ########################################
    x_force, y_force, z_force = accel.forces()

    # ########################################
    # Change the World
    # ########################################

    # Move spaceship
    if x_force > TILT_FORCE:
        spaceship_position -= SPACESHIP_STEP
    elif x_force < -TILT_FORCE:
        spaceship_position += SPACESHIP_STEP
    spaceship_position = max(0, min(fb.width - 1, spaceship_position))
def default_erased():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    return fb._framebuffer() == erased_fb()
from rstem.led_matrix import FrameBuffer

fb = FrameBuffer()

fb.erase()
fb.point(3, 0)
fb.show()
예제 #39
0
from rstem.led_matrix import FrameBuffer, Sprite
from itertools import cycle
import time

fb = FrameBuffer()

animation = [
    Sprite("""
        00000000
        0000FF00
        000FF000
        000FFF00
        00FFFF00
        000FFF00
        0FF00F00
        0F0000FF
        """),
    Sprite("""
        00000000
        0000FF00
        0000F000
        00FFFF00
        0F0FF0F0
        000FFF00
        0FF00F00
        00000FF0
        """),
    Sprite("""
        00000000
        0000FF00
        000FF000
def out_of_bounds_point4():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.point(0, fb.height)
    return fb._framebuffer() == erased_fb()
def verify_cpu():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    return testing.verify_cpu()
def out_of_bounds_point3():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.point(fb.width, 0)
    return fb._framebuffer() == erased_fb()
from rstem.accel import Accel
from rstem.led_matrix import FrameBuffer
import time

fb = FrameBuffer()
accel = Accel()

spaceship_position = fb.width / 2

TILT_FORCE = 0.1
SPACESHIP_STEP = 0.1

while True:
    # ########################################
    # Get inputs
    # ########################################
    x_force, y_force, z_force = accel.forces()

    # ########################################
    # Change the World
    # ########################################

    # Move spaceship
    if x_force > TILT_FORCE:
        spaceship_position -= SPACESHIP_STEP
    elif x_force < -TILT_FORCE:
        spaceship_position += SPACESHIP_STEP
    spaceship_position = max(0, min(fb.width - 1, spaceship_position))

    # ########################################
    # Show world
예제 #44
0
from rstem.led_matrix import FrameBuffer
import time

fb = FrameBuffer()

x, y = (3, 3)
while True:
    fb.erase()
    fb.point(x, y)
    fb.show()

    time.sleep(.1)
def time_point():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    return timeit(partial(fb.point, 0, 0)) > 40000
예제 #46
0
from rstem.led_matrix import FrameBuffer
from rstem.mcpi import minecraft, control
import time

control.show()

mc = minecraft.Minecraft.create()

SCALE = 25
fb = FrameBuffer()

count = 0
FLASH_COUNT = 3
flash_lit = True
while True:
	pos = mc.player.getTilePos()

	x = round(pos.x/SCALE + (fb.width-1)/2)
	x_out_of_bounds = not 0 <= x < fb.width
	x = min(fb.width-1, max(0, x))

	z = round(pos.z/SCALE + (fb.height-1)/2)
	z_out_of_bounds = not 0 <= z < fb.height
	z = min(fb.height-1, max(0, z))

	fb.erase()
	count += 1
	if count > FLASH_COUNT:
		flash_lit = not flash_lit
		count = 0
	if not x_out_of_bounds and not z_out_of_bounds or flash_lit:
def out_of_bounds_point4():
    fb = FrameBuffer(matrix_layout=[(0, 0, 0)])
    fb.point(0, fb.height)
    return fb._framebuffer() == erased_fb()
from rstem.led_matrix import FrameBuffer, Text
import traceback
import sys
import time

def failed(s, tb=False):
    if tb:
        print(traceback.format_exc())
    print("ERROR: ", s)
    print("TEST FAILED")
    sys.exit()

try:
    fb = FrameBuffer([(0,0,-90),(8,0,-90),(8,8,-90)])
except:
    failed("FrameBuffer() could not be intialized", True)

if FrameBuffer.detect() != 3:
    failed("Should be exactly 3 LED Matrices attached")

print("INFO: Three matrices found.")
try:
    for i in range(20):
        if FrameBuffer.detect() != 3:
            failed("Retesting detect() failure: Should be exactly 3 LED Matrices attached")
except:
    failed("Retesting detect() failure: exception caught", True)

print("""
Verify that the following sequence occurs:
    - A, B, C is printed on the LEDs (1 second)
예제 #49
0
    Sprite(arrow75deg).rotate(180),
    Sprite(arrow0deg).rotate(270),
    Sprite(arrow15deg).rotate(270),
    Sprite(arrow30deg).rotate(270),
    Sprite(arrow45deg).rotate(270),
    Sprite(arrow60deg).rotate(270),
    Sprite(arrow75deg).rotate(270),
]


def vector_angle(start, end):
    return degrees(atan2(end.x - start.x, end.z - start.z))


compass = Button(24)
fb = FrameBuffer()
while block.Block(mc.getBlock(gold_pos)) == block.GOLD_BLOCK:
    if compass.presses():
        heading = control.get_heading(mc)
        angle_to_gold = vector_angle(mc.player.getPos(), gold_pos)
        compass_angle = 90 + (angle_to_gold - heading)
        fb.erase()
        arrow_index = round(compass_angle / 15) % 24
        fb.draw(arrows[arrow_index])
        fb.show()

    for button, action in keymap.items():
        if button.is_pressed():
            action()
        else:
            action(release=True)
예제 #50
0
from rstem.accel import Accel
from rstem.button import Button
from rstem.led_matrix import FrameBuffer
import time

fire_button = Button(25)

fb = FrameBuffer()
accel = Accel()

spaceship_position = fb.width / 2

alien_columns = [0, 1, 2, 3]
alien_row = fb.height - 1
alien_start_time = time.time()

ALIENS_STEP_TIME = 0.8

missile_x, missile_y = -1, -1

MISSILE_COLOR = 10
MISSILE_STEP_TIME = 0.1

TILT_FORCE = 0.1
SPACESHIP_STEP = 0.1

while True:
    # ########################################
    # Get inputs
    # ########################################
    presses = fire_button.presses()
예제 #51
0
from rstem.led_matrix import FrameBuffer, Text
import time

fb = FrameBuffer()
start_text = Text("PUSH TO PLAY! ")
lose_text = Text("YOU LOSE! ")
win_text = Text("YOU WIN! ")

def scroll(text, cancel=None, speed=0.07):
    for i in range(text.width + fb.width):
        fb.erase()
        fb.draw(text, origin=(fb.width-i,0))
        fb.show()
        time.sleep(speed)
        if cancel and cancel():
            return True
    return False

# ###############################################################
#
# SPACE INVADERS GAME
#
# ###############################################################

# ################################
# Initialize the game environment
# ################################
        
# Import required functions/modules
from rstem.accel import Accel
from rstem.button import Button
from rstem.accel import Accel
from rstem.led_matrix import FrameBuffer
import time

fb = FrameBuffer()
accel = Accel()

TILT_FORCE = 0.1
STEP = 0.1
x, y = (3, 3)
while True:
    x_force, y_force, z_force = accel.forces()
    
    if x_force > TILT_FORCE:
        x -= STEP
    elif x_force < -TILT_FORCE:
        x += STEP
    x = max(min(fb.width, x), 0)

    fb.erase()
    fb.point(round(x), round(y))
    fb.show()
    
    time.sleep(.1)


예제 #53
0
from rstem.led_matrix import FrameBuffer, Sprite
from rstem.gpio import Output
import time
from random import random

fb = FrameBuffer([(0, 0, 90)])

outs = [
    Output(17),
    Output(27),
    Output(22),
    Output(23),
    Output(4),
    Output(14),
    Output(15),
    Output(18),
]

with open('scroller1.spr') as f:
    s1 = Sprite(f.read())
s1_width = s1.width
s1 += s1

with open('scroller2.spr') as f:
    s2 = Sprite(f.read())
s2_width = s2.width
s2 += s2

i = 0
i1 = 0
i2 = 0
def erase1():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.erase(0xf)
    fb.erase()
    return fb._framebuffer() == makefb('00000000\n' * 8)
    Sprite(arrow45deg).rotate(0),
    Sprite(arrow60deg).rotate(0),
    Sprite(arrow75deg).rotate(0),
    Sprite(arrow0deg).rotate(90),
    Sprite(arrow15deg).rotate(90),
    Sprite(arrow30deg).rotate(90),
    Sprite(arrow45deg).rotate(90),
    Sprite(arrow60deg).rotate(90),
    Sprite(arrow75deg).rotate(90),
    Sprite(arrow0deg).rotate(180),
    Sprite(arrow15deg).rotate(180),
    Sprite(arrow30deg).rotate(180),
    Sprite(arrow45deg).rotate(180),
    Sprite(arrow60deg).rotate(180),
    Sprite(arrow75deg).rotate(180),
    Sprite(arrow0deg).rotate(270),
    Sprite(arrow15deg).rotate(270),
    Sprite(arrow30deg).rotate(270),
    Sprite(arrow45deg).rotate(270),
    Sprite(arrow60deg).rotate(270),
    Sprite(arrow75deg).rotate(270),
    ]

fb = FrameBuffer()
while True:
    for arrow in arrows:
        fb.erase()
        fb.draw(arrow)
        fb.show()
        time.sleep(0.1)
def erase2():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.erase(3)
    return fb._framebuffer() == makefb('33333333\n' * 8)
예제 #57
0
from rstem.led_matrix import FrameBuffer
from rstem.mcpi import minecraft, control
import time

control.show()

mc = minecraft.Minecraft.create()

SCALE = 25
fb = FrameBuffer()

count = 0
FLASH_COUNT = 3
flash_lit = True
while True:
    pos = mc.player.getTilePos()

    x = round(pos.x / SCALE + (fb.width - 1) / 2)
    x_out_of_bounds = not 0 <= x < fb.width
    x = min(fb.width - 1, max(0, x))

    z = round(pos.z / SCALE + (fb.height - 1) / 2)
    z_out_of_bounds = not 0 <= z < fb.height
    z = min(fb.height - 1, max(0, z))

    fb.erase()
    count += 1
    if count > FLASH_COUNT:
        flash_lit = not flash_lit
        count = 0
    if not x_out_of_bounds and not z_out_of_bounds or flash_lit:
def erase3():
    fb = FrameBuffer(matrix_layout=[(0,0,0)])
    fb.erase(7)
    return fb._framebuffer() == makefb('77777777\n' * 8)