def __init__(self, address=0x20, busnum=I2C.get_default_bus(), cols=16, lines=2): """Initialize the character LCD plate. Can optionally specify a separate I2C address or bus number, but the defaults should suffice for most needs. Can also optionally specify the number of columns and lines on the LCD (default is 16x2). """ # Configure MCP23017 device. self._mcp = MCP.MCP23017(address=address, busnum=busnum) # Set LCD R/W pin to low for writing only. self._mcp.setup(LCD_PLATE_RW, GPIO.OUT) self._mcp.output(LCD_PLATE_RW, GPIO.LOW) # Set buttons as inputs with pull-ups enabled. for button in (SELECT, RIGHT, DOWN, UP, LEFT): self._mcp.setup(button, GPIO.IN) self._mcp.pullup(button, True) # Initialize LCD (with no PWM support). super(Adafruit_CharLCDPlate, self).__init__(LCD_PLATE_RS, LCD_PLATE_EN, LCD_PLATE_D4, LCD_PLATE_D5, LCD_PLATE_D6, LCD_PLATE_D7, cols, lines, LCD_PLATE_RED, LCD_PLATE_GREEN, LCD_PLATE_BLUE, enable_pwm=False, gpio=self._mcp)
def __init__(self, width, height, rst, dc=None, sclk=None, din=None, cs=None, gpio=None, spi=None, i2c_bus=I2C.get_default_bus(), i2c_address=SSD1306_I2C_ADDRESS): self._log = logging.getLogger('Adafruit_SSD1306.SSD1306Base') self._spi = None self._i2c = None self.width = width self.height = height self._pages = height/8 self._buffer = [0]*(width*self._pages) # Default to platform GPIO if not provided. self._gpio = gpio if gpio is not None else GPIO.get_platform_gpio() # Setup reset pin. self._rst = rst self._gpio.setup(self._rst, GPIO.OUT) # Handle hardware SPI if spi is not None: self._log.debug('Using hardware SPI') self._spi = spi # Handle software SPI elif sclk is not None and din is not None and cs is not None: self._log.debug('Using software SPI') self._spi = SPI.BitBang(self._gpio, sclk, din, None, cs) # Handle hardware I2C elif i2c_bus is not None: self._log.debug('Using hardware I2C') self._i2c = I2C.Device(i2c_address, i2c_bus) else: raise ValueError('Unable to determine if using SPI or I2C.') # Initialize DC pin if using SPI. if self._spi is not None: if dc is None: raise ValueError('DC pin must be provided when using SPI.') self._dc = dc self._gpio.setup(self._dc, GPIO.OUT)
def _configure_logging(self): logging.basicConfig(level=logging.WARNING, format='%(asctime)-15s %(levelname)-5s %(message)s') # TODO put log configuration in a (yaml) config file # The basic config doesn't hold through tests radio_logger = logging.getLogger("RPiNWR") radio_logger.setLevel(logging.DEBUG) radio_log_handler = logging.FileHandler("radio.log", encoding='utf-8') radio_log_handler.setFormatter(logging.Formatter(fmt='%(asctime)-15s %(levelname)-5s %(message)s', datefmt="")) radio_log_handler.setLevel(logging.DEBUG) radio_logger.addHandler(radio_log_handler) message_logger = logging.getLogger("RPiNWR.same.message") message_logger.setLevel(logging.DEBUG) message_log_handler = logging.FileHandler("messages.log", encoding='utf-8') message_log_handler.setFormatter(logging.Formatter(datefmt="")) message_log_handler.setLevel(logging.DEBUG) # DEBUG=test, INFO=watches & emergencies, WARN=warnings message_logger.addHandler(message_log_handler) # Since this is logging lots of things, best to not also log every time we check for status try: import Adafruit_GPIO.I2C as i2c i2cLogger = logging.getLogger('Adafruit_I2C.Device.Bus.{0}.Address.{1:#0X}' .format(i2c.get_default_bus(), 0x11)) except ImportError: i2cLogger = logging.getLogger( 'Adafruit_I2C.Device.Bus') # a little less specific, but probably just as good i2cLogger.addFilter(Radio.exclude_routine_status_checks)
def __init__(self, address=MCP9808_I2CADDR_DEFAULT, busnum=I2C.get_default_bus()): """Initialize MCP9808 device on the specified I2C address and bus number. Address defaults to 0x18 and bus number defaults to the appropriate bus for the hardware. """ self._logger = logging.getLogger('Adafruit_MCP9808.MCP9808') self._device = I2C.Device(address, busnum)
def __init__(self, address=0x23, busnum=I2C.get_default_bus()): """Initialize the LEDs """ # Configure MCP23017 device. super(Looper_LEDs, self).__init__(address=address, busnum=busnum) self.setAllPinsOutput() self.initPWM() #initalize GPIO for PWM self.startPWM()
def __init__(self, mode=BMP280_STANDARD, address=BMP280_I2CADDR, busnum=I2C.get_default_bus()): self._logger = logging.getLogger('Adafruit_BMP.BMP280') # Check that mode is valid. if mode not in [BMP280_ULTRALOWPOWER, BMP280_STANDARD, BMP280_HIGHRES, BMP280_ULTRAHIGHRES]: raise ValueError('Unexpected mode value {0}. Set mode to one of BMP280_ULTRALOWPOWER, BMP280_STANDARD, BMP280_HIGHRES, or BMP280_ULTRAHIGHRES'.format(mode)) self._mode = mode # Create I2C device. self._device = I2C.Device(address, busnum) # Load calibration values. self._load_calibration()
def __init__(self, mode=BMP085_STANDARD, address=BMP085_I2CADDR, busnum=I2C.get_default_bus()): self._logger = logging.getLogger('Adafruit_BMP.BMP085') # Check that mode is valid. if mode not in [BMP085_ULTRALOWPOWER, BMP085_STANDARD, BMP085_HIGHRES, BMP085_ULTRAHIGHRES]: raise ValueError('Unexpected mode value {0}. Set mode to one of BMP085_ULTRALOWPOWER, BMP085_STANDARD, BMP085_HIGHRES, or BMP085_ULTRAHIGHRES'.format(mode)) self._mode = mode # Create I2C device. self._device = I2C.Device(address, busnum) # Load calibration values. self._load_calibration()
def __init__(self, address=0x20, busnum=I2C.get_default_bus(), cols=16, lines=2): """Initialize the character LCD plate. Can optionally specify a separate I2C address or bus number, but the defaults should suffice for most needs. Can also optionally specify the number of columns and lines on the LCD (default is 16x2). """ # Configure the MCP23008 device. self._mcp = MCP.MCP23008(address=address, busnum=busnum) # Initialize LCD (with no PWM support). super(Adafruit_CharLCDBackpack, self).__init__(LCD_BACKPACK_RS, LCD_BACKPACK_EN, LCD_BACKPACK_D4, LCD_BACKPACK_D5, LCD_BACKPACK_D6, LCD_BACKPACK_D7, cols, lines, LCD_BACKPACK_LITE, enable_pwm=False, gpio=self._mcp)
def __init__(self, rst, dc=None, sclk=None, din=None, cs=None, gpio=None, spi=None, i2c_bus=I2C.get_default_bus()): # Call base class constructor. super(SSD1306_128_32, self).__init__(128, 32, rst, dc, sclk, din, cs, gpio, spi, i2c_bus)
def __init__(self, address=SI1145_ADDR, busnum=I2C.get_default_bus()): self._logger = logging.getLogger('SI1145') # Create I2C device. self._device = I2C.Device(address, busnum) # reset device self._reset() # Load calibration values. self._load_calibration()
def __init__(self, address=SI1145_ADDR, busnum=I2C.get_default_bus()): self._logger = logging.getLogger('SI1145') # Create I2C device. self._device = I2C.Device(address, busnum) #reset device self._reset() # Load calibration values. self._load_calibration()
def update_sensors_data(): """Read all sensors and set data to DB""" owproxy = pyownet.protocol.proxy(host="owfs", port=4304) ow_sensors = owproxy.dir() if len(ow_sensors) > 0: temperature = float(owproxy.read(ow_sensors[0] + 'temperature')) db_set("solution-t", '{:.1f}'.format(temperature)) db_set("solution-t-at", '{}'.format(datetime.now())) # Setup pH and EC bus = Adafruit_PureIO.smbus.SMBus(I2C.get_default_bus()) ec_adc = MCP342x.MCP342x(bus, 0x68, channel=0, resolution=18) ph_adc = MCP342x.MCP342x(bus, 0x68, channel=1, resolution=18) # EC ec_voltage = ec_adc.convert_and_read() RES2 = 820.0 ECREF = 200.0 raw_ec = 1000 * ec_voltage / RES2 / ECREF # temperature compensation ec = raw_ec / (1.0 + 0.0185 * (temperature - 25.0)) if ec > 0.1: db_set("solution-ec", '{:.1f}'.format(float(ec))) db_set("solution-ec-at", '{}'.format(datetime.now())) # pH ph_voltage = ph_adc.convert_and_read() # TODO: should be calibrated ph = -5.70 * ph_voltage + 10 if ph > 2 and ph < 10: db_set("solution-ph", '{:.1f}'.format(float(ph))) db_set("solution-ph-at", '{}'.format(datetime.now())) for idx, channel in enumerate([PCA9548A_CH0, PCA9548A_CH1]): pca9548a_setup(channel) bme280 = BME280.BME280(address=0x76, t_mode=BME280.BME280_OSAMPLE_8, p_mode=BME280.BME280_OSAMPLE_8, h_mode=BME280.BME280_OSAMPLE_8) degrees = bme280.read_temperature() db_set("l{}-t".format(idx), '{:.1f}'.format(degrees)) db_set("l{}-t-at".format(idx), '{}'.format(datetime.now())) humidity = bme280.read_humidity() db_set("l{}-h".format(idx), '{:.0f}'.format(humidity)) db_set("l{}-h-at".format(idx), '{}'.format(datetime.now()))
def __init__(self, mode=TMP007_CFG_16SAMPLE, address=TMP007_I2CADDR, busnum=I2C.get_default_bus()): self._logger = logging.getLogger('TMP007') # Check that mode is valid. if mode not in [TMP007_CFG_1SAMPLE, TMP007_CFG_2SAMPLE, TMP007_CFG_4SAMPLE, TMP007_CFG_8SAMPLE, TMP007_CFG_16SAMPLE]: raise ValueError('Unexpected mode value {0}. Set mode to one of TMP007_CFG_1SAMPLE, TMP007_CFG_2SAMPLE, TMP007_CFG_4SAMPLE, TMP007_CFG_8SAMPLE or TMP007_CFG_16SAMPLE'.format(mode)) self._mode = mode # Create I2C device. self._device = I2C.Device(address, busnum) # Load calibration values. self._load_calibration()
def __init__(self, rst, dc=None, sclk=None, din=None, cs=None, gpio=None, spi=None, i2c_bus=I2C.get_default_bus(), i2c_address=SSD1306_I2C_ADDRESS): # Call base class constructor. super(SSD1306_128_64, self).__init__(128, 64, rst, dc, sclk, din, cs, gpio, spi, i2c_bus, i2c_address)
def __init__(self, address=0x22, busnum=I2C.get_default_bus()): # Configure MCP23017 device. super(Looper_Switches, self).__init__(address=address, busnum=busnum) #self.setAllPinsInput() for pin in self.footswitch_pins: self.setup(pin, GPIO.IN) self.pullup(pin, GPIO.HIGH) for pin in self.footswitch_pins: self.gpinten_pin(pin, True) self.intcon_pin(pin, False) # interrupt compared to last state self.defval_pin(pin, True) self.inputPolarity_pin(self.SELECTOR_SWITCH, True) self.readGPIO(self.allPins) self.ioconSetup(0x02) #active high interrupt polarity
def __init__(self, address=SI1145_ADDR, busnum=I2C.get_default_bus()): try: self._logger = logging.getLogger('SI1145') # Create I2C device. self._device = I2C.Device(address, busnum) #reset device self._reset() # Load calibration values. self._load_calibration() isFound = True except: logging.info("si1145 not found.")
def __init__(self, address=SI1145_ADDR, busnum=I2C.get_default_bus()): ''' (default [I2C address of SI1145=0x60], [I2C bus number]) intitalizes to default mode (UV,Vis,IR and Prox 1) enables all interupts and starts in autonomous mode''' self._logger = logging.getLogger('SI1145') # Create I2C device. self._device = I2C.Device(address, busnum) #reset device self._reset() # Load calibration values, default settings, enables interupts # and starts in autonomous mode. self._load_setup()
def __init__(self, address, busnum=I2C.get_default_bus()): """Initialize MCP230xx at specified I2C address and bus number. If bus is not specified it will default to the appropriate platform detected bus. """ self._i2c = I2C.Device(address, busnum) # Assume starting in ICON.BANK = 0 mode (sequential access). # Compute how many bytes are needed to store count of GPIO. self.gpio_bytes = int(math.ceil(self.NUM_GPIO / 8.0)) # Buffer register values so they can be changed without reading. self.iodir = [0x00 ] * self.gpio_bytes # Default direction to all inputs. self.gppu = [0x00] * self.gpio_bytes # Default to pullups disabled. self.gpio = [0x00] * self.gpio_bytes # Write current direction and pullup buffer state. self.write_iodir() self.write_gppu()
def connectDeviceI2C(devAddress, regAddress, command, writeVal=0x00): busNumber = i2c.get_default_bus() print "Bus Number: " + str(busNumber) print "Device Address: " + hex(devAddress) print "Register Address: " + hex(regAddress) print "Command: " + str(command) Device = i2c.get_i2c_device(devAddress) print "" if not(command): print "Write Val: " + hex(writeVal) write(Device, regAddress,writeVal) else: print "Reading from Register with address: " + str(regAddress) read(Device, regAddress)
def __init__(self, mode=BMP085_STANDARD, address=BMP085_I2CADDR, busnum=I2C.get_default_bus()): self._logger = logging.getLogger('Adafruit_BMP.BMP085') # Check that mode is valid. if mode not in [BMP085_ULTRALOWPOWER, BMP085_STANDARD, BMP085_HIGHRES, BMP085_ULTRAHIGHRES]: raise ValueError('Unexpected mode value {0}. Set mode to one of BMP085_ULTRALOWPOWER, BMP085_STANDARD, BMP085_HIGHRES, or BMP085_ULTRAHIGHRES'.format(mode)) self._mode = mode # Create I2C device. self._device = I2C.Device(address, busnum) #(chip_id, version) = bus.read_i2c_block_data(addr, 0xD0, 2) chip_id = self._device.readU8(0xD0) version = self._device.readU8(0xD0 + 1) self._logger.debug('Chip Id: {0} Version: {1}'.format(chip_id, version)) # Load calibration values. self._load_calibration() self._compute_polynomials() self.temperature=None
def __init__(self, width, height, rst, dc=None, sclk=None, din=None, cs=None, gpio=None, spi=None, i2c_bus=I2C.get_default_bus(), i2c_address=SSD1306_I2C_ADDRESS): self._log = logging.getLogger('Adafruit_SSD1306.SSD1306Base') self._spi = None self._i2c = None self.width = width self.height = height self._pages = height / 8 self._buffer = [0] * (width * self._pages) # Default to platform GPIO if not provided. self._gpio = gpio if gpio is not None else GPIO.get_platform_gpio() # Setup reset pin. self._rst = rst self._gpio.setup(self._rst, GPIO.OUT) # Handle hardware SPI if spi is not None: self._log.debug('Using hardware SPI') self._spi = spi # Handle software SPI elif sclk is not None and din is not None and cs is not None: self._log.debug('Using software SPI') self._spi = SPI.BitBang(self._gpio, sclk, din, None, cs) # Handle hardware I2C elif i2c_bus is not None: self._log.debug('Using hardware I2C') self._i2c = I2C.Device(i2c_address, i2c_bus) else: raise ValueError('Unable to determine if using SPI or I2C.') # Initialize DC pin if using SPI. if self._spi is not None: if dc is None: raise ValueError('DC pin must be provided when using SPI.') self._dc = dc self._gpio.setup(self._dc, GPIO.OUT)
def __init__(self, addr, desc='I2C Sensor', busnum=None): """ Parameters ----------- addr : int i2c sensor address. Can be specified as (for example) 0x10 desc : string Human-readable description to identify this sensor busnum : int or None The number of the I2C device to connect to. For example, to connect to /dev/i2c-0, put 0. To connect to the default bus, put None. """ self._address = addr self._description = desc # Open the bus if busnum == None: busnum = I2C.get_default_bus() self._bus = smbus.SMBus(busnum)
def __init__(self, address, FSS=0.5, differential=True): """ Parameters ----------- address : int i2c sensor address. Can be specified as (for example) 0x10 FSS : float Sensor full scale level. span in inH2O differential : bool If this is a differential pressure sensor (True) or a guage pressure sensor (False) """ # Save attributes self.address = address self.FSS = FSS self.differential = differential if differential: self.OSdig = 8192 else: self.OSdig = 1638 # Open the bus busnum = I2C.get_default_bus() self._bus = smbus.SMBus(busnum)
def __init__(self, rst, dc=None, sclk=None, din=None, cs=None, gpio=None, spi=None, i2c_bus=I2C.get_default_bus(), i2c_address=SSD1306_I2C_ADDRESS): # Call base class constructor. super(SSD1306_96_16, self).__init__(96, 16, rst, dc, sclk, din, cs, gpio, spi, i2c_bus, i2c_address)
def __init__(self, address=0x21, busnum=I2C.get_default_bus()): """Initialize the LEDs """ # Configure MCP23017 device. super(Looper_Routes, self).__init__(address=address, busnum=busnum) self.setAllPinsOutput()
def test_beaglebone_black(self): I2C = safe_import_i2c() bus = I2C.get_default_bus() self.assertEqual(bus, 1)
def __init__(self, address, busnum): busnum = busnum or I2C.get_default_bus() self._device = I2C.get_i2c_device(address, busnum)
def get_i2c_device(self, address, busnum=None, i2c_interface=None, **kwargs): if busnum is None: busnum = I2C.get_default_bus() return MUX_Device(self.port, address, busnum, i2c_interface, **kwargs)
def test_raspberry_pi_rev2(self): I2C = safe_import_i2c() bus = I2C.get_default_bus() self.assertEqual(bus, 1)
def __init__(self, address=DEFAULT_ADDRESS, busnum=I2C.get_default_bus()): """Create an HT16K33 driver for devie on the specified I2C address (defaults to 0x70) and I2C bus (defaults to platform specific bus). """ self._i2c = I2C.Device(address, busnum) self.buffer = bytearray([0]*16)
def __init__(self): busnum = I2C.get_default_bus() super(MM7150, self).__init__(0x40, busnum) self.DescLen = None
def __init__(self, LSB_ADDR): self._uvl = I2C.Device(LSB_ADDR, I2C.get_default_bus()) self._uvm = I2C.Device(LSB_ADDR + 1, I2C.get_default_bus())
def __init__(self, addr): self._addr = addr self._i2cDev = I2C.Device(self._addr, I2C.get_default_bus())
#import serial #if using pi for MIDI. I changed to using the Arduino Pro Micro #import smbus #used for i2c #THE OLD i2c WAY import Adafruit_GPIO.I2C as I2C # THE NEW WAY import time i2c_device = I2C.get_i2c_device(address=0x04, busnum=I2C.get_default_bus()) #class MIDI(serial.Serial, object): #when using the raspPi for MIDI class MIDI(object): CCdict = {"1":"\xB0", "2":"\xB1", "3":"\xB2", "4":"\xB3", "5":"\xB4", "6":"\xB5", "7":"\xB6", "8":"\xB7", "9":"\xB8", "10":"\xB9", "11":"\xBA", "12":"\xBB", "13":"\xBC", "14":"\xBD", "15":"\xBE", "16":"\xBF"} PCdict = {"1":"\xC0", "2":"\xC1", "3":"\xC2", "4":"\xC3", "5":"\xC4", "6":"\xC5", "7":"\xC6", "8":"\xC7", "9":"\xC8", "10":"\xC9", "11":"\xCA", "12":"\xCB", "13":"\xCC", "14":"\xCD", "15":"\xCE", "16":"\xCF"} def __init__(self, channel): #self.bus = smbus.SMBus(1) #The old i2c way #set MIDI serial port and baudrate of UART #super(MIDI, self).__init__('/dev/ttyAMA0', baudrate=31250) self.CCchannel = self.CCdict[str(channel)] self.PCchannel = self.PCdict[str(channel)] def MIDI_CC_TX(self, changeNum, value): message = self.CCchannel + changeNum + value self.write(message) def MIDI_PC_TX(self, changeNum, value): message = self.PCchannel + changeNum + value self.write(message)