Package: gpiocrust
A concise, pythonic wrapper around the Raspberry Pi’sRPi.GPIOlibrary. An encrusting, if you will.With (almost silent) fallback to mock objects, you can prototype pin I/O locally on your favorite computer, even when your Pi is on the other side of town (seeMock APIfor more details).gpiocrustis fully compatible with Python 2 and Python 3.In a nutshellDownload from PyPI witheasy_installorpip.pipinstallgpiocrustIn a.pyfile, import the library and start setting pin values. It’s easy! Here’s a bare-bones example that will turn on GPIO pin 15:fromgpiocrustimportHeader,OutputPinwithHeader()asheader:pin=OutputPin(15,value=0)pin.value=1APIgpiocrustprovides four classes to help give GPIO pin manipulation a more object-oriented feel. They are:HeaderOutputPinPWMOutputPinInputPinHeaderTheHeaderclass just wraps the GPIO setup and teardown methods.
Most importantly, it ensures thatGPIO.cleanup()is called. For
example:fromgpiocrustimportHeaderwithHeader()asheader:# Application logic goes herepass# All cleaned up now.OutputPinTheOutputPinclass controls a single GPIO pin for output. Set its
value toTrue(1) orFalse(0). That’s all there is to
it!fromgpiocrustimportHeader,OutputPinwithHeader()asheader:shiny_led=OutputPin(11)shiny_led.value=True...valuedefaults toFalse, but can be set with a keyword argument.shiny_led=OutputPin(11,value=True)PWMOutputPinThePWMOutputPinclass controls a single GPIO pin for output, but
allows for variablevalues via software pulse width modulation.fromgpiocrustimportHeader,PWMOutputPinwithHeader()asheader:soft_led=PWMOutputPin(11)soft_led.value=0.25...You can set the frequency (Hz) via thefrequencyproperty. For
example:fromgpiocrustimportHeader,PWMOutputPinwithHeader()asheader:soft_led=PWMOutputPin(11,frequency=100)soft_led.frequency=50NOTE:the RPi.GPIO implementation uses duty cycle values from0to100. To be consistent withOutputPin,PWMOutputPinuses
decimal values0.0to1.0.For a good overview of how to use theRPi.GPIOimplementation, seethis video.InputPinTheInputPinclass controls a single GPIO pin for input. You can
watch for edge events using acallbackargument or via the@changedecorator. For now,InputPinonly supports watchingGPIO.BOTH(risingandfalling) events.fromgpiocrustimportHeader,InputPindefalert_president(value):passwithHeader()asheader:the_red_button=InputPin(11,callback=alert_president)It’s even cleaner with the@changedecorator.fromgpiocrustimportHeader,InputPinwithHeader()asheader:the_red_button=InputPin(11,value=0)@the_red_button.changedefalert_president(value):passMock APIMock classes are included that mimick the native GPIO functionality. The
library falls back to mock objects when theRPi.GPIOpackage cannot
be loaded. This allows one to code the general I/O flow of an
application in development environments where running code on a physical
Raspberry Pi is inconvenient or impossible (i.e, the computer you’re
reading this on).Fallback is automatic, so your import statements will look just as
before.OutputPin exampleimporttimefromgpiocrustimportHeader,OutputPin,PWMOutputPinwithHeader()asheader:pin11=OutputPin(11)pin15=PWMOutputPin(15,frequency=100,value=0)try:while1:# Going uppin11.value=Trueforiinrange(100):pin15.value=i/100.0time.sleep(0.01)time.sleep(0.5)# Going downpin11.value=Falseforiinrange(100):pin15.value=(100-i)/100.0time.sleep(0.01)time.sleep(0.5)exceptKeyboardInterrupt:pass