示例#1
0
class Renderer(object):
    def __init__(self):
        self.client = Client(settings.FC_SERVER + ':' + str(settings.FC_PORT))
        self.brightness = 0.5
        self.pixels = [
            [1, 1, 1],
            [1, 1, 1],
            [1, 1, 1],
            [1, 1, 1]
        ]
        self.render_pixels = self.calculate_pixels()
        self.render_thread = Thread(target=self.render)
        self.render_thread.daemon = True
        self.render_thread.start()

    def calculate_pixels(self):
        ret_val = []
        for pixel in self.pixels:
            px = [(
                int(pixel[0] * self.brightness * settings.RGB_MAX),
                int(pixel[1] * self.brightness * settings.RGB_MAX),
                int(pixel[2] * self.brightness * settings.RGB_MAX)
             )] * settings.PIXEL_COUNT

            ret_val.extend(px)
        return ret_val

    def update(self):
        self.render_pixels = self.calculate_pixels()

    def render(self):
        while True:
            self.client.put_pixels(self.render_pixels)
            sleep(1.0 / settings.FRAMES)
示例#2
0
class Driver:
    def __init__(self):
        print 'Initializing Driver'
        session = Session()
        self.modules = session.query(LightModule).all()
        self.strips = {s.name: s for s in session.query(Strip).all()}
        session.close()
        print 'starting engine'

        self.engine = LightEngine(self.modules)
        print 'engine running'
        self.sp = Spotify()
        print 'spotify loaded'
        if SIM:
            self.sim = Client('127.0.0.1:7890')

    def act_on_kf(self, kf, strip):
        for ctrl in kf.controls:
            ctrl = ctrl.create()
            if ctrl.type == 'intensity':
                strip.add_intensity_control(ctrl, clear=kf.clear)
            if ctrl.type == 'color':
                strip.add_color_control(ctrl, clear=kf.clear)
                print strip.color_controls
            if ctrl.type == 'combo':
                strip.add_combo_control(ctrl, clear=kf.clear)

    def run(self, track_id):
        with open(join(CDP, track_id)) as f:
            data = json.load(f)
            print data
        show = Show()
        show.load(data)
        self.show = show
        #self.visited_kfs = set()
        self.idxs = {str(o): 0 for o in show.__dict__}
        self.start_pos = self.sp.get_player_pos()
        self.start_time = time.time() - self.start_pos
        self.update()

    def update_strip(self, strip, time):
        curr_idx = self.idxs[strip.name]
        kfs = getattr(self.show, strip.name)
        if len(kfs) == 0:
            return
        for kf in kfs[curr_idx:]:
            if kf.time <= time:
                self.act_on_kf(kf, strip)
                #self.visited_kfs.add(kf)
                curr_idx += 1
        self.idxs[strip.name] = curr_idx

    def update(self):
        curr_time = time.time() - self.start_time
        if SIM:
            pixels = []
        for o in self.show.__dict__:
            strip = self.strips[o]
            self.update_strip(strip, curr_time)
            if SIM:
                strip.compile(curr_time)
                pixels += strip.compiled_rgb
        if SIM:
            self.sim.put_pixels(pixels)
        t = threading.Timer(1.0 / REFRESH_RATE, self.update)
        t.setDaemon(True)
        t.start()
import json

import time

from opc import Client

with open("stop_configuration.json") as config:
    stops = json.load(config)

current_led = 0
total_leds = 64 * 8
client = Client('localhost:7890')

while True:
    for stop_id, stop_config in stops.items():
        print("%s: %s (%s)" %
              (stop_config['led'], stop_id, stop_config['name']))
        pixels = [(0, 0, 0)] * total_leds
        pixels[stop_config['led']] = (255, 255, 255)
        client.put_pixels(pixels)
        client.put_pixels(pixels)
        time.sleep(0.1)