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
MCP300X¶
Sound Detector¶
-
class
Sound_Detector.
Sound_Detector
(_gate_pin, _envelope_pin, _audio_pin)[source]¶ 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
Parameters: Example: 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)
DHT22¶
RGB LED¶
-
class
RGB_LED.
RGB_LED
(_red_pin, _green_pin, _blue_pin, _is_common_anode=False)[source]¶ 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)
Color¶
-
class
RGB_LED.
Color
[source]¶ Setters and getters for an RGB color, used as a utility for the RGB_LED library.
-
get_color
()[source]¶ Returns: a triplet representing each color channel Example: (red, green, blue) = myColor.getColor()
-