def func(lock, cur_running, my_priority): print "Spawning rainbow..." colors = [(15, 0, 0), \ (15, 3, 0), \ (15, 15, 0), \ (0, 15, 0), \ (0, 0, 15), \ (4, 14, 13), \ (14, 0, 14) ] strand = rgb_strand.RGBStrand(NUM_BULBS) strand.set_strand_pattern(colors) strand.set_strand_brightness(200) i = 0 currently_running = False while True: # rainbow doesn't get any time off, as it's the filler background # (it should also thus be the lowest priority (1)) acquire_lock_priority(lock, cur_running, my_priority, 30, 0) strand.push_top(200, colors[i][0], colors[i][1], colors[i][2]) i += 1 i %= len(colors) # Let other people use it if they want it lock.release()
def write_led(self, led_id, brightness, red, green, blue): # every once in a while, we should release the lock if time.time() > self.next_acquire_time: #if self.have_lock: # self.lock.release() acquire_lock_priority(self.lock, self.cur_running, self.my_priority, self.run_time, self.off_time) self.lock.release() # can't hold lock in case user calls sleep #self.have_lock = True # just true after the first time self.next_acquire_time = time.time() + 0.01 self.driver.write_led(led_id, brightness, blue, green, red)
def busy_wait(self, duration=None): if duration is None: duration = 100 while duration > 0: try: #self.write_led(100, 0, 0, 0, 0) acquire_lock_priority(self.lock, self.cur_running, self.my_priority, self.run_time, self.off_time) self.lock.release() time.sleep(min(duration, 0.5)) duration -= 0.5 except: return