Files
@ 6e77747319ab
Branch filter:
Location: led-matrix-software/demos/paint.py
6e77747319ab
3.6 KiB
text/x-python
Added base software to select and run demos/games, along with a couple demos
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 | ### 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):
self.matrix.set_matrix((255,0,0))
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)
|