def test_setup_mock_invalid_timeout(): """Test an attempt to set up the BH1745 with a reset timeout.""" from tools import SMBusFakeDevice smbus = mock.Mock() smbus.SMBus = SMBusFakeDevice sys.modules['smbus'] = smbus from bh1745 import BH1745 with pytest.raises(ValueError): bh1745 = BH1745() bh1745.setup(timeout=0) with pytest.raises(ValueError): bh1745 = BH1745() bh1745.setup(timeout=-1)
def test_setup_id_mismatch(): """Test an attempt to set up the BH1745 with invalid sensor present.""" sys.modules['smbus'] = mock.MagicMock() from bh1745 import BH1745 bh1745 = BH1745() with pytest.raises(RuntimeError): bh1745.setup()
def _setup(): global bh1745 from tools import SMBusFakeDeviceNoTimeout smbus = mock.Mock() smbus.SMBus = SMBusFakeDeviceNoTimeout sys.modules['smbus'] = smbus from bh1745 import BH1745 bh1745 = BH1745()
def test_setup_mock_present(): """Test an attempt to set up a present and working (mocked) BH1745.""" from tools import SMBusFakeDeviceNoTimeout smbus = mock.Mock() smbus.SMBus = SMBusFakeDeviceNoTimeout sys.modules['smbus'] = smbus from bh1745 import BH1745 bh1745 = BH1745() bh1745.setup(timeout=0.01)
def test_i2c_addr(): """Test various valid and invalid i2c addresses for BH1745.""" from tools import SMBusFakeDeviceNoTimeout smbus = mock.Mock() smbus.SMBus = SMBusFakeDeviceNoTimeout sys.modules['smbus'] = smbus from bh1745 import BH1745 with pytest.raises(ValueError): bh1745 = BH1745(i2c_addr=0x40) with pytest.raises(ValueError): bh1745 = BH1745() bh1745.setup(i2c_addr=0x40) bh1745 = BH1745(i2c_addr=0x38) bh1745 = BH1745(i2c_addr=0x39) del bh1745
def test_setup_not_present(): """Test an attempt to set up the BH1745 with no sensor present.""" from tools import SMBusFakeDeviceIOError smbus = mock.Mock() smbus.SMBus = SMBusFakeDeviceIOError sys.modules['smbus'] = smbus from bh1745 import BH1745 bh1745 = BH1745() with pytest.raises(RuntimeError): bh1745.setup()
def test_is_setup(): """Test ready() returns correct state.""" from tools import SMBusFakeDeviceNoTimeout smbus = mock.Mock() smbus.SMBus = SMBusFakeDeviceNoTimeout sys.modules['smbus'] = smbus from bh1745 import BH1745 bh1745 = BH1745() assert bh1745.ready() is False bh1745.setup() assert bh1745.ready() is True
#!/usr/bin/env python import time from bh1745 import BH1745 bh1745 = BH1745() bh1745.setup() bh1745.set_leds(1) time.sleep(1.0) # Skip the reading that happened before the LEDs were enabled try: while True: r, g, b, c = bh1745.get_rgbc_raw() print('RGBC: {:10.1f} {:10.1f} {:10.1f} {:10.1f}'.format(r, g, b, c)) time.sleep(0.5) except KeyboardInterrupt: bh1745.set_leds(0)
#!/usr/bin/env python ### reset BH1745 LEDs from bh1745 import BH1745 bh = BH1745() bh.setup() bh.set_leds(1) bh.set_leds(0) print('turning off LED')
#!/usr/bin/env python import time from bh1745 import BH1745 bh1745_a = BH1745(0x38) # Stock BH1745 breakout or jumper soldered bh1745_b = BH1745(0x39) # Cuttable trace cut bh1745_a.setup() bh1745_b.setup() bh1745_a.set_leds(1) bh1745_b.set_leds(1) try: while True: r, g, b = bh1745_a.get_rgb_scaled() print('A: #{:02x}{:02x}{:02x}'.format(r, g, b)) r, g, b = bh1745_b.get_rgb_scaled() print('B: #{:02x}{:02x}{:02x}'.format(r, g, b)) time.sleep(1.0) except KeyboardInterrupt: bh1745_a.set_leds(0) bh1745_b.set_leds(0)