Files @ 835bac5d3b3c
Branch filter:

Location: led-matrix-software/demos/paint.py

matthewreed
Added new demos and handle splash screens and controller input
### 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)