-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledon.py
221 lines (188 loc) · 7.3 KB
/
ledon.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#/*
#Python and Tkinter Programming
#John E. Grayson
#ISBN: 1884777813
#Publisher: Manning
#*/
from Tkinter import *
SQUARE = 1
ROUND = 2
ARROW = 3
POINT_DOWN = 0
POINT_UP = 1
POINT_RIGHT = 2
POINT_LEFT = 3
STATUS_OFF = 1
STATUS_ON = 2
STATUS_WARN = 3
STATUS_ALARM = 4
STATUS_SET = 5
class DummyClass:
pass
Color = DummyClass()
Color.PANEL = '#545454'
Color.OFF = '#656565'
Color.ON = '#00FF33'
Color.WARN = '#ffcc00'
Color.ALARM = '#ff4422'
class LED:
def __init__(self, master=None, width=25, height=25,
appearance=FLAT,
status=STATUS_ON, bd=1,
bg=None,
shape=SQUARE, outline="",
blink=0, blinkrate=1,
orient=POINT_UP,
takefocus=0):
# preserve attributes
self.master = master
self.shape = shape
self.onColor = Color.ON
self.offColor = Color.OFF
self.alarmColor = Color.ALARM
self.warningColor = Color.WARN
self.specialColor = '#00ffdd'
self.status = status
self.blink = blink
self.blinkrate = int(blinkrate)
self.on = 0
self.onState = None
if not bg:
bg = Color.PANEL
## Base frame to contain light
self.frame=Frame(master, relief=appearance, bg=bg, bd=bd,
takefocus=takefocus)
basesize = width
d = center = int(basesize/2)
if self.shape == SQUARE:
self.canvas=Canvas(self.frame, height=height, width=width,
bg=bg, bd=0, highlightthickness=0)
self.light=self.canvas.create_rectangle(0, 0, width, height,
fill=Color.ON)
elif self.shape == ROUND:
r = int((basesize-2)/2)
self.canvas=Canvas(self.frame, width=width, height=width,
highlightthickness=0, bg=bg, bd=0)
if bd > 0:
self.border=self.canvas.create_oval(center-r, center-r,
center+r, center+r)
r = r - bd
self.light=self.canvas.create_oval(center-r-1, center-r-1,
center+r, center+r, fill=Color.ON,
outline=outline)
else: # Default is an ARROW
self.canvas=Canvas(self.frame, width=width, height=width,
highlightthickness=0, bg=bg, bd=0)
x = d
y = d
if orient == POINT_DOWN:
self.light=self.canvas.create_polygon(x-d,y-d, x,y+d,
x+d,y-d, x-d,y-d, outline=outline)
elif orient == POINT_UP:
self.light=self.canvas.create_polygon(x,y-d, x-d,y+d,
x+d,y+d, x,y-d, outline=outline)
elif orient == POINT_RIGHT:
self.light=self.canvas.create_polygon(x-d,y-d, x+d,y,
x-d,y+d, x-d,y-d, outline=outline)
elif orient == POINT_LEFT:
self.light=self.canvas.create_polygon(x-d,y, x+d,y+d,
x+d,y-d, x-d,y, outline=outline)
self.canvas.pack(side=TOP, fill=X, expand=NO)
self.update()
def turnon(self):
self.status = STATUS_ON
if not self.blink: self.update()
def turnoff(self):
self.status = STATUS_OFF
if not self.blink: self.update()
def alarm(self):
self.status = STATUS_ALARM
if not self.blink: self.update()
def warn(self):
self.status = STATUS_WARN
if not self.blink: self.update()
def set(self, color):
self.status = STATUS_SET
self.specialColor = color
self.update()
def blinkon(self):
if not self.blink:
self.blink = 1
self.onState = self.status
self.update()
def blinkoff(self):
if self.blink:
self.blink = 0
self.status = self.onState
self.onState = None
self.on = 0
self.update()
def blinkstate(self, blinkstate):
if blinkstate:
self.blinkon()
else:
self.blinkoff()
def update(self):
# First do the blink, if set to blink
if self.blink:
if self.on:
if not self.onState:
self.onState = self.status
self.status = STATUS_OFF
self.on = 0
else:
if self.onState:
self.status = self.onState # Current ON color
self.on = 1
if self.status == STATUS_ON:
self.canvas.itemconfig(self.light, fill=self.onColor)
elif self.status == STATUS_OFF:
self.canvas.itemconfig(self.light, fill=self.offColor)
elif self.status == STATUS_WARN:
self.canvas.itemconfig(self.light, fill=self.warningColor)
elif self.status == STATUS_SET:
self.canvas.itemconfig(self.light, fill=self.specialColor)
else:
self.canvas.itemconfig(self.light, fill=self.alarmColor)
self.canvas.update_idletasks()
if self.blink:
self.frame.after(self.blinkrate * 1000, self.update)
if __name__ == '__main__':
class TestLEDs(Frame):
def __init__(self, parent=None):
# List of Colors and Blink On/Off
states = [(STATUS_OFF, 0),
(STATUS_ON, 0),
(STATUS_WARN, 0),
(STATUS_ALARM, 0),
(STATUS_SET, 0),
(STATUS_ON, 1),
(STATUS_WARN, 1),
(STATUS_ALARM, 1),
(STATUS_SET, 1)]
# List of LED types to display,
# with sizes and other attributes
leds = [(ROUND, 25, 25, FLAT, 0, None, ""),
(ROUND, 15, 15, RAISED, 1, None, ""),
(SQUARE, 20, 20, SUNKEN, 1, None, ""),
(SQUARE, 8, 8, FLAT, 0, None, ""),
(SQUARE, 8, 8, RAISED, 1, None, ""),
(SQUARE, 16, 8, FLAT, 1, None, ""),
(ARROW, 14, 14, RIDGE, 1, POINT_UP, ""),
(ARROW, 14, 14, RIDGE, 0, POINT_RIGHT, ""),
(ARROW, 14, 14, FLAT, 0, POINT_DOWN, "white")]
Frame.__init__(self) # Do superclass init
self.pack()
self.master.title('LED Example - Stage 1')
# Iterate for each type of led
for shape, w, h, app, bd, orient, outline in leds:
frame = Frame(self, bg=Color.PANEL)
frame.pack(anchor=N, expand=YES, fill=X)
# Iterate for selected states
for state, blink in states:
LED(frame, shape=shape, status=state,
width=w, height=h, appearance=app,
orient=orient, blink=blink, bd=bd,
outline=outline).frame.pack(side=LEFT,
expand=YES, padx=1, pady=1)
TestLEDs().mainloop()