Skip to content

Commit 67726d6

Browse files
committed
Add comments to lotus-led-matrix.py
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 175d814 commit 67726d6

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

lotus-led-matrix.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
#!/usr/bin/env python3
2+
# Script to generate the mapping of LEDs to the CS and SW registers of the
3+
# IS31FL3741A controller.
4+
#
5+
# The output looks like:
6+
# (0x00, 0), // x:1, y:1, sw:1, cs:1, id:1
7+
# (0x1e, 0), // x:2, y:1, sw:2, cs:1, id:2
8+
# [...]
29

310
from dataclasses import dataclass
411

12+
WIDTH = 9
13+
HEIGHT = 34
14+
515

616
@dataclass
717
class Led:
@@ -14,6 +24,7 @@ class Led:
1424
cs: int
1525

1626
def led_register(self):
27+
# See the IS31FL3741A for how the data pages are separated
1728
if self.cs <= 30 and self.sw >= 7:
1829
page = 1
1930
register = self.cs - 1 + (self.sw-7) * 30
@@ -22,63 +33,64 @@ def led_register(self):
2233
register = self.cs - 1 + (self.sw-1) * 30
2334
if self.cs >= 31:
2435
page = 1
25-
register = 0x5a + self.cs - 31 + (self.sw-1) * 9
36+
register = 0x5A + self.cs - 31 + (self.sw-1) * 9
2637
return (register, page)
2738

2839

2940
def get_leds():
3041
leds = []
3142

43+
# Generate LED mapping as how they are mapped in the Lotus LED Matrix Module
44+
3245
# First down and then right
3346
# CS1 through CS4
3447
for cs in range(1, 5):
35-
for sw in range(1, 10):
36-
leds.append(Led(id=9 * (cs-1) + sw, x=sw, y=cs, sw=sw, cs=cs))
48+
for sw in range(1, WIDTH+1):
49+
leds.append(Led(id=WIDTH * (cs-1) + sw, x=sw, y=cs, sw=sw, cs=cs))
3750

3851
# First right and then down
3952
# CS5 through CS7
4053
base_cs = 4
41-
base_id = 9 * base_cs
54+
base_id = WIDTH * base_cs
4255
for cs in range(1, 5):
43-
for sw in range(1, 10):
56+
for sw in range(1, WIDTH+1):
4457
leds.append(Led(id=base_id + 4 * (sw-1) + cs, x=sw,
4558
y=cs+base_cs, sw=sw, cs=cs+base_cs))
4659

4760
# First right and then down
4861
# CS9 through CS16
4962
base_cs = 8
50-
base_id = 9 * base_cs
63+
base_id = WIDTH * base_cs
5164
for cs in range(1, 9):
52-
for sw in range(1, 10):
65+
for sw in range(1, WIDTH+1):
5366
leds.append(Led(id=base_id + 8 * (sw-1) + cs, x=sw,
5467
y=cs+base_cs, sw=sw, cs=cs+base_cs))
5568

5669
# First right and then down
5770
# CS17 through CS32
5871
base_cs = 16
59-
base_id = 9 * base_cs
72+
base_id = WIDTH * base_cs
6073
for cs in range(1, 17):
61-
for sw in range(1, 10):
74+
for sw in range(1, WIDTH+1):
6275
leds.append(Led(id=base_id + 16 * (sw-1) + cs, x=sw,
6376
y=cs+base_cs, sw=sw, cs=cs+base_cs))
6477

6578
# First down and then right
6679
# CS33 through CS34
6780
base_cs = 32
68-
base_id = 9 * base_cs
81+
base_id = WIDTH * base_cs
6982
for cs in range(1, 3):
70-
for sw in range(1, 10):
83+
for sw in range(1, WIDTH+1):
7184
leds.append(Led(id=base_id + 9 * (cs-1) + sw, x=sw,
7285
y=cs+base_cs, sw=sw, cs=cs+base_cs))
7386

7487
return leds
7588

7689

7790
def main():
91+
# Assumes that the index in the LEDs list is: index = x + y * 9
7892
leds = get_leds()
7993

80-
# Assume that the index in the leds list is: index = x + y * 9
81-
8294
debug = False
8395

8496
for led in leds:
@@ -88,19 +100,18 @@ def main():
88100
else:
89101
print("(0x{:02x}, {}), // x:{}, y:{}, sw:{}, cs:{}, id:{}".format(
90102
register, page, led.x, led.y, led.sw, led.cs, led.id))
91-
92103
# print_led(leds, 0, 30)
93104

94105
# For debugging
95106

96107

97108
def get_led(leds, x, y):
98-
return leds[x + y * 9]
109+
return leds[x + y * WIDTH]
99110

100111

101112
# For debugging
102113
def print_led(leds, x, y):
103-
led = get_led(leds, 0, 30)
114+
led = get_led(leds, x, y)
104115
(register, page) = led.led_register()
105116
print(led, "(0x{:02x}, {})".format(register, page))
106117

0 commit comments

Comments
 (0)