-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLed.cpp
237 lines (208 loc) · 6.63 KB
/
Led.cpp
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
///////////////////////////////////////////////////////////////////////////////
// Led.cpp : implementation file
// Visual Source Safe: $Revision: 1 $
//
// Led static control. Will display a LED in 4 different colors and two shapes.
///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 1998-1999 Monte Variakojis ( [email protected] )
// All rights reserved - not to be sold.
///////////////////////////////////////////////////////////////////////////////
#include "resource.h"
#include "Led.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLed
#define TIMER_ID_PING 1 // Timer Ping ID
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
CLed::CLed()
{
m_LedBitmap.LoadBitmap(IDB_LEDS);
m_nLedColor = LED_COLOR_RED;
m_nLedMode = LED_OFF;
m_nLedShape = LED_ROUND;
}
CLed::~CLed()
{
VERIFY(m_LedBitmap.DeleteObject());
}
BEGIN_MESSAGE_MAP(CLed, CStatic)
//{{AFX_MSG_MAP(CLed)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_CREATE()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLed message handlers
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void CLed::OnPaint()
{
CPaintDC dc(this); // device context for painting
DrawLed(&dc,m_nLedColor,m_nLedMode,m_nLedShape);
// Do not call CStatic::OnPaint() for painting messages
}
///////////////////////////////////////////////////////////////////////////////
// Name: SetLed
// Description: This method will draw the LED to the specified DC.
//
// Entry:
// CDC *pDC - DC to draw to
//
// int iLedColor - Where color is defined by:
// LED_COLOR_RED
// LED_COLOR_GREEN
// LED_COLOR_YELLOW
// LED_COLOR_BLUE
//
// int iMode - where mode is defined by:
// LED_ON
// LED_OFF
// LED_DISABLED
//
// int iShape - where shape is defined by:
// LED_ROUND
// LED_SQUARE
///////////////////////////////////////////////////////////////////////////////
void CLed::DrawLed(CDC *pDC,int nLEDColor, int nMode, int nShape)
{
CRect rect;
GetClientRect(&rect);
//
// Center led within an oversized window
//
if(rect.Width() >= LED_SIZE && rect.Height() >= LED_SIZE)
{
int nWidth = rect.Width();
int nHeight = rect.Height();
rect.left += (nWidth - LED_SIZE)/2;
rect.right -= (nWidth - LED_SIZE)/2;
rect.top += (nHeight - LED_SIZE)/2;
rect.bottom -= (nHeight - LED_SIZE)/2;
}
//
// Prepare temporary DCs and bitmaps
//
CBitmap TransBitmap;
TransBitmap.CreateBitmap(LED_SIZE,LED_SIZE,1,1,NULL);
CBitmap bitmapTemp;
CBitmap* pBitmap = &m_LedBitmap;
CDC srcDC;
CDC dcMask;
CDC TempDC;
TempDC.CreateCompatibleDC(pDC);
srcDC.CreateCompatibleDC(pDC);
dcMask.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = srcDC.SelectObject(pBitmap);
CBitmap* pOldMaskbitmap = dcMask.SelectObject(&TransBitmap);
bitmapTemp.CreateCompatibleBitmap(pDC,LED_SIZE,LED_SIZE);
//
// Work with tempDC and bitmapTemp to reduce flickering
//
CBitmap *pOldBitmapTemp = TempDC.SelectObject(&bitmapTemp);
TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, pDC, rect.left, rect.top, SRCCOPY);
//
// Create mask
//
COLORREF OldBkColor = srcDC.SetBkColor(RGB(255,0,255));
dcMask.BitBlt(0, 0, LED_SIZE, LED_SIZE,&srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCCOPY);
TempDC.SetBkColor(OldBkColor);
//
// Using the IDB_LEDS bitmap, index into the bitmap for the appropriate
// LED. By using the mask color (RGB(255,0,255)) a mask has been created
// so the bitmap will appear transparent.
//
TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, &srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCINVERT);
TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE,&dcMask, 0, 0, SRCAND);
TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, &srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCINVERT);
//
// Since the actual minipulation is done to tempDC so there is minimal
// flicker, it is now time to draw the result to the screen.
//
pDC->BitBlt(rect.left, rect.top, LED_SIZE, LED_SIZE, &TempDC, 0, 0, SRCCOPY);
//
// House cleaning
//
srcDC.SelectObject(pOldBitmap);
dcMask.SelectObject(pOldMaskbitmap);
TempDC.SelectObject(pOldBitmapTemp);
VERIFY(srcDC.DeleteDC());
VERIFY(dcMask.DeleteDC());
VERIFY(TempDC.DeleteDC());
VERIFY(TransBitmap.DeleteObject());
VERIFY(bitmapTemp.DeleteObject());
}
///////////////////////////////////////////////////////////////////////////////
// Name: SetLed
// Description: This method will draw and set led parameters.
//
// Entry: int iLedColor - Where color is defined by:
// LED_COLOR_RED
// LED_COLOR_GREEN
// LED_COLOR_YELLOW
// LED_COLOR_BLUE
//
// int iMode - where mode is defined by:
// LED_ON
// LED_OFF
// LED_DISABLED
//
// int iShape - where shape is defined by:
// LED_ROUND
// LED_SQUARE
///////////////////////////////////////////////////////////////////////////////
void CLed::SetLed(int nLedColor, int nMode, int nShape)
{
m_nLedColor = nLedColor;
m_nLedMode = nMode;
m_nLedShape = nShape;
CDC *pDC;
pDC = GetDC();
DrawLed(pDC,m_nLedColor,m_nLedMode,m_nLedShape);
ReleaseDC(pDC);
}
///////////////////////////////////////////////////////////////////////////////
// Name: Ping
// Description: This method will turn the led on for dwTimeout milliseconds and
// then turn it off.
//
// Entry: DWORD dwTimeout - Time out in milliseconds
///////////////////////////////////////////////////////////////////////////////
void CLed::Ping(DWORD dwTimeout)
{
// Return if pinging
if(m_bPingEnabled == TRUE)
{
KillTimer(TIMER_ID_PING);
}
m_bPingEnabled = TRUE;
SetLed(m_nLedColor,CLed::LED_ON,m_nLedShape);
SetTimer(TIMER_ID_PING,dwTimeout,NULL);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void CLed::OnTimer(UINT nIDEvent)
{
if(nIDEvent == TIMER_ID_PING)
{
SetLed(m_nLedColor,CLed::LED_OFF,m_nLedShape);
KillTimer(nIDEvent);
m_bPingEnabled = FALSE;
}
CStatic::OnTimer(nIDEvent);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
BOOL CLed::OnEraseBkgnd(CDC* /*pDC*/)
{
// No background rendering
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////