### matrix.py
### Author: Matthew Reed
import sys
import time
import signal
import logging
import threading
import configparser
from enum import Enum
import opc
import color_utils
import math
class Colors(Enum):
OFF = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
PURPLE = (128, 0, 128)
YELLOW = (255, 255, 0)
ORANGE = (255, 140, 0)
WHITE = (255, 255, 255)
WHITE_LOW = (100, 100, 100)
class Matrix:
def __init__(self, config):
self.logger = logging.getLogger('matrix')
self.config = config
#init leds
self.ADDRESS = 'localhost:7890'
# Create a client object
self.led_client = opc.Client(self.ADDRESS)
# Test if it can connect (optional)
if self.led_client.can_connect():
self.logger.info('connected to FadeCandy %s' % self.ADDRESS)
else:
# We could exit here, but instead let's just print a warning
# and then keep trying to send pixels in case the server
# appears later
self.logger.error('WARNING: could not connect to %s' % self.ADDRESS)
self.num_channels = int(self.config.get('leds', 'num_channels'))
self.NUM_LEDS = int(self.config.get('leds', 'num_leds'))
self.my_pixels = [(0,0,0)] * self.NUM_LEDS
self.led_client.put_pixels(self.my_pixels)
#map out a matrix that represents the physical layout of the display
self.led_map = [
[63, 48, 47, 32, 31, 16, 15, 0],
[62, 49, 46, 33, 30, 17, 14, 1],
[61, 50, 45, 34, 29, 18, 13, 2],
[60, 51, 44, 35, 28, 19, 12, 3],
[59, 52, 43, 36, 27, 20, 11, 4],
[58, 53, 42, 37, 26, 21, 10, 5],
[57, 54, 41, 38, 25, 22, 9, 6],
[56, 55, 40, 39, 24, 23, 8, 7],
]
#x axis
self.WIDTH = 8
#y axis
self.HEIGHT = 8
#initialize matrix with zeros
self.led_matrix = [[(0,0,0) for x in range(self.WIDTH)] for y in range(self.HEIGHT)]
def update(self):
for x in range(self.WIDTH):
for y in range(self.HEIGHT):
self.my_pixels[self.led_map[x][y]] = self.led_matrix[x][y]
self.led_client.put_pixels(self.my_pixels)
def set_pixel(self, x, y, color):
self.led_matrix[x][y] = color
def get_pixel(self, x, y):
return self.led_matrix[x][y]
def set_matrix(self, color):
for x in range(self.WIDTH):
for y in range(self.HEIGHT):
self.led_matrix[x][y] = color
def stop(self):
self.logger.info('Turning off leds')
self.my_pixels = [(0,0,0)] * self.NUM_LEDS
self.led_client.put_pixels(self.my_pixels)