def test_check_ledoutput_range_raises_valueerror_on_invalid_arguments( self): test_ranges = { 'brightness': (-10, 50), 'red': (-10, 0x10f), 'green': (-10, 0x10f), 'blue': (-10, 0x10f) } indices = {'brightness': 0, 'red': 1, 'green': 2, 'blue': 3} valid_ranges = { 'brightness': range(32), 'red': range(0x100), 'green': range(0x100), 'blue': range(0x100) } for (tested_component, (rs, rend)) in test_ranges.items(): for i in range(rs, rend, 1): output = [0, 0, 0, 0] output[indices[tested_component]] = i if i not in valid_ranges[tested_component]: with self.assertRaisesRegex( ValueError, '.*?' + tested_component + ' setting invalid'): apa102._check_ledoutput_range( apa102.LedOutput(*output)) else: apa102._check_ledoutput_range(apa102.LedOutput(*output)) # Test that ValueError is also raised for non-integer types with self.assertRaisesRegex( ValueError, '.*?' + tested_component + ' setting invalid'): output = [0, 0, 0, 0] output[indices[tested_component]] = float( valid_ranges[tested_component][0]) apa102._check_ledoutput_range(apa102.LedOutput(*output))
def test_contains_magic_method_returns_correct_result(self): output_present = apa102.LedOutput(5, 5, 5, 5) output_not_present = apa102.LedOutput(0, 123, 111, 200) for i in range(len(self.instance)): self.instance[i] = apa102.LedOutput(0, 0, 0, 0) self.instance[0] = output_present self.assertTrue((output_present in self.instance) is True) self.assertTrue((output_not_present in self.instance) is False)
def test_getitem_setitem_magic_methods_raises_indexerror_on_invalid_index( self): output = apa102.LedOutput(1, 4, 5, 6) for i in range(-5, 12, 1): if (i >= 0) and (i < 8): __ = self.instance[i] self.instance[i] = output else: with self.assertRaisesRegex(IndexError, '.*? out-of-range'): __ = self.instance[i] with self.assertRaisesRegex(IndexError, '.*? out-of-range'): self.instance[i] = output
def test_setitem_magic_method_raises_valueerror_on_invalid_ledoutput(self): # Since the _check_ledoutput_range() function is used to perform # checking on validity of the passed in item, we simply check if # __setitem__() calls this function. If it does, then it does # raise ValueError on invalid LedOutput named tuples passed to the # magic method, because the _check_ledoutput_range() function has # already been tested to do that. with patch('apa102_gpiod.apa102._check_ledoutput_range', autospec=True, spec_set=True) as mock_check: self.setUp() mock_check.reset_mock() output = apa102.LedOutput(0, 0, 0, 0) self.instance[0] = output mock_check.assert_called_once_with(output)
def test_init_magic_method_correctly_zeroes_framebuffer_and_resets_leds( self): self.assertSequenceEqual( self.instance, [apa102.LedOutput(0, 0, 0, 0) for __ in range(8)]) with patch('apa102_gpiod.apa102.gpiod.Chip', autospec=True, spec_set=True) as __: with patch('apa102_gpiod.apa102.APA102.commit', autospec=True, spec_set=True) as mock_commit: instance = apa102.APA102('/dev/gpiochip0', 8, 24, 23, True) mock_commit.assert_called_once_with(instance) mock_commit.reset_mock() __ = apa102.APA102('/dev/gpiochip0', 8, 24, 23, False) mock_commit.assert_not_called()
action='store', type=int, help='number of LEDs to drive') parser.add_argument('brightness', action='store', type=int, choices=range(32), help='led brightness setting') parser.add_argument('r', action='store', type=int, help='red component of the color, ' '[0, 255]') parser.add_argument('g', action='store', type=int, help='green component of the color, ' '[0, 255]') parser.add_argument('b', action='store', type=int, help='blue component of the color, ' '[0, 255]') args = parser.parse_args() leds = apa102.APA102(args.chip, args.leds, args.clk, args.data, True) for led in range(len(leds)): leds[led] = apa102.LedOutput(args.brightness, args.r, args.g, args.b) leds.commit() leds.close()
'the LEDs') parser.add_argument('clk', action='store', type=int, help='gpio line offset of the gpio line ' 'corresponding to the clock line') parser.add_argument('data', action='store', type=int, help='gpio line offset of the gpio line ' 'corresponding to the data line') parser.add_argument('leds', action='store', type=int, help='number of LEDs to drive') parser.add_argument('brightness', action='store', type=int, choices=range(32), help='led brightness' ' setting') args = parser.parse_args() leds = apa102.APA102(args.chip, args.leds, args.clk, args.data, True) for led in range(len(leds)): rgb = colorsys.hsv_to_rgb(led / len(leds), 1, 1) leds[led] = apa102.LedOutput(args.brightness, round(rgb[0] * 255), round(rgb[1] * 255), round(rgb[2] * 255)) leds.commit() leds.close()
def test_check_led_output_from_led_command_returns_correct_ledoutput_tuple( self): output = apa102.LedOutput(11, 0xde, 0x11, 0xff) packed = apa102._pack_brgb(output) unpacked = apa102._ledoutput_from_led_command(packed) self.assertEquals(unpacked, output)
def test_check_pack_brgb_returns_correct_command_sequence(self): output = apa102.LedOutput(0x0f, 0xde, 0xad, 0xbe) packed = apa102._pack_brgb(output) self.assertSequenceEqual(packed, b'\xef\xbe\xad\xde')
def test_setitem_getitem_magic_methods_correctly_sets_and_gets_item(self): # Since it uses set_brgb_unchecked() internally, the success of this # test also means that the set_brgb_unchecked() method succedded. output = apa102.LedOutput(1, 4, 5, 6) self.instance[0] = output self.assertEquals(self.instance[0], output)