Skip to content

Commit

Permalink
scale and clamp brightness
Browse files Browse the repository at this point in the history
  • Loading branch information
LelandSindt committed Sep 18, 2021
1 parent b51d804 commit 96da040
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lumen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
Expand All @@ -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)
Expand Down

0 comments on commit 96da040

Please sign in to comment.