-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor_diode.py
More file actions
99 lines (76 loc) · 2.23 KB
/
color_diode.py
File metadata and controls
99 lines (76 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
__author__ = 'bhavinpatel'
import RPi.GPIO as g
import time
import random
from pprint import pprint
class Diode:
def __init__(self , red=12,blue=18,green=16):
self.red = red
self.blue = blue
self.green = green
self.g = g
try:
self.g.setmode( g.BOARD)
self.g.setup( self.red , g.OUT)
self.g.setup( self.blue , g.OUT)
self.g.setup( self.green , g.OUT)
except:
pass
def redLed(self, status ):
self.g.output( self.red , status)
time.sleep(status)
def cleanUp(self):
self.g.cleanup()
def __del__(self):
self.g.cleanup()
class Pallet(Diode):
initialFrequency = 100
initialDutyCycle = 0
def __init( self , red , blue , green ):
self = Diode.__init__(self, red, blue , green)
self.RED = self.g.PWM( self.red , self.initialFrequency)
self.BLUE = self.g.PWM( self.blue , self.initialFrequency)
self.GREEN = self.g.PWM( self.green , self.initialFrequency)
self.RED.start( self.initialDutyCycle )
self.BLUE.start( self.initialDutyCycle )
self.GREEN.start( self.initialDutyCycle )
def set_pwm_for_red(self):
self.RED = self.g.PWM( self.red , self.initialFrequency)
self.RED.start( self.initialDutyCycle )
def set_pwm_for_blue(self):
self.BLUE = self.g.PWM( self.blue , self.initialFrequency)
self.BLUE.start( self.initialDutyCycle )
def set_pwm_for_green(self):
self.GREEN = self.g.PWM( self.green , self.initialFrequency)
self.GREEN.start( self.initialDutyCycle )
def mapping(self, val):
retval = round((100*val)/255,1)
pprint( retval )
return retval
def color (self , r, b, g ):
if( not hasattr(self,'RED') ) :
self.set_pwm_for_red()
if( not hasattr(self,'BLUE') ) :
self.set_pwm_for_blue()
if( not hasattr(self,'GREEN') ) :
self.set_pwm_for_green()
self.RED.ChangeDutyCycle( self.mapping( r ) )
self.BLUE.ChangeDutyCycle( self.mapping( b ) )
self.GREEN.ChangeDutyCycle( self.mapping( g ) )
def wait(self ,waitPeriod=1):
time.sleep( waitPeriod)
if __name__ == "__main__":
p = Pallet()
p.color(255,0,0)
p.wait()
p.color(0,255,0)
p.wait()
p.color(0,0,255)
p.wait()
#exit(0)
for r in range(0,255,10):
for b in range(0, 255,10):
for g in range(0,255,10):
print "\n r(%d), b(%d), g(%d)" %( r, b ,g )
p.color(r,b,g)
p.wait(0.1)