Python RPi.GPIO.getmode() Examples

The following are 8 code examples of RPi.GPIO.getmode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module RPi.GPIO , or try the search function .
Example #1
Source File: index.py    From rpiapi with MIT License 6 votes vote down vote up
def index(environ, response):

	modes = {
		-1: "MODE_UNKNOWN",
		10: "BOARD",
		11: "BCM",
		40: "SERIAL",
		41: "SPI",
		42: "I2C",
		43: "PWM"
	}
	
	status = "200 OK"
	
	header = [("Content-Type", "application/json")]
	
	result = {
		"GPIO.RPI_INFO": GPIO.RPI_INFO,
		"GPIO.VERSION": GPIO.VERSION,
		"MODE": modes[ GPIO.getmode() ]
	}
	
	response(status, header)
	
	return [json.dumps(result).encode()] 
Example #2
Source File: ServoPi.py    From ABElectronics_Python_Libraries with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, address=0x40):
        """
        init object with i2c address, default is 0x40 for ServoPi board

        :param address: device i2c address, defaults to 0x40
        :type address: int, optional
        """
        self.__address = address
        self.__bus = self.__get_smbus()
        self.__write(self.__MODE1, self.__mode1_default)
        self.__write(self.__MODE2, self.__mode2_default)
        GPIO.setwarnings(False)

        mode = GPIO.getmode()  # check if the GPIO mode has been set

        if (mode == 10):  # Mode set to GPIO.BOARD
            self.__oe_pin = 7
        elif (mode == 11):  # Mode set to GPIO.BCM
            self.__oe_pin = 4
        else:  # Mode not set
            GPIO.setmode(GPIO.BOARD)
            self.__oe_pin = 7

        GPIO.setup(self.__oe_pin, GPIO.OUT) 
Example #3
Source File: __init__.py    From OctoPrint-PSUControl with GNU Affero General Public License v3.0 5 votes vote down vote up
def _gpio_get_pin(self, pin):
        if (GPIO.getmode() == GPIO.BOARD and self.GPIOMode == 'BOARD') or (GPIO.getmode() == GPIO.BCM and self.GPIOMode == 'BCM'):
            return pin
        elif GPIO.getmode() == GPIO.BOARD and self.GPIOMode == 'BCM':
            return self._gpio_bcm_to_board(pin)
        elif GPIO.getmode() == GPIO.BCM and self.GPIOMode == 'BOARD':
            return self._gpio_board_to_bcm(pin)
        else:
            return 0 
Example #4
Source File: __init__.py    From platypush with MIT License 5 votes vote down vote up
def _init_board(self):
        import RPi.GPIO as GPIO

        with self._init_lock:
            if self._initialized and GPIO.getmode():
                return

            GPIO.setmode(self.mode)
            self._initialized = True 
Example #5
Source File: __init__.py    From OctoPrint-Enclosure with GNU General Public License v3.0 5 votes vote down vote up
def setup_gpio(self):
        try:
            current_mode = GPIO.getmode()
            set_mode = GPIO.BOARD if self._settings.get(["use_board_pin_number"]) else GPIO.BCM
            if current_mode is None:
                outputs = list(filter(
                    lambda item: item['output_type'] == 'regular' or item['output_type'] == 'pwm' or item[
                        'output_type'] == 'temp_hum_control' or item['output_type'] == 'neopixel_direct',
                    self.rpi_outputs))
                inputs = list(filter(lambda item: item['input_type'] == 'gpio', self.rpi_inputs))
                gpios = outputs + inputs
                if gpios:
                    GPIO.setmode(set_mode)
                    tempstr = "BOARD" if set_mode == GPIO.BOARD else "BCM"
                    self._logger.info("Setting GPIO mode to %s", tempstr)
            elif current_mode != set_mode:
                GPIO.setmode(current_mode)
                tempstr = "BOARD" if current_mode == GPIO.BOARD else "BCM"
                self._settings.set(["use_board_pin_number"], True if current_mode == GPIO.BOARD else False)
                warn_msg = "GPIO mode was configured before, GPIO mode will be forced to use: " + tempstr + " as pin numbers. Please update GPIO accordingly!"
                self._logger.info(warn_msg)
                self._plugin_manager.send_plugin_message(self._identifier,
                    dict(is_msg=True, msg=warn_msg, msg_type="error"))
            GPIO.setwarnings(False)
        except Exception as ex:
            self.log_error(ex) 
Example #6
Source File: l298n.py    From letsrobot with Apache License 2.0 5 votes vote down vote up
def setup(robot_config):
    global StepPinForward
    global StepPinBackward
    global StepPinLeft
    global StepPinRight
    global sleeptime
    global rotatetimes
    
    sleeptime = robot_config.getfloat('l298n', 'sleeptime')
    rotatetimes = robot_config.getfloat('l298n', 'rotatetimes')
    
    log.debug("GPIO mode : %s", str(GPIO.getmode()))

    GPIO.setwarnings(False)
    GPIO.cleanup()
    
    if robot_config.getboolean('tts', 'ext_chat'): #ext_chat enabled, add motor commands
        extended_command.add_command('.set_rotate_time', set_rotate_time)
        extended_command.add_command('.set_sleep_time', set_sleep_time)

# TODO passing these as tuples may be unnecessary, it may accept lists as well. 
    StepPinForward = tuple(map(int, robot_config.get('l298n', 'StepPinForward').split(',')))
    StepPinBackward = tuple(map(int,robot_config.get('l298n', 'StepPinBackward').split(',')))
    StepPinLeft = tuple(map(int,robot_config.get('l298n', 'StepPinLeft').split(',')))
    StepPinRight = tuple(map(int,robot_config.get('l298n', 'StepPinRight').split(',')))
	
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(StepPinForward, GPIO.OUT)
    GPIO.setup(StepPinBackward, GPIO.OUT)
    GPIO.setup(StepPinLeft, GPIO.OUT)
    GPIO.setup(StepPinRight, GPIO.OUT) 
Example #7
Source File: MFRC522.py    From MFRC522-python with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, bus=0, device=0, spd=1000000, pin_mode=10, pin_rst=-1, debugLevel='WARNING'):
        self.spi = spidev.SpiDev()
        self.spi.open(bus, device)
        self.spi.max_speed_hz = spd

        self.logger = logging.getLogger('mfrc522Logger')
        self.logger.addHandler(logging.StreamHandler())
        level = logging.getLevelName(debugLevel)
        self.logger.setLevel(level)

        gpioMode = GPIO.getmode()
        
        if gpioMode is None:
            GPIO.setmode(pin_mode)
        else:
            pin_mode = gpioMode
            
        if pin_rst == -1:
            if pin_mode == 11:
                pin_rst = 15
            else:
                pin_rst = 22
            
        GPIO.setup(pin_rst, GPIO.OUT)
        GPIO.output(pin_rst, 1)
        self.MFRC522_Init() 
Example #8
Source File: __init__.py    From OctoPrint-PSUControl with GNU Affero General Public License v3.0 4 votes vote down vote up
def _configure_gpio(self):
        if not self._hasGPIO:
            self._logger.error("RPi.GPIO is required.")
            return
        
        self._logger.info("Running RPi.GPIO version %s" % GPIO.VERSION)
        if GPIO.VERSION < "0.6":
            self._logger.error("RPi.GPIO version 0.6.0 or greater required.")
        
        GPIO.setwarnings(False)

        for pin in self._configuredGPIOPins:
            self._logger.debug("Cleaning up pin %s" % pin)
            try:
                GPIO.cleanup(self._gpio_get_pin(pin))
            except (RuntimeError, ValueError) as e:
                self._logger.error(e)
        self._configuredGPIOPins = []

        if GPIO.getmode() is None:
            if self.GPIOMode == 'BOARD':
                GPIO.setmode(GPIO.BOARD)
            elif self.GPIOMode == 'BCM':
                GPIO.setmode(GPIO.BCM)
            else:
                return
        
        if self.sensingMethod == 'GPIO':
            self._logger.info("Using GPIO sensing to determine PSU on/off state.")
            self._logger.info("Configuring GPIO for pin %s" % self.senseGPIOPin)

            if self.senseGPIOPinPUD == 'PULL_UP':
                pudsenseGPIOPin = GPIO.PUD_UP
            elif self.senseGPIOPinPUD == 'PULL_DOWN':
                pudsenseGPIOPin = GPIO.PUD_DOWN
            else:
                pudsenseGPIOPin = GPIO.PUD_OFF
    
            try:
                GPIO.setup(self._gpio_get_pin(self.senseGPIOPin), GPIO.IN, pull_up_down=pudsenseGPIOPin)
                self._configuredGPIOPins.append(self.senseGPIOPin)
            except (RuntimeError, ValueError) as e:
                self._logger.error(e)
        
        if self.switchingMethod == 'GPIO':
            self._logger.info("Using GPIO for On/Off")
            self._logger.info("Configuring GPIO for pin %s" % self.onoffGPIOPin)
            try:
                if not self.invertonoffGPIOPin:
                    initial_pin_output=GPIO.LOW
                else:
                    initial_pin_output=GPIO.HIGH
                GPIO.setup(self._gpio_get_pin(self.onoffGPIOPin), GPIO.OUT, initial=initial_pin_output)
                self._configuredGPIOPins.append(self.onoffGPIOPin)
            except (RuntimeError, ValueError) as e:
                self._logger.error(e)