def test(): """Bouncing box.""" try: # Baud rate of 14500000 seems about the max spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) display.clear() colors = [ color565(255, 0, 0), color565(0, 255, 0), color565(0, 0, 255), color565(255, 255, 0), color565(0, 255, 255), color565(255, 0, 255) ] sizes = [12, 11, 10, 9, 8, 7] boxes = [Box(128, 128, sizes[i], display, colors[i]) for i in range(6)] while True: timer = ticks_us() for b in boxes: b.update_pos() b.draw() # Attempt to set framerate to 30 FPS timer_dif = 33333 - ticks_diff(ticks_us(), timer) if timer_dif > 0: sleep_us(timer_dif) except KeyboardInterrupt: display.cleanup()
def test(): """Test code.""" # Baud rate of 14500000 seems about the max spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) x, y = 0, 0 angle = 0.0 # Loop all angles from 0 to 2 * PI radians while angle < PI2: # Calculate x, y from a vector with known length and angle x = int(CENTER_X * sin(angle) + HALF_WIDTH) y = int(CENTER_Y * cos(angle) + HALF_HEIGHT) color = color565(*hsv_to_rgb(angle / PI2, 1, 1)) display.draw_line(x, y, CENTER_X, CENTER_Y, color) angle += ANGLE_STEP_SIZE sleep(5) for r in range(CENTER_X, 0, -1): color = color565(*hsv_to_rgb(r / HALF_WIDTH, 1, 1)) display.fill_circle(CENTER_X, CENTER_Y, r, color) sleep(9) display.cleanup()
def test(): """Test code.""" spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(4), cs=Pin(5), rst=Pin(21)) while 1: display.draw_image('1.raw', 0, 32, 128, 96) sleep(5) display.draw_image('2.raw', 0, 32, 128, 96) sleep(5) display.cleanup()
def test(): """Test code.""" # Baud rate of 14500000 seems about the max spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) c = 0 for x in range(0, 128, 16): for y in range(0, 128, 16): color = color565(*hsv_to_rgb(c / 64, 1, 1)) display.fill_circle(x + 7, y + 7, 7, color) c += 1 sleep(9) display.cleanup()
def test(): """CircuitPython Text, Shape & Sprite""" if implementation.name != 'circuitpython': print() print('This demo is for CircuitPython only!') exit() try: # Configuratoin for CS and DC pins: cs_pin = DigitalInOut(board.P0_15) dc_pin = DigitalInOut(board.P0_17) rst_pin = DigitalInOut(board.P0_20) # Setup SPI bus using hardware SPI: spi = SPI(clock=board.P0_24, MOSI=board.P0_22) # Create the SSD1351 display: display = Display(spi, dc=dc_pin, cs=cs_pin, rst=rst_pin) display.clear() # Load Fixed Font fixed = XglcdFont('fonts/FixedFont5x8.c', 5, 8, letter_count=96) # Title WIDTH = 128 text = 'CircuitPython Demo' # Measure text and center length = fixed.measure_text(text) x = int((WIDTH / 2) - (length / 2)) display.draw_text(x, 6, text, fixed, color565(255, 255, 0)) # Draw title outline display.draw_rectangle(0, 0, 127, 20, color565(0, 255, 0)) # Load sprite logo = BouncingSprite('images/blinka45x48.raw', 45, 48, 128, 128, 1, display) while True: timer = monotonic() logo.update_pos() logo.draw() # Attempt to set framerate to 30 FPS timer_dif = .033333333 - (monotonic() - timer) if timer_dif > 0: sleep(timer_dif) except KeyboardInterrupt: display.cleanup()
def test(): """Test code.""" spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) display.contrast(0) display.draw_image('images/MicroPython128x128.raw', 0, 0, 128, 128) fixed_font = XglcdFont('fonts/FixedFont5x8.c', 5, 8) contrast_range = list(range(1, 16)) + list(reversed(range(15))) for c in contrast_range: display.contrast(c) display.draw_text(30, 120, 'contrast: {0:02d}'.format(c), fixed_font, color565(255, 255, 255)) sleep(1) display.cleanup()
def test(): """Scrolling Marquee""" try: # Implementation dependant pin and SPI configuration if implementation.name == 'circuitpython': import board from busio import SPI from digitalio import DigitalInOut cs_pin = DigitalInOut(board.P0_15) dc_pin = DigitalInOut(board.P0_17) rst_pin = DigitalInOut(board.P0_20) spi = SPI(clock=board.P0_24, MOSI=board.P0_22) else: from machine import Pin, SPI cs_pin = Pin(5) dc_pin = Pin(17) rst_pin = Pin(16) spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) # Create the SSD1351 display: display = Display(spi, dc=dc_pin, cs=cs_pin, rst=rst_pin) display.clear() # Draw non-moving circles display.fill_circle(63, 63, 63, color565(27, 72, 156)) display.fill_circle(63, 63, 53, color565(0, 0, 0)) display.fill_circle(63, 63, 43, color565(189, 0, 36)) display.fill_circle(63, 63, 33, color565(0, 0, 0)) # Load Marquee image display.draw_image('images\Rototron128x26.raw', 0, 50, 128, 26) # Set up scrolling display.set_scroll(horiz_offset=1, vert_start_row=50, vert_row_count=26, vert_offset=0, speed=1) display.scroll(True) while True: # Do nothing, scrolling handled by hardware sleep(1) except KeyboardInterrupt: display.cleanup()
def test(): """Test code.""" spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) display.draw_text8x8(0, 0, 'Built-in', color565(255, 0, 255)) display.draw_text8x8(16, 16, 'MicroPython', color565(255, 255, 0)) display.draw_text8x8(32, 32, '8x8 Font', color565(0, 0, 255)) display.draw_text8x8(63, 120, "Portrait", color565(0, 255, 0)) display.draw_text8x8(0, 56, "Landscape", color565(255, 0, 0), landscape=True) sleep(9) display.cleanup()
def test(): """Test code.""" spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) display.draw_image('images/Tabby128x128.raw', 0, 0, 128, 128) print("Loading fonts, please wait.") fixed_font = XglcdFont('fonts/FixedFont5x8.c', 5, 8) unispace = XglcdFont('fonts/Unispace12x24.c', 12, 24) print("Fonts loaded.") display.draw_text(0, 0, 'Not transparent', fixed_font, color565(255, 0, 255)) display.draw_text(0, 80, 'Transparent', unispace, color565(0, 128, 255), spacing=0, transparent=True) display.draw_text(0, 103, 'Background', unispace, color565(0, 128, 255), color565(255, 255, 255), spacing=0) display.draw_text(103, 20, 'Test', unispace, color565(0, 255, 128), landscape=True, spacing=2, transparent=True) display.draw_text(0, 20, 'Test', unispace, color565(128, 255, 0), landscape=True) sleep(9) display.cleanup()
def test(): """Test code.""" spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) display.draw_image('images/RaspberryPiWB128x128.raw', 0, 0, 128, 128) sleep(5) display.draw_image('images/MicroPython128x128.raw', 0, 0, 128, 128) sleep(5) display.draw_image('images/Tabby128x128.raw', 0, 0, 128, 128) sleep(5) display.draw_image('images/Tortie128x128.raw', 0, 0, 128, 128) sleep(9) display.cleanup()
def test(): """Test code.""" # Baud rate of 14500000 seems about the max spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) # Build color list from all upper case constants (lazy approach) colors = [getattr(modules[__name__], name) for name in dir( modules[__name__]) if name.isupper() and name is not 'SPI'] colors.sort() c = 0 for x in range(1, 126, 25): for y in range(1, 126, 25): display.fill_rectangle(x, y, 25, 25, colors[c]) c += 1 sleep(9) display.cleanup()
def test(): """Bouncing sprite.""" try: # Baud rate of 14500000 seems about the max spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) display.clear() # Load sprite logo = BouncingSprite('images/Python41x49.raw', 41, 49, 128, 128, 1, display) while True: timer = ticks_us() logo.update_pos() logo.draw() # Attempt to set framerate to 30 FPS timer_dif = 33333 - ticks_diff(ticks_us(), timer) if timer_dif > 0: sleep_us(timer_dif) except KeyboardInterrupt: display.cleanup()
def main(): """Initialize display.""" # Baud rate of 14500000 seems about the max # spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) # display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) # use Wifiboy screen pins spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) # Draw background image display.draw_image('images/Arkanoid_Border128x118.raw', 0, 10, 128, 118) # Initialize ADC on VP pin 36 adc = ADC(Pin(36)) # Set attenuation 0-2V (Will use resistor to limit pot to 2V). adc.atten(ADC.ATTN_6DB) # Seed random numbers seed(ticks_us()) # Generate bricks MAX_LEVEL = const(9) level = 1 bricks = load_level(level, display) # Initialize paddle paddle = Paddle(display) # Initialize score score = Score(display) # Initialize balls balls = [] # Add first ball balls.append(Ball(59, 111, -2, -1, display, frozen=True)) # Initialize lives lives = [] for i in range(1, 3): lives.append(Life(i, display)) # Initialize power-ups powerups = [] try: while True: timer = ticks_us() # Set paddle position to ADC spinner (scale 6 - 98) paddle.h_position(adc.read() // 44 + 5) # Handle balls score_points = 0 for ball in balls: # Position ball.set_position(paddle.x, paddle.y, paddle.x2, paddle.center) # Check for collision with bricks if not frozen if not ball.frozen: prior_collision = False ball_x = ball.x ball_y = ball.y ball_x2 = ball.x2 ball_y2 = ball.y2 ball_center_x = ball.x + ((ball.x2 + 1 - ball.x) // 2) ball_center_y = ball.y + ((ball.y2 + 1 - ball.y) // 2) # Check for hits for brick in bricks: if (ball_x2 >= brick.x and ball_x <= brick.x2 and ball_y2 >= brick.y and ball_y <= brick.y2): # Hit if not prior_collision: ball.x_speed, ball.y_speed = brick.bounce( ball.x, ball.y, ball.x2, ball.y2, ball.x_speed, ball.y_speed, ball_center_x, ball_center_y) prior_collision = True score_points += 1 brick.clear() bricks.remove(brick) # Generate random power-ups if score_points > 0 and randint(1, 20) == 7: powerups.append(Powerup(ball.x, 64, display)) # Check for missed if ball.y2 > display.height - 3: ball.clear_previous() balls.remove(ball) if not balls: # Clear powerups powerups.clear() # Lose life if last ball on screen if len(lives) == 0: score.game_over() else: # Subtract Life lives.pop().clear() # Add ball balls.append( Ball(59, 112, 2, -3, display, frozen=True)) else: # Draw ball ball.draw() # Update score if changed if score_points: score.increment(score_points) # Handle power-ups for powerup in powerups: powerup.set_position(paddle.x, paddle.y, paddle.x2, paddle.center) powerup.draw() if powerup.collected: # Power-up collected powerup.clear() # Add ball balls.append( Ball(powerup.x, 112, 2, -1, display, frozen=False)) powerups.remove(powerup) elif powerup.y2 > display.height - 3: # Power-up missed powerup.clear() powerups.remove(powerup) # Check for level completion if not bricks: for ball in balls: ball.clear() balls.clear() for powerup in powerups: powerup.clear() powerups.clear() level += 1 if level > MAX_LEVEL: level = 1 bricks = load_level(level, display) balls.append(Ball(59, 111, -2, -1, display, frozen=True)) # Attempt to set framerate to 30 FPS timer_dif = 33333 - ticks_diff(ticks_us(), timer) if timer_dif > 0: sleep_us(timer_dif) except KeyboardInterrupt: display.cleanup()
def test(): """Test code.""" spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) print("Loading fonts, please wait.") arcadepix = XglcdFont('fonts/ArcadePix9x11.c', 9, 11) bally = XglcdFont('fonts/Bally7x9.c', 7, 9) broadway = XglcdFont('fonts/Broadway17x15.c', 17, 15) espresso_dolce = XglcdFont('fonts/EspressoDolce18x24.c', 18, 24) fixed_font = XglcdFont('fonts/FixedFont5x8.c', 5, 8) neato = XglcdFont('fonts/Neato5x7.c', 5, 7, letter_count=223) robotron = XglcdFont('fonts/Robotron7x11.c', 7, 11) unispace = XglcdFont('fonts/Unispace12x24.c', 12, 24) wendy = XglcdFont('fonts/Wendy7x8.c', 7, 8) print("Fonts loaded.") display.draw_text(0, 0, 'Arcade Pix 9x11', arcadepix, color565(255, 0, 0)) display.draw_text(0, 12, 'Bally 7x9', bally, color565(0, 255, 0)) display.draw_text(0, 23, 'Broadway', broadway, color565(0, 0, 255)) display.draw_text(0, 36, 'Espresso', espresso_dolce, color565(0, 255, 255)) display.draw_text(0, 64, 'Fixed Font 5x8', fixed_font, color565(255, 0, 255)) display.draw_text(0, 76, 'Neato 5x7', neato, color565(255, 255, 0)) display.draw_text(0, 85, 'Robotron 7x11', robotron, color565(255, 255, 255)) display.draw_text(0, 96, 'Unispace', unispace, color565(255, 128, 0)) display.draw_text(0, 120, 'Wendy 7x8', wendy, color565(255, 0, 128)) sleep(9) display.clear() display.draw_text(0, 0, 'Arcade Pix 9x11', arcadepix, color565(255, 0, 0), landscape=True) display.draw_text(12, 0, 'Bally 7x9', bally, color565(0, 255, 0), landscape=True) display.draw_text(23, 0, 'Broadway', broadway, color565(0, 0, 255), landscape=True) display.draw_text(36, 0, 'Espresso', espresso_dolce, color565(0, 255, 255), landscape=True) display.draw_text(64, 0, 'Fixed Font 5x8', fixed_font, color565(255, 0, 255), landscape=True) display.draw_text(76, 0, 'Neato 5x7', neato, color565(255, 255, 0), landscape=True) display.draw_text(85, 0, 'Robotron 7x11', robotron, color565(255, 255, 255), landscape=True) display.draw_text(96, 0, 'Unispace', unispace, color565(255, 128, 0), landscape=True) display.draw_text(120, 0, 'Wendy 7x8', wendy, color565(255, 0, 128), landscape=True) sleep(9) display.clear() display.draw_text(0, 0, 'Arcade Pix 9x11', arcadepix, color565(255, 0, 0), background=color565(0, 255, 255)) display.draw_text(0, 12, 'Bally 7x9', bally, color565(0, 255, 0), background=color565(0, 0, 128)) display.draw_text(0, 23, 'Broadway', broadway, color565(0, 0, 255), background=color565(255, 255, 0)) display.draw_text(0, 36, 'Espresso', espresso_dolce, color565(0, 255, 255), background=color565(255, 0, 0)) display.draw_text(0, 64, 'Fixed Font 5x8', fixed_font, color565(255, 0, 255), background=color565(0, 128, 0)) display.draw_text(0, 76, 'Neato 5x7', neato, color565(255, 255, 0), background=color565(0, 0, 255)) display.draw_text(0, 85, 'Robotron 7x11', robotron, color565(255, 255, 255), background=color565(128, 128, 128)) display.draw_text(0, 96, 'Unispace', unispace, color565(255, 128, 0), background=color565(0, 128, 255)) display.draw_text(0, 120, 'Wendy 7x8', wendy, color565(255, 0, 128), background=color565(255, 255, 255)) sleep(9) display.cleanup()
def test(): """Test code.""" # Baud rate of 14500000 seems about the max spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) print('spi started') display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) print('display started') display.clear(color565(64, 0, 255)) sleep(1) display.clear() display.draw_hline(10, 127, 63, color565(255, 0, 255)) sleep(1) display.draw_vline(10, 0, 127, color565(0, 255, 255)) sleep(1) display.fill_hrect(23, 50, 30, 75, color565(255, 255, 255)) sleep(1) display.draw_hline(0, 0, 127, color565(255, 0, 0)) sleep(1) display.draw_line(127, 0, 64, 127, color565(255, 255, 0)) sleep(2) display.clear() coords = [[0, 63], [78, 80], [122, 92], [50, 50], [78, 15], [0, 63]] display.draw_lines(coords, color565(0, 255, 255)) sleep(1) display.clear() display.fill_polygon(7, 63, 63, 50, color565(0, 255, 0)) sleep(1) display.fill_rectangle(0, 0, 15, 127, color565(255, 0, 0)) sleep(1) display.clear() display.fill_rectangle(0, 0, 63, 63, color565(128, 128, 255)) sleep(1) display.draw_rectangle(0, 64, 63, 63, color565(255, 0, 255)) sleep(1) display.fill_rectangle(64, 0, 63, 63, color565(128, 0, 255)) sleep(1) display.draw_polygon(3, 96, 96, 30, color565(0, 64, 255), rotate=15) sleep(3) display.clear() display.fill_circle(32, 32, 30, color565(0, 255, 0)) sleep(1) display.draw_circle(32, 96, 30, color565(0, 0, 255)) sleep(1) display.fill_ellipse(96, 32, 30, 16, color565(255, 0, 0)) sleep(1) display.draw_ellipse(96, 96, 16, 30, color565(255, 255, 0)) sleep(5) display.cleanup()