def test_alert_flags(): import mcp9600 smbus = MockSMBus(1, default_registers={ 0x04: 0b00000101, 0x20: 0x40}) device = mcp9600.MCP9600(i2c_dev=smbus) assert device.check_alerts() == (1, 0, 1, 0)
def test_get_cold_junction_temperature(): import mcp9600 smbus = MockSMBus(1, default_registers={ 0x02: 1, 0x03: 96, 0x20: 0x40}) device = mcp9600.MCP9600(i2c_dev=smbus) assert round(device.get_cold_junction_temperature(), 1) == 22.0
def test_alert_clear(): import mcp9600 smbus = MockSMBus(1, default_registers={0x04: 0b00000101, 0x20: 0x40}) device = mcp9600.MCP9600(i2c_dev=smbus) device.clear_alert(1) assert smbus.regs[0x08] & 0b10000000 == 0b10000000
def test_setup_no_device(): class MockSMBusIOError(MockSMBus): def read_i2c_block_data(self, i2c_address, register, length): raise IOError("Simulated IOError") import mcp9600 with pytest.raises(RuntimeError): device = mcp9600.MCP9600(i2c_dev=MockSMBusIOError(1, default_registers={0x20: 0x99})) del device
def test_get_thermocouple_type(): import mcp9600 smbus = MockSMBus(1, default_registers={ 0x04: 0b00000101, 0x05: 0b00110000, 0x20: 0x40}) device = mcp9600.MCP9600(i2c_dev=smbus) assert device.get_thermocouple_type() == 'N'
def test_get_hot_junction_temperature(): # Test the junction read and conversion formula import mcp9600 smbus = MockSMBus(1, default_registers={ 0x00: 1, 0x01: 96, 0x20: 0x40}) device = mcp9600.MCP9600(i2c_dev=smbus) assert round(device.get_hot_junction_temperature(), 1) == 22.0
def __init__(self, address): self.mAddress = address self.mTempDevice = mcp9600.MCP9600(i2c_dev=address) self.mCurrentTemp = -1 self.mWindowMinTemp = 0 self.mRunning = True self.mReadings = deque() self.mReadingMean = 0 self.mReadingStdDev = 0
def __init__(self, address): threading.Thread.__init__(self) self.mAddress = address self.mTempDevice = mcp9600.MCP9600(i2c_addr=address, i2c_dev=None) self.mCurrentTemp = -1 self.mRunning = True self.mLastReading = 0 self.mRecentReadings = deque() self.mLastDelta = 0 self.mBaseline = 0
def test_set_thermocouple_type(): import mcp9600 smbus = MockSMBus(1, default_registers={ 0x04: 0b00000101, 0x05: 0b00000000, 0x20: 0x40}) device = mcp9600.MCP9600(i2c_dev=smbus) device.set_thermocouple_type('N') assert (smbus.regs[0x05] >> 4) & 0b111 == 0b011
def test_alert_configure(): import mcp9600 smbus = MockSMBus(1, default_registers={0x04: 0b00000101, 0x20: 0x40}) device = mcp9600.MCP9600(i2c_dev=smbus) device.configure_alert( 1, limit=25.25, # Test to 1/4th degree precision hysteresis=0x99, clear_interrupt=True, monitor_junction=0, rise_fall=1, state=1, mode=1, enable=True) assert smbus.regs[0x10:0x12] == [1, 148] assert smbus.regs[0x0C] == 0x99 assert device.get_alert_hysteresis(1) == 0x99 assert device.get_alert_limit(1) == 25.25
# Get I2C bus, this is I2C Bus 1 bus = smbus.SMBus(1) #kwargs is a Python set that contains the address of your device as well as desired range and bandwidth #since this device uses a somewhat imprecise way of defining an address we built a a function you can call before # object instantiation to find the address of the device. This function is only accurate when there are no other # I2C devices on the bus (there can be, but they can't have an address between 96 and 104. 0x60 through 0x68 for hex heads) #refer to the chip's datasheet to determine what value you need for the kwargs to suit your project #the address can be set statically by simply setting kwargs['address'] to the board's address. discover_address() is just for convenience. #the kwargs below is set with all of the default values kwargs = { 'address': mcp9600.discover_address(bus), 'sensor_type': 0x00, 'filter': 0x00, 'scale': 0x00, 'sensor_resolution': 0x00, 'adc_resolution': 0x40, 'samples': 0x00, 'mode': 0x00, 'read_register': 0x00 } #create the MCP9600 object from the MCP9600 library and pass it the kwargs and com bus. #the object requires that you pass it the bus object so that it can communicate and share the bus with other chips/boards if necessary mcp9600 = mcp9600.MCP9600(bus, kwargs) while True: #print out the readings. #the readings will be return as a float in whatever scale/unit of measurement you initialized the board with in kwargs. #Scaling/Unit of measurement will be Celsius unless set as something else in kwargs before intialization. print mcp9600.take_readings() #this sleep is not required time.sleep(.25)
def __init__(self): m = mcp9600.MCP9600()
def test_setup(): # Test that library setup succeeds with correct CHIP ID import mcp9600 device = mcp9600.MCP9600(i2c_dev=MockSMBus(1, default_registers={0x20: 0x40})) del device
def test_legacy_setup(): # Test a stub setup function exists despite it being depricated import mcp9600 device = mcp9600.MCP9600(i2c_dev=MockSMBus(1, default_registers={0x20: 0x40})) device.setup()
def test_setup_wrong_device(): # Test that reading the wrong CHIP ID throws a RuntimeError import mcp9600 with pytest.raises(RuntimeError): device = mcp9600.MCP9600(i2c_dev=MockSMBus(1, default_registers={0x20: 0x99})) del device
#!/usr/bin/env python import mcp9600 import time m = mcp9600.MCP9600() print("Resetting alerts") for x in range(1, 5): m.clear_alert(x) m.configure_alert(x, enable=False) print("Configuring alerts") m.configure_alert(1, monitor_junction=0, limit=40, mode=1, enable=True) m.configure_alert(2, monitor_junction=0, limit=40, mode=1, enable=True, rise_fall=0) m.configure_alert(3, monitor_junction=0, limit=40, mode=1, enable=True, rise_fall=1) while True: t = m.get_hot_junction_temperature() c = m.get_cold_junction_temperature() d = m.get_temperature_delta() alerts = m.check_alerts() for x in range(1, 5): if alerts[x - 1] == 1: m.clear_alert(x) print(alerts) print(t, c, d)