Modified app.py to migrate to server-side rendering
This commit is contained in:
parent
ebb25aee9c
commit
1819bbfa2e
@ -11,7 +11,7 @@ import json
|
||||
|
||||
# Set to True to run without a physical BLE device for testing purposes.
|
||||
# Set to False to connect to the actual lamp matrix.
|
||||
DEBUG_MODE = False
|
||||
DEBUG_MODE = True
|
||||
|
||||
# --- BLE Device Configuration (Ignored in DEBUG_MODE) ---
|
||||
DEVICE_NAME = "Pupilometer LED Billboard"
|
||||
@ -65,7 +65,7 @@ def get_spiral_address(row, col, spiral_map):
|
||||
|
||||
SPIRAL_MAP_5x5 = create_spiral_map(5)
|
||||
|
||||
async def set_full_matrix_on_ble(full_matrix):
|
||||
async def set_full_matrix_on_ble(serial_colors):
|
||||
global ble_client
|
||||
global ble_characteristics
|
||||
|
||||
@ -78,25 +78,6 @@ async def set_full_matrix_on_ble(full_matrix):
|
||||
else:
|
||||
print("Confirmed BLE connection status. Proceeding with lamp update.")
|
||||
|
||||
print("Initializing blank canvas...")
|
||||
serial_colors = [b'\x00\x00\x00'] * lampAmount
|
||||
print(f"Initialized: {serial_colors}")
|
||||
|
||||
for row in range(5):
|
||||
for col in range(5):
|
||||
|
||||
lamp_data = full_matrix[row][col]
|
||||
print(f"Construcing lamp {row},{col} data: {lamp_data}")
|
||||
ww = int(lamp_data['ww'])
|
||||
cw = int(lamp_data['cw'])
|
||||
blue = int(lamp_data['blue'])
|
||||
|
||||
color_bytes = bytes([ww, cw, blue])
|
||||
spiral_pos = get_spiral_address(row, col, SPIRAL_MAP_5x5)
|
||||
print(f"Constructed data for {spiral_pos}: {color_bytes}")
|
||||
if spiral_pos != -1:
|
||||
serial_colors[spiral_pos] = color_bytes
|
||||
|
||||
# =====================================================================
|
||||
# SNIPPET TO PATCH SWAPPED LAMP POSITIONS
|
||||
# =====================================================================
|
||||
@ -158,6 +139,43 @@ async def connect_to_ble_device():
|
||||
except Exception as e:
|
||||
print(f"An error occurred during BLE connection: {e}")
|
||||
return False
|
||||
# =================================================================================================
|
||||
# COLOR MIXING
|
||||
# =================================================================================================
|
||||
|
||||
def calculate_rgb(ww, cw, blue):
|
||||
"""
|
||||
Calculates the combined RGB color from warm white, cool white, and blue light values.
|
||||
This function is a Python equivalent of the JavaScript color mixer in index.html.
|
||||
"""
|
||||
# Define the RGB components for each light source based on slider track colors
|
||||
warm_white_r, warm_white_g, warm_white_b = 255, 192, 128
|
||||
cool_white_r, cool_white_g, cool_white_b = 192, 224, 255
|
||||
blue_r, blue_g, blue_b = 0, 0, 255
|
||||
|
||||
# Normalize the slider values (0-255) and apply them to the base colors
|
||||
r = (ww / 255) * warm_white_r + (cw / 255) * cool_white_r + (blue / 255) * blue_r
|
||||
g = (ww / 255) * warm_white_g + (cw / 255) * cool_white_g + (blue / 255) * blue_g
|
||||
b = (ww / 255) * warm_white_b + (cw / 255) * cool_white_b + (blue / 255) * blue_b
|
||||
|
||||
# Clamp the values to 255 and convert to integer
|
||||
r = min(255, round(r))
|
||||
g = min(255, round(g))
|
||||
b = min(255, round(b))
|
||||
|
||||
return r, g, b
|
||||
|
||||
def rgb_to_hex(r, g, b):
|
||||
"""
|
||||
Converts RGB color values to a hex color string.
|
||||
"""
|
||||
# Ensure values are within the valid range (0-255) and are integers
|
||||
r = int(max(0, min(255, r)))
|
||||
g = int(max(0, min(255, g)))
|
||||
b = int(max(0, min(255, b)))
|
||||
|
||||
# Convert each component to a two-digit hexadecimal string
|
||||
return f'#{r:02x}{g:02x}{b:02x}'
|
||||
|
||||
# =================================================================================================
|
||||
# FLASK APPLICATION
|
||||
@ -170,6 +188,7 @@ lamp_matrix = [['#000000' for _ in range(5)] for _ in range(5)]
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
print(f"Getting current lamp matrix info: {lamp_matrix}")
|
||||
if DEBUG_MODE:
|
||||
return render_template('index.html', matrix=lamp_matrix)
|
||||
else:
|
||||
@ -182,30 +201,34 @@ def index():
|
||||
def set_matrix():
|
||||
data = request.get_json()
|
||||
full_matrix = data.get('matrix', [])
|
||||
|
||||
matrixDataSerialized = []
|
||||
if not full_matrix or len(full_matrix) != 5 or len(full_matrix[0]) != 5:
|
||||
return jsonify(success=False, message="Invalid matrix data received"), 400
|
||||
else:
|
||||
print(f"Received the following matrix data: {full_matrix}")
|
||||
|
||||
#Creating empty byte array
|
||||
serial_colors = [b'\x00\x00\x00'] * lampAmount
|
||||
|
||||
try:
|
||||
if DEBUG_MODE:
|
||||
# === DEBUG MODE: Update in-memory matrix ===
|
||||
for row in range(5):
|
||||
for col in range(5):
|
||||
lamp_data = full_matrix[row][col]
|
||||
ww = lamp_data['ww']
|
||||
cw = lamp_data['cw']
|
||||
blue = lamp_data['blue']
|
||||
|
||||
# Convert ww, cw, blue to a hex color for UI display
|
||||
r = min(255, ww + cw)
|
||||
g = min(255, ww + cw + blue)
|
||||
b = min(255, blue + cw)
|
||||
new_color_hex = f'#{r:02x}{g:02x}{b:02x}'
|
||||
lamp_matrix[row][col] = new_color_hex
|
||||
ww = int(lamp_data['ww'])
|
||||
cw = int(lamp_data['cw'])
|
||||
blue = int(lamp_data['blue'])
|
||||
|
||||
#Preparing byte data for control command
|
||||
color_bytes = bytes([ww, cw, blue])
|
||||
spiral_pos = get_spiral_address(row, col, SPIRAL_MAP_5x5)
|
||||
print(f"Constructed data for {spiral_pos}: {color_bytes}")
|
||||
if spiral_pos != -1:
|
||||
serial_colors[spiral_pos] = color_bytes
|
||||
#Preparing hex color data for frontend
|
||||
lampColorR, lampColorG, lampColorB = calculate_rgb(ww,cw,blue)
|
||||
lamp_matrix[row][col] = rgb_to_hex(lampColorR, lampColorG, lampColorB)
|
||||
if DEBUG_MODE:
|
||||
# === DEBUG MODE: Update in-memory matrix ===
|
||||
return jsonify(success=True)
|
||||
else:
|
||||
# === LIVE MODE: Communicate with the BLE device ===
|
||||
@ -214,11 +237,12 @@ def set_matrix():
|
||||
ble_event_loop
|
||||
)
|
||||
return jsonify(success=True)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in set_matrix route: {e}")
|
||||
return jsonify(success=False, message=str(e)), 500
|
||||
|
||||
print(f"Getting current lamp matrix info: {lamp_matrix}")
|
||||
|
||||
# =================================================================================================
|
||||
# APP STARTUP
|
||||
# =================================================================================================
|
||||
|
||||
Loading…
Reference in New Issue
Block a user