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:
  • _gate_pin (int) – the GPIO pin being used for the Gate (binary 0/1)
  • _envelope_pin (int) – the GPIO pin being used for the Envelope (??)
  • _audio_pin (int) – the GPIO pin being used for the Audio (analog 0-1023)
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)
audio()[source]
Returns:The GPIO pin connected to the Audio
envelope()[source]
Returns:The GPIO pin connected to the Envelope
gate()[source]
Returns:The GPIO pin connected to the Gate

DHT22

SSD1306

SSD1306 Demonstration

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)
set_color(color)[source]

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.

Color

class RGB_LED.Color[source]

Setters and getters for an RGB color, used as a utility for the RGB_LED library.

get_blue()[source]
Returns:Numeric value in the blue channel
get_color()[source]
Returns:a triplet representing each color channel
Example:(red, green, blue) = myColor.getColor()
get_green()[source]
Returns:Numeric value in the green channel
get_red()[source]
Returns:Numeric value in the red channel
set_color(red, green, blue)[source]

Sets the color via ints.

Parameters:
  • red – value 0-255 representing intensity of red
  • green – value 0-255 representing intensity of green
  • blue – value 0-255 representing intensity of blue
Example:

myColor.set_color(255, 0, 255)

set_color_string(color)[source]

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”)