if temp_msb & 0x10 == 0x10: temp_msb = temp_msb & 0x0F temp = 256 - (temp_msb * 16 + temp_lsb / 16) else: temp = temp_msb * 16 + temp_lsb / 16 return temp def get_ids(self): """ Read and return the manufacturer and device IDs as a tuple """ man_id = self.i2c.regrd(self.addr, self.MCP_MANUF_ID_REG, ">H") dev_id = self.i2c.regrd(self.addr, self.MCP_DEVICE_ID_REG, ">H") return (man_id, dev_id) if __name__ == '__main__': i2c_bus = i2cdriver.I2CDriver("/dev/cu.usbserial-DO029IEZ") sensor = MCP9808(i2c_bus) # Check that we can read the manufacturer ID manufacturer, device = sensor.get_ids() assert manufacturer == 0x54 # Loop and display temperature every 5 seconds while True: reading = sensor.get_temperature() print(f"Temperature: {reading:.2f}ÂșC") time.sleep(5.0)
def connect(self, dev): self.sd = i2cdriver.I2CDriver(dev) [w.Enable(True) for w in self.allw] self.refresh(None)
def setUp(self): self.i2 = i2cdriver.I2CDriver(DUT) self.ag = i2cdriver.I2CDriver(AGG)
import i2cdriver i2c = i2cdriver.I2CDriver("COM41") """ Needs to implement bits 13:15 of the temperature register. """ class MCP9808: def __init__(self, deviceAddress = 0x18): self.address = deviceAddress self.configReg = 0x01 self.tUpper = 0x02 self.tLower = 0x03 self.tCrit = 0x04 self.temperature = 0x05 self.manufacturerID = 0x06 self.deviceID = 0x07 self.resolution = 0x08 if (self.getDeviceID() != 0x0400): print("WARNING! Could not detect an MCP9808 at address "\ + str(hex(self.address)) + ", check the wiring.\n") """ Internal function to read bytes from the MCP9808. """ def _read(self, register, numBytes, bus = i2c): # Open a channel to the device at address "self.address" in write mode
Monash University, Australia This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. """ import sys import i2cdriver from termcolor import cprint if __name__ == '__main__': if len(sys.argv) < 2: ttys = ["/dev/ttyUSB1"] else: ttys = sys.argv[1:] for tty in ttys: cprint("Connecting to i2cmini on %s" % tty, 'cyan') i2c = i2cdriver.I2CDriver(tty) cprint("Scanning for devices", 'white') i2c.scan()
def char(c): set_segs = [] for s in chars[c]: set_segs.append(s) i2c.regwr(0x40, segs[s], 8) for s in range(1, 8): if s not in set_segs: i2c.regwr(0x40, segs[s], 0) def foo(c): v = [0] * 8 for s in chars[c]: v[segs[s] - 2] = 8 i2c.regwr(0x40, 0x82, v) i2c = i2cdriver.I2CDriver() # enable oscillator i2c.regwr(0x40, 0x0, 0x01) # enable all outputs i2c.regwr(0x40, 0xc, 0xff) i2c.regwr(0x40, 0xd, 0xff) while True: for i in range(16): foo(i) sleep(0.25)