document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import web
  2. import RPi.GPIO as GPIO, time, os
  3.  
  4. DEBUG = False
  5.  
  6. GPIO.setmode(GPIO.BCM)
  7.  
  8. urls = (
  9.   \'/hello\', \'Index\'
  10. )
  11.  
  12. app = web.application(urls, globals())
  13.  
  14. render = web.template.render(\'templates/\', base="layout")
  15.  
  16. class LightSwitch(object):
  17.    
  18.     def __init__(self, color):
  19.        
  20.         self.GREEN_LED = 24
  21.         self.RED_LED = 23
  22.         self.BLUE_LED = 18         
  23.         self.LED = None
  24.        
  25.         GPIO.setup(self.GREEN_LED, GPIO.OUT)
  26.         GPIO.setup(self.RED_LED, GPIO.OUT)
  27.         GPIO.setup(self.BLUE_LED, GPIO.OUT)
  28.        
  29.         if color:  
  30.             if color in \'green\':
  31.                 self.LED = self.GREEN_LED
  32.             elif color in \'blue\':
  33.                 self.LED = self.BLUE_LED
  34.             elif color in \'red\':
  35.                 self.LED = self.RED_LED
  36.             else:
  37.                 self.LED = None
  38.  
  39.         if DEBUG: print "LED COLOR: %r" % color
  40.        
  41.     def flip_switch(self):
  42.         if self.LED == None:
  43.             return None
  44.            
  45.         if DEBUG: print "LED on GPIO pin: %r" % self.LED
  46.        
  47.        
  48.         led_status = GPIO.input(self.LED)
  49.         if led_status == GPIO.LOW:
  50.             GPIO.output(self.LED, True)
  51.         else:
  52.             GPIO.output(self.LED, False)
  53.            
  54.         return GPIO.input(self.LED)
  55.        
  56.     def get_light_statuses(self):
  57.         return {\'green\': GPIO.input(self.GREEN_LED),
  58.         \'blue\': GPIO.input(self.BLUE_LED),
  59.         \'red\': GPIO.input(self.RED_LED)}
  60.        
  61. class Index(object):
  62.     def GET(self):
  63.         return render.hello_form(LightSwitch(None).get_light_statuses())
  64.  
  65.     def POST(self):
  66.        
  67.         form = web.input()
  68.         light_switch = LightSwitch(form.led)
  69.         light_switch.flip_switch()
  70.        
  71.         if DEBUG:
  72.             print \'-\' * 20
  73.             for color, status in light_switch.get_light_statuses().items():
  74.                 str_status = None
  75.            
  76.                 if status == GPIO.LOW:
  77.                     str_status = "off"
  78.                 else:
  79.                     str_status = "on"
  80.                
  81.                 print "The %s LED is %s" % (color, str_status)
  82.            
  83.             print \'-\' * 20
  84.            
  85.         return render.hello_form(light_switch.get_light_statuses())
  86.  
  87. if __name__ == "__main__":
  88.     app.run()
');