-
Notifications
You must be signed in to change notification settings - Fork 0
/
brokenClock.py
104 lines (83 loc) · 3.61 KB
/
brokenClock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import config
import ezdxf
import random
from drawDxf import calculate_clock_position, draw_polyline_boarder
from clockFuntion import draw_clock
def get_led_index(n):
# Constants for the 3x3 LED tiles
leds_per_tile_side = 3
leds_per_tile = leds_per_tile_side ** 2
tiles_per_row = config.grid_cols // leds_per_tile_side
# Calculate overall LED position
r = n // config.grid_cols # Overall row
c = n % config.grid_cols # Overall column
# Determine tile row and adjust column for snaking at the tile level
tile_row = r // leds_per_tile_side
tile_column = c // leds_per_tile_side
if tile_row % 2 == 1: # Reverse direction for snaking rows
tile_column = tiles_per_row - 1 - tile_column
# Calculate position within the tile
within_tile_row = r % leds_per_tile_side
within_tile_column = c % leds_per_tile_side
# Adjust the within-tile column for snaking tiles in reverse rows
# No need to adjust within-tile positions since tiles themselves are not snaking internally
# Calculate the linear index within the tile and overall index
index_in_tile = within_tile_row * leds_per_tile_side + within_tile_column
tile_linear_index = tile_row * tiles_per_row + tile_column
overall_index = tile_linear_index * leds_per_tile + index_in_tile
return overall_index
def main():
doc = ezdxf.new('R2010')
unit_codes = {
"mm": 4, # Millimeters
"cm": 5, # Centimeters
"feet": 2, # Feet
"inches": 1 # Inches
}
doc.header['$INSUNITS'] = unit_codes[config.units.lower().strip()]
msp = doc.modelspace()
times = list(range(720))
if config.randomize_clocks:
random.shuffle(times)
time_to_position = {}
for i, time in enumerate(times):
hour = time // 60
minute = time % 60
row, col = divmod(i, config.grid_cols)
center_x, center_y = calculate_clock_position(row, col)
draw_clock(config.clock_style, (center_x, center_y), config.clock_diameter / 2, hour, minute,
msp)
# Formatting time for 12-hour clock with AM/PM notation
formatted_time = f"{hour % 12 if hour % 12 else 12}:{minute:02d}"
time_to_position[formatted_time] = {"col": col, "row": row}
draw_polyline_boarder(msp)
# Save the DXF document
doc.saveas("720_clocks.dxf")
# Output the time to position mapping
led_indices = [-1] * 720
for time, pos in time_to_position.items():
hour, minute = [int(x) for x in time.split(':')]
minute_index = (hour % 12) * 60 + minute
led_index = pos['row'] * config.grid_cols + pos['col']
led_indices[minute_index] = led_index
# output led indexes for arduino library.
with open("./arduino/led_arrays.h", "w") as file:
header = "#ifndef LED_ARRAYS\n#define LED_ARRAYS \n\n"
footer = "\n#endif"
file.write(header)
file.write("const uint16_t ledMap[720] PROGMEM = {")
for index in led_indices:
file.write(f" {index},")
file.write("};\n")
file.write("const uint16_t matrixMap[720] PROGMEM = {")
lookup_table = [get_led_index(n) for n in range(720)]
for index in lookup_table:
file.write(f" {index},")
file.write("};\n")
file.write(footer)
# output mapping index in human-readable format.
with open("720_clocks_index.txt", "w") as file:
for time, pos in sorted(time_to_position.items(), key=lambda x: x[1]['row'] * config.grid_cols + x[1]['col']):
file.write(f'"{time}": {{"col": {pos["col"]}, "row": {pos["row"]}}},\n')
if __name__ == "__main__":
main()