Files
@ 835bac5d3b3c
Branch filter:
Location: led-matrix-software/demos/paint.py
835bac5d3b3c
4.5 KiB
text/x-python
Added new demos and handle splash screens and controller input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | ### paint.py
### Author: Matthew Reed
### Draw on the canvas using the D-Pad, A button changes color
import sys
import time
import signal
import logging
import configparser
from enum import Enum
import math
import matrix
class Paint:
class DIRECTION(Enum):
NONE = 0
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
def __init__(self, config, parent, matrix, controller):
self.logger = logging.getLogger('paint')
self.config = config
self.parent = parent
self.matrix = matrix
self.controller = controller
def reset(self):
pass
def splash(self):
w = matrix.Colors.WHITE.value
r = matrix.Colors.RED.value
g = matrix.Colors.GREEN.value
b = matrix.Colors.BLUE.value
l = matrix.Colors.LIGHT_BLUE.value
p = matrix.Colors.PURPLE.value
O = matrix.Colors.ORANGE.value
y = matrix.Colors.YELLOW.value
v = matrix.Colors.VIOLET.value
m = matrix.Colors.MAGENTA.value
o = matrix.Colors.OFF.value
splash = [
[o, o, o, o, o, o, o, o],
[o, o, w, w, w, o, o, o],
[o, o, w, o, w, o, o, o],
[o, o, w, w, w, o, o, o],
[o, o, w, o, o, r, O, y],
[o, o, w, o, o, m, w, g],
[o, o, w, o, o, v, b, l],
[o, o, o, o, o, o, o, o],
]
for x in range(0, self.matrix.WIDTH):
for y in range(0, self.matrix.HEIGHT):
self.matrix.set_pixel(x, y, splash[y][x])
self.matrix.update()
def run(self):
pointer = 0
direction = self.DIRECTION.NONE
color = matrix.Colors.WHITE.value
color_index = 7
#start timers and counters
self.start_time = time.time()
last_time = time.time()
led_iteration_count = 0
frame_count = 0
keep_going = True
while keep_going:
for event in self.controller.read_input():
if event.code == 313 and event.value == 1:
keep_going = False
elif event.code == 305 and event.value == 1:
color_index = (color_index + 1) % len(list(matrix.Colors))
color = list(matrix.Colors)[color_index].value
elif event.code == 16:
if event.value == 1:
#dpad right
direction = self.DIRECTION.RIGHT
if event.value == 0:
#dpad none
direction = self.DIRECTION.NONE
if event.value == -1:
#dpad left
direction = self.DIRECTION.LEFT
elif event.code == 17:
if event.value == 1:
#dpad down
direction = self.DIRECTION.DOWN
if event.value == 0:
#dpad none
direction = self.DIRECTION.NONE
if event.value == -1:
#dpad up
direction = self.DIRECTION.UP
if time.time() > last_time + 0.1:
last_time = time.time()
pointerx = pointer % self.matrix.WIDTH
pointery = math.floor(pointer / self.matrix.HEIGHT)
if direction == self.DIRECTION.UP:
pointery = (pointery - 1) % self.matrix.HEIGHT
elif direction == self.DIRECTION.DOWN:
pointery = (pointery + 1) % self.matrix.HEIGHT
elif direction == self.DIRECTION.LEFT:
pointerx = (pointerx - 1) % self.matrix.WIDTH
elif direction == self.DIRECTION.RIGHT:
pointerx = (pointerx + 1) % self.matrix.WIDTH
pointer = pointery * self.matrix.HEIGHT + pointerx
self.matrix.set_pixel(pointerx, pointery, color)
self.matrix.update()
led_iteration_count = (led_iteration_count + 1) % self.matrix.NUM_LEDS
frame_count = frame_count + 1
time.sleep(0.01)
|