def main(): """Turn all the lights on for 2 minutes""" # initialize your hardware for use hc.initialize() # turn on all the lights hc.turn_on_lights() # run for 2 minutes end = time.time() + 120 # working loop will run as long as time.time() is less then "end" while time.time() < end: # try except block to catch keyboardinterrupt by user to stop try: # do nothing, just wait pass # if the user pressed <CTRL> + C to exit early break out of the loop except KeyboardInterrupt: print "\nstopped" break # This ends and cleans up everything hc.clean_up()
def lights_on(): global lights_are_on, playlist_playing initialize_interface() if playlist_playing: stop_playlist() turn_on_lights() lights_are_on = True
def main(): """ Play a message Play a recorded message for the people and go through the lights one channel at a time in order, then back down to the first """ # initialize your hardware for use hc.initialize() # start with all the lights off hc.turn_off_lights() # Before we start the lights we should start playing the audio # we have installed mpg123 to make this easy # if you do not have mpg123 installed then use this command to install it # sudo apt-get install mpg123 # now all you have to do is use the below command to play an mp3 file message_file = "/home/pi/lightshowpi/py/examples/message.mp3" message = subprocess.Popen(["mpg123", "-q", message_file]) # subprocess.Popen will open mpg123 player and play an audio file for you # and give you a few options that will come in real handy # you can stop mpg123 before the audio has finished using the instance # variable we just created by calling message.kill() # or at any point in the script you can make everything wait for the audio # to finish playing with message.wait() that could be usefull if you # ran a short seuqence like in the default preshow and your audio as longer # then your sequence and you wanted the audio to finish before continuing # and if you use message.poll() or message.returncode you could find out # if it has finished, then you might start something else or end everything # and shutdown your pi. # working loop while True: # try except block to catch keyboardinterrupt by user to stop try: hc.turn_on_lights() except KeyboardInterrupt: print "\nstopped" break # if audio playback has finished break out of the loop if message.poll() != None: break # This ends and cleans up everything hc.clean_up()
def execute_preshow(config): '''Execute the "Preshow" for the given preshow configuration''' for transition in config['transitions']: start = time.time() if transition['type'].lower() == 'on': hc.turn_on_lights(True) else: hc.turn_off_lights(True) logging.debug('Transition to ' + transition['type'] + ' for ' + str(transition['duration']) + ' seconds') while transition['duration'] > (time.time() - start): cm.load_state() # Force a refresh of state from file play_now = int(cm.get_state('play_now', 0)) if play_now: return # Skip out on the rest of the preshow # Check once every ~ .1 seconds to break out time.sleep(0.1)
def main(): """ Test pattern2 Unlights one channel at a time in order """ # this is a list of all the channels you have access to lights = hc._GPIO_PINS # initialize your hardware for use hc.initialize() # start with all the lights off hc.turn_off_lights() # pause for 1 second time.sleep(2) # working loop for _ in range(50): # try except block to catch keyboardinterrupt by user to stop try: # here we just loop over the gpio pins and do something with them for light in lights: # turn on all the lights hc.turn_on_lights() # then turn off one hc.turn_off_light(light) # wait a little bit before the for loop # starts again and turns off the next light time.sleep(.4) # if the user pressed <CTRL> + C to exit early break out of the loop except KeyboardInterrupt: print "\nstopped" break # This ends and cleans up everything hc.clean_up()
def main(): """ Random flashing lights """ # this is a list of all the channels you have access to # I'm also tracking the time here so that I know when I turned a light off # So I'm putting everything in a dict gpio_pins = hc._GPIO_PINS lights = dict.fromkeys(range(0, len(gpio_pins)), [True, time.time()]) # get a number that is about 40% the length of your gpio's # this will be use to make sure that no more then 40% of # the light will be off at any one time max_off = int(round(len(lights) * .4)) # initialize your hardware for use hc.initialize() print "Press <CTRL>-C to stop" # start with all the lights on hc.turn_on_lights() # lets run for 2 minutes end = time.time() + 120 # working loop will run as long as time.time() is less then "end" while time.time() < end: # try except block to catch keyboardinterrupt by user to stop try: # here we just loop over the gpio pins for light in lights: # this is where we check to see if we have any light # that are turned off # if they are off we will check the time to see if we # want to turn them back on yet, if we do then turn it on if not lights[light][0]: if lights[light][1] < time.time(): lights[light][0] = True hc.turn_on_light(light) # count the number of lights that are off off = [k for (k, v) in lights.iteritems() if v.count(1) == False] # if less then out max count of light that we chose # we can turn one off if len(off) < max_off: # pick a light at random to turn off choice = random.randrange(0, len(gpio_pins)) # if it's on then lets turn it off if lights[choice][0]: # pick a duration for that light to be off # default times are between 1/2 and secong and 1.8 seconds duration = random.uniform(0.5, 1.8) # store this informatin in our dict lights[choice] = [False, time.time() + duration] # and turn that light off then continue with the main loop # and do it all over again hc.turn_off_light(choice) # if the user pressed <CTRL> + C to exit early break out of the loop except KeyboardInterrupt: print "\nstopped" break # This ends and cleans up everything hc.clean_up()
def execute(self): """ Execute the pre/post show as defined by the current config Returns the exit status of the show, either done if the show played to completion, or play_now_interrupt if the show was interrupted by a play now command. """ # Is there a show to launch? if self.config == None: return PrePostShow.done # Is the config a script or a transition based show # launch the script if it is if not isinstance(self.config, dict) and os.path.exists(self.config): logging.debug("Launching external script " + self.config + " as " \ + self.show) return self.start_script() # start the audio if there is any self.start_audio() if 'transitions' in self.config: try: # display transition based show for transition in self.config['transitions']: start = time.time() if transition['type'].lower() == 'on': hc.turn_on_lights(True) else: hc.turn_off_lights(True) logging.debug('Transition to ' + transition['type'] + ' for ' \ + str(transition['duration']) + ' seconds') if 'channel_control' in transition: channel_control = transition['channel_control'] for key in channel_control.keys(): mode = key channels = channel_control[key] for channel in channels: if mode == 'on': hc.turn_on_light(int(channel) - 1, 1) elif mode == 'off': hc.turn_off_light(int(channel) - 1, 1) else: logging.error("Unrecognized channel_control mode " "defined in preshow_configuration " \ + str(mode)) # hold transition for specified time while transition['duration'] > (time.time() - start): # check for play now if check_state(): # kill the audio playback if playing if self.audio: os.killpg(self.audio.pid, signal.SIGTERM) self.audio = None return PrePostShow.play_now_interrupt time.sleep(0.1) except: pass # hold show until audio has finished if we have audio # or audio is not finished return_value = self.hold_for_audio() return return_value
def execute(self): """ Execute the pre/post show as defined by the current config Returns the exit status of the show, either done if the show played to completion, or play_now_interrupt if the show was interrupted by a play now command. """ # Is there a show to launch? if self.config == None: return PrePostShow.done # Is the config a script or a transition based show # launch the script if it is if not isinstance(self.config, dict) and os.path.exists(self.config): logging.debug("Launching external script " + self.config + " as " + self.show) return self.start_script() # start the audio if there is any self.start_audio() if "transitions" in self.config: try: # display transition based show for transition in self.config["transitions"]: start = time.time() if transition["type"].lower() == "on": hc.turn_on_lights(True) else: hc.turn_off_lights(True) logging.debug( "Transition to " + transition["type"] + " for " + str(transition["duration"]) + " seconds" ) if "channel_control" in transition: channel_control = transition["channel_control"] for key in channel_control.keys(): mode = key channels = channel_control[key] for channel in channels: if mode == "on": hc.turn_on_light(int(channel) - 1, 1) elif mode == "off": hc.turn_off_light(int(channel) - 1, 1) else: logging.error( "Unrecognized channel_control mode " "defined in preshow_configuration " + str(mode) ) # hold transition for specified time while transition["duration"] > (time.time() - start): # check for play now if check_state(): # kill the audio playback if playing if self.audio: os.killpg(self.audio.pid, signal.SIGTERM) self.audio = None return PrePostShow.play_now_interrupt time.sleep(0.1) except: pass # hold show until audio has finished if we have audio # or audio is not finished return_value = self.hold_for_audio() return return_value