Beispiel #1
0
 def __init__(self, address=0x39, debug=0, pause=0.8):
     self.i2c = Adafruit_I2C(address)
     self.address = address
     self.pause = pause
     self.debug = debug
     self.gain = 0 # no gain preselected
     self.i2c.write8(0x80, 0x03)     # enable the device
	def begin(self, addr = 0x70, bus = -1):
		"""Initialize the Trellis at the provided I2C address and bus number."""
		self._i2c = Adafruit_I2C.Adafruit_I2C(addr, bus)
		self._i2c.writeList(0x21, []) # Turn on the oscillator.
		self.blinkRate(HT16K33_BLINK_OFF)
		self.setBrightness(15) # Max brightness.
		self._i2c.writeList(0xA1, []) # Turn on interrupt, active high.
Beispiel #3
0
 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()
Beispiel #4
0
	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()
Beispiel #5
0
    def __init__(self, address=0x40, debug=False):
        self.i2c = Adafruit_I2C(address)
        self.i2c.debug = debug
        self.address = address
        self.debug = debug
        if (self.debug):
            print("Reseting PCA9685 MODE1 (without SLEEP) and MODE2")
        self.setAllPWM(0, 0)
        self.i2c.write8(self.__MODE2, self.__OUTDRV)
        self.i2c.write8(self.__MODE1, self.__ALLCALL)
        time.sleep(0.005)  # wait for oscillator

        mode1 = self.i2c.readU8(self.__MODE1)
        mode1 = mode1 & ~self.__SLEEP  # wake up (reset sleep)
        self.i2c.write8(self.__MODE1, mode1)
        time.sleep(0.005)  # wait for oscillator
Beispiel #6
0
    def __init__(self, devnum=0):
        if (devnum not in (0, 1)):
            raise ValueError("devnum must be 0 or 1")
        self.addr = L3GD20_I2C.base_device_address + devnum
        self.i2c_dev = Adafruit_I2C.Adafruit_I2C(self.addr)
        self.reboot_memory_content()

        if not self.check_who_am_i():
            raise RuntimeError(
                "there does not seem to ba an L3GD20 attached at address " +
                str(self.addr))

        self.enable_xyz()
        self.enable_continuous_update()

        self.set_sensitivity(250)

        self.detect_drift()
        self.zero()
Beispiel #7
0
 def __init__(self,
              integration_time=TCS34725_INTEGRATIONTIME_2_4MS,
              gain=TCS34725_GAIN_4X,
              address=TCS34725_ADDRESS,
              i2c=None,
              **kwargs):
     """Initialize the TCS34725 sensor."""
     # Setup I2C interface for the device.
     if i2c is None:
         import Adafruit_I2C as I2C
         i2c = I2C.Adafruit_I2C(address)
     self._device = i2c
     # Make sure we're connected to the sensor.
     chip_id = self._readU8(TCS34725_ID)
     if chip_id != 0x44:
         raise RuntimeError(
             'Failed to read TCS34725 chip ID, check your wiring.')
     # Set default integration time and gain.
     self.set_integration_time(integration_time)
     self.set_gain(gain)
     # Enable the device (by default, the device is in power down mode on bootup).
     self.enable()
Beispiel #8
0
    def __init__(self, xtal_capacitance, xtal_frequency):
        self.plla_frequency = 0
        self.pllb_frequency = 0
        self.xtal_frequency = xtal_frequency
        self.xtal_capacitance = xtal_capacitance
        self.i2c = Adafruit_I2C(Si5351_I2C_ADDRESS)

        # Turn all outputs off
        self.i2c.write8(Si5351_OUTPUT_ENABLE_REG, Si5351_ALL_OUTPUTS_DISABLED)

        # Power down all 8 clocks
        self.i2c.writeList(Si5351_CLK0_CONTROL_REG,
                           [Si5351_CLOCK_POWERED_DOWN] * 8)

        # Set Crystal Capacitance
        if xtal_capacitance == "XTAL_6pF":
            self.i2c.write8(Si5351_XTAL_REG,
                            Si5351_XTAL_6PF | Si5351_XTAL_REG_LOWER_BITS)
            pass
        elif xtal_capacitance == "XTAL_8pF":
            self.i2c.write8(Si5351_XTAL_REG,
                            Si5351_XTAL_8PF | Si5351_XTAL_REG_LOWER_BITS)
            pass
        elif xtal_capacitance == "XTAL_10pF":
            self.i2c.write8(Si5351_XTAL_REG,
                            Si5351_XTAL_10PF | Si5351_XTAL_REG_LOWER_BITS)
            pass
        else:
            raise Exception(
                "Specify crystal capacitance as \"XTAL_6pF\", \"XTAL_8pF\" or \"XTAL_10pF\""
            )

        # Sanity check crystal frequency (must be between 10MHz and 40MHz, recommended 25MHz or 27MHz)
        if (xtal_frequency < 10e6 or xtal_frequency > 40e6):
            raise Exception(
                "Crystal frequency must be between 10MHz and 40MHz, specified in Hz"
            )
Beispiel #9
0
class PWM:
    # Registers/etc.
    __MODE1 = 0x00
    __MODE2 = 0x01
    __SUBADR1 = 0x02
    __SUBADR2 = 0x03
    __SUBADR3 = 0x04
    __PRESCALE = 0xFE
    __LED0_ON_L = 0x06
    __LED0_ON_H = 0x07
    __LED0_OFF_L = 0x08
    __LED0_OFF_H = 0x09
    __ALL_LED_ON_L = 0xFA
    __ALL_LED_ON_H = 0xFB
    __ALL_LED_OFF_L = 0xFC
    __ALL_LED_OFF_H = 0xFD

    # Bits
    __RESTART = 0x80
    __SLEEP = 0x10
    __ALLCALL = 0x01
    __INVRT = 0x10
    __OUTDRV = 0x04

    general_call_i2c = Adafruit_I2C(0x00)

    @classmethod
    def softwareReset(cls):
        "Sends a software reset (SWRST) command to all the servo drivers on the bus"
        cls.general_call_i2c.writeRaw8(0x06)  # SWRST

    def __init__(self, address=0x40, debug=False):
        self.i2c = Adafruit_I2C(address)
        self.i2c.debug = debug
        self.address = address
        self.debug = debug
        if (self.debug):
            print("Reseting PCA9685 MODE1 (without SLEEP) and MODE2")
        self.setAllPWM(0, 0)
        self.i2c.write8(self.__MODE2, self.__OUTDRV)
        self.i2c.write8(self.__MODE1, self.__ALLCALL)
        time.sleep(0.005)  # wait for oscillator

        mode1 = self.i2c.readU8(self.__MODE1)
        mode1 = mode1 & ~self.__SLEEP  # wake up (reset sleep)
        self.i2c.write8(self.__MODE1, mode1)
        time.sleep(0.005)  # wait for oscillator

    def setPWMFreq(self, freq):
        "Sets the PWM frequency"
        prescaleval = 25000000.0  # 25MHz
        prescaleval /= 4096.0  # 12-bit
        prescaleval /= float(freq)
        prescaleval -= 1.0
        if (self.debug):
            print("Setting PWM frequency to %d Hz" % freq)
            print("Estimated pre-scale: %d" % prescaleval)
        prescale = math.floor(prescaleval + 0.5)
        if (self.debug):
            print("Final pre-scale: %d" % prescale)

        oldmode = self.i2c.readU8(self.__MODE1)
        newmode = (oldmode & 0x7F) | 0x10  # sleep
        self.i2c.write8(self.__MODE1, newmode)  # go to sleep
        self.i2c.write8(self.__PRESCALE, int(math.floor(prescale)))
        self.i2c.write8(self.__MODE1, oldmode)
        time.sleep(0.005)
        self.i2c.write8(self.__MODE1, oldmode | 0x80)

    def setPWM(self, channel, on, off):
        "Sets a single PWM channel"
        self.i2c.write8(self.__LED0_ON_L + 4 * channel, on & 0xFF)
        self.i2c.write8(self.__LED0_ON_H + 4 * channel, on >> 8)
        self.i2c.write8(self.__LED0_OFF_L + 4 * channel, off & 0xFF)
        self.i2c.write8(self.__LED0_OFF_H + 4 * channel, off >> 8)

    def setAllPWM(self, on, off):
        "Sets a all PWM channels"
        self.i2c.write8(self.__ALL_LED_ON_L, on & 0xFF)
        self.i2c.write8(self.__ALL_LED_ON_H, on >> 8)
        self.i2c.write8(self.__ALL_LED_OFF_L, off & 0xFF)
        self.i2c.write8(self.__ALL_LED_OFF_H, off >> 8)
Beispiel #10
0
 def __init__(self):
     self.ds3231 = Adafruit_I2C(DS3231_I2C_ADDR)
#
#

import Adafruit_I2C as I2C
import time
import sys

#if True:
#    mcp = Adafruit_MCP230XX(busnum = 1, address = 0x20, num_gpios = 16)
#    mcp.config(0, OUTPUT)
#    mcp.output(0, 1)

IODIR = 0x00
GPIO = 0x09
GPPU = 0x06
i2c = I2C.Adafruit_I2C(address=0x20)


def setAllInput():
    i2c.write8(IODIR, 0xFF)


def setAllOutput():
    i2c.write8(IODIR, 0x00)


def setPinMode(pin, input, pullup=False):
    '''Pin is the pin number to modify input/output state of.
       Input is True to set the pin as input, False to set it as output'''

    direction = i2c.readU8(IODIR)
Beispiel #12
0
import argparse
import datetime
import time

I2C_BUS = 1
FUEL_ADDRESS = 0x36

VCELL_REGISTER = 0x02
SOC_REGISTER = 0x04
MODE_REGISTER = 0x06
VERSION_REGISTER = 0x08
CONFIG_REGISTER = 0x0C
COMMAND_REGISTER = 0xFE

wire = Adafruit_I2C.Adafruit_I2C(0x36, busnum=I2C_BUS)


def writeReset():
    """ reset the fuel guage """
    return writeRegister(COMMAND_REGISTER, 0x00, 0x54)


def writeQuickStart():
    """ set the fuel guage to quick start mode """
    return writeRegister(MODE_REGISTER, 0x40, 0x00)


def writeAlertThreshold(threshold):
    """ set the alert threshold % between 1 and 32 """
    msb, lsb = readRegister(CONFIG_REGISTER)