diff --git a/lumen.py b/lumen.py index c405135..3dc04e4 100644 --- a/lumen.py +++ b/lumen.py @@ -15,6 +15,7 @@ #lumenCommand = { 'animation' : 'None' } num_pixels = 12 +max_bright = 255 if os.environ.get('PIXELS') != None: num_pixels = int(os.environ.get('PIXELS')) @@ -199,6 +200,21 @@ def do_PUT(self): self.send_header("Content-type", "json") self.end_headers() self.wfile.write(bytes('{"response":"fail"}', "utf-8")) + elif self.path == "/lumen/max_bright": + length = self.headers['Content-Length'] + global max_bright + max_bright = parseMaxBright(self.rfile.read(int(length)).decode("utf-8")) + if max_bright != None: + self.send_response(200) + self.send_header("Content-type", "json") + self.end_headers() + self.wfile.write(bytes('{"response":"ok"}', "utf-8")) + logging.debug("new max bright value: " + str(max_bright)) + else: + self.send_response(400) + self.send_header("Content-type", "json") + self.end_headers() + self.wfile.write(bytes('{"response":"fail"}', "utf-8")) else: self.send_response(404) self.end_headers() @@ -218,18 +234,26 @@ def valueTransition(valueFrom, valueTo): def pixelFillWrapper(color): if os.environ.get('SKIP_PIXELS') == None: - pixels.fill(color) + pixels.fill(apply_bright(color, max_bright)) pixels.show() + else: + logging.debug(msg="color: " + str(color) + ", max_bright: " + str(max_bright)) def pixelWrapper(pixel, color): try: if os.environ.get('SKIP_PIXELS') == None: - pixels[pixel] = color + pixels[pixel] = apply_bright(color, max_bright) else: - logging.debug(msg="pixel: " + str(pixel) + ", color: " + str(color) ) + logging.debug(msg="pixel: " + str(pixel) + ", color: " + str(color) + ", max_bright: " + str(max_bright)) except Exception as e: logging.debug("doh! ", exc_info=e) +def parseMaxBright(payload): + try: + return json.loads(payload).get('max_bright',255) + except: + return 255 + def parseCommand(payload): command = {} try: