Пример #1
0
def require_repeated_start():
    """Enable repeated start conditions for I2C register reads.  This is the
    normal behavior for I2C, however on some platforms like the Raspberry Pi
    there are bugs which disable repeated starts unless explicitly enabled with
    this function.  See this thread for more details:
      http://www.raspberrypi.org/forums/viewtopic.php?f=44&t=15840
    """
    plat = Platform.platform_detect()
    if plat == Platform.RASPBERRY_PI and Platform.pi_version() == "BCM2708":
        # On the Raspberry Pi there is a bug where register reads don't send a
        # repeated start condition like the kernel smbus I2C driver functions
        # define.  As a workaround this bit in the BCM2708 driver sysfs tree can
        # be changed to enable I2C repeated starts.
        subprocess.check_call(
            'chmod 666 /sys/module/i2c_bcm2708/parameters/combined',
            shell=True)
        subprocess.check_call(
            'echo -n 1 > /sys/module/i2c_bcm2708/parameters/combined',
            shell=True)
Пример #2
0
import sys

# We intentionally are patching into this namespace as module names so skip the name check.
# pylint: disable=invalid-name
platform = sys.platform

board_id = None
if platform is not None:
    if platform == "esp8266":  # TODO more conservative board-guessing
        board_id = "feather_huzzah"
    elif platform == "samd21":
        board_id = "feather_m0_express"
    elif platform == "pyboard":
        platform = "stm32"
        board_id = "pyboard"
    elif platform == "linux":
        from Adafruit_GPIO import Platform
        if Platform.platform_detect() == Platform.RASPBERRY_PI:
            if Platform.pi_version() == 1:
                board_id = "raspi_1"
            elif Platform.pi_version() == 2:
                board_id = "raspi_2"
            elif Platform.pi_version() == 3:
                board_id = "raspi_3"

implementation = sys.implementation.name
if implementation == "micropython":
    from utime import sleep
elif implementation == "circuitpython" or implementation == "cpython":
    from time import sleep
Пример #3
0
def init(config_data={}):
    """
    Initializes the module
    """
    def build_pin(name, pin_config, pin=None, index=0):
        if pin is None:
            if isinstance(pin_config[CONF_KEY_PIN], list):
                pin = pin_config[CONF_KEY_PIN][index]
                index_sub = index + pin_config[CONF_KEY_FIRST_INDEX]
            else:
                pin = pin_config[CONF_KEY_PIN]
                index_sub = ""
        if isinstance(pin_config[CONF_KEY_TOPIC], list):
            topic = pin_config[CONF_KEY_TOPIC][index]
        else:
            topic = pin_config[CONF_KEY_TOPIC]
        return {
            "name":
            name.format(index=index + pin_config[CONF_KEY_FIRST_INDEX]),
            CONF_KEY_TOPIC:
            resolve_topic(topic,
                          subtopics=["{module_topic}"],
                          substitutions={
                              "module_topic": raw_config[CONF_KEY_TOPIC],
                              "module_name": TEXT_NAME,
                              "pin": pin,
                              "pin_name": name,
                              "index": index_sub
                          }),
            CONF_KEY_DIRECTION:
            pin_config[CONF_KEY_DIRECTION],
            CONF_KEY_INTERRUPT:
            pin_config[CONF_KEY_INTERRUPT],
            CONF_KEY_RESISTOR:
            pin_config[CONF_KEY_RESISTOR],
            CONF_KEY_INVERT:
            pin_config[CONF_KEY_INVERT],
            CONF_KEY_INITIAL:
            pin_config[CONF_KEY_INITIAL].format(
                payload_on=raw_config[CONF_KEY_PAYLOAD_ON],
                payload_off=raw_config[CONF_KEY_PAYLOAD_OFF])
        }

    global gpio, GPIO_PINS

    pi_version = Platform.pi_version()
    if pi_version:
        log.debug("Platform is Raspberry Pi {}".format(pi_version))
        if pi_version == 1:
            log.error("No platform config for Raspberry Pi 1")
            return False
        elif pi_version == 2:
            log.error("No platform config for Raspberry Pi 2")
            return False
        elif pi_version == 3:
            GPIO_PINS = GPIO_PINS_RPI3

        gpio = GPIO.get_platform_gpio(mode=11)
    else:
        log.error("Unknown platform")
        return False

    raw_config = parse_config(config_data, CONF_OPTIONS, log)
    if raw_config:
        log.debug("Config loaded")

        for name in [
                key for key in raw_config if isinstance(raw_config[key], dict)
        ]:
            named_config = raw_config.pop(name)
            if isinstance(named_config[CONF_KEY_PIN], int):
                pin = named_config[CONF_KEY_PIN]
                if pin not in pins:
                    pins[pin] = build_pin(name, named_config)
                    log.debug(
                        "Configured GPIO{pin:02d} with options: {options}".
                        format(pin=pin, options=pins[pin]))
                else:
                    log.warn(
                        "Duplicate configuration for GPIO{pin:02d} found in '{name}' will be ignored, pin already configured under '{original}'"
                        .format(pin=pin, name=name,
                                original=pins[pin]["name"]))
            elif isinstance(named_config[CONF_KEY_PIN], list):
                for index in range(len(named_config[CONF_KEY_PIN])):
                    pin = named_config[CONF_KEY_PIN][index]
                    if pin not in pins:
                        pins[pin] = build_pin(name, named_config, index=index)
                        log.debug(
                            "Configured GPIO{pin:02d} with options: {options}".
                            format(pin=pin, options=pins[pin]))
                    else:
                        log.warn(
                            "Duplicate configuration for GPIO{pin:02d} found in '{name}' will be ignored, pin already configured under '{original}'"
                            .format(pin=pin,
                                    name=name,
                                    original=pins[pin]["name"]))

        config.update(raw_config)
        return True
    else:
        log.error("Error loading config")
        return False