-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSD1306_Adaptor.cpp
99 lines (86 loc) · 1.93 KB
/
SSD1306_Adaptor.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
#include "SSD1306_Adaptor.h"
static const uint8_t s_init_cmds[] = {
0xae, // off
0xd5, 0x80, // frequency set
0xa1, // segments remap
0xc8, // reversed scan
0xd3, 0, // no offset
0x40, // start line 0
0x81, 0xcf, // contrast
0xdb, 0x40, // vcom deselect level
0x8d, 0x14, // pump on
0x20, 2, // set page addressing mode
0x40, // display start line
0xa4, // display memory
0xa6, // no inverse
0x2e, // no scroll
0xaf, // on
};
/* Set display model. Must be called before init() */
void SSD1306_Adaptor::set_model(SSD1306_Model_t m)
{
switch (m) {
case SSD1306_128x64:
m_height = 64;
m_mode = 0x12;
break;
case SSD1306_128x32:
m_height = 32;
m_mode = 0x2;
break;
default:
;
}
}
bool SSD1306_Adaptor::probe()
{
return wr_cmd(0xE3);
}
void SSD1306_Adaptor::enable(bool on)
{
wr_cmd(0xAE + uint8_t(on));
}
void SSD1306_Adaptor::init()
{
uint8_t hmode_cmds[] = {0xa8, (uint8_t)(m_height-1), 0xda, m_mode};
wr_cmds(s_init_cmds, sizeof(s_init_cmds));
wr_cmds(hmode_cmds, sizeof(hmode_cmds));
clear();
}
void SSD1306_Adaptor::light_all(bool active)
{
wr_cmd(0xA4 + uint8_t(active));
}
void SSD1306_Adaptor::set_inverse(bool active)
{
wr_cmd(0xA6 + uint8_t(active));
}
void SSD1306_Adaptor::set_brightness(uint8_t val)
{
uint8_t cmds[] = {0x81, val};
wr_cmds(cmds, sizeof(cmds));
}
bool SSD1306_Adaptor::wr_start(uint8_t col, uint8_t pg)
{
uint8_t cmds[] = {
(uint8_t)(col & 0xf),
(uint8_t)(0x10 | ((col >> 4) & 0xf)),
(uint8_t)(0xb0 | (pg & 0xf))
};
return wr_cmds(cmds, sizeof(cmds));
}
void SSD1306_Adaptor::write(uint8_t col, uint8_t pg, uint8_t const* data, unsigned len)
{
if (col > DISP_W)
col = DISP_W;
if (col + len > DISP_W)
len = DISP_W - col;
wr_start(col, pg);
wr_data(data, len);
}
void SSD1306_Adaptor::clear_region(uint8_t col, uint8_t pg, uint8_t w, uint8_t h)
{
for (uint8_t p = 0; p < h; ++p) {
write(col, pg + p, NULL, w);
}
}