Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
96 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,68 @@ | ||
import sys | ||
import mock | ||
import pytest | ||
|
||
from i2cdevice import MockSMBus | ||
|
||
|
||
class _SMBusFakeDevice(MockSMBus): | ||
def __init__(self, i2c_bus): | ||
MockSMBus.__init__(self, i2c_bus) | ||
self.regs[0x86] = 0x09 << 4 # Fake part number | ||
self.regs[0x86] |= 0x02 # Fake revision | ||
self.regs[0x87] = 0x05 # Fake manufacturer ID | ||
|
||
self.regs[0x88:0x8C] = [0xff, 0xff, 0xff, 0xff] # ALS CH0 & CH1 | ||
|
||
self.regs[0x8C] = 0xff # ALS / PS Status | ||
self.regs[0x8D] = 0xf0 # Proximity Data (240) | ||
|
||
|
||
class _SMBusFakeDeviceNoTimeout(_SMBusFakeDevice): | ||
def __init__(self, i2c_bus): | ||
_SMBusFakeDevice.__init__(self, i2c_bus) | ||
|
||
def write_i2c_block_data(self, i2c_address, register, values): | ||
if register == 0x80: # ALS_CONTROL | ||
values[0] &= ~0b00000010 # Mask out the soft reset bit | ||
return _SMBusFakeDevice.write_i2c_block_data(self, i2c_address, register, values) | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def GPIO(): | ||
"""Mock RPi.GPIO module.""" | ||
GPIO = mock.MagicMock() | ||
# Fudge for Python < 37 (possibly earlier) | ||
sys.modules['RPi'] = mock.Mock() | ||
sys.modules['RPi'].GPIO = GPIO | ||
sys.modules['RPi.GPIO'] = GPIO | ||
yield GPIO | ||
del sys.modules['RPi'] | ||
del sys.modules['RPi.GPIO'] | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def smbus(): | ||
"""Mock smbus module.""" | ||
smbus = mock.MagicMock() | ||
smbus.SMBus = _SMBusFakeDevice | ||
sys.modules['smbus'] = smbus | ||
yield smbus | ||
del sys.modules['smbus'] | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def LTR559(): | ||
from ltr559 import LTR559 | ||
yield LTR559 | ||
del sys.modules['ltr559'] | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def SMBusFakeDevice(): | ||
return _SMBusFakeDevice(1) | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def SMBusFakeDeviceNoTimeout(): | ||
return _SMBusFakeDeviceNoTimeout(1) | ||
import sys | ||
import mock | ||
import pytest | ||
|
||
from i2cdevice import MockSMBus | ||
|
||
|
||
class _SMBusFakeDevice(MockSMBus): | ||
def __init__(self, i2c_bus): | ||
MockSMBus.__init__(self, i2c_bus) | ||
self.regs[0x86] = 0x09 << 4 # Fake part number | ||
self.regs[0x86] |= 0x02 # Fake revision | ||
self.regs[0x87] = 0x05 # Fake manufacturer ID | ||
|
||
self.regs[0x88:0x8C] = [0xff, 0xff, 0xff, 0xff] # ALS CH0 & CH1 | ||
|
||
self.regs[0x8C] = 0xff # ALS / PS Status | ||
self.regs[0x8D] = 0xf0 # Proximity Data (240) | ||
|
||
|
||
class _SMBusFakeDeviceNoTimeout(_SMBusFakeDevice): | ||
def __init__(self, i2c_bus): | ||
_SMBusFakeDevice.__init__(self, i2c_bus) | ||
|
||
def write_i2c_block_data(self, i2c_address, register, values): | ||
if register == 0x80: # ALS_CONTROL | ||
values[0] &= ~0b00000010 # Mask out the soft reset bit | ||
return _SMBusFakeDevice.write_i2c_block_data(self, i2c_address, register, values) | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def GPIO(): | ||
"""Mock RPi.GPIO module.""" | ||
GPIO = mock.MagicMock() | ||
# Fudge for Python < 37 (possibly earlier) | ||
sys.modules['RPi'] = mock.Mock() | ||
sys.modules['RPi'].GPIO = GPIO | ||
sys.modules['RPi.GPIO'] = GPIO | ||
yield GPIO | ||
del sys.modules['RPi'] | ||
del sys.modules['RPi.GPIO'] | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def smbus(): | ||
"""Mock smbus module.""" | ||
smbus = mock.MagicMock() | ||
smbus.SMBus = _SMBusFakeDevice | ||
sys.modules['smbus'] = smbus | ||
yield smbus | ||
del sys.modules['smbus'] | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def LTR559(): | ||
from ltr559 import LTR559 | ||
yield LTR559 | ||
del sys.modules['ltr559'] | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def SMBusFakeDevice(): | ||
return _SMBusFakeDevice(1) | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def SMBusFakeDeviceNoTimeout(): | ||
return _SMBusFakeDeviceNoTimeout(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
def test_get_proximity(LTR559, SMBusFakeDeviceNoTimeout): | ||
ltr559 = LTR559(i2c_dev=SMBusFakeDeviceNoTimeout, timeout=0.5) | ||
proximity = ltr559.get_proximity() | ||
assert proximity == 240 | ||
|
||
|
||
def test_get_lux(LTR559, SMBusFakeDeviceNoTimeout): | ||
ltr559 = LTR559(i2c_dev=SMBusFakeDeviceNoTimeout, timeout=0.5) | ||
lux = ltr559.get_lux() | ||
assert int(lux) == 76141 | ||
|
||
|
||
def test_set_light_integration_time_ms(LTR559, SMBusFakeDeviceNoTimeout): | ||
ltr559 = LTR559(i2c_dev=SMBusFakeDeviceNoTimeout, timeout=0.5) | ||
ltr559.set_light_integration_time_ms(150) | ||
assert SMBusFakeDeviceNoTimeout.regs[0x85] & 0b00111000 == 0b100 << 3 | ||
|
||
|
||
def test_set_interrupt_mode(LTR559, SMBusFakeDeviceNoTimeout): | ||
ltr559 = LTR559(i2c_dev=SMBusFakeDeviceNoTimeout, timeout=0.5) | ||
ltr559.set_interrupt_mode(enable_light=True, enable_proximity=True) | ||
assert SMBusFakeDeviceNoTimeout.regs[0x8F] & 0b11 == 0b11 | ||
|
||
|
||
def test_set_proximity_active(LTR559, SMBusFakeDeviceNoTimeout): | ||
ltr559 = LTR559(i2c_dev=SMBusFakeDeviceNoTimeout, timeout=0.5) | ||
ltr559.set_proximity_active(active=True) | ||
assert SMBusFakeDeviceNoTimeout.regs[0x81] & 0b11 == 0b11 | ||
def test_get_proximity(LTR559, SMBusFakeDeviceNoTimeout): | ||
ltr559 = LTR559(i2c_dev=SMBusFakeDeviceNoTimeout, timeout=0.5) | ||
proximity = ltr559.get_proximity() | ||
assert proximity == 240 | ||
|
||
|
||
def test_get_lux(LTR559, SMBusFakeDeviceNoTimeout): | ||
ltr559 = LTR559(i2c_dev=SMBusFakeDeviceNoTimeout, timeout=0.5) | ||
lux = ltr559.get_lux() | ||
assert int(lux) == 76141 | ||
|
||
|
||
def test_set_light_integration_time_ms(LTR559, SMBusFakeDeviceNoTimeout): | ||
ltr559 = LTR559(i2c_dev=SMBusFakeDeviceNoTimeout, timeout=0.5) | ||
ltr559.set_light_integration_time_ms(150) | ||
assert SMBusFakeDeviceNoTimeout.regs[0x85] & 0b00111000 == 0b100 << 3 | ||
|
||
|
||
def test_set_interrupt_mode(LTR559, SMBusFakeDeviceNoTimeout): | ||
ltr559 = LTR559(i2c_dev=SMBusFakeDeviceNoTimeout, timeout=0.5) | ||
ltr559.set_interrupt_mode(enable_light=True, enable_proximity=True) | ||
assert SMBusFakeDeviceNoTimeout.regs[0x8F] & 0b11 == 0b11 | ||
|
||
|
||
def test_set_proximity_active(LTR559, SMBusFakeDeviceNoTimeout): | ||
ltr559 = LTR559(i2c_dev=SMBusFakeDeviceNoTimeout, timeout=0.5) | ||
ltr559.set_proximity_active(active=True) | ||
assert SMBusFakeDeviceNoTimeout.regs[0x81] & 0b11 == 0b11 |