-
Notifications
You must be signed in to change notification settings - Fork 0
/
LedControl.py
152 lines (126 loc) · 4.84 KB
/
LedControl.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
#!/usr/bin/env python
import wx
import sys
import os
import builtins as __builtin__
import inspect
moduleList = {'osvmGlobals':'globs'}
for k,v in moduleList.items():
print('Loading: %s as %s' % (k,v))
mod = __import__(k, fromlist=[None])
globals()[v] = globals().pop('mod') # Rename module in globals()
####
#print(__name__)
class ColorLED(wx.Control):
"""
Creates a LED widget. State is controlled by SetState()/GetState() methods
"""
def __init__(self, parent, color, id=-1, style=wx.NO_BORDER):
size = (17, 17)
wx.Control.__init__(self, parent, id, size, (-1,-1), style)
self.MinSize = size
self._color = color
self.SetState(color)
self.Bind(wx.EVT_PAINT, self.OnPaint, self)
def SetState(self, color):
self._color = color
ascii_led_header = (
'/* XPM */\n',
'static char * led_xpm[] = {\n',
'"17 17 3 1",\n',
'"0 c None", \n',
'"* c #FFFFFF",\n')
ascii_led_footer = (
'"000000XXXXX000000",\n',
'"0000XXXXXXXXX0000",\n',
'"000XXXXXXXXXXX000",\n',
'"00XXXXXXXXXXXXX00",\n',
'"0XXXXXX**XXXXXXX0",\n',
'"0XXXX***XXXXXXXX0",\n',
'"XXXXX**XXXXXXXXXX",\n',
'"XXXX**XXXXXXXXXXX",\n',
'"XXXXXXXXXXXXXXXXX",\n',
'"XXXXXXXXXXXXXXXXX",\n',
'"XXXXXXXXXXXXXXXXX",\n',
'"0XXXXXXXXXXXXXXX0",\n',
'"0XXXXXXXXXXXXXXX0",\n',
'"00XXXXXXXXXXXXX00",\n',
'"000XXXXXXXXXXX000",\n',
'"0000XXXXXXXXX0000",\n',
'"000000XXXXX000000"};\n')
xpmFilePath = os.path.join(globs.tmpDir, 'LED_%s.xpm' % color)
if not os.path.exists(xpmFilePath):
# Create an XPM file with desired color
f = open(xpmFilePath, 'w')
for l in ascii_led_header:
f.write(l)
f.write('"X c %s",\n' % color)
for l in ascii_led_footer:
f.write(l)
f.close()
self.bmp = wx.Bitmap(xpmFilePath, type=wx.BITMAP_TYPE_XPM)
self.Refresh()
def GetState(self):
return self._color
State = property(GetState, SetState)
def OnPaint(self, e):
dc = wx.PaintDC(self)
dc.DrawBitmap(self.bmp, 0, 0, True)
def myprint(*args, **kwargs):
"""My custom print() function."""
# Adding new arguments to the print function signature
# is probably a bad idea.
# Instead consider testing if custom argument keywords
# are present in kwargs
__builtin__.print('%s():' % inspect.stack()[1][3], *args, **kwargs)
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
panel = wx.Panel(self)
topBoxSizer = wx.BoxSizer(orient=wx.VERTICAL)
fgs = wx.FlexGridSizer(cols=3, hgap=8, vgap=8)
gbs = wx.GridBagSizer(hgap=15,vgap=0)
topBoxSizer.Add(gbs, 0, border=10, flag=wx.ALL | wx.EXPAND)
topBoxSizer.Add(fgs, 0, border=10, flag=wx.ALL | wx.EXPAND)
RED = '#DC0A0A'
GREEN = '#0ADC0A'
ORANGE = '#FAC800'
self.ledsColours = [GREEN,ORANGE,RED]
self.ledsColoursLen = len(self.ledsColours)
led = ColorLED(panel, RED)
gbs.Add(led, pos=(0,0), flag=wx.ALL | wx.EXPAND, border=5)
led = ColorLED(panel, GREEN)
gbs.Add(led, pos=(0,1), flag=wx.ALL | wx.EXPAND, border=5)
led = ColorLED(panel, ORANGE)
gbs.Add(led, pos=(0,2), flag=wx.ALL | wx.EXPAND, border=5)
self.led0 = ColorLED(panel, GREEN)
self.led1 = ColorLED(panel, ORANGE)
self.led2 = ColorLED(panel, RED)
gbs.Add(self.led0, pos=(1,0), flag=wx.ALL | wx.EXPAND, border=5)
gbs.Add(self.led1, pos=(1,1), flag=wx.ALL | wx.EXPAND, border=5)
gbs.Add(self.led2, pos=(1,2), flag=wx.ALL | wx.EXPAND, border=5)
self.idx = 0
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, lambda evt: self.OnTimer(evt), self.timer)
self.timer.Start(globs.TIMER6_FREQ)
panel.SetSizerAndFit(topBoxSizer)
self.SetClientSize(topBoxSizer.GetSize())
self.Centre()
self.Show()
def OnTimer(self, event):
self.led0.SetState(self.ledsColours[self.idx%self.ledsColoursLen])
self.idx += 1
self.led1.SetState(self.ledsColours[self.idx%self.ledsColoursLen])
self.idx += 1
self.led2.SetState(self.ledsColours[self.idx%self.ledsColoursLen])
self.idx += 2
def main():
# Init Globals instance
globs.tmpDir = '/tmp'
# Create DemoFrame frame, passing globals instance as parameter
app = wx.App(False)
frame = MyFrame(None, -1, title="Test")
frame.Show()
app.MainLoop()
if __name__ == "__main__":
main()