Ejemplo n.º 1
0
class BuzzerResource(resource.Resource):
    def get_link_description(self):
        # Publish additional data in .well-known/core
        return dict(**super().get_link_description(),
                    title=f"Buzzer Resource - pin: {self.pin}")

    def __init__(self, pin, active_high=True, initial_value=False):
        super().__init__()

        self.pin = pin
        self.resource = Buzzer(pin,
                               active_high=active_high,
                               initial_value=initial_value)

    async def render_get(self, request):
        payload = f"{self.resource.value}"
        print(f'BUZZER {self.pin}: GET')
        return Message(payload=payload.encode(), code=Code.CONTENT)

    async def render_post(self, request):
        payload = request.payload.decode()
        print(f'BUZZER {self.pin}: POST {payload}')
        if payload in ['0', 'off']:
            self.resource.off()
        elif payload in ['1', 'on']:
            self.resource.on()
        elif payload in ['-1', 'toggle']:
            self.resource.toggle()
        elif 'beep' in payload:
            p = payload.split(" ")
            if p[0] != 'beep':
                return Message(code=Code.BAD_REQUEST)

            on_time, off_time, n = 1, 1, None
            if len(p) > 1 and p[1].isdigit():
                on_time = int(p[1])
            if len(p) > 2 and p[2].isdigit():
                off_time = int(p[2])
            if len(p) > 3 and p[3].isdigit():
                n = int(p[3])

            self.resource.beep(on_time, off_time, n)
        else:
            return Message(code=Code.BAD_REQUEST)

        return Message(code=Code.CHANGED)
from gpiozero import Buzzer
from time import sleep

buzzer = Buzzer(17)

while True:
    buzzer.toggle()
    sleep(2)
    
Ejemplo n.º 3
0
class buzz:
    def __init__(self, buzzPin, quietMode=False):
        self.pin = buzzPin
        self.buzz = Buzzer(buzzPin)
        self.quietMode = quietMode

    def buzz_init(self, buzzPin):
        self.buzz = Buzzer(buzzPin)

    def quiet(self, mode):
        self.quietMode = mode

    def set_pin(self, newPin):
        self.pin = newPin
        self.buzz = Buzzer(newPin)

    def notes_range(self, up=True, duration=250):
        for x in range(1, 1000):
            if up:
                beepDelay = x
            else:
                beepDelay = 1000 - x
            times = int(round(duration / beepDelay))
            beepDelay = round((beepDelay) / 1000, 6)
            for y in range(times):
                self.buzz.on()
                sleep(beepDelay)
                self.buzz.off()
                sleep(beepDelay)
            self.buzz.off()
            sleep(0.01)  #some delay between notes

    def sound(self, note, duration):
        #notes etc
        notePairs = [['cL', 129], ['cLS', 139], ['dL', 146], ['dLS', 156],
                     ['eL', 163], ['fL', 173], ['fLS', 185], ['gL', 194],
                     ['gLS', 207], ['aL', 219], ['aLS', 228], ['bL', 232],
                     ['c', 261], ['cS', 277], ['d', 294], ['dS', 311],
                     ['e', 329], ['f', 349], ['fS', 370], ['g', 391],
                     ['gS', 415], ['a', 440], ['aS', 455], ['b', 466],
                     ['cH', 523], ['cHS', 554], ['dH', 587], ['dHS', 622],
                     ['eH', 659], ['fH', 698], ['fHS', 740], ['gH', 784],
                     ['gHS', 830], ['aH', 880], ['aHS', 910], ['bH', 933]]
        notes = dict(notePairs)
        beepDelay = round((1000000 / notes[note]))  #in miliseconds
        times = int(round((duration * 1000) / (beepDelay * 2)))
        beepDelay = round(beepDelay / 1000000, 6)
        if not self.quietMode:
            print("note:%d| times: %d| beepDelay[ms]:%f" %
                  (notes[note], times, beepDelay))
        for x in range(times):
            self.buzz.on()
            sleep(beepDelay)
            self.buzz.off()
            sleep(beepDelay)
        #some delay between notes
        self.buzz.off()
        sleep(0.020)

    def play_buzzer(self):
        print("beep!")
        self.buzz.toggle()
        duration = 1 / 12
        sleep(duration)
        print(duration)
        self.buzz.toggle()