-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssd1306driver.cpp
82 lines (66 loc) · 1.83 KB
/
ssd1306driver.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
#include "ssd1306driver.h"
#include <QVector>
#include <QRgb>
#include <stdint.h>
extern "C" {
int i2c_open(int bus);
int i2c_select(int file, int addr);
int ssd1306_init(int file, int col, int line);
int ssd1306_cls(int file, int col, int line);
int i2c_write_data(int file, uint8_t data[], size_t len);
}
Ssd1306Driver::Ssd1306Driver(QObject *parent)
: QObject(parent)
, m_file(-1)
{
}
bool Ssd1306Driver::openDevice(QSize size, int busId, int address)
{
m_file = i2c_open(busId);
if (m_file < 0) {
return false;
}
int res = i2c_select(m_file, address);
if (res < 0) {
return false;
}
ssd1306_init(m_file, size.width(), size.height());
ssd1306_cls(m_file, size.width(), size.height()); // SSD1306 may have a SRAM-based GDDRAM, some parts of the graphic are perserved after power cycle.
m_size = size;
return true;
}
void Ssd1306Driver::clearScreen()
{
if (m_file > -1) {
ssd1306_cls(m_file, m_size.width(), m_size.height());
}
}
void Ssd1306Driver::close()
{
m_file = -1; // TODO: close file ?
}
void Ssd1306Driver::writeImage(const QImage &image)
{
const uint8_t SSD1306_CONT_DATA_HDR = 0x40;
if (m_file < 0) {
return;
}
int height = m_size.height() / 8;
int width = m_size.width();
int len = height * width + 1;
QVector<uint8_t> vector(len);
int pos = 0;
vector[pos] = SSD1306_CONT_DATA_HDR;
pos++;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
uint8_t pixel = 0u;
for (int i = 0; i < 8; ++i) {
pixel |= static_cast<uint8_t>(image.pixelIndex(x, y * 8 + i) == 1) << i;
}
vector[pos] = pixel;
pos++;
}
}
i2c_write_data(m_file, vector.data(), static_cast<size_t>(vector.size()));
}