def getPredefinedColor(self, name): for color in self.server.config.getValue("userDefinedColors"): if color["name"] == name: return LEDState(color["values"]["red"], color["values"]["green"], color["values"]["blue"]) return LEDState(0.0, 0.0, 0.0)
def _startProgram(self): if "name" not in self._jsonBody: self.send_response(400, "no program name in request body") self.end_headers() return progName = self._jsonBody["name"] try: if progName == "wheel": self._startWheel() elif progName == "sunrise": self._startSunrise() elif progName == "freak": self._startFreak() elif progName == "predefined": self._startPredefined() elif progName == "single": self._startSingle() elif progName == "softOff": logging.getLogger("main").info(progName) self.server.ledManager.startProgram( SmoothNextColorProgram(LEDState(0.0, 0.0, 0.0, 1.0), 1, 3)) elif progName == "off": logging.getLogger("main").info(progName) self.server.ledManager.startProgram(OffProgram()) elif progName == "colorloop": self._startColorLoop() elif progName == "white": logging.getLogger("main").info(progName) self.server.ledManager.startProgram( SmoothNextColorProgram(LEDState(1.0, 1.0, 1.0, 1.0), 0.5, 2)) elif progName == "feed": logging.getLogger("main").info(progName) self.server.ledManager.startProgram( SmoothNextColorProgram( LEDState( self.server.config.getValue( "programs/feed/brightness"), 0.0, 0.0, 1.0), 0.5, 3)) elif progName == "randomPath": logging.getLogger("main").info(progName) self.server.ledManager.startProgram( RandomPathProgram( self.getPredefinedColors(), self.server.config.getValue( "programs/randomPath/timePerColor"))) elif progName == "scheduledOff": self._startScheduledOff() elif progName == "cancelScheduledOff": self.server.ledManager.cancelPowerOff() else: self.send_response(400, "invalid program name " + progName) self.end_headers() self.send_response(200) self.end_headers() except ParameterExeption as e: self.send_response(400, e) self.end_headers()
def __init__(self, duration): colorPath = [ LEDState(0.0, 0.0, 0.0), LEDState(0.0, 0.0, 0.2), LEDState(0.5, 0.5, 0.2), LEDState(1.0, 0.9, 0.2), ] interpolationPoints = 60 timePerColor = duration / ((len(colorPath) - 1) * interpolationPoints) super().__init__(colorPath, interpolationPoints, timePerColor)
def __init__(self, timePerColor=3): programs = [ SingleColorProgram(LEDState(1.0, 0.0, 0.0, 0.0)), WaitProgram(timePerColor), SingleColorProgram(LEDState(0.0, 1.0, 0.0, 0.0)), WaitProgram(timePerColor), SingleColorProgram(LEDState(0.0, 0.0, 1.0, 0.0)), WaitProgram(timePerColor), SingleColorProgram(LEDState(0.0, 0.0, 0.0, 0.0)) ] super().__init__(programs)
def run(self): self._waitIfNotStopped(self.secondsPerColor) for i in range(0, self.stepsPerColor): for j in range(0, self.stepsPerColor): for k in range(0, self.stepsPerColor): r = i / (self.stepsPerColor - 1) g = j / (self.stepsPerColor - 1) b = k / (self.stepsPerColor - 1) logging.getLogger("main").debug("r: {}, g: {}, b: {}".format(r, g, b)) self._setValue(LEDState(r, g, b, 1.0)) self._waitIfNotStopped(self.secondsPerColor) self._setValue(LEDState(0.0, 0.0, 0.0, 1.0))
def __init__(self, iterations, minValue, maxValue, timePerColor): self._minValue = min(1.0, max(0.0, minValue)) self._maxValue = min(1.0, max(0.0, maxValue)) colorPath = [ LEDState(maxValue, minValue, minValue), LEDState(minValue, maxValue, minValue), LEDState(minValue, minValue, maxValue) ] interpolationsPoints = 30 program = ColorPathProgram( colorPath, interpolationsPoints, float(timePerColor) / float(interpolationsPoints), True) super().__init__(program, iterations)
def getPredefinedColors(self): result = [] for color in self.server.config.getValue("userDefinedColors"): result.append( LEDState(color["values"]["red"], color["values"]["green"], color["values"]["blue"])) return result
def _writePiBlasterValue(self, channel, channelName, value): piBlasterPath = "/dev/pi-blaster" try: piblaster = open(piBlasterPath, "w") except: self._ledState = LEDState() errorMessage = "error opening " + piBlasterPath + " " + traceback.format_exc() raise PiBlasterUnavailableException(errorMessage) else: try: print("{}={}".format(channel, value), file=piblaster) piblaster.close() except: self._ledState = LEDState() errorMessage = "error writing {}={} to {} ({})".format(channel, value, piBlasterPath, channelName) piblaster.close() raise PiBlasterUnavailableException(errorMessage)
def powerOffWaiter(self, duration, cancelEvent): cancelEvent.wait(duration) if cancelEvent.is_set(): logging.getLogger("main").info("canceled power off") return logging.getLogger("main").info("wait finished starting SoftOffProgram") self.startProgram( SmoothNextColorProgram(LEDState(0.0, 0.0, 0.0, 1.0), 1, 3)) self._cancelPowerOffEvent = None
def run(self): ledValue = LEDState() ledValue.red = round(random.uniform(self._minColor, self._maxColor), 3) ledValue.green = round(random.uniform(self._minColor, self._maxColor), 3) ledValue.blue = round(random.uniform(self._minColor, self._maxColor), 3) if not self._lastValue is None and not self._lastValue.brightness is None: ledValue.brightness = self._lastValue.brightness else: ledValue.brightness = 1.0 self._setValue(ledValue) self._waitIfNotStopped(self._secondsPerColor)
def getInterpolationPoint(currentPoint, nextPoint, pointIndex, numberOfPoints): red = getInterpolationValue(currentPoint.red, nextPoint.red, pointIndex, numberOfPoints) green = getInterpolationValue(currentPoint.green, nextPoint.green, pointIndex, numberOfPoints) blue = getInterpolationValue(currentPoint.blue, nextPoint.blue, pointIndex, numberOfPoints) if nextPoint.brightness is not None: brightness = getInterpolationValue(currentPoint.brightness, nextPoint.brightness, pointIndex, numberOfPoints) else: brightness = currentPoint.brightness return LEDState(red, green, blue, brightness)
def run(self): r = round(random.uniform(0.0, 0.5), 3) g = round(random.uniform(0.0, 0.5), 3) b = round(random.uniform(0.0, 0.5), 3) while True: logging.getLogger("main").debug("r: {}, g: {}, b: {}".format(r, g, b)) self._setValue(LEDState(r, g, b, 1.0)) self._waitIfNotStopped(0.05) colorRand = randint(0, 2) if colorRand == 0: r = self._getNextValue(r) elif colorRand == 1: g = self._getNextValue(g) else: b = self._getNextValue(b)
def _startSingle(self): params = {"red": 0.0, "green": 0.0, "blue": 0.0} params = self.getParamsFromJson(params) logMethodAndParams(params) if not 0 <= params["red"] <= 255 or not 0 <= params[ "green"] <= 255 or not 0 <= params["blue"] <= 255: raise ParameterExeption( "invalid values red: {}, green: {}, blue: {}".format( params["red"], params["green"], params["blue"])) else: red = params["red"] / 255 green = params["green"] / 255 blue = params["blue"] / 255 self.server.ledManager.startProgram( SingleColorProgram( LEDState(red, green, blue, self.server.ledManager.getBrightness())))
def _startPredefined(self): params = {"colorName": ""} params = self.getParamsFromJson(params) logMethodAndParams(params) colors = [] predefinedColors = self.server.config.getValue("userDefinedColors") for color in predefinedColors: colors.append(color["name"]) if not params["colorName"] in colors: raise ParameterExeption(params["colorName"] + " not in " + str(colors)) else: for predefinedColor in predefinedColors: if params["colorName"] == predefinedColor["name"]: color = predefinedColor["values"] ledState = LEDState(color["red"], color["green"], color["blue"], self.server.ledManager.getBrightness()) self.server.ledManager.startProgram( SingleColorProgram(ledState)) break
class ColorSetter: def __init__(self, brightness): self._ledState = LEDState() self._ledState.brightness = brightness self._colorRounding = 5 def setBrightness(self, brightness): self._ledState.brightness = brightness if self._ledState.isComplete(): logging.getLogger("main").info( "resetting color after brightness change {}, {}".format( self._ledState.brightness, self._ledState.getColor())) self.setValue(self._ledState) def getBrightness(self): return self._ledState.brightness def _writePiBlasterValue(self, channel, channelName, value): piBlasterPath = "/dev/pi-blaster" try: piblaster = open(piBlasterPath, "w") except: self._ledState = LEDState() errorMessage = "error opening " + piBlasterPath + " " + traceback.format_exc() raise PiBlasterUnavailableException(errorMessage) else: try: print("{}={}".format(channel, value), file=piblaster) piblaster.close() except: self._ledState = LEDState() errorMessage = "error writing {}={} to {} ({})".format(channel, value, piBlasterPath, channelName) piblaster.close() raise PiBlasterUnavailableException(errorMessage) def getValueForPiBlaster(self): myFilter = PolynomialBrightnessFilter(2.0) filteredState = myFilter.filter(self._ledState) return {"red": round(filteredState.red * filteredState.brightness, self._colorRounding), "green": round(filteredState.green * filteredState.brightness, self._colorRounding), "blue": round(filteredState.blue * filteredState.brightness, self._colorRounding)} def setValue(self, ledState): if (ledState.brightness is not None and ledState.brightness != self._ledState.brightness): logging.getLogger("main").warning( "updating brightness in setValue from " + str(self._ledState.brightness) + " to " + str(ledState.brightness)) self._ledState.updateAvailableValues(ledState) if self._ledState.isComplete(): piValue = self.getValueForPiBlaster() self._writePiBlasterValue(17, "red", piValue["red"]) self._writePiBlasterValue(22, "green", piValue["green"]) self._writePiBlasterValue(24, "blue", piValue["blue"]) logging.getLogger("main").debug( "updated pi-blaster: red={}, green={}, blue={}".format( piValue["red"], piValue["green"], piValue["blue"])) try: statuspublisher.getStatusPublisher().publishStatus() except Exception as e: logging.getLogger("main").warning("Error during publishStatus " + str(e)) def getCurrentValue(self): return self._ledState
def run(self): self._setValue(LEDState(0.0, 0.0, 0.0, 1.0))
def run(self): r = round(random.uniform(0.0, 0.5), 3) g = round(random.uniform(0.0, 0.5), 3) b = round(random.uniform(0.0, 0.5), 3) stepsInOneDirection = 50 while True: colorRand = randint(0, 2) upDownRand = bool(random.getrandbits(1)) if colorRand == 0: if upDownRand: for i in range(0, stepsInOneDirection): r = max(0, r - round(random.uniform(0, self._maxDiff), 3)) logging.getLogger("main").debug( "r: {}, g: {}, b: {}".format(r, g, b)) self._setValue(LEDState(r, g, b, 0.0)) self._waitIfNotStopped(self._secondsPerColor) if r == 0: break else: for i in range(0, stepsInOneDirection): r = min(1, r + round(random.uniform(0, self._maxDiff), 3)) logging.getLogger("main").debug( "r: {}, g: {}, b: {}".format(r, g, b)) self._setValue(LEDState(r, g, b, 0.0)) self._waitIfNotStopped(self._secondsPerColor) if r == 1: break elif colorRand == 1: if upDownRand: for i in range(0, stepsInOneDirection): g = max(0, r - round(random.uniform(0, self._maxDiff), 3)) logging.getLogger("main").debug( "r: {}, g: {}, b: {}".format(r, g, b)) self._setValue(LEDState(r, g, b, 0.0)) self._waitIfNotStopped(self._secondsPerColor) if r == 0: break if upDownRand: for i in range(0, stepsInOneDirection): g = min(1, r + round(random.uniform(0, self._maxDiff), 3)) logging.getLogger("main").debug( "r: {}, g: {}, b: {}".format(r, g, b)) self._setValue(LEDState(r, g, b, 0.0)) self._waitIfNotStopped(self._secondsPerColor) if r == 1: break else: if upDownRand: for i in range(0, stepsInOneDirection): b = max(0, r - round(random.uniform(0, self._maxDiff), 3)) logging.getLogger("main").debug( "r: {}, g: {}, b: {}".format(r, g, b)) self._setValue(LEDState(r, g, b, 0.0)) self._waitIfNotStopped(self._secondsPerColor) if r == 0: break if upDownRand: for i in range(0, stepsInOneDirection): b = min(1, r + round(random.uniform(0, self._maxDiff), 3)) logging.getLogger("main").debug( "r: {}, g: {}, b: {}".format(r, g, b)) self._setValue(LEDState(r, g, b, 0.0)) self._waitIfNotStopped(self._secondsPerColor) if r == 1: break
def filter(self, ledstate): return LEDState(ledstate.red, ledstate.green, ledstate.blue, ledstate.brightness**self._degree)
def run(self): self.setLastValue( LEDState(self._maxValue, self._minValue, self._minValue, 1.0)) LoopedProgram.run(self)
def __init__(self, brightness): self._ledState = LEDState() self._ledState.brightness = brightness self._colorRounding = 5