Skip to content
Permalink
Browse files

make sure to always send a multiple of three leds

  • Loading branch information...
jabdoa2 committed Aug 30, 2019
1 parent 8138de7 commit bae40db64e1496506f44596d24b58dbe85241b09
@@ -181,16 +181,17 @@ def add_pixel(self, channel, led):
"""
if len(self.channels) < channel + 1:

channels_to_add = channel + 1 - len(self.channels)

self.channels += [list() for _ in range(channels_to_add)]
self.dirty_leds += [dict() for _ in range(channels_to_add)]
self.msg += [None for _ in range(channels_to_add)]

if len(self.channels[channel]) < led + 1:

leds_to_add = led + 1 - len(self.channels[channel])
new_total = led + 1
if new_total % 3 != 0:
new_total += 3 - (new_total % 3)
leds_to_add = new_total - len(self.channels[channel])
if leds_to_add > 0:
self.channels[channel] += [0 for _ in range(leds_to_add)]

def set_pixel_color(self, channel, pixel, callback: Callable[[int], Tuple[float, int]]):
@@ -9,4 +9,4 @@ lights:
type: grb
test_led3:
number: 1-99
type: grb
type: grb
@@ -2,3 +2,25 @@

config:
- config.yaml

lights:
test_rgbw:
channels:
red:
- number: 2-0
green:
- number: 2-1
blue:
- number: 2-2
white:
- number: 2-3
test_rgbw2:
channels:
red:
- number: 2-4
green:
- number: 2-5
blue:
- number: 2-6
white:
- number: 2-7
@@ -27,24 +27,24 @@ def setUp(self):
firmware = self._messages.pop(0)
self.assertEqual(firmware, b'\x00\xff\x00\x05\x00\x01\x00\x02\x00')

self.assertOpenPixelLedsSent({}, {})
self.assertOpenPixelLedsSent({}, {}, {})

def tearDown(self):
self._messages = []
super().tearDown()
# test that we blanked all LEDs at shutdown
self.assertOpenPixelLedsSent({}, {})
self.assertOpenPixelLedsSent({}, {}, {})

def _mock_loop(self):
self._mock_socket = MockSocket()
self.clock.mock_socket("localhost", 7890, self._mock_socket)
# connect socket to test
self._mock_socket.send = self._send_mock

def _build_message(self, channel, leds):
def _build_message(self, channel, leds, max_leds=300):
out = bytearray()
out.extend([channel, 0, 1, 44])
for i in range(0, 100):
out.extend([channel, 0, int(max_leds / 256), max_leds % 256])
for i in range(0, int(max_leds / 3)):
if i in leds:
out.extend([leds[i][0], leds[i][1], leds[i][2]])
else:
@@ -56,92 +56,98 @@ def _send_mock(self, message):
self._messages.append(message)
return len(message)

def assertOpenPixelLedsSent(self, leds1, leds2):
def assertOpenPixelLedsSent(self, leds1, leds2, leds3):
bank1 = self._build_message(0, leds1)
bank2 = self._build_message(1, leds2)
bank3 = self._build_message(2, leds3, 9)
found1 = False
found2 = False
found3 = False
for message in self._messages:
if not (message == bank1 or message == bank2):
if message == bank1:
found1 = True
elif message == bank2:
found2 = True
elif message == bank3:
found3 = True
else:
print("Received:", ":".join("{:02x}".format(c) for c in message))
print("Expected bank1:", ":".join("{:02x}".format(c) for c in bank1))
print("Expected bank2:",":".join("{:02x}".format(c) for c in bank2))
print("Expected bank3:", ":".join("{:02x}".format(c) for c in bank3))
raise AssertionError("Invalid Message")
if message == bank1:
found1 = True
else:
found2 = True

self.assertTrue(found1)
self.assertTrue(found2)
self.assertTrue(found3)
self._messages = []

def test_led_color(self):
# test led on channel 0. position 99
self.machine.lights.test_led.on()
self.machine.lights["test_led"].on()
self.advance_time_and_run(.02)
self._messages = []
self.advance_time_and_run(1)
self.assertOpenPixelLedsSent({99: (255, 255, 255)}, {})
self.assertOpenPixelLedsSent({99: (255, 255, 255)}, {}, {})

# test led 20 ond channel 0
self.machine.lights.test_led2.color(RGBColor((255, 0, 0)))
self.machine.lights["test_led2"].color(RGBColor((255, 0, 0)))
self.advance_time_and_run(.02)
self._messages = []
self.advance_time_and_run(1)
self.assertOpenPixelLedsSent({20: (255, 0, 0), 99: (255, 255, 255)}, {})
self.assertOpenPixelLedsSent({20: (255, 0, 0), 99: (255, 255, 255)}, {}, {})

self.machine.lights.test_led.off()
self.machine.lights["test_led"].off()
self.advance_time_and_run(.02)
self._messages = []
self.advance_time_and_run(1)
self.assertOpenPixelLedsSent({20: (255, 0, 0), 99: (0, 0, 0)}, {})
self.assertOpenPixelLedsSent({20: (255, 0, 0), 99: (0, 0, 0)}, {}, {})
self._messages = []

# test led color
self.machine.lights.test_led.color(RGBColor((2, 23, 42)))
self.machine.lights["test_led"].color(RGBColor((2, 23, 42)))
self.advance_time_and_run(.02)
self._messages = []
self.advance_time_and_run(1)
self.assertOpenPixelLedsSent({20: (255, 0, 0), 99: (2, 23, 42)}, {})
self.assertOpenPixelLedsSent({20: (255, 0, 0), 99: (2, 23, 42)}, {}, {})

# test led on channel 1
self.machine.lights.test_led3.on()
self.machine.lights["test_led3"].on()
self.advance_time_and_run(.02)
self._messages = []
self.advance_time_and_run(1)
self.assertOpenPixelLedsSent({20: (255, 0, 0), 99: (2, 23, 42)}, {99: (255, 255, 255)})
self.assertOpenPixelLedsSent({20: (255, 0, 0), 99: (2, 23, 42)}, {99: (255, 255, 255)}, {})

self.machine.lights.test_led.off()
self.machine.lights.test_led2.off()
self.machine.lights.test_led3.off()
self.machine.lights["test_led"].off()
self.machine.lights["test_led2"].off()
self.machine.lights["test_led3"].off()
self.advance_time_and_run(.02)
self._messages = []
self.advance_time_and_run(.1)
self.assertOpenPixelLedsSent({}, {})
self.machine.lights.test_led.color("blue", priority=5, fade_ms=20, key="test2")
self.assertOpenPixelLedsSent({}, {}, {})
self.machine.lights["test_led"].color("blue", priority=5, fade_ms=20, key="test2")
self.advance_time_and_run()

self.machine.lights.test_led.on(priority=10, fade_ms=1000, key="test")
self.machine.lights["test_led"].on(priority=10, fade_ms=1000, key="test")
self.advance_time_and_run(2)
self._messages = []
self.advance_time_and_run(.1)
self.assertOpenPixelLedsSent({99: (255, 255, 255)}, {})
self.assertOpenPixelLedsSent({99: (255, 255, 255)}, {}, {})
# test crossfade on same key
self._messages = []
self.machine.lights.test_led.on(priority=10, fade_ms=1000, key="test")
self.machine.lights["test_led"].on(priority=10, fade_ms=1000, key="test")
self.advance_time_and_run(.1)
self.assertOpenPixelLedsSent({99: (255, 255, 255)}, {})
self.assertOpenPixelLedsSent({99: (255, 255, 255)}, {}, {})

# test remove and readd
self._messages = []
self.machine.lights.test_led.remove_from_stack_by_key(fade_ms=1000, key="test")
self.machine.lights.test_led.on(priority=10, fade_ms=1000, key="test")
self.machine.lights["test_led"].remove_from_stack_by_key(fade_ms=1000, key="test")
self.machine.lights["test_led"].on(priority=10, fade_ms=1000, key="test")
self.advance_time_and_run(.1)
self.assertOpenPixelLedsSent({99: (255, 255, 255)}, {})
self.assertOpenPixelLedsSent({99: (255, 255, 255)}, {}, {})

# test fade out
self.machine.lights.test_led.remove_from_stack_by_key(fade_ms=1000, key="test")
self.machine.lights["test_led"].remove_from_stack_by_key(fade_ms=1000, key="test")
self.advance_time_and_run(.5)
self._messages = []
self.advance_time_and_run(.1)
@@ -150,18 +156,30 @@ def test_led_color(self):
self.advance_time_and_run(.5)
self._messages = []
self.advance_time_and_run(.1)
self.assertOpenPixelLedsSent({99: (0, 0, 255)}, {})
self.assertOpenPixelLedsSent({99: (0, 0, 255)}, {}, {})

# fade over fade
self.machine.lights.test_led.on(priority=10, key="test")
self.machine.lights.test_led.remove_from_stack_by_key(fade_ms=10000, key="test2")
self.machine.lights["test_led"].on(priority=10, key="test")
self.machine.lights["test_led"].remove_from_stack_by_key(fade_ms=10000, key="test2")
self.advance_time_and_run(.1)
self._messages = []
self.advance_time_and_run(.1)
self.assertOpenPixelLedsSent({99: (255, 255, 255)}, {})
self.machine.lights.test_led.remove_from_stack_by_key(fade_ms=1000, key="test")
self.assertOpenPixelLedsSent({99: (255, 255, 255)}, {}, {})
self.machine.lights["test_led"].remove_from_stack_by_key(fade_ms=1000, key="test")
self.advance_time_and_run(.5)
self.advance_time_and_run(20)
self._messages = []
self.advance_time_and_run(.1)
self.assertOpenPixelLedsSent({99: (0, 0, 0)}, {})
self.assertOpenPixelLedsSent({}, {}, {})

# set RGBW
self._messages = []
self.machine.lights["test_rgbw"].on(priority=10, key="test")
self.advance_time_and_run(.1)
self.assertOpenPixelLedsSent({}, {}, {0: (255, 255, 255), 1: (0, 255, 0)})

self._messages = []
self.machine.lights["test_rgbw"].color("red", priority=20, key="test")
self._messages = []
self.advance_time_and_run(.1)
self.assertOpenPixelLedsSent({}, {}, {0: (0, 255, 0), 1: (0, 0, 0)})

0 comments on commit bae40db

Please sign in to comment.
You can’t perform that action at this time.