Exemplo n.º 1
0
 def changed(self, handler, sensitivity=None):
     self._handler = handler
     if sensitivity != None:
         self._sensitivity = sensitivity
     if self._t_watch == None:
         self._t_watch = AsyncWorker(self._watch)
         self._t_watch.start()
Exemplo n.º 2
0
    def fade(self, start, end, duration):
        """Fades an LED to a specific brightness over a specific time in seconds

        @param self Object pointer.
        @param start Starting brightness %
        @param end Ending brightness %
        @param duration Time duration ( in seconds ) of the fade"""
        self.stop()
        time_start = time.time()
        self.pwm(PULSE_FREQUENCY, start)

        def _fade():
            self.fading = True

            if time.time() - time_start >= duration:
                self.duty_cycle(end)
                self.fading = False
                return False

            current = (time.time() - time_start) / duration
            brightness = start + (float(end - start) * current)
            self.duty_cycle(round(brightness))
            time.sleep(1.0 / PULSE_FPS)

        self.fader = AsyncWorker(_fade)
        self.fader.start()
        return True
Exemplo n.º 3
0
def startup(
        cycle_interruptor=(lambda ticks, cycles: None), rate=0.1,
        fade_out=0.2):

    lights = [exp.light.blue, exp.light.yellow, exp.light.red, exp.light.green]

    passes = list(islice(cycle([lights, lights[::-1]]), None, 2))

    serie = [
        item for sublist in [passes[0]] + map(lambda p: p[1:], passes[1:])
        for item in sublist
    ]

    ticks = 0
    cycles = 0

    def _cycler():
        global ticks
        global cycles

        cycles += 1
        for s in serie:
            ticks += 1
            s.fade(100, 0, fade_out)
            sleep(rate)
            if cycle_interruptor(ticks, cycle):
                exp.light.off()
                return False

    exp.light.off()

    worker = AsyncWorker(_cycler)
    worker.start()
    return True
Exemplo n.º 4
0
 def set_timeout(self,function,seconds):
     def fn_timeout():
         time.sleep(seconds)
         function()
         return False
     timeout = AsyncWorker(fn_timeout)
     timeout.start()
     return True
Exemplo n.º 5
0
    def melody(self,notes,duration = 0.5,loop = True):
        self.stop()
        time_start = time.time()
        is_notation = False

        if notes[0] == 'N':
            is_notation = True
            notes.pop(0)

        if duration <= 0.0001:
            raise ValueError('Duration must be greater than 0.0001')

        if len(notes) == 0:
            raise ValueError('You must provide at least one note')

        # Get the total length of the tune
        # so we can play it!
        total = len(notes) * duration

        def melody():

            now = time.time() - time_start

            # Play only once if loop is false
            if loop == False and int(now / total) > 0:
                self._stop_buzzer()
                return False

            # Figure out how far we are into the current iteration
            # Then divide by duration to find the current note index
            delta = round( (now % total) / duration )

            # Select the note from the notes array
            note = notes[int(delta)-1]


            if is_notation:
                # this note and above would be OVER NINE THOUSAND Hz!
                # Treat it as an explicit pitch instead
                if note == 0:
                    self._stop_buzzer()
                else:
                    pibrella.buzzer.buzz(note)
            else:
                if note == '-':
                    self._stop_buzzer()
                else:
                    # Play the note
                    pibrella.buzzer.note(note)

            # Sleep a bit
            time.sleep(0.0001)

        self._melody = AsyncWorker(melody)
        self.fps = 100
        self._melody.start()
Exemplo n.º 6
0
    def fade(self,start,end,duration):
        self.stop()
        time_start = time.time()
        self.pwm(PULSE_FREQUENCY,start)
        def _fade():
            if time.time() - time_start >= duration:
                self.duty_cycle(end)
                return False

            current = (time.time() - time_start) / duration
            brightness = start + (float(end-start) * current)
            self.duty_cycle(round(brightness))
            time.sleep(0.1)

        self.fader = AsyncWorker(_fade)
        self.fader.start()
        return True
Exemplo n.º 7
0
 def async_start(self, name, function):
     self.workers[name] = AsyncWorker(function)
     self.workers[name].start()
     return True
Exemplo n.º 8
0
def async_start(name, function):
    global workers
    workers[name] = AsyncWorker(function)
    workers[name].start()
    return True