Advertisement
Guest User

Fading2.py

a guest
Oct 4th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. #
  5.  # -----------------------------------------------------
  6.  # File        fading.py
  7.  # Authors     David <popoklopsi> Ordnung
  8.  # License     GPLv3
  9.  # Web         http://popoklopsi.de
  10.  # -----------------------------------------------------
  11.  #
  12.  # Copyright (C) 2014-2015 David <popoklopsi> Ordnung
  13.  #
  14.  # This program is free software: you can redistribute it and/or modify
  15.  # it under the terms of the GNU General Public License as published by
  16.  # the Free Software Foundation, either version 3 of the License, or
  17.  # any later version.
  18.  #  
  19.  # This program is distributed in the hope that it will be useful,
  20.  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.  # GNU General Public License for more details.
  23.  #
  24.  # You should have received a copy of the GNU General Public License
  25.  # along with this program. If not, see <http://www.gnu.org/licenses/>
  26. #
  27.  
  28.  
  29. # This script needs running pigpio (http://abyz.co.uk/rpi/pigpio/)
  30.  
  31.  
  32. ###### CONFIGURE THIS ######
  33.  
  34. # The Pins. Use Broadcom numbers.
  35. RED_PIN   = 17
  36. GREEN_PIN = 22
  37. BLUE_PIN  = 24
  38.  
  39. # Number of color changes per step (more is faster, less is slower).
  40. # You also can use 0.X floats.
  41. STEPS     = 0.01
  42.  
  43. ###### END ######
  44.  
  45.  
  46.  
  47.  
  48. import os
  49. import sys
  50. import termios
  51. import tty
  52. import pigpio
  53. import time
  54. from thread import start_new_thread
  55.  
  56. bright = 255
  57. r = 255.0
  58. g = 0.00
  59. b = 0.00
  60.  
  61. brightChanged = False
  62. abort = False
  63. state = True
  64.  
  65. pi = pigpio.pi()
  66.  
  67. def updateColor(color, step):
  68.     color += step
  69.    
  70.     if color > 255:
  71.         return 255
  72.     if color < 0:
  73.         return 0
  74.        
  75.     return color
  76.  
  77.  
  78. def setLights(pin, brightness):
  79.     realBrightness = int(int(brightness) * (float(bright) / 255.0))
  80.     pi.set_PWM_dutycycle(pin, realBrightness)
  81.  
  82.  
  83. def getCh():
  84.     fd = sys.stdin.fileno()
  85.     old_settings = termios.tcgetattr(fd)
  86.    
  87.     try:
  88.         tty.setraw(fd)
  89.         ch = sys.stdin.read(1)
  90.     finally:
  91.         termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  92.        
  93.     return ch
  94.  
  95.  
  96. def checkKey():
  97.     global bright
  98.     global brightChanged
  99.     global state
  100.     global abort
  101.    
  102.     while True:
  103.         c = getCh()
  104.        
  105.         if c == '+' and bright < 255 and not brightChanged:
  106.             brightChanged = True
  107.             time.sleep(0.01)
  108.             brightChanged = False
  109.            
  110.             bright = bright + 1
  111.             print ("Current brightness: %d" % bright)
  112.            
  113.         if c == '-' and bright > 0 and not brightChanged:
  114.             brightChanged = True
  115.             time.sleep(0.01)
  116.             brightChanged = False
  117.            
  118.             bright = bright - 1
  119.             print ("Current brightness: %d" % bright)
  120.            
  121.         if c == 'p' and state:
  122.             state = False
  123.             print ("Pausing...")
  124.            
  125.             time.sleep(0.1)
  126.            
  127.             setLights(RED_PIN, 0)
  128.             setLights(GREEN_PIN, 0)
  129.             setLights(BLUE_PIN, 0)
  130.            
  131.         if c == 'r' and not state:
  132.             state = True
  133.             print ("Resuming...")
  134.            
  135.         if c == 'c' and not abort:
  136.             abort = True
  137.             break
  138.  
  139. start_new_thread(checkKey, ())
  140.  
  141.  
  142. print ("+ / - = Increase / Decrease brightness")
  143. print ("p / r = Pause / Resume")
  144. print ("c = Abort Program")
  145.  
  146.  
  147. setLights(RED_PIN, r)
  148. setLights(GREEN_PIN, g)
  149. setLights(BLUE_PIN, b)
  150.  
  151.  
  152. while abort == False:
  153.     if state and not brightChanged:
  154.         if r == 255 and b == 0 and g < 255:
  155.             g = updateColor(g, STEPS)
  156.             setLights(GREEN_PIN, g)
  157.        
  158.         elif g == 255 and b == 0 and r > 0:
  159.             r = updateColor(r, -STEPS)
  160.             setLights(RED_PIN, r)
  161.        
  162.         elif r == 0 and g == 255 and b < 255:
  163.             b = updateColor(b, STEPS)
  164.             setLights(BLUE_PIN, b)
  165.        
  166.         elif r == 0 and b == 255 and g > 0:
  167.             g = updateColor(g, -STEPS)
  168.             setLights(GREEN_PIN, g)
  169.        
  170.         elif g == 0 and b == 255 and r < 255:
  171.             r = updateColor(r, STEPS)
  172.             setLights(RED_PIN, r)
  173.        
  174.         elif r == 255 and g == 0 and b > 0:
  175.             b = updateColor(b, -STEPS)
  176.             setLights(BLUE_PIN, b)
  177.    
  178. print ("Aborting...")
  179.  
  180. setLights(RED_PIN, 0)
  181. setLights(GREEN_PIN, 0)
  182. setLights(BLUE_PIN, 0)
  183.  
  184. time.sleep(0.5)
  185.  
  186. pi.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement