PiB Python Libraries¶
The following classes are available to students and hobbyists completing a Project in a Box project. They provide examples and enumerate the functions for each hardware device used in our projects. In the content of each project, you are asked to write scripts that interact with circuits, often to save or display readings. We provide these libraries to help you interface with hardware to fulfill those tasks.
In your driver scripts, import these libraries with the following line, replacing DHT22 with the sensor you need:
from libraries.DHT22 import DHT22
Sound_Detector(_gate_pin, _envelope_pin, _audio_pin)¶
A class that holds the pins used for the Sound Detector, so that driver scripts can reference them as arguments to the MCP device when reading their values
mySoundDetector = Sound_Detector(17, 22, 27)
It must be used in conjunction with the MCP300X library, with calls such as:
Example: gate_value = myMCP.read(mySoundDetector.gate())
Since the MCP300X library exposes channel aliases, you may wish to increase readability by initializing the sound detector like this. See MCP300X documentation for details.
Example: mySoundDetector = MCP300X(17, myMCP.CH0, myMCP.CH1)
Returns: The GPIO pin connected to the Audio
Returns: The GPIO pin connected to the Envelope
Returns: The GPIO pin connected to the Gate
RGB_LED(_red_pin, _green_pin, _blue_pin, _is_common_anode=False)¶
A library for interfacing with common anode or common cathode RGB LEDs. The LEDs provided in PiB kits are common anode. Utilizes two helper utilities, COLOR and GPIO_PWM.
GPIO_PWM abstracts exec commands which use the PiBits ServoBlaster tool (mega props to this convenient, well-documented solution to the problem of Raspberry Pi’s only having one PWM pin!) to write to each channel of the LED The COLOR utility accepts either a string of the form “FFFFFF” or values between 0 and 255 for each color channel, and is the only type accepted by the RGB_LED when setting a color.
Example usage in a driver script:
red_pin = 22 green_pin = 27 blue_pin = 17 is_common_anode=True rgb = RGB_LED(red_pin, green_pin, blue_pin,is_common_anode) color = Color() color.set_color(0, 255, 0) # sets color to pure green rgb.set_color(color)
Sets the LED on hardware to the RGB color stored in the color parameter.
Param: A color object, which must be set to the desired color before being passed. Upon construction, the color defaults to black, a.k.a., off.
Setters and getters for an RGB color, used as a utility for the RGB_LED library.
Returns: Numeric value in the blue channel
Returns: a triplet representing each color channel Example: (red, green, blue) = myColor.getColor()
Returns: Numeric value in the green channel
Returns: Numeric value in the red channel
set_color(red, green, blue)¶
Sets the color via ints.
- red – value 0-255 representing intensity of red
- green – value 0-255 representing intensity of green
- blue – value 0-255 representing intensity of blue
myColor.set_color(255, 0, 255)
Sets the color by parsing a hex color code into RGB values.
Parameters: color – A string with 6 hex digits, e.g. “FFFFFF” Example: myColor.set_color_string(“0F0F0F”)