Skip to content

Commit

Permalink
Merge pull request #2 from gaussmeter/max_bright
Browse files Browse the repository at this point in the history
Max bright
  • Loading branch information
LelandSindt committed Jul 2, 2022
2 parents 4db9fa7 + 96da040 commit de297b9
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions lumen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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()
Expand All @@ -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 clamp(scale(json.loads(payload).get('max_bright',255), 0, 100, 0, 255), 0, 255)
except:
return 255

def parseCommand(payload):
command = {}
try:
Expand All @@ -243,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))
Expand All @@ -267,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)
Expand Down

0 comments on commit de297b9

Please sign in to comment.