Welcome

This documentation will guide you through the methods available in the Rainbow HAT python library.

Rainbow HAT is a Raspberry Pi add-on designed for Android Things, it includes:

At A Glance

buzzer.note(frequency, duration=1.0)
buzzer.midi_note(note_number, duration=1.0)
buzzer.clear_timeout()
buzzer.stop()
rainbow.set_brightness(brightness)
rainbow.clear()
rainbow.show()
rainbow.set_all(r, g, b, brightness=None)
rainbow.set_pixel(x, r, g, b, brightness=None)
rainbow.set_clear_on_exit(value=True)
weather.temperature()
weather.pressure()
weather.altitude(qnh=1020)
weather.update()
rainbowhat.lights
rainbowhat.touch

Buzzer: Play A Note

buzzer.note(frequency, duration=1.0)

Play a single note.

Parameters:
  • frequency – Musical frequency in hertz
  • duration – Optional duration in seconds, use None to sustain note

Buzzer: Play A Midi Note

buzzer.midi_note(note_number, duration=1.0)

Play a single note by MIDI note number.

Converts a MIDI note number into a frequency and plays it. A5 is 69.

Parameters:
  • note_number – MIDI note number of note
  • duration – Optional duration in seconds, use None to sustain note

Buzzer: Stop The Buzzer

buzzer.stop()

Stop buzzer.

Immediately silences the buzzer.

Buzzer: Clear Note-off Timeout

buzzer.clear_timeout()

Clear any note timeout set.

Will cause any pending playing note to be sustained.

Rainbow: Set Brightness

rainbow.set_brightness(brightness)

Set the brightness of all pixels

Parameters:brightness – Brightness: 0.0 to 1.0

Rainbow: Clear

rainbow.clear()

Clear the pixel buffer

Rainbow: Show

rainbow.show()

Output the buffer

Rainbow: Set All Pixels

rainbow.set_all(r, g, b, brightness=None)

Set the RGB value and optionally brightness of all pixels

If you don’t supply a brightness value, the last value set for each pixel be kept.

Parameters:
  • r – Amount of red: 0 to 255
  • g – Amount of green: 0 to 255
  • b – Amount of blue: 0 to 255
  • brightness – Brightness: 0.0 to 1.0 (default around 0.2)

Rainbow: Set Single Pixel

rainbow.set_pixel(x, r, g, b, brightness=None)

Set the RGB value, and optionally brightness, of a single pixel

If you don’t supply a brightness value, the last value will be kept.

Parameters:
  • x – The horizontal position of the pixel: 0 to 7
  • r – Amount of red: 0 to 255
  • g – Amount of green: 0 to 255
  • b – Amount of blue: 0 to 255
  • brightness – Brightness: 0.0 to 1.0 (default around 0.2)

Rainbow: Set Clear-on-exit

rainbow.set_clear_on_exit(value=True)

Set whether the APA102 should be cleared upon exit

By default the APA102 will turn off the pixels on exit, but calling:

set_clear_on_exit(False)

Will ensure that it does not.

Parameters:value – True or False (default True)

Weather: Get Temperature

weather.temperature()

Return the current temperature.

Note: This value may be affected by nearby sources of heat, including the Pi itself.

Weather: Get Pressure

weather.pressure()

Return the current air pressure.

Weather: Get Altitutde

weather.altitude(qnh=1020)

Return the current approximate altitude.

Parameters:qnh – Your local value for atmospheric pressure adjusted to sea level.

Weather: Update

weather.update()

Update stored temperature and pressure values.

This function is called automatically when calling temperature() or pressure().

Lights

Rainbow HAT has three lights: Red, Green and Blue. They can be accessed by name like so:

rainbowhat.light.red

rainbowhat.light.green

rainbowhat.light.blue

Each light is an instance of the Light class, the following methods are available:

class rainbowhat.lights.Light(gpio_pin)[source]
off()[source]

Turn the light off.

on()[source]

Turn the light on.

toggle()[source]

Toggle the light.

write(value)[source]

Write a value to the light.

Parameters:value – Either True or False to turn light on or off/

Eg: rainbowhat.light.red.on()

Touch

Rainbow HAT has three touch inputs: A, B and C. They can be accessed lke so:

rainbowhat.touch.A

rainbowhat.touch.B

rainbowhat.touch.C

Each touch input is an instance of the Button class, the following methods are available

class rainbowhat.touch.Button(index, gpio_pin)[source]
press(handler=None)[source]

Bind a function to handle touch press.

release(handler=None)[source]

Bind a funciton to handle touch release.

You can use touch handlers either by passing in a function by name, or using the method as a decorator. Eg:

@rainbowhat.touch.A.press():
def press(button):
    print("A pressed!")

Or:

def press(button):
    print("Button {} pressed!".format(button))

rainbowhat.touch.A.press(press)
rainbowhat.touch.B.press(press)