How to use gpiozero - 10 common examples

To help you get started, we’ve selected a few gpiozero examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gpiozero / gpiozero / gpiozero / tones.py View on Github external
def note(self):
        """
        Return the (nearest) note to the tone's frequency. This will be a
        string in the form accepted by :meth:`from_note`. If the frequency is
        outside the range represented by this format ("A0" is approximately
        27.5Hz, and "G9" is approximately 12.5Khz) a :exc:`ValueError`
        exception will be raised.
        """
        offset = self.midi - 60  # self.midi - A4_midi + Tone.tones.index('A')
        index = offset % 12      # offset % len(Tone.tones)
        octave = 4 + offset // 12
        if 0 <= octave <= 9:
            return (
                Tone.tones[index] +
                ('#' if Tone.tones[index] == Tone.tones[index - 1] else '') +
                str(octave)
            )
        raise ValueError('%f is outside the notation range' % self.frequency)
github tinue / apa102-pi / driver / apa102.py View on Github external
"""
        self.num_led = num_led  # The number of LEDs in the Strip
        order = order.lower()
        self.rgb = RGB_MAP.get(order, RGB_MAP['rgb'])
        # Limit the brightness to the maximum if it's set higher
        if global_brightness > self.MAX_BRIGHTNESS:
            self.global_brightness = self.MAX_BRIGHTNESS
        else:
            self.global_brightness = global_brightness

        self.leds = [self.LED_START,0,0,0] * self.num_led #Pixel buffer
        self.start_frame = [0] * 4 #32 bits of zeroes
        self.end_frame = [0] * ((self.num_led + 15) // 8) #num_led/2 bits, rounded up to the next byte

        factory = PiGPIOFactory(host='localhost')
        self.spi=factory.spi(mosi_pin=mosi, clock_pin=sclk)
github gpiozero / gpiozero / tests / test_tones.py View on Github external
def test_tone_init(A4):
    with warnings.catch_warnings(record=True) as w:
        warnings.resetwarnings()
        assert Tone(440) == A4
        assert Tone("A4") == A4
        assert len(w) == 0
        assert Tone(69) == A4
        assert len(w) == 1
        assert isinstance(w[0].message, AmbiguousTone)
    assert Tone(frequency=440) == A4
    assert Tone(note="A4") == A4
    assert Tone(midi=69) == A4
    with pytest.raises(TypeError):
        Tone()
    with pytest.raises(TypeError):
        Tone(foo=1)
    with pytest.raises(TypeError):
        Tone(frequency=440, midi=69)
github gpiozero / gpiozero / tests / test_tones.py View on Github external
def test_tone_init(A4):
    with warnings.catch_warnings(record=True) as w:
        warnings.resetwarnings()
        assert Tone(440) == A4
        assert Tone("A4") == A4
        assert len(w) == 0
        assert Tone(69) == A4
        assert len(w) == 1
        assert isinstance(w[0].message, AmbiguousTone)
    assert Tone(frequency=440) == A4
    assert Tone(note="A4") == A4
    assert Tone(midi=69) == A4
    with pytest.raises(TypeError):
        Tone()
    with pytest.raises(TypeError):
        Tone(foo=1)
    with pytest.raises(TypeError):
        Tone(frequency=440, midi=69)
github gpiozero / gpiozero / tests / test_tones.py View on Github external
def test_tone_init(A4):
    with warnings.catch_warnings(record=True) as w:
        warnings.resetwarnings()
        assert Tone(440) == A4
        assert Tone("A4") == A4
        assert len(w) == 0
        assert Tone(69) == A4
        assert len(w) == 1
        assert isinstance(w[0].message, AmbiguousTone)
    assert Tone(frequency=440) == A4
    assert Tone(note="A4") == A4
    assert Tone(midi=69) == A4
    with pytest.raises(TypeError):
        Tone()
    with pytest.raises(TypeError):
        Tone(foo=1)
    with pytest.raises(TypeError):
        Tone(frequency=440, midi=69)
github gpiozero / gpiozero / tests / test_tones.py View on Github external
def test_tone_init(A4):
    with warnings.catch_warnings(record=True) as w:
        warnings.resetwarnings()
        assert Tone(440) == A4
        assert Tone("A4") == A4
        assert len(w) == 0
        assert Tone(69) == A4
        assert len(w) == 1
        assert isinstance(w[0].message, AmbiguousTone)
    assert Tone(frequency=440) == A4
    assert Tone(note="A4") == A4
    assert Tone(midi=69) == A4
    with pytest.raises(TypeError):
        Tone()
    with pytest.raises(TypeError):
        Tone(foo=1)
    with pytest.raises(TypeError):
        Tone(frequency=440, midi=69)
github gpiozero / gpiozero / tests / test_tones.py View on Github external
def test_tone_init(A4):
    with warnings.catch_warnings(record=True) as w:
        warnings.resetwarnings()
        assert Tone(440) == A4
        assert Tone("A4") == A4
        assert len(w) == 0
        assert Tone(69) == A4
        assert len(w) == 1
        assert isinstance(w[0].message, AmbiguousTone)
    assert Tone(frequency=440) == A4
    assert Tone(note="A4") == A4
    assert Tone(midi=69) == A4
    with pytest.raises(TypeError):
        Tone()
    with pytest.raises(TypeError):
        Tone(foo=1)
    with pytest.raises(TypeError):
        Tone(frequency=440, midi=69)
github gpiozero / gpiozero / tests / test_tones.py View on Github external
def test_tone_init(A4):
    with warnings.catch_warnings(record=True) as w:
        warnings.resetwarnings()
        assert Tone(440) == A4
        assert Tone("A4") == A4
        assert len(w) == 0
        assert Tone(69) == A4
        assert len(w) == 1
        assert isinstance(w[0].message, AmbiguousTone)
    assert Tone(frequency=440) == A4
    assert Tone(note="A4") == A4
    assert Tone(midi=69) == A4
    with pytest.raises(TypeError):
        Tone()
    with pytest.raises(TypeError):
        Tone(foo=1)
    with pytest.raises(TypeError):
        Tone(frequency=440, midi=69)
github gpiozero / gpiozero / tests / test_tools.py View on Github external
def test_zip_values(mock_factory):
    with Button(2) as btn1, Button(3) as btn2:
        zv = zip_values(btn1, btn2)
        assert next(zv) == (False, False)
        btn1.pin.drive_low()
        assert next(zv) == (True, False)
        btn2.pin.drive_low()
        assert next(zv) == (True, True)
        btn1.pin.drive_high()
        assert next(zv) == (False, True)
        btn2.pin.drive_high()
        assert next(zv) == (False, False)
    with Button(2) as btn1, Button(3) as btn2, Button(4) as btn3, Button(5) as btn4:
        zv = zip_values(btn1, btn2, btn3, btn4)
        assert next(zv) == (False, False, False, False)
        btn1.pin.drive_low()
        btn3.pin.drive_low()
        assert next(zv) == (True, False, True, False)
        btn2.pin.drive_low()
        btn4.pin.drive_low()
        assert next(zv) == (True, True, True, True)
        btn1.pin.drive_high()
        btn2.pin.drive_high()
        btn3.pin.drive_high()
        btn4.pin.drive_high()
        sleep(epsilon)
        assert next(zv) == (False, False, False, False)
github vwillcox / speedtest-cron / buttons.py View on Github external
import subprocess
from PIL import Image
from PIL import ImageOps
import ImageDraw
import ImageFont
import ipgetter
import socket
import subprocess
import shlex
from EPD import EPD

led = RGBLED(red=6, green=12, blue=5)
button1 = Button(16)
button2 = Button(19)
button3 = Button(20)
button4 = Button(26)


if os.name != "nt":
    import fcntl
    import struct

    def get_interface_ip(ifname):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s',
                                ifname[:15]))[20:24])

def get_lan_ip():
    ip = socket.gethostbyname(socket.gethostname())
    if ip.startswith("127.") and os.name != "nt":
        interfaces = [
            "eth0",