<s> import Adafruit_GPIO . Platform as Platform <EOL> OUT = <NUM_LIT:0> <EOL> IN = <NUM_LIT:1> <EOL> HIGH = True <EOL> LOW = False <EOL> RISING = <NUM_LIT:1> <EOL> FALLING = <NUM_LIT:2> <EOL> BOTH = <NUM_LIT:3> <EOL> PUD_OFF = <NUM_LIT:0> <EOL> PUD_DOWN = <NUM_LIT:1> <EOL> PUD_UP = <NUM_LIT:2> <EOL> class BaseGPIO ( object ) : <EOL> """<STR_LIT>""" <EOL> def setup ( self , pin , mode , pull_up_down = PUD_OFF ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def output ( self , pin , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def input ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def set_high ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> self . output ( pin , HIGH ) <EOL> def set_low ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> self . output ( pin , LOW ) <EOL> def is_high ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> return self . input ( pin ) == HIGH <EOL> def is_low ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> return self . input ( pin ) == LOW <EOL> def output_pins ( self , pins ) : <EOL> """<STR_LIT>""" <EOL> for pin , value in iter ( pins . items ( ) ) : <EOL> self . output ( pin , value ) <EOL> def setup_pins ( self , pins ) : <EOL> """<STR_LIT>""" <EOL> for pin , value in iter ( pins . items ( ) ) : <EOL> self . setup ( pin , value ) <EOL> def input_pins ( self , pins ) : <EOL> """<STR_LIT>""" <EOL> return [ self . input ( pin ) for pin in pins ] <EOL> def add_event_detect ( self , pin , edge ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def remove_event_detect ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def add_event_callback ( self , pin , callback ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def event_detected ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def wait_for_edge ( self , pin , edge ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def cleanup ( self , pin = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def _validate_pin ( self , pin ) : <EOL> if pin < <NUM_LIT:0> or pin >= self . NUM_GPIO : <EOL> raise ValueError ( '<STR_LIT>' . format ( self . NUM_GPIO ) ) <EOL> def _bit2 ( self , src , bit , val ) : <EOL> bit = <NUM_LIT:1> << bit <EOL> return ( src | bit ) if val else ( src & ~ bit ) <EOL> class RPiGPIOAdapter ( BaseGPIO ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rpi_gpio , mode = None ) : <EOL> self . rpi_gpio = rpi_gpio <EOL> rpi_gpio . setwarnings ( False ) <EOL> if mode == rpi_gpio . BOARD or mode == rpi_gpio . BCM : <EOL> rpi_gpio . setmode ( mode ) <EOL> elif mode is not None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> rpi_gpio . setmode ( rpi_gpio . BCM ) <EOL> self . _dir_mapping = { OUT : rpi_gpio . OUT , <EOL> IN : rpi_gpio . IN } <EOL> self . _pud_mapping = { PUD_OFF : rpi_gpio . PUD_OFF , <EOL> PUD_DOWN : rpi_gpio . PUD_DOWN , <EOL> PUD_UP : rpi_gpio . PUD_UP } <EOL> self . _edge_mapping = { RISING : rpi_gpio . RISING , <EOL> FALLING : rpi_gpio . FALLING , <EOL> BOTH : rpi_gpio . BOTH } <EOL> def setup ( self , pin , mode , pull_up_down = PUD_OFF ) : <EOL> """<STR_LIT>""" <EOL> self . rpi_gpio . setup ( pin , self . _dir_mapping [ mode ] , <EOL> pull_up_down = self . _pud_mapping [ pull_up_down ] ) <EOL> def output ( self , pin , value ) : <EOL> """<STR_LIT>""" <EOL> self . rpi_gpio . output ( pin , value ) <EOL> def input ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> return self . rpi_gpio . input ( pin ) <EOL> def input_pins ( self , pins ) : <EOL> """<STR_LIT>""" <EOL> return [ self . rpi_gpio . input ( pin ) for pin in pins ] <EOL> def add_event_detect ( self , pin , edge , callback = None , bouncetime = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { } <EOL> if callback : <EOL> kwargs [ '<STR_LIT>' ] = callback <EOL> if bouncetime > <NUM_LIT:0> : <EOL> kwargs [ '<STR_LIT>' ] = bouncetime <EOL> self . rpi_gpio . add_event_detect ( pin , self . _edge_mapping [ edge ] , ** kwargs ) <EOL> def remove_event_detect ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> self . rpi_gpio . remove_event_detect ( pin ) <EOL> def add_event_callback ( self , pin , callback ) : <EOL> """<STR_LIT>""" <EOL> self . rpi_gpio . add_event_callback ( pin , callback ) <EOL> def event_detected ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> return self . rpi_gpio . event_detected ( pin ) <EOL> def wait_for_edge ( self , pin , edge ) : <EOL> """<STR_LIT>""" <EOL> self . rpi_gpio . wait_for_edge ( pin , self . _edge_mapping [ edge ] ) <EOL> def cleanup ( self , pin = None ) : <EOL> """<STR_LIT>""" <EOL> if pin is None : <EOL> self . rpi_gpio . cleanup ( ) <EOL> else : <EOL> self . rpi_gpio . cleanup ( pin ) <EOL> class AdafruitBBIOAdapter ( BaseGPIO ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , bbio_gpio ) : <EOL> self . bbio_gpio = bbio_gpio <EOL> self . _dir_mapping = { OUT : bbio_gpio . OUT , <EOL> IN : bbio_gpio . IN } <EOL> self . _pud_mapping = { PUD_OFF : bbio_gpio . PUD_OFF , <EOL> PUD_DOWN : bbio_gpio . PUD_DOWN , <EOL> PUD_UP : bbio_gpio . PUD_UP } <EOL> self . _edge_mapping = { RISING : bbio_gpio . RISING , <EOL> FALLING : bbio_gpio . FALLING , <EOL> BOTH : bbio_gpio . BOTH } <EOL> def setup ( self , pin , mode , pull_up_down = PUD_OFF ) : <EOL> """<STR_LIT>""" <EOL> self . bbio_gpio . setup ( pin , self . _dir_mapping [ mode ] , <EOL> pull_up_down = self . _pud_mapping [ pull_up_down ] ) <EOL> def output ( self , pin , value ) : <EOL> """<STR_LIT>""" <EOL> self . bbio_gpio . output ( pin , value ) <EOL> def input ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> return self . bbio_gpio . input ( pin ) <EOL> def input_pins ( self , pins ) : <EOL> """<STR_LIT>""" <EOL> return [ self . bbio_gpio . input ( pin ) for pin in pins ] <EOL> def add_event_detect ( self , pin , edge , callback = None , bouncetime = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { } <EOL> if callback : <EOL> kwargs [ '<STR_LIT>' ] = callback <EOL> if bouncetime > <NUM_LIT:0> : <EOL> kwargs [ '<STR_LIT>' ] = bouncetime <EOL> self . bbio_gpio . add_event_detect ( pin , self . _edge_mapping [ edge ] , ** kwargs ) <EOL> def remove_event_detect ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> self . bbio_gpio . remove_event_detect ( pin ) <EOL> def add_event_callback ( self , pin , callback , bouncetime = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { } <EOL> if bouncetime > <NUM_LIT:0> : <EOL> kwargs [ '<STR_LIT>' ] = bouncetime <EOL> self . bbio_gpio . add_event_callback ( pin , callback , ** kwargs ) <EOL> def event_detected ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> return self . bbio_gpio . event_detected ( pin ) <EOL> def wait_for_edge ( self , pin , edge ) : <EOL> """<STR_LIT>""" <EOL> self . bbio_gpio . wait_for_edge ( pin , self . _edge_mapping [ edge ] ) <EOL> def cleanup ( self , pin = None ) : <EOL> """<STR_LIT>""" <EOL> if pin is None : <EOL> self . bbio_gpio . cleanup ( ) <EOL> else : <EOL> self . bbio_gpio . cleanup ( pin ) <EOL> class AdafruitMinnowAdapter ( BaseGPIO ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mraa_gpio ) : <EOL> self . mraa_gpio = mraa_gpio <EOL> self . _dir_mapping = { OUT : self . mraa_gpio . DIR_OUT , <EOL> IN : self . mraa_gpio . DIR_IN } <EOL> self . _pud_mapping = { PUD_OFF : self . mraa_gpio . MODE_STRONG , <EOL> PUD_UP : self . mraa_gpio . MODE_HIZ , <EOL> PUD_DOWN : self . mraa_gpio . MODE_PULLDOWN } <EOL> self . _edge_mapping = { RISING : self . mraa_gpio . EDGE_RISING , <EOL> FALLING : self . mraa_gpio . EDGE_FALLING , <EOL> BOTH : self . mraa_gpio . EDGE_BOTH } <EOL> def setup ( self , pin , mode ) : <EOL> """<STR_LIT>""" <EOL> self . mraa_gpio . Gpio . dir ( self . mraa_gpio . Gpio ( pin ) , self . _dir_mapping [ mode ] ) <EOL> def output ( self , pin , value ) : <EOL> """<STR_LIT>""" <EOL> self . mraa_gpio . Gpio . write ( self . mraa_gpio . Gpio ( pin ) , value ) <EOL> def input ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> return self . mraa_gpio . Gpio . read ( self . mraa_gpio . Gpio ( pin ) ) <EOL> def add_event_detect ( self , pin , edge , callback = None , bouncetime = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { } <EOL> if callback : <EOL> kwargs [ '<STR_LIT>' ] = callback <EOL> if bouncetime > <NUM_LIT:0> : <EOL> kwargs [ '<STR_LIT>' ] = bouncetime <EOL> self . mraa_gpio . Gpio . isr ( self . mraa_gpio . Gpio ( pin ) , self . _edge_mapping [ edge ] , ** kwargs ) <EOL> def remove_event_detect ( self , pin ) : <EOL> """<STR_LIT>""" <EOL> self . mraa_gpio . Gpio . isrExit ( self . mraa_gpio . Gpio ( pin ) ) <EOL> def wait_for_edge ( self , pin , edge ) : <EOL> """<STR_LIT>""" <EOL> self . bbio_gpio . wait_for_edge ( self . mraa_gpio . Gpio ( pin ) , self . _edge_mapping [ edge ] ) <EOL> def get_platform_gpio ( ** keywords ) : <EOL> """<STR_LIT>""" <EOL> plat = Platform . platform_detect ( ) <EOL> if plat == Platform . RASPBERRY_PI : <EOL> import RPi . GPIO <EOL> return RPiGPIOAdapter ( RPi . GPIO , ** keywords ) <EOL> elif plat == Platform . BEAGLEBONE_BLACK : <EOL> import Adafruit_BBIO . GPIO <EOL> return AdafruitBBIOAdapter ( Adafruit_BBIO . GPIO , ** keywords ) <EOL> elif plat == Platform . MINNOWBOARD : <EOL> import mraa <EOL> return AdafruitMinnowAdapter ( mraa , ** keywords ) <EOL> elif plat == Platform . UNKNOWN : <EOL> raise RuntimeError ( '<STR_LIT>' ) </s>
