-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlcddisplayarea.cpp
168 lines (142 loc) · 4.01 KB
/
lcddisplayarea.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
#include <QDebug>
#include "lcddisplayarea.h"
using namespace stubserver;
/**
* Size of the internal LCD border
*/
static const unsigned BORDER_SIZE = 10;
/**
* Dots per char in horizontal direction
*/
static const unsigned COLS_PER_CHAR = CharMask::NUM_COLS - 1;
/**
* Dots per char in vertical direction: there are 9 dots,
* but normally only 8 are used and the 9th is already a separator
* which optionally can be used.
*/
static const unsigned ROWS_PER_CHAR = CharMask::NUM_LINES;
/**
* Create the LCD with a blue background.
*/
LCDDisplayArea::LCDDisplayArea(QWidget *parent)
: ButtonWidget(parent)
, dotSize(5)
, dot6Size(CharMask::NUM_COLS * dotSize)
, dot9Size(CharMask::NUM_LINES * dotSize)
, backlight(false)
, whitePen(Qt::white, dotSize - 1)
, grayPen(Qt::gray, dotSize - 1)
, lcdFont(false)
, lcdState(NULL)
, myMutex()
{
showBacklight();
connect(this, &LCDDisplayArea::triggerUpdate, this, &LCDDisplayArea::doUpdate);
}
/**
* Cleanup datasctructures.
*/
LCDDisplayArea::~LCDDisplayArea()
{
}
void LCDDisplayArea::doUpdate()
{
update();
}
void LCDDisplayArea::showBacklight()
{
QPalette Pal(palette());
Pal.setColor(QPalette::Window, backlight ? Qt::blue : Qt::darkBlue);
setAutoFillBackground(true);
setPalette(Pal);
}
/**
* Calculate X position for a dot in a char
*/
unsigned LCDDisplayArea::getX(unsigned charX, unsigned dot)
{
return BORDER_SIZE + (charX * dot6Size) + (dot * dotSize);
}
/**
* Calculate Y position for a dot in a char
*/
unsigned LCDDisplayArea::getY(unsigned charY, unsigned dot)
{
return BORDER_SIZE + (charY * dot9Size) + (dot * dotSize);
}
/**
* Draw one single char on the LCD.
* @param actCh
* @param painter
*/
void LCDDisplayArea::drawChar(char actCh, unsigned col, unsigned line, QPainter &painter)
{
unsigned numPoints = 0;
QPoint points[CharMask::NUM_COLS * CharMask::NUM_LINES];
// PCharMask m = chars[actCh < 0 ? actCh + 255 : actCh];
PCharMask m = lcdFont.get(actCh & 255);
for (unsigned x = 0; x < COLS_PER_CHAR; ++x)
for (unsigned y = 0; y < ROWS_PER_CHAR; ++y)
{
if (m->isOn(x, y)) {
points[numPoints] = QPoint(getX(col, x), getY(line, y));
++numPoints;
}
}
painter.drawPoints(points, numPoints);
}
/**
* Redraw the screen with the current text content.
*/
void LCDDisplayArea::paintEvent(QPaintEvent * /* event */)
{
utils::MutexLock lock(myMutex);
if (lcdState == NULL)
return;
unsigned lines = lcdState->getLines();
unsigned cols = lcdState->getCols();
if (backlight != lcdState->isBacklightOn())
{
backlight = lcdState->isBacklightOn();
showBacklight();
}
QPainter painter(this);
painter.setPen(backlight ? whitePen : grayPen);
for (unsigned line = 0; line < lines; ++line)
{
const std::string& text = lcdState->getLine(line);
for (unsigned actCol = 0; actCol < cols; ++actCol)
{
char actCh = text[actCol];
// do not really print a space
if (actCh == ' ')
continue;
drawChar(actCh, actCol, line, painter);
}
}
if (lcdState->isCursorVisible())
{
drawChar(255, lcdState->getCursorX(), lcdState->getCursorY(), painter);
}
}
void LCDDisplayArea::notify(const VisibleDeviceState &hint)
{
//qDebug() << "LCDDisplayArea::notify " << hint.getChangeCode();
if (hint.getChangeCode() == VisibleDeviceState::DISCONNECT)
lcdState = NULL;
else {
lcdState = dynamic_cast<const LcdState*>(&hint);
if (lcdState && hint.getChangeCode() == LcdState::CUSTOM_CHAR)
{
unsigned line = lcdState->getChangedLine();
if (line < 8)
{
// custom chars are defined by 0..7 and index by char 8..15
lcdFont.set(line + 8, new CharMask(lcdState->getCustomerChar(line)));
}
}
else {
emit triggerUpdate();
}
}
}