diff --git a/demos/paint.py b/demos/paint.py new file mode 100644 --- /dev/null +++ b/demos/paint.py @@ -0,0 +1,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) + \ No newline at end of file