I’ve been playing with my RPi fairly frequently as of late. I ordered some tactile switches, motors, and motor controllers from AdaFruit and since they came in I’ve been learning to control motor speed, direction, and LED brightness using soft-PWM.

I quickly learned that I would want a class to wrap up all the GPIO sets/gets. I’ve been writing a package in Python which I’m calling RPiComponents containing the basic interfaces necessary for controlling the GPIO pins in an object-oriented fashion. I’ll be posting it to GitHub as soon as I finish the documentation for it. As a preview though, you can currently do things like:

	import RPiComponents as parts
	red_led_pin = 21  # using the BCM pin numbering
	initial_brightness = 0  # initially off
	red_led = parts.LED.FadableLED(red_led_pin, initial_brightness)
	red_led.set_brightness(50)  # set the brightness to 50%
	red_led.set_brightness(100) # '   '   '          '  100%

	motor_enable_pin = 21
	motor_fwd_pin = 20
	motor_rev_pin = 16

	motor = parts.L293DMotor.VariableSpeedL293DMotor(motor_enable_pin, motor_fwd_pin, motor_rev_pin)
	motor.enable()  # sets the motor controller enable bit to HIGH
	motor.set_speed(50)  # sets the motor speed to 50% of it's max



	motor.rev(90)  # starts the motor in reverse at 90% of the max speed

	motor.disable() # all speed and direction setting are kept but the motor will stop

	def some_callback(some_arg0):
		print "Called some_callback with argument %s" % (some_arg0)

	switch_pin_number = 6

	switch = parts.Switch.SwitchWithThreadedCallback(switch_pin_number, callback=some_callback, callback_args=[1,2,3])

	#  calling switch.start() starts a monitor thread that waits for a rising or falling edge
	#  due to the switch changing state, it will then fire off callback with the arguments given
	#  or if callback_args is None, it will simply call the callback


	#  switch.stop() properly terminates the monitor thread

	parts.finalize()  # clean up after ourselves and reset the GPIO pins for some other use

There’s also a few super simple abstractions like BasicToggleOutput with methods high() and low() in a module called BasicLogic. I hope to add some more neat functionality later on as well. I’ll post again when the source is on GitHub and attach a few examples for starters!

Thanks for reading!!