Advertisement
Killtastic

randomglow.py

Jun 29th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. ## randomglow.py (by Cyrox @ngb.to)
  2. ## Last edited: 29/06/2014
  3. ## -----------------------------
  4. ## Lights up random LEDs of the PiGlow (http://shop.pimoroni.com/products/piglow)
  5. ## Needs "PyGlow" python module (https://github.com/benleb/PyGlow/blob/master/pyglow.py)
  6. ##
  7. ##
  8. ## Usage:
  9. ##
  10. ## RandomGlow.py [[<MinimumBrightness>] [<MaximumBrightness>] [<UpdateRate>] [<ResetLEDs>]]
  11. ##
  12. ## <MinimumBrightness> - Value between 0 and 255 (default: 1)
  13. ## <MaximumBrightness> - Value between 1 and 255, can't be less than <MinimumBrightness> (default: 150)
  14. ## <UpdateRate> - Defines how fast the LEDs are switched in seconds (default: 0.05)
  15. ## <ResetLEDs> - True or False, if true, the LED will go off after each turn (default: True)
  16.  
  17. from pyglow import PyGlow
  18. from time import sleep
  19. import random
  20. import sys
  21.  
  22. ### DEFAULT SETTINGS ###
  23.  
  24. rndMinBnDef = 1
  25. rndMaxBnDef = 155
  26. updateRateDef = 0.05
  27. resetLEDsDef = True
  28.  
  29. ########################
  30.  
  31. rndMinBn = rndMinBnDef
  32. rndMaxBn = rndMaxBnDef
  33. updateRate = updateRateDef
  34. resetLEDs = resetLEDsDef
  35.  
  36. if len(sys.argv) >= 2 and 0 <= int(sys.argv[1]) <= 255:
  37.         rndMinBn = int(sys.argv[1])
  38.  
  39. if len(sys.argv) >= 3 and 1 <= int(sys.argv[2]) <= 255:
  40.         rndMaxBn = int(sys.argv[2])
  41.  
  42. if rndMinBn > rndMaxBn:
  43.         rndMinBn = rndMinBnDef
  44.         rndMaxBn = rndMaxBnDef
  45.  
  46. if len(sys.argv) >= 4:
  47.         try:
  48.                 updateRate = float(sys.argv[3])
  49.         except ValueError:
  50.                 pass
  51.  
  52. if len(sys.argv) >= 5:
  53.         resetLEDs = (sys.argv[4].lower() != 'false')
  54.        
  55. print "All arguments:", str(sys.argv)
  56. print "Minimum Brightness:", rndMinBn
  57. print "Maxmimum Brightness:", rndMaxBn
  58. print "Update rate (in secs):", updateRate
  59. print "Reset LEDs:", resetLEDs
  60. print "[PRESS CTRL+C TO EXIT]"
  61.  
  62. pyglow = PyGlow()
  63.  
  64. try:
  65.         while True:
  66.                 rndLed = random.randint(1, 18)
  67.                 rndBn = random.randint(rndMinBn, rndMaxBn)
  68.                 pyglow.led(rndLed, rndBn)
  69.                 sleep(updateRate)
  70.                 if resetLEDs:
  71.                          pyglow.all(0)
  72.  
  73. except KeyboardInterrupt:
  74.         pyglow.all(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement