Repository navigation
Expand file tree
/
Copy pathLCDDisplay.cpp
More file actions
252 lines (217 loc) · 7.25 KB
/
Copy pathLCDDisplay.cpp
File metadata and controls
252 lines (217 loc) · 7.25 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "LCDDisplay.hpp"
#include "lcd_fonts.hpp"
#include <QDebug>
#include <bb/cascades/application.h>
#include <bb/cascades/Image>
#include <bb/cascades/Container>
#include <bb/cascades/ImageView>
#include <bb/cascades/AbsoluteLayout>
#include <bb/cascades/AbsoluteLayoutProperties>
#include <bb/cascades/Color>
using namespace bb::cascades;
template<>
int InstanceCounter<LCDDisplay>::s_count;
template<>
int InstanceCounter<LCDDisplay>::s_maxCount;
LCDDisplay::LCDDisplay(Container *parent)
: CustomControl(parent),
m_timer(new QTimer(this)),
m_rootContainer(NULL),
m_image_on(Canvas::loadImageData("asset:///images/led/led_on.png")),
m_image_off(Canvas::loadImageData("asset:///images/led/led_off.png")),
m_numLetters(14),
m_rows(8),
m_columns(6),
m_canvas(new Canvas(m_columns * m_image_on.width() * m_numLetters,
m_rows * m_image_on.height(),
this)),
m_scroll(0),
m_wiggle(0),
m_wiggleDirection(1) {
bool rc;
Q_UNUSED(rc);
rc = QObject::connect(m_timer, SIGNAL(timeout()),
this, SLOT(onUpdateTimeout()));
Q_ASSERT(rc);
rc = QObject::connect(bb::cascades::Application::instance(),
SIGNAL(fullscreen()),
this,
SLOT(enableAnimation()));
Q_ASSERT(rc);
rc = QObject::connect(bb::cascades::Application::instance(),
SIGNAL(thumbnail()),
this,
SLOT(disableAnimation()));
Q_ASSERT(rc);
rc = QObject::connect(bb::cascades::Application::instance(),
SIGNAL(invisible()),
this,
SLOT(disableAnimation()));
Q_ASSERT(rc);
rc = QObject::connect(bb::cascades::Application::instance(),
SIGNAL(asleep()),
this,
SLOT(disableAnimation()));
Q_ASSERT(rc);
createLCD();
}
void LCDDisplay::enableAnimation()
{
qDebug() << "LCDDisplay: animation enabled";
m_timer->start();
}
void LCDDisplay::disableAnimation()
{
qDebug() << "LCDDisplay: animation disabled";
m_timer->stop();
}
void LCDDisplay::createLCD(){
if(m_rootContainer != NULL)
{
setRoot(NULL);
m_rootContainer->setParent(0);
delete m_rootContainer;
m_rootContainer = NULL;
}
const int width = m_columns * m_image_on.width() * m_numLetters;
const int height = m_rows * m_image_on.height();
m_rootContainer = Container::create().top(0)
.topMargin(0)
.bottom(0)
.bottomMargin(0)
.left(0)
.leftMargin(0)
.right(0)
.rightMargin(0)
.preferredWidth(width)
.preferredHeight(height)
.background(Color::Transparent)
.layout(AbsoluteLayout::create());
m_rootContainer->setMinWidth(width);
m_rootContainer->setMaxWidth(width);
m_rootContainer->setMinHeight(height);
m_rootContainer->setMaxHeight(height);
ImageView * view = ImageView::create()
.topMargin(0)
.bottomMargin(0)
.leftMargin(0)
.rightMargin(0)
.loadEffect(ImageViewLoadEffect::None)
.scalingMethod(ScalingMethod::None)
.implicitLayoutAnimations(false)
.layoutProperties(AbsoluteLayoutProperties::create().x(0).y(0));
m_rootContainer->add(view);
updateLCD();
setRoot(m_rootContainer);
}
void LCDDisplay::updateLCD() {
const int textLength = m_text.length();
const int length = std::max(textLength, (int)m_numLetters);
int newPixelWidth = m_columns * m_image_on.width() * length;
if(m_canvas != NULL) {
if(newPixelWidth != m_canvas->width()) {
m_canvas->setParent(0);
delete m_canvas;
m_canvas = NULL;
}
}
if(m_canvas == NULL) {
int newPixelHeight = m_rows * m_image_on.height();
m_canvas = new Canvas(newPixelWidth, newPixelHeight, this);
}
const int numDots = m_columns * length;
for(int row = 0; row < m_rows; ++row)
{
for(int col = 0; col < numDots; ++col)
{
const int index = col / m_columns;
const QChar letter = index > textLength ? QChar(' ') : m_text[index];
const unsigned char ascii = static_cast<const unsigned char>(letter.toAscii());
const unsigned char bitmask = lcd_font_6x8[ascii * m_rows + row];
const unsigned shift = col % m_columns;
const bool is_set = (bitmask & (0x80 >> shift));
m_canvas->blit(col * m_image_on.width(),
row * m_image_on.height(),
(is_set ? m_image_on : m_image_off));
}
}
ImageView * view = qobject_cast<ImageView *>(m_rootContainer->at(0));
Image image = m_canvas->image();
view->setImage(image);
}
int LCDDisplay::numLetters() const {
return m_numLetters;
}
void LCDDisplay::setNumLetters(int value) {
if(value < 1) {
value = 1;
}
if(value > 64) {
value = 64;
}
if(m_numLetters != value)
{
m_numLetters = value;
createLCD();
}
}
int LCDDisplay::scroll() const {
return m_scroll;
}
void LCDDisplay::setScroll(int value) {
if(value < 0) {
value = 0;
}
if(m_scroll != value)
{
m_scroll = value;
Control * view = m_rootContainer->at(0);
AbsoluteLayoutProperties * p = qobject_cast<AbsoluteLayoutProperties*>(view->layoutProperties());
p->setPositionX(-m_scroll * m_image_on.width());
}
}
QString const& LCDDisplay::text() const {
return m_text;
}
void LCDDisplay::setText(QString const& text) {
if(text != m_text)
{
m_text = text;
updateLCD();
const int numDots = m_columns * m_numLetters;
const int requiredDots = m_columns * m_text.length();
m_wiggle = requiredDots > numDots ? requiredDots - numDots : 0;
if(m_wiggle > 0)
{
m_timer->start(100);
}
else
{
m_timer->stop();
}
setScroll(0);
}
}
void LCDDisplay::onUpdateTimeout() {
int currentScroll = scroll();
int newScroll = currentScroll;
if(m_wiggleDirection > 0)
{
newScroll = currentScroll + m_wiggleDirection;
if(newScroll > m_wiggle)
{
m_wiggleDirection = -m_wiggleDirection;
return;
}
}
else
{
if(currentScroll < -m_wiggleDirection)
{
m_wiggleDirection = -m_wiggleDirection;
return;
}
newScroll = currentScroll + m_wiggleDirection;
}
setScroll(newScroll);
}