def light_piglow(colour,rotations): colourmap = {14 : "red", 13 : "green", 11 : "blue", 1: "orange", 4 : "yellow", 15 : "white" } from piglow import PiGlow piglow = PiGlow() # piglow.all(0) if ( colour != "all" ): ledcolour = colourmap[colour] for j in range(rotations): piglow.colour(ledcolour,j) sleep(0.001*j) # As the intensity increases, sleep for longer periods else: # print ("Trying to run all ") for j in range(rotations): for colour in (colourmap.values()): piglow.colour(("%s" % colour), 255) sleep(0.2) piglow.colour(colour, 0) sleep(0.01) piglow.all(0)
sleep(1) piglow.red(0) sleep(1) j += 1 gameCont = False else: print "Congratulations! moving to next level..." print "Pattern was " for i in numberOrder: print i k = 0 while k < 4: piglow.all(10) sleep(1) piglow.all(0) sleep(1) k += 1 if gameCont == False: print "Thanks for playing!" piglow.all(0) except KeyboardInterrupt: print '^C received, shutting down the web server' server.socket.close() piglow.all(0)
def main(): piglow = PiGlow() piglow.all(0)
class PiGlow_Status_Server: def __init__(self): self.cfg = PiGlow_Status_Config() self.commands = PiGlow_Status_Commands() self.idle_job = self.commands.CLOCK self.jobs = [] self.running = None self.locked_thread = None self.check_jobs_thread = None self.socket_manager_thread = None self.piglow = None self.clock = None self.alert = None self.in_progress = None self.job_interval = 0.1 self.quiet_time = False def start(self): """Creates the socket and starts the threads""" try: self.piglow = PiGlow() except IOError as e: if e[0] == errno.EACCES: print >> sys.stderr, "Permission denied, try running as root" else: print >> sys.stderr, "Unknown error accessing the PiGlow" sys.exit(1) self.piglow.all(0) self.clock = Clock(self.piglow) self.alert = Alert(self.piglow) self.in_progress = In_Progress(self.piglow) address = (self.cfg.HOST, self.cfg.PORT) serversock = socket(AF_INET, SOCK_STREAM) serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) serversock.bind(address) serversock.listen(5) self.check_jobs_thread = Thread(None, self.check_jobs, None, ()) self.socket_manager_thread = Thread(None, self.socket_manager, None, (serversock, )) self.start_threads() while self.running == True: sleep(1) self.stop() def stop(self): """Closes the threads and returns""" self.stop_threads() self.piglow.all(0) def start_threads(self): """Starts the threads""" self.running = True self.check_jobs_thread.start() self.socket_manager_thread.start() def stop_threads(self): """Stops the threads""" self.running = False self.unlock() try: self.check_jobs_thread.join() except (KeyboardInterrupt, SystemExit): pass try: self.socket_manager_thread.join() except (KeyboardInterrupt, SystemExit): pass def check_jobs(self): """Performs the actions in the job list""" while self.running == True: if self.quit_requested(): self.running = False break if self.entering_quiet_time() == True: self.unlock() self.piglow.all(0) if self.in_quiet_time() == False: if self.locked_thread is None: # No currently locking jobs, we can process the next job in the list as # normal or run the idle task if none are scheduled self.run_jobs() else: # A locking job is currently running, screen the job list for tasks # relating to it. self.check_locked_jobs() sleep(self.job_interval) def quit_requested(self): """Returns true if the quit command is in the job list""" for job in self.jobs: if job[0] == self.commands.QUIT: return True return False def check_locked_jobs(self): """Goes through the job list searching for tasks relating to the current locked job""" jobs = self.jobs self.jobs = [] for job in jobs: if job[0] == self.commands.CYCLE: try: self.in_progress.set_speed(job[1]) except IndexError: pass elif job[0] == self.commands.UNLOCK: self.unlock() elif job[0] == self.commands.OFF: self.unlock() self.jobs.append(job) else: self.jobs.append(job) def run_jobs(self): """First the first job in the list or the current idle job""" if len(self.jobs) > 0: job = self.jobs[:1].pop() self.jobs = self.jobs[1:] self.handle_job(job) else: if self.idle_job is not None: self.run_idle_job() else: self.piglow.all(0) def run_idle_job(self): """Runs the current idle job""" if self.idle_job == self.commands.CLOCK: self.clock.run() def handle_job(self, job): """Performs the given job""" command = job[:1].pop() args = job[1:] if command == self.commands.QUIT: self.running = False elif command == self.commands.CYCLE: self.locked_thread = self.in_progress if len(args) > 0: self.in_progress.set_speed(args[0]) self.in_progress.start() elif command == self.commands.ALERT: self.alert.show(*args) elif command == self.commands.OFF: self.idle_job = None elif command == self.commands.CLOCK: self.idle_job = self.commands.CLOCK def unlock(self): """Stops the currently locking thread""" if self.locked_thread is None: return self.locked_thread.stop() self.locked_thread = None def socket_manager(self, serversock): """Creates handlers for new data given to this process via the socket""" rlist = [serversock] wlist = [] xlist = [] while self.running == True: readable, writable, errored = select.select( rlist, wlist, xlist, self.cfg.TIMEOUT) for s in readable: if s is serversock: clientsock, addr = serversock.accept() self.socket_buffer_handler(clientsock) def socket_buffer_handler(self, clientsock): """Handles data in the socket buffer""" data = clientsock.recv(self.cfg.BUFF).rstrip() command = data.split(" ") if command == self.commands.CLOSE: clientsock.close() else: self.add_job(command) def add_job(self, job): """Adds a job to the list""" if self.cfg.quiet_time() == False or job == self.command.QUIT: self.jobs.append(job) def entering_quiet_time(self): """Returns true if we're entering quiet time""" return self.quiet_time == False and self.cfg.quiet_time() == True def in_quiet_time(self): """Returns true if we're in quiet time""" self.quiet_time = self.cfg.quiet_time() return self.quiet_time
#!/usr/bin/env python from piglow import PiGlow piglow = PiGlow() piglow.all(255)
## Author: Daniel Pullan ## Github: GitHub.com/DanielPullan ## Website: DanielPullan.co.uk from time import sleep from config import email, password, mail from piglow import PiGlow ## Init the current device piglow = PiGlow() ## Device parameters piglow.all(0) if mail == 0: print "no mail" piglow.all(0) sleep(0.5) elif mail < 5: print "mail count low" piglow.arm1(10) sleep(0.5) elif mail < 10: print "mail count medium" piglow.arm1(10) piglow.arm2(10) sleep(0.5) elif mail < 15: print "mail count high" piglow.arm1(10) piglow.arm2(10)
###################################################### ## Set each colour to a brightness of your choosing ## ## ## ## Example by Jason - @Boeeerb ## ###################################################### from piglow import PiGlow piglow = PiGlow() val = input("White: ") piglow.white(val) val = input("Blue: ") piglow.blue(val) val = input("Green: ") piglow.green(val) val = input("Yellow: ") piglow.yellow(val) val = input("Orange: ") piglow.orange(val) val = input("Red: ") piglow.red(val) val = input("All: ") piglow.all(val)
time.sleep(0.1) piglow.arm3(0) #Test Completed i = i+1 while True: response = urllib2.urlopen('http://ryanteck.org.uk/nginx_status') html = response.read() data = html.split('\n') active = data[0].split(":") count = active[1] count = int(count) -1; #print(count) if(count ==0): piglow.all(0) #top leg if(count >=1): piglow.led(6,127) if(count >=2): piglow.led(5,127) if(count >=3): piglow.led(4,127) if(count >=4): piglow.led(3,127) if(count >=5): piglow.led(2,127) if(count >=6): piglow.led(1,127) #bottom leg if(count >=7):
class PiGlow_Status_Server: def __init__ (self): self.cfg = PiGlow_Status_Config () self.commands = PiGlow_Status_Commands () self.idle_job = self.commands.CLOCK self.jobs = [] self.running = None self.locked_thread = None self.check_jobs_thread = None self.socket_manager_thread = None self.piglow = None self.clock = None self.alert = None self.in_progress = None self.job_interval = 0.1 self.quiet_time = False def start (self): """Creates the socket and starts the threads""" try: self.piglow = PiGlow () except IOError as e: if e[0] == errno.EACCES: print >> sys.stderr, "Permission denied, try running as root" else: print >> sys.stderr, "Unknown error accessing the PiGlow" sys.exit (1) self.piglow.all (0) self.clock = Clock (self.piglow) self.alert = Alert (self.piglow) self.in_progress = In_Progress (self.piglow) address = (self.cfg.HOST, self.cfg.PORT) serversock = socket (AF_INET, SOCK_STREAM) serversock.setsockopt (SOL_SOCKET, SO_REUSEADDR, 1) serversock.bind (address) serversock.listen (5) self.check_jobs_thread = Thread (None, self.check_jobs, None, ()) self.socket_manager_thread = Thread (None, self.socket_manager, None, (serversock, )) self.start_threads () while self.running == True: sleep (1) self.stop () def stop (self): """Closes the threads and returns""" self.stop_threads () self.piglow.all (0) def start_threads (self): """Starts the threads""" self.running = True self.check_jobs_thread.start () self.socket_manager_thread.start () def stop_threads (self): """Stops the threads""" self.running = False self.unlock () try: self.check_jobs_thread.join () except (KeyboardInterrupt, SystemExit): pass try: self.socket_manager_thread.join () except (KeyboardInterrupt, SystemExit): pass def check_jobs (self): """Performs the actions in the job list""" while self.running == True: if self.quit_requested (): self.running = False break if self.entering_quiet_time () == True: self.unlock () self.piglow.all (0) if self.in_quiet_time () == False: if self.locked_thread is None: # No currently locking jobs, we can process the next job in the list as # normal or run the idle task if none are scheduled self.run_jobs () else: # A locking job is currently running, screen the job list for tasks # relating to it. self.check_locked_jobs () sleep (self.job_interval) def quit_requested (self): """Returns true if the quit command is in the job list""" for job in self.jobs: if job[0] == self.commands.QUIT: return True return False def check_locked_jobs (self): """Goes through the job list searching for tasks relating to the current locked job""" jobs = self.jobs self.jobs = [] for job in jobs: if job[0] == self.commands.CYCLE: try: self.in_progress.set_speed (job[1]) except IndexError: pass elif job[0] == self.commands.UNLOCK: self.unlock () elif job[0] == self.commands.OFF: self.unlock () self.jobs.append (job) else: self.jobs.append (job) def run_jobs (self): """First the first job in the list or the current idle job""" if len (self.jobs) > 0: job = self.jobs[:1].pop () self.jobs = self.jobs[1:] self.handle_job (job) else: if self.idle_job is not None: self.run_idle_job () else: self.piglow.all (0) def run_idle_job (self): """Runs the current idle job""" if self.idle_job == self.commands.CLOCK: self.clock.run () def handle_job (self, job): """Performs the given job""" command = job[:1].pop () args = job[1:] if command == self.commands.QUIT: self.running = False elif command == self.commands.CYCLE: self.locked_thread = self.in_progress if len (args) > 0: self.in_progress.set_speed (args[0]) self.in_progress.start () elif command == self.commands.ALERT: self.alert.show (*args) elif command == self.commands.OFF: self.idle_job = None elif command == self.commands.CLOCK: self.idle_job = self.commands.CLOCK def unlock (self): """Stops the currently locking thread""" if self.locked_thread is None: return self.locked_thread.stop () self.locked_thread = None def socket_manager (self, serversock): """Creates handlers for new data given to this process via the socket""" rlist = [serversock] wlist = [] xlist = [] while self.running == True: readable, writable, errored = select.select (rlist, wlist, xlist, self.cfg.TIMEOUT) for s in readable: if s is serversock: clientsock, addr = serversock.accept () self.socket_buffer_handler (clientsock) def socket_buffer_handler (self, clientsock): """Handles data in the socket buffer""" data = clientsock.recv (self.cfg.BUFF).rstrip () command = data.split (" ") if command == self.commands.CLOSE: clientsock.close () else: self.add_job (command) def add_job (self, job): """Adds a job to the list""" if self.cfg.quiet_time () == False or job == self.command.QUIT: self.jobs.append (job) def entering_quiet_time (self): """Returns true if we're entering quiet time""" return self.quiet_time == False and self.cfg.quiet_time () == True def in_quiet_time (self): """Returns true if we're in quiet time""" self.quiet_time = self.cfg.quiet_time () return self.quiet_time
###################################################### ## Set each colour to a brightness of your choosing ## ## ## ## Example by Jason - @Boeeerb ## ###################################################### from piglow import PiGlow piglow = PiGlow() val = eval(input("White: ")) piglow.white(val) val = eval(input("Blue: ")) piglow.blue(val) val = eval(input("Green: ")) piglow.green(val) val = eval(input("Yellow: ")) piglow.yellow(val) val = eval(input("Orange: ")) piglow.orange(val) val = eval(input("Red: ")) piglow.red(val) val = eval(input("All: ")) piglow.all(val)
from piglow import PiGlow from time import sleep piglow = PiGlow() while True ###All LEDs### #Fade all LEDs on at the same time piglow.all(51) sleep(0.1) piglow.all(102) sleep(0.1) piglow.all(153) sleep(0.1) piglow.all(204) sleep(0.1) piglow.all(255) sleep(0.5) #Fade all LEDs off at the same time piglow.all(255) sleep(0.1) piglow.all(204) sleep(0.1) piglow.all(153) sleep(0.1) piglow.all(102) sleep(0.1) piglow.all(51) sleep(0.1) piglow.all(0)
# Used to define led and brightness maxbrite = 100 # The maximum brightness to use, absolute max is 255 y = 1 # Initialize y for main loop m = 0 # Initialize key for led list selection loop # LED selection list is below # Odd then even leds #l=[1,3,5,7,9,11,13,14,17,2,4,6,8,10,12,14,16,18] # An alternate writing pattern that starts at center and moves # out, around the center counter clockwise. # Uncomment only one at a time l=[12,6,18,11,5,17,10,4,16,9,3,15,8,2,14,7,1,13 piglow.all(0) # Turn off all led, just a housekeeping line while y > 0: # Begin the pulse loop while x < maxbrite : # Start the brighten loop. The max is 255 for brightness of led for m in range(18): # Loop to set the leds to current brightness piglow.led(l[m],x) # Set the led l[m]=led, n=brightness m = m + 1 # Move to next in list l time.sleep(q) # Delay the loop by q x = x + 1 # Add one to x and loop while x > 0 : # Start the dimming loop
################################################## ## Test the brightness of all the LEDs together ## ## ## ## Example by Jason - @Boeeerb ## ################################################## from piglow import PiGlow from time import sleep piglow = PiGlow() while True: count = range(0, 256, +1) for item in count: if item < 256: piglow.all(item) sleep(0.01) if item == 256: break count = range(255, 0, -1) print "Brightest" for item in count: if item > 0: piglow.all(item) sleep(0.01) if item == 0: piglow.all(0) break print "Fin" break
class Luces: def __init__(self): self.piglow = PiGlow() def get_intensity(self, value): pass def encenderblanco(self, value=100): self.piglow.white(value) def apagarblanco(self): self.piglow.white(0) def parpadearblanco(self, tiempo=1, value=100): self.encenderblanco(value) time.sleep(tiempo) self.apagarblanco() def encenderazul(self, value=100): self.piglow.blue(value) def apagarazul(self): self.piglow.blue(0) def parpadearazul(self, tiempo=1, value=100): self.encenderazul(value) time.sleep(tiempo) self.apagarazul() def encenderverde(self, value=100): self.piglow.green(value) def apagarverde(self): self.piglow.green(0) def parpadearverde(self, tiempo=1, value=100): self.encenderverde(value) time.sleep(tiempo) self.apagarverde() def encenderamarillo(self, value=100): self.piglow.yellow(value) def apagaramarillo(self): self.piglow.yellow(0) def parpadearamarillo(self, tiempo=1, value=100): self.encenderamarillo(value) time.sleep(tiempo) self.apagaramarillo() def encendernaranja(self, value=100): self.piglow.orange(value) def apagarnaranja(self): self.piglow.orange(0) def parpadearnaranja(self, tiempo=1, value=100): self.encendernaranja(value) time.sleep(tiempo) self.apagarnarnanja() def encenderrojo(self, value=100): self.piglow.red(value) def apagarrojo(self): self.piglow.red(0) def parpadearrojo(self, tiempo=1, value=100): self.encenderrojo(value) time.sleep(tiempo) self.apagarrojo() def encendertodas(self, value=100): self.piglow.all(value) def apagartodas(self): self.piglow.all(0) def parpadeartodas(self, tiempo=1, value=100): self.encendertodas(value) time.sleep(tiempo) self.apagartodas() def encenderbrazo(self, arm, value=100): self.piglow.arm(arm, value) def apagarbrazo(self, arm): self.piglow.arm(arm, 0) def parpadearbrazo(self, arm, tiempo=1, value=100): self.encenderbrazo(arm, value) time.sleep(tiempo) self.apagarbrazo(arm) def encenderbrazo1(self, value=100): self.piglow.arm1(value) def apagarbrazo1(self): self.piglow.arm1(0) def parpadearbrazo1(self, tiempo=1, value=100): self.encenderbrazo1(value) time.sleep(tiempo) self.apagarbrazo1() def encenderbrazo2(self, value=100): self.piglow.arm2(value) def apagarbrazo2(self): self.piglow.arm2(0) def parpadearbrazo2(self, tiempo=1, value=100): self.encenderbrazo2(value) time.sleep(tiempo) self.apagarbrazo2() def encenderbrazo3(self, value=100): self.piglow.arm3(value) def apagarbrazo3(self): self.piglow.arm3(0) def parpadearbrazo3(self, tiempo=1, value=100): self.encenderbrazo3(value) time.sleep(tiempo) self.apagarbrazo3() def encendercolor(self, colour, value=10): if colour == 1 or colour == "blanco": self.piglow.colour("white", value) elif colour == 2 or colour == "azul": self.piglow.colour("blue", value) elif colour == 3 or colour == "verde": self.piglow.colour("green", value) elif colour == 4 or colour == "amarillo": self.piglow.colour("yellow", value) elif colour == 5 or colour == "naranja": self.piglow.colour("orange", value) elif colour == 6 or colour == "rojo": self.piglow.colour("red", value) else: print "Solo los colores de 1 - 6 o los nombres de colores estan permitidos" def apagarcolor(self, colour): value = 0 if colour == 1 or colour == "blanco": self.piglow.colour("white", value) elif colour == 2 or colour == "azul": self.piglow.colour("blue", value) elif colour == 3 or colour == "verde": self.piglow.colour("green", value) elif colour == 4 or colour == "amarillo": self.piglow.colour("yellow", value) elif colour == 5 or colour == "naranja": self.piglow.colour("orange", value) elif colour == 6 or colour == "rojo": self.piglow.colour("red", value) else: print "Solo los colores de 1 - 6 o los nombres de colores estan permitidos" def parpadearcolor(self, colour, tiempo=1, value=100): self.encendercolor(colour, value) time.sleep(tiempo) self.apagarcolor(colour) def encenderluz(self, led, value=100): self.piglow.led(led, value) def apagarluz(self, led): self.piglow.led(led, 0) def parpadearluz(self, led, tiempo=1, value=100): self.encenderluz(led, value) time.sleep(tiempo) self.apagarluz(led)
############################################################### # Set the LEDs to turn on/off in pairs of 2 toward the centre, # whilst increasing and decreasing in brightness # shifty051 ############################################################### from piglow import PiGlow from time import sleep piglow = PiGlow() piglow.all(0) while True: i=0 piglow.red(1) sleep(0.1) piglow.red(2) sleep(0.1) piglow.red(3) piglow.orange(1) sleep(0.1) piglow.red(4) piglow.orange(2) sleep(0.1) piglow.red(3)
piglow = PiGlow() ### You can customise these settings ### show12hr = 1 # Show 12 or 24hr clock - 0= 24hr, 1= 12hr ledbrightness = 10 # Set brightness of LED - 1-255 (recommend 10-20, put 0 and you won't see it!) hourflash = 1 # Choose how to flash change of hour - 1= white leds, 2= all flash armtop = "s" # h= hour, m= minutes, s= seconds armright = "m" armbottom = "h" ### End of customising ### piglow.all(0) hourcount = 0 hourcurrent = 0 while True: time = datetime.now().time() hour, min, sec = str(time).split(":") sec, micro = str(sec).split(".") hour = int(hour) if show12hr == 1: if hour > 12: hour = hour - 12 min = int(min)
###################################################### # Import needed modules import time from piglow import PiGlow # An alias, so you can type piglow rather than PiGlow() piglow = PiGlow() q = 0.0003 # Delay for time.sleep in seconds x = 1 # Iniialize x, 0 causes .led to turn them off # Used to define led and brightness y = 1 # Initialize y for main loop piglow.all(0) # turn off all led, while y > 0: # Begin the pulse loop for x in range(255): # Start the brighten loop m = (x % 19) # Make sure to only have 1-18 for led # by dividing by 19 and using the remainder if m == 0: # LED can't be zero so if it is, set to 1 m= m + 1 n = (x % 255) # Make sure the brightness value does not exceed 255 # by dividing by 255 and using the remainder piglow.led(m,n) # Set the led m=led, n=brightness
piglow = PiGlow() ### You can customise these settings ### show12hr = 1 # Show 12 or 24hr clock - 0= 24hr, 1= 12hr ledbrightness = 10 # Set brightness of LED - 1-255 (recommend 10-20, put 0 and you won't see it!) hourflash = 1 # Choose how to flash change of hour - 1= white leds, 2= all flash armtop = "s" # h= hour, m= minutes, s= seconds armright = "m" armbottom = "h" ### End of customising ### piglow.all(0) hourcount = 0 hourcurrent = 0 while True: time = datetime.now().time() hour,min,sec = str(time).split(":") # Bug fix by Phil Moyer - Currently under test try: rv = str(sec).index(".") sec,micro = str(sec).split(".") except ValueError: sec = str(sec) micro = "0"
class GracefulKiller: kill_now = False def __init__(self): signal.signal(signal.SIGINT, self.exit_gracefully) signal.signal(signal.SIGTERM, self.exit_gracefully) def exit_gracefully(self,signum, frame): self.kill_now = True if __name__ == '__main__': killer = GracefulKiller() piglow = PiGlow() while True: cpu = psutil.cpu_percent() piglow.all(0) if cpu < 5: piglow.red(20) elif cpu < 20: piglow.red(20) piglow.orange(20) elif cpu < 40: piglow.red(20) piglow.orange(20) piglow.yellow(20) elif cpu < 60: piglow.red(20) piglow.orange(20) piglow.yellow(20) piglow.green(20)
#!/usr/bin/python from time import sleep from piglow import PiGlow piglow = PiGlow() piglow.all(0) # the police for i in range(1, 20): piglow.all(0) piglow.blue(100) sleep(.2) piglow.all(0) piglow.red(100) sleep(.2) piglow.all(0) piglow.all(200) piglow.all(0)