Compare commits
1 commit
master
...
invalid-li
| Author | SHA1 | Date | |
|---|---|---|---|
| 646198d107 |
4 changed files with 157 additions and 286 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -2,5 +2,3 @@ __pycache__
|
||||||
_build
|
_build
|
||||||
*.pyc
|
*.pyc
|
||||||
*~
|
*~
|
||||||
|
|
||||||
*.mpy
|
|
||||||
|
|
|
||||||
|
|
@ -32,197 +32,191 @@ import board
|
||||||
import adafruit_dotstar
|
import adafruit_dotstar
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
class DotstarFeatherwing(object):
|
||||||
|
"""Test, Image, and Animation support for the DotStar featherwing"""
|
||||||
|
|
||||||
class DotstarFeatherwing:
|
blank_stripe = [(0, 0, 0),
|
||||||
"""Test, Image, and Animation support for the DotStar featherwing"""
|
(0, 0, 0),
|
||||||
|
(0, 0, 0),
|
||||||
|
(0, 0, 0),
|
||||||
|
(0, 0, 0),
|
||||||
|
(0, 0, 0)]
|
||||||
|
"""A blank stripe, used internally to separate characters as they are shifted onto the display."""
|
||||||
|
|
||||||
|
def __init__(self, clock, data, brightness=1.0):
|
||||||
|
"""Create an interface for the display.
|
||||||
|
|
||||||
blank_stripe = [(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)]
|
:param pin clock: The clock pin for the featherwing
|
||||||
"""A blank stripe, used internally to separate characters as they are shifted onto the display."""
|
:param pin data: The data pin for the featherwing
|
||||||
|
:param float brightness: Optional brightness (0.0-1.0) that defaults to 1.0
|
||||||
|
"""
|
||||||
|
self.rows = 6
|
||||||
|
self.columns = 12
|
||||||
|
self.display = adafruit_dotstar.DotStar(clock, data, self.rows * self.columns, brightness, False)
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, clock, data, brightness=1.0):
|
def clear(self):
|
||||||
"""Create an interface for the display.
|
"""Clear the display.
|
||||||
|
Does NOT update the LEDs
|
||||||
|
"""
|
||||||
|
self.display.fill((0,0,0))
|
||||||
|
|
||||||
:param pin clock: The clock pin for the featherwing
|
|
||||||
:param pin data: The data pin for the featherwing
|
|
||||||
:param float brightness: Optional brightness (0.0-1.0) that defaults to 1.0
|
|
||||||
"""
|
|
||||||
self.rows = 6
|
|
||||||
self.columns = 12
|
|
||||||
self.display = adafruit_dotstar.DotStar(
|
|
||||||
clock,
|
|
||||||
data,
|
|
||||||
self.rows * self.columns,
|
|
||||||
brightness=brightness,
|
|
||||||
auto_write=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
def clear(self):
|
def fill(self, color):
|
||||||
"""Clear the display.
|
"""Fills the wing with a color.
|
||||||
Does NOT update the LEDs
|
Does NOT update the LEDs.
|
||||||
"""
|
|
||||||
self.display.fill((0, 0, 0))
|
|
||||||
|
|
||||||
def fill(self, color):
|
:param (int, int, int) color: the color to fill with
|
||||||
"""Fills the wing with a color.
|
"""
|
||||||
Does NOT update the LEDs.
|
self.display.fill(color)
|
||||||
|
|
||||||
:param (int, int, int) color: the color to fill with
|
|
||||||
"""
|
|
||||||
self.display.fill(color)
|
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
"""Update the LEDs.
|
"""Update the LEDs.
|
||||||
"""
|
"""
|
||||||
self.display.show()
|
self.display.show()
|
||||||
|
|
||||||
def set_color(self, row, column, color):
|
|
||||||
"""Set the color of the specified pixel.
|
|
||||||
|
|
||||||
:param int row: The row (0-5) of the pixel to set
|
def set_color(self, row, column, color):
|
||||||
:param int column: The column (0-11) of the pixel to set
|
"""Set the color of the specified pixel.
|
||||||
:param (int, int, int) color: The color to set the pixel to
|
|
||||||
"""
|
|
||||||
self.display[row * self.columns + column] = color
|
|
||||||
|
|
||||||
def get_color(self, row, column):
|
:param int row: The row (0-5) of the pixel to set
|
||||||
"""Get the color of the specified pixel. Returns teh color as an RGB tripple.
|
:param int column: The column (0-11) of the pixel to set
|
||||||
|
:param (int, int, int) color: The color to set the pixel to
|
||||||
|
"""
|
||||||
|
self.display[row * self.columns + column] = color
|
||||||
|
|
||||||
|
|
||||||
|
def shift_into_left(self, stripe):
|
||||||
|
""" Shift a column of pixels into the left side of the display.
|
||||||
|
|
||||||
:param int row: The row (0-5) of the pixel to set
|
:param [(int, int, int)] stripe: A column of pixel colors. The first at the top.
|
||||||
:param int column: The column (0-11) of the pixel to set
|
"""
|
||||||
"""
|
for r in range(self.rows):
|
||||||
return self.display[row * self.columns + column]
|
rightmost = r * self.columns
|
||||||
|
for c in range(self.columns - 1):
|
||||||
|
self.display[rightmost + c] = self.display[rightmost + c + 1]
|
||||||
|
self.display[rightmost + self.columns - 1] = stripe[r]
|
||||||
|
|
||||||
def shift_into_left(self, stripe):
|
|
||||||
""" Shift a column of pixels into the left side of the display.
|
|
||||||
|
|
||||||
:param [(int, int, int)] stripe: A column of pixel colors. The first at the top.
|
def shift_into_right(self, stripe):
|
||||||
"""
|
""" Shift a column of pixels into the rightside of the display.
|
||||||
for r in range(self.rows):
|
|
||||||
rightmost = r * self.columns
|
|
||||||
for c in range(self.columns - 1):
|
|
||||||
self.display[rightmost + c] = self.display[rightmost + c + 1]
|
|
||||||
self.display[rightmost + self.columns - 1] = stripe[r]
|
|
||||||
|
|
||||||
def shift_into_right(self, stripe):
|
:param [(int, int, int)] stripe: A column of pixel colors. The first at the top.
|
||||||
""" Shift a column of pixels into the rightside of the display.
|
"""
|
||||||
|
for r in range(self.rows):
|
||||||
|
leftmost = ((r + 1) * self.columns) - 1
|
||||||
|
for c in range(self.columns - 1):
|
||||||
|
self.display[leftmost - c] = self.display[(leftmost - c) -1]
|
||||||
|
self.display[(leftmost - self.columns) + 1] = stripe[r]
|
||||||
|
|
||||||
|
|
||||||
:param [(int, int, int)] stripe: A column of pixel colors. The first at the top.
|
def number_to_pixels(self, x, color):
|
||||||
"""
|
"""Convert an integer (0..63) into an array of 6 pixels.
|
||||||
for r in range(self.rows):
|
|
||||||
leftmost = ((r + 1) * self.columns) - 1
|
|
||||||
for c in range(self.columns - 1):
|
|
||||||
self.display[leftmost - c] = self.display[(leftmost - c) - 1]
|
|
||||||
self.display[(leftmost - self.columns) + 1] = stripe[r]
|
|
||||||
|
|
||||||
def shift_into_top(self, stripe, offset=0):
|
:param int x: integer to convert into binary pixel values; LSB is topmost.
|
||||||
""" Shift a column of pixels into the rightside of the display.
|
:param (int, int, int) color: the color to set "on" pixels to
|
||||||
|
"""
|
||||||
|
val = x
|
||||||
|
pixels = []
|
||||||
|
for b in range(self.rows):
|
||||||
|
if val & 1 == 0:
|
||||||
|
pixels.append((0, 0, 0))
|
||||||
|
else:
|
||||||
|
pixels.append(color)
|
||||||
|
val = val >> 1
|
||||||
|
return pixels
|
||||||
|
|
||||||
|
|
||||||
:param [(int, int, int)] stripe: A column of pixel colors. The first at the top.
|
def character_to_numbers(self, font, char):
|
||||||
:param int offset: The offset into stripe of the first pixel value to take.
|
"""Convert a letter to the sequence of column values to display.
|
||||||
"""
|
|
||||||
for c in range(self.columns):
|
|
||||||
bottommost = (self.rows - 1) * self.columns
|
|
||||||
for r in range(self.rows - 1):
|
|
||||||
self.display[bottommost + c - (r * self.columns)] = self.display[
|
|
||||||
bottommost + c - ((r + 1) * self.columns)
|
|
||||||
]
|
|
||||||
self.display[c] = stripe[c + offset]
|
|
||||||
|
|
||||||
def number_to_pixels(self, x, color, bit_count=6):
|
:param {char -> [int]} font: the font to use to convert characters to glyphs
|
||||||
"""Convert an integer (0..63) into an array of 6 pixels.
|
:param char letter: the char to convert
|
||||||
|
"""
|
||||||
|
return font[char]
|
||||||
|
|
||||||
:param int x: integer to convert into binary pixel values; LSB is topmost.
|
|
||||||
:param (int, int, int) color: the color to set "on" pixels to
|
|
||||||
"""
|
|
||||||
val = x
|
|
||||||
pixels = []
|
|
||||||
for b in range(bit_count):
|
|
||||||
if val & 1 == 0:
|
|
||||||
pixels.append((0, 0, 0))
|
|
||||||
else:
|
|
||||||
pixels.append(color)
|
|
||||||
val = val >> 1
|
|
||||||
return pixels
|
|
||||||
|
|
||||||
def character_to_numbers(self, font, char):
|
def shift_in_character(self, font, c, color=(0x00, 0x40, 0x00), delay=0.2):
|
||||||
"""Convert a letter to the sequence of column values to display.
|
"""Shifts a single character onto the display from the right edge.
|
||||||
|
|
||||||
:param {char -> [int]} font: the font to use to convert characters to glyphs
|
:param {char -> [int]} font: the font to use to convert characters to glyphs
|
||||||
:param char letter: the char to convert
|
:param char c: the char to convert
|
||||||
"""
|
:param (int, int, int) color: the color to use for each pixel turned on
|
||||||
return font[char]
|
:param float delay: the time to wait between shifting in columns
|
||||||
|
"""
|
||||||
|
if c.upper() in font:
|
||||||
|
matrix = self.character_to_numbers(font, c.upper())
|
||||||
|
else:
|
||||||
|
matrix = self.character_to_numbers(font, 'UNKNOWN')
|
||||||
|
for stripe in matrix:
|
||||||
|
self.shift_into_right(self.number_to_pixels(stripe, color))
|
||||||
|
self.show()
|
||||||
|
time.sleep(delay)
|
||||||
|
self.shift_into_right(self.blank_stripe)
|
||||||
|
self.show()
|
||||||
|
time.sleep(delay)
|
||||||
|
|
||||||
def shift_in_character(self, font, c, color=(0x00, 0x40, 0x00), delay=0.2):
|
|
||||||
"""Shifts a single character onto the display from the right edge.
|
|
||||||
|
|
||||||
:param {char -> [int]} font: the font to use to convert characters to glyphs
|
def shift_in_string(self, font, s, color=(0x00, 0x40, 0x00), delay=0.2):
|
||||||
:param char c: the char to convert
|
"""Shifts a string onto the display from the right edge.
|
||||||
:param (int, int, int) color: the color to use for each pixel turned on
|
|
||||||
:param float delay: the time to wait between shifting in columns
|
|
||||||
"""
|
|
||||||
if c.upper() in font:
|
|
||||||
matrix = self.character_to_numbers(font, c.upper())
|
|
||||||
else:
|
|
||||||
matrix = self.character_to_numbers(font, "UNKNOWN")
|
|
||||||
for stripe in matrix:
|
|
||||||
self.shift_into_right(self.number_to_pixels(stripe, color))
|
|
||||||
self.show()
|
|
||||||
time.sleep(delay)
|
|
||||||
self.shift_into_right(self.blank_stripe)
|
|
||||||
self.show()
|
|
||||||
time.sleep(delay)
|
|
||||||
|
|
||||||
def shift_in_string(self, font, s, color=(0x00, 0x40, 0x00), delay=0.2):
|
:param {char -> [int]} font: the font to use to convert characters to glyphs
|
||||||
"""Shifts a string onto the display from the right edge.
|
:param string s: the char to convert
|
||||||
|
:param (int, int, int)) color: the color to use for each pixel turned on
|
||||||
|
:param float delay: the time to wait between shifting in columns
|
||||||
|
"""
|
||||||
|
for c in s:
|
||||||
|
self.shift_in_character(font, c, color, delay)
|
||||||
|
|
||||||
:param {char -> [int]} font: the font to use to convert characters to glyphs
|
|
||||||
:param string s: the char to convert
|
# Display an image
|
||||||
:param (int, int, int)) color: the color to use for each pixel turned on
|
def display_image(self, image, color):
|
||||||
:param float delay: the time to wait between shifting in columns
|
"""Display an mono-colored image.
|
||||||
"""
|
|
||||||
for c in s:
|
|
||||||
self.shift_in_character(font, c, color, delay)
|
|
||||||
|
|
||||||
# Display an image
|
:param [string] image: the textual bitmap, 'X' for set pixels, anything else for others
|
||||||
def display_image(self, image, color):
|
:param (int) color: the color to set "on" pixels to
|
||||||
"""Display an mono-colored image.
|
"""
|
||||||
|
self.display_colored_image(image, {'X': color})
|
||||||
|
|
||||||
|
|
||||||
:param [string] image: the textual bitmap, 'X' for set pixels, anything else for others
|
def display_colored_image(self, image, colors):
|
||||||
:param (int) color: the color to set "on" pixels to
|
"""Display an multi-colored image.
|
||||||
"""
|
|
||||||
self.display_colored_image(image, {"X": color})
|
:param [string] image: the textual bitmap, character are looked up in colors for the
|
||||||
|
corresponding pixel color, anything not in the map is off
|
||||||
|
:param {char -> (int, int, int)} colors: a map of characters in the image data to colors to use
|
||||||
|
"""
|
||||||
|
for r in range(self.rows):
|
||||||
|
for c in range(self.columns):
|
||||||
|
index = r * self.columns + ((self.columns - 1) - c)
|
||||||
|
key = image[r][c]
|
||||||
|
if key in colors:
|
||||||
|
self.display[index] = colors[key]
|
||||||
|
else:
|
||||||
|
self.display[index] = (0, 0, 0)
|
||||||
|
self.display.show()
|
||||||
|
|
||||||
|
|
||||||
|
def display_animation(self, animation, colors, count=1, delay=0.1):
|
||||||
|
"""Display a multi-colored animation.
|
||||||
|
|
||||||
|
:param [[string]] animation: a list of textual bitmaps, each as described in display_colored_image
|
||||||
|
:param {char -> (int, int, int)} colors: a map of characters in the image data to colors to use
|
||||||
|
:param int count: the number of times to play the animation
|
||||||
|
:param float delay: the amount of time (seconds) to wait between frames
|
||||||
|
"""
|
||||||
|
self.clear()
|
||||||
|
first_frame = True
|
||||||
|
while count > 0:
|
||||||
|
for frame in animation:
|
||||||
|
if not first_frame:
|
||||||
|
time.sleep(delay)
|
||||||
|
first_frame = False
|
||||||
|
self.display_colored_image(frame, colors)
|
||||||
|
count = count - 1
|
||||||
|
|
||||||
|
|
||||||
def display_colored_image(self, image, colors):
|
|
||||||
"""Display an multi-colored image.
|
|
||||||
|
|
||||||
:param [string] image: the textual bitmap, character are looked up in colors for the
|
|
||||||
corresponding pixel color, anything not in the map is off
|
|
||||||
:param {char -> (int, int, int)} colors: a map of characters in the image data to colors to use
|
|
||||||
"""
|
|
||||||
for r in range(self.rows):
|
|
||||||
for c in range(self.columns):
|
|
||||||
index = r * self.columns + ((self.columns - 1) - c)
|
|
||||||
key = image[r][c]
|
|
||||||
if key in colors:
|
|
||||||
self.display[index] = colors[key]
|
|
||||||
else:
|
|
||||||
self.display[index] = (0, 0, 0)
|
|
||||||
self.display.show()
|
|
||||||
|
|
||||||
def display_animation(self, animation, colors, count=1, delay=0.1):
|
|
||||||
"""Display a multi-colored animation.
|
|
||||||
|
|
||||||
:param [[string]] animation: a list of textual bitmaps, each as described in display_colored_image
|
|
||||||
:param {char -> (int, int, int)} colors: a map of characters in the image data to colors to use
|
|
||||||
:param int count: the number of times to play the animation
|
|
||||||
:param float delay: the amount of time (seconds) to wait between frames
|
|
||||||
"""
|
|
||||||
self.clear()
|
|
||||||
first_frame = True
|
|
||||||
while count > 0:
|
|
||||||
for frame in animation:
|
|
||||||
if not first_frame:
|
|
||||||
time.sleep(delay)
|
|
||||||
first_frame = False
|
|
||||||
self.display_colored_image(frame, colors)
|
|
||||||
count = count - 1
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
font = {' ': [ 0, 0, 0],
|
font = {' ': [ 0, 0, 0],
|
||||||
'A': [62, 05, 62],
|
'A': [62, 5, 62],
|
||||||
'B': [63, 37, 26],
|
'B': [63, 37, 26],
|
||||||
'C': [30, 33, 18],
|
'C': [30, 33, 18],
|
||||||
'D': [63, 33, 30],
|
'D': [63, 33, 30],
|
||||||
|
|
|
||||||
|
|
@ -1,121 +0,0 @@
|
||||||
# The MIT License (MIT)
|
|
||||||
#
|
|
||||||
# Copyright (c) 2018 Dave Astels
|
|
||||||
#
|
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
|
||||||
# in the Software without restriction, including without limitation the rights
|
|
||||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
|
||||||
# furnished to do so, subject to the following conditions:
|
|
||||||
#
|
|
||||||
# The above copyright notice and this permission notice shall be included in
|
|
||||||
# all copies or substantial portions of the Software.
|
|
||||||
#
|
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
# THE SOFTWARE.
|
|
||||||
|
|
||||||
"""
|
|
||||||
A gaunlet running game using the dotstar wing and the joy wing.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from time import sleep
|
|
||||||
from random import randint
|
|
||||||
|
|
||||||
import board
|
|
||||||
import busio
|
|
||||||
import dotstar_featherwing
|
|
||||||
import Adafruit_seesaw
|
|
||||||
|
|
||||||
i2c = busio.I2C(board.SCL, board.SDA)
|
|
||||||
ss = Adafruit_seesaw.Seesaw(i2c)
|
|
||||||
wing = dotstar_featherwing.DotstarFeatherwing(board.D13, board.D11, 0.1)
|
|
||||||
|
|
||||||
black = 0x000000
|
|
||||||
wall = 0x200800 # must not have any blue, must have red
|
|
||||||
pellet = 0x000040 # must have blue
|
|
||||||
player = 0x00FF00
|
|
||||||
|
|
||||||
numbers = {
|
|
||||||
' ': [0, 0, 0],
|
|
||||||
'0': [30, 33, 30],
|
|
||||||
'1': [34, 63, 32],
|
|
||||||
'2': [50, 41, 38],
|
|
||||||
'3': [33, 37, 26],
|
|
||||||
'4': [ 7, 4, 63],
|
|
||||||
'5': [23, 37, 25],
|
|
||||||
'6': [30, 41, 25],
|
|
||||||
'7': [49, 9, 7],
|
|
||||||
'8': [26, 37, 26],
|
|
||||||
'9': [38, 41, 30],
|
|
||||||
}
|
|
||||||
|
|
||||||
row = (wall, wall, wall, wall,
|
|
||||||
wall, wall, wall, wall,
|
|
||||||
black, black, black, black, black,
|
|
||||||
wall, wall, wall, wall,
|
|
||||||
wall, wall, wall, wall)
|
|
||||||
|
|
||||||
|
|
||||||
def run():
|
|
||||||
"""Play the game."""
|
|
||||||
|
|
||||||
player_x = 6
|
|
||||||
score = 0
|
|
||||||
steps = 0
|
|
||||||
step_delay = 0.15
|
|
||||||
offset = 4
|
|
||||||
|
|
||||||
for _ in range(wing.rows):
|
|
||||||
wing.shift_into_top(row, offset)
|
|
||||||
wing.show()
|
|
||||||
|
|
||||||
|
|
||||||
while True:
|
|
||||||
# remove player sprite
|
|
||||||
wing.set_color(3, player_x, black)
|
|
||||||
|
|
||||||
# shift/advance the track
|
|
||||||
offset = min(max(0, offset + randint(-1, 1)), 9)
|
|
||||||
wing.shift_into_top(row, offset)
|
|
||||||
|
|
||||||
# Maybe add a pellet
|
|
||||||
if randint(1, 20) == 1:
|
|
||||||
wing.set_color(0, randint(8, 12) - offset, pellet)
|
|
||||||
|
|
||||||
# Adjust player position
|
|
||||||
joy_x = ss.analog_read(3)
|
|
||||||
if joy_x < 256 and player_x > 0:
|
|
||||||
player_x -= 1
|
|
||||||
elif joy_x > 768 and player_x < 11:
|
|
||||||
player_x += 1
|
|
||||||
|
|
||||||
# Check for collisions
|
|
||||||
r, _, b = wing.get_color(3, player_x)
|
|
||||||
if b:
|
|
||||||
score += 1
|
|
||||||
elif r:
|
|
||||||
return steps, score
|
|
||||||
|
|
||||||
# Show player sprite
|
|
||||||
wing.set_color(3, player_x, player)
|
|
||||||
|
|
||||||
# Update some things and sleep a bit
|
|
||||||
wing.show()
|
|
||||||
steps += 1
|
|
||||||
if steps % 25 == 0:
|
|
||||||
score += 1
|
|
||||||
if steps % 100 == 0:
|
|
||||||
step_delay *= 0.9
|
|
||||||
sleep(step_delay)
|
|
||||||
|
|
||||||
while True:
|
|
||||||
result = run()
|
|
||||||
# got here because of a crash, so report score and restart
|
|
||||||
wing.shift_in_string(numbers, '{:03d}'.format(result[1]), 0x101010)
|
|
||||||
sleep(5)
|
|
||||||
Loading…
Reference in a new issue