131 lines
3.7 KiB
Python
131 lines
3.7 KiB
Python
from flask import Flask, render_template, request, jsonify
|
|
|
|
# ... (paste the two helper functions here if they are not already present) ...
|
|
|
|
app = Flask(__name__)
|
|
|
|
def create_spiral_map(n=5):
|
|
"""
|
|
Creates a pre-computed spiral mapping for an n x n matrix.
|
|
The spiral starts from the center, moves up, then right, and spirals outwards.
|
|
|
|
Args:
|
|
n (int): The size of the square matrix.
|
|
|
|
Returns:
|
|
list of lists: A 2D list where each element is the spiraling address.
|
|
"""
|
|
if n % 2 == 0:
|
|
raise ValueError("Matrix size must be odd for a unique center point.")
|
|
|
|
spiral_map = [[0] * n for _ in range(n)]
|
|
|
|
# Starting position and index
|
|
r, c = n // 2, n // 2
|
|
address = 0
|
|
spiral_map[r][c] = address
|
|
|
|
# Directions: Up, Right, Down, Left
|
|
# This is the key change to adjust the spiral path
|
|
dr = [-1, 0, 1, 0]
|
|
dc = [0, 1, 0, -1]
|
|
direction = 0
|
|
|
|
segment_length = 1
|
|
steps = 0
|
|
|
|
while address < n * n - 1:
|
|
for _ in range(segment_length):
|
|
address += 1
|
|
r += dr[direction]
|
|
c += dc[direction]
|
|
if 0 <= r < n and 0 <= c < n:
|
|
spiral_map[r][c] = address
|
|
|
|
direction = (direction + 1) % 4
|
|
steps += 1
|
|
|
|
if steps % 2 == 0:
|
|
segment_length += 1
|
|
|
|
return spiral_map
|
|
|
|
def get_spiral_address(row, col, spiral_map):
|
|
"""
|
|
Converts a standard (row, col) address to a spiraling address
|
|
using a pre-computed map.
|
|
|
|
Args:
|
|
row (int): The row index (0-indexed).
|
|
col (int): The column index (0-indexed).
|
|
spiral_map (list of lists): The pre-computed spiral map.
|
|
|
|
Returns:
|
|
int: The spiraling address, or -1 if the coordinates are out of bounds.
|
|
"""
|
|
n = len(spiral_map)
|
|
if 0 <= row < n and 0 <= col < n:
|
|
return spiral_map[row][col]
|
|
else:
|
|
return -1
|
|
|
|
# Pre-compute the spiral map for a 5x5 matrix when the app starts
|
|
SPIRAL_MAP_5x5 = create_spiral_map(5)
|
|
|
|
# Initialize a 5x5 matrix with all lamps off (black color code)
|
|
lamp_matrix = [['#000000' for _ in range(5)] for _ in range(5)]
|
|
|
|
# Define a helper function to convert hex to RGB for template use
|
|
def hex_to_rgb(hex_color):
|
|
hex_color = hex_color.lstrip('#')
|
|
return {
|
|
'r': int(hex_color[0:2], 16),
|
|
'g': int(hex_color[2:4], 16),
|
|
'b': int(hex_color[4:6], 16)
|
|
}
|
|
|
|
app.jinja_env.globals.update(hex_to_rgb=hex_to_rgb)
|
|
|
|
@app.route('/')
|
|
def index():
|
|
"""
|
|
Renders the main control interface for the RGB lamp matrix.
|
|
"""
|
|
return render_template('index.html', matrix=lamp_matrix)
|
|
|
|
@app.route('/set_color', methods=['POST'])
|
|
def set_color():
|
|
"""
|
|
Sets the color for multiple lamps and turns all others black.
|
|
"""
|
|
data = request.get_json()
|
|
lamps_to_update = data.get('lamps', [])
|
|
r = data.get('r')
|
|
g = data.get('g')
|
|
b = data.get('b')
|
|
|
|
try:
|
|
r, g, b = int(r), int(g), int(b)
|
|
new_color = '#{:02x}{:02x}{:02x}'.format(r, g, b)
|
|
|
|
# First, turn all lamps black
|
|
for row in range(5):
|
|
for col in range(5):
|
|
lamp_matrix[row][col] = '#000000'
|
|
|
|
# Then, apply the new color to the selected lamps
|
|
for lamp in lamps_to_update:
|
|
row = lamp['row']
|
|
col = lamp['col']
|
|
if 0 <= row < 5 and 0 <= col < 5:
|
|
lamp_matrix[row][col] = new_color
|
|
print(get_spiral_address(row, col, SPIRAL_MAP_5x5))
|
|
|
|
return jsonify(success=True, new_color=new_color)
|
|
|
|
except (ValueError, TypeError, IndexError):
|
|
return jsonify(success=False, message="Invalid data received")
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True,host='0.0.0.0')
|