Ejemplo n.º 1
0
class LightController(object):
    def __init__(self, analyzer, config):
        self.analyzer = ref(analyzer)

        self.songConfig = SongConfig(config)

        GPIO.setmode(GPIO.BCM)
        for pin in pins:
            GPIO.setup(pin, GPIO.OUT)
            GPIO.output(pin, False)

        self.lastLightUpdate = datetime.datetime.now()
        self.previousLightStates = [False] * analyzer.frequencyBands

    def _onChunk(self):
        now = datetime.datetime.now()
        if (now - self.lastLightUpdate).total_seconds() > delayBetweenUpdates:
            self.lastLightUpdate = now

            spectrum = self.analyzer().spectrum
            bands = [spectrum[i] for i in self.songConfig.frequencyBandOrder]
            lightStates = [level > self.songConfig.frequencyThresholds[channel] for channel, level in enumerate(bands)]

            for channel, value in enumerate(lightStates):
                if not value:
                    if self.previousLightStates[channel]:
                        lightStates[channel] = bands[channel] > self.songConfig.frequencyOffThresholds[channel]

            for channel, value in enumerate(lightStates):
                if self.previousLightStates[channel] != value:
                    GPIO.output(pins[channel], value)

            self.previousLightStates = lightStates

    def onMessage(self, message):
        messageType = message[0]

        if messageType == "songChange":
            self.songConfig.loadSongSettings(*message[1:])

        elif messageType == "chunk":
            self._onChunk()
Ejemplo n.º 2
0
    def __init__(self, analyzer, config):
        self.analyzer = ref(analyzer)

        self.songConfig = SongConfig(config)

        GPIO.setmode(GPIO.BCM)
        for pin in pins:
            GPIO.setup(pin, GPIO.OUT)
            GPIO.output(pin, False)

        self.lastLightUpdate = datetime.datetime.now()
        self.previousLightStates = [False] * analyzer.frequencyBands
Ejemplo n.º 3
0
    def __init__(self, analyzer, config):
        self.analyzer = ref(analyzer)

        self.songConfig = SongConfig(config)

        ansi.info('Serial connecting to {} at {} bps', serialDevice, serialSpeed)
        self.serial = serial.Serial(serialDevice, serialSpeed, timeout=1)

        # Assume we're already started unless we're using the USB interface to the Arduino.
        self.ready = 'ttyACM' not in serialDevice
        while not self.ready:
            if self.readFromSerial().startswith('LSDG Holiday Light controller ver '):
                self.ready = True

        self.lastLightUpdate = datetime.datetime.now()
        self.previousLightStates = [False] * analyzer.frequencyBands
        self.lightUpdateTimes = [time.time() for x in range(int(config.get('spectrum', 'frequencyBands')))]
Ejemplo n.º 4
0
class LightController(object):
    def __init__(self, analyzer, config):
        self.analyzer = ref(analyzer)

        self.songConfig = SongConfig(config)

        ansi.info('Serial connecting to {} at {} bps', serialDevice, serialSpeed)
        self.serial = serial.Serial(serialDevice, serialSpeed, timeout=1)

        # Assume we're already started unless we're using the USB interface to the Arduino.
        self.ready = 'ttyACM' not in serialDevice
        while not self.ready:
            if self.readFromSerial().startswith('LSDG Holiday Light controller ver '):
                self.ready = True

        self.lastLightUpdate = datetime.datetime.now()
        self.previousLightStates = [False] * analyzer.frequencyBands
        self.lightUpdateTimes = [time.time() for x in range(int(config.get('spectrum', 'frequencyBands')))]

    def readFromSerial(self):
        data = self.serial.readline()
        if serialDebug:
            ansi.stdout(
                    "{style.bold.fg.black} Arduino -> RPi:{style.none} {data!r}",
                    data=data
                    )
        return data

    def writeToSerial(self, data):
        if serialDebug:
            ansi.stdout(
                    "{style.bold.fg.black}Arduino <- RPi :{style.none} {data!r}",
                    data=data
                    )
        self.serial.write(data)

    def _onChunk(self):
        spectrum = self.analyzer().spectrum

        if spectrum is not None:
            spectrumBands = len(spectrum) - 1
            bands = [spectrum[min(i, spectrumBands)] for i in self.songConfig.frequencyBandOrder]
            lightStates = [level > self.songConfig.frequencyThresholds[channel] for channel, level in enumerate(bands)]

            changeCmd = []
            for channel, value in enumerate(lightStates):
                if self.previousLightStates[channel] != value  and (time.time() - self.lightUpdateTimes[channel] > 0.2):
                    self.lightUpdateTimes[channel] = time.time()
                    changeCmd.append('p{}s{}'.format(channel, 1 if value else 0))

            if changeCmd:
                self.writeToSerial(''.join(changeCmd) + '\n')

                self.previousLightStates = lightStates

    def onMessage(self, message):
        messageType = message[0]

        if messageType == 'songChange':
            self.songConfig.loadSongSettings(*message[1:])

        elif messageType == 'chunk':
            self._onChunk()