-
Notifications
You must be signed in to change notification settings - Fork 1
/
ledmatrix.cpp
89 lines (77 loc) · 2.27 KB
/
ledmatrix.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
#include "ledmatrix.h"
LedMatrix::LedMatrix(QWidget *v_parent)
:
QTableWidget(v_parent),
m_offColor(QColor(0,0,0)),
m_onColor(QColor(255,0,0)),
m_justPressed(false)
{
connect(this, SIGNAL(cellEntered(int,int)), this, SLOT(reactEntered(int,int)));
connect(this, SIGNAL(cellPressed(int,int)), this, SLOT(reactPressed(int,int)));
}
void LedMatrix::init(int v_cellWidth, int v_cellHeight, int v_width, int v_height)
{
createDiodes(8,80);
setGridSize(v_cellWidth, v_cellHeight);
setFixedHeight(v_height);
setFixedWidth(v_width);
}
void LedMatrix::setGridSize(int v_width, int v_height)
{
for(int i = 0; i < rowCount(); i++)
setRowHeight(i, v_height);
for(int j = 0; j < columnCount(); j++)
setColumnWidth(j, v_width);
}
void LedMatrix::createDiodes(int v_rows, int v_cols)
{
setRowCount(v_rows);
setColumnCount(v_cols);
for(int i = 0; i < rowCount(); i++)
for(int j = 0; j < columnCount(); j++)
{
setItem(i, j, new QTableWidgetItem());
item(i, j)->setBackgroundColor(m_offColor);
}
}
void LedMatrix::setDiodeState(int v_row, int v_col, bool v_state)
{
item(v_row, v_col)->setBackgroundColor(v_state ? m_onColor : m_offColor);
}
bool LedMatrix::getDiodeState(int v_row, int v_col)
{
return item(v_row, v_col)->backgroundColor() == m_onColor;
}
void LedMatrix::toggleDiodeState(int v_row, int v_col)
{
setDiodeState(v_row, v_col, !getDiodeState(v_row, v_col));
}
void LedMatrix::setSimFrame(const SimFrame &v_frame)
{
for(int i = 0; i < rowCount(); i++)
for(int j = 0; j < columnCount(); j++)
setDiodeState(i, j, v_frame.getPixel(i,j));
}
void LedMatrix::getSimFrame(SimFrame &v_frame)
{
for(int i = 0; i < rowCount(); i++)
for(int j = 0; j < columnCount(); j++)
v_frame.setPixel(i ,j , getDiodeState(i,j));
}
void LedMatrix::clearDisplay()
{
for(int i = 0; i < rowCount(); i++)
for(int j = 0; j < columnCount(); j++)
setDiodeState(i, j, false);
}
void LedMatrix::reactPressed(int v_row, int v_col)
{
m_justPressed = true;
toggleDiodeState(v_row, v_col);
}
void LedMatrix::reactEntered(int v_row, int v_col)
{
if(!m_justPressed)
toggleDiodeState(v_row, v_col);
m_justPressed = false;
}