From b51d804242283113a7407e736faf46b20d0394de Mon Sep 17 00:00:00 2001 From: Leland Sindt Date: Tue, 7 Sep 2021 21:11:16 -0500 Subject: [PATCH 1/2] maximum brightness --- lumen.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) 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: From 96da040eafdcb169494944499390cc4baedb4fb5 Mon Sep 17 00:00:00 2001 From: Leland Sindt Date: Fri, 17 Sep 2021 21:36:33 -0500 Subject: [PATCH 2/2] scale and clamp brightness --- lumen.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lumen.py b/lumen.py index 3dc04e4..df1dc1b 100644 --- a/lumen.py +++ b/lumen.py @@ -250,7 +250,7 @@ def pixelWrapper(pixel, color): def parseMaxBright(payload): try: - return json.loads(payload).get('max_bright',255) + return clamp(scale(json.loads(payload).get('max_bright',255), 0, 100, 0, 255), 0, 255) except: return 255 @@ -267,7 +267,7 @@ def parseCommand(payload): command['length'] = percent if command['length'] > 100: command['length'] = 100 - command['bright'] = command.get('bright', 255) + command['bright'] = clamp(scale(command.get('bright', 100), 0, 100, 0, 255), 0, 255) command['velocity'] = command.get('velocity', 100) command['r'] = int(command.get('r', 0)) command['g'] = int(command.get('g', 0)) @@ -291,6 +291,12 @@ def parseCommand(payload): command['w2'] = int(rgbw2.split(',')[3]) return command +def scale(x, in_min, in_max, out_min, out_max): + return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) + +def clamp(num, min_value, max_value): + return max(min(num, max_value), min_value) + lumenCommand = parseCommand('{}') myServer = HTTPServer(('', hostPort), MyServer)