Skip to content

Instantly share code, notes, and snippets.

@agscala
Created June 14, 2010 21:53
Show Gist options
  • Save agscala/438372 to your computer and use it in GitHub Desktop.
Save agscala/438372 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# led.py - Sample classes and methods to get you started with LED's the OOP way.
def send_val_to_pin(pin, colorval):
print "Pin: %s | ColorVal: %s" % (str(pin), str(colorval))
class LED:
def __init__(self, pin_r, pin_g, pin_b):
self.pin_r = pin_r
self.pin_g = pin_g
self.pin_b = pin_b
def set_color(self, color):
self.color = color
def draw(self):
send_val_to_pin(self.pin_r, self.color.red)
send_val_to_pin(self.pin_g, self.color.blue)
send_val_to_pin(self.pin_b, self.color.green)
def off(self):
self.color.alpha = 0
class Color:
def __init__(self, red, blue, green, alpha):
self.max_value = 255 # If color vals go from 0 - 255, this should be 255. Otherwise if it's from 0.0 - 1.0, use 1.0
self.red = red
self.blue = blue
self.green = green
self.alpha = alpha
def red(self):
return int((self.alpha / self.max_value) * self.red)
def blue(self):
return int((self.alpha / self.max_value) * self.blue)
def green(self):
return int((self.alpha / self.max_value) * self.green)
if __name__ == "__main__":
# This function would be like the main arduino function
led1 = LED(11, 12, 13) # Pins 11, 12, and 13 for rgb
red = Color(255, 0, 0, 255)
green = Color(0, 255, 0, 255)
blue = Color(0, 0, 255, 255)
horrible_pink = Color(255, 0, 255, 255)
available_colors = [red, green, blue, horrible_pink]
for color in available_colors:
led1.set_color(color)
led1.draw()
print "==== SLEEP ====="
# Sleep possibly if you want to see each color for a little while
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment