-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssd1315.c
231 lines (204 loc) · 5.91 KB
/
ssd1315.c
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
#include "ssd1315.h"
#include <stdint.h>
#include <string.h>
// TODO: STB-style cat code
#define SSD1315_USE_CAT
#ifdef SSD1315_USE_CAT
#include "ssd1315_cat.h"
#endif
static SSD1315Driver *drv = NULL;
static uint8_t buffer[SSD1315_BUFF_SIZE + 1] = { 0x00 };
void ssd1315_init(SSD1315Driver *driver)
{
if (driver) {
drv = driver;
}
// Default initialization
ssd1315_charge_pump_mode_set(SSD1315_CHARGE_PUMP_MODE7V);
ssd1315_display_on(false);
ssd1315_memory_mode_set(SSD1315_MEM_MODE_HORIZ);
ssd1315_display_start_line_set(0);
ssd1315_com_scan_invert(true);
ssd1315_segment_remap(true);
ssd1315_display_invert(false);
ssd1315_display_on(true);
ssd1315_entire_display_on(false);
drv->delay_ms(100);
memset(buffer, 0x00, sizeof(buffer));
#ifdef SSD1315_USE_CAT
uint8_t* dst = &buffer[1];
for (uint16_t y = 0; y < 64; y++) {
for (uint16_t x = 0; x < 16; x++) {
uint8_t src_pixel = image_data_cat[y * 16 + x];
for (uint8_t i = 0; i < 8; i++) {
uint16_t dst_y = y / 8;
uint16_t dst_x = x * 8 + dst_y * 128;
uint16_t dst_i = dst_x + i;
dst[dst_i] |= ((src_pixel >> (7 - i)) & 0x01) << (y - dst_y * 8);
}
}
}
#endif
// buffer[SSD1315_COL_COUNT * 3 + SSD1315_COL_COUNT / 2] = 0xFF;
for (uint8_t i = 0; i < SSD1315_PAGE_COUNT; i++) {
uint16_t pos = SSD1315_COL_COUNT * i + 1;
buffer[pos - 1] = SSD1315_DATA;
drv->i2c_write(SSD1315_I2C_ADDR, &buffer[pos - 1], SSD1315_COL_COUNT + 1);
}
}
void ssd1315_display_on(bool enable)
{
uint8_t cmd[] = {
SSD1315_CMD,
SSD1315_CMD_DON | (uint8_t)enable
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_entire_display_on(bool enable)
{
uint8_t cmd[] = {
SSD1315_CMD,
SSD1315_CMD_EDON | (uint8_t)enable
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_column_start_address_set(uint8_t addr)
{
uint8_t cmd[] = {
SSD1315_CMD,
(SSD1315_CMD_LCS | (addr & SSD1315_CMD_LCS_MASK))
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
cmd[1] = (SSD1315_CMD_HCS | ((addr >> 4) & SSD1315_CMD_HCS_MASK));
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_display_start_line_set(uint8_t line)
{
uint8_t cmd[] = {
SSD1315_CMD,
(SSD1315_CMD_DSL | (line & SSD1315_CMD_DSL_MASK))
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_memory_mode_set(SSD1315MemMode mode)
{
uint8_t cmd[] = {
SSD1315_CMD,
SSD1315_CMD_MAM,
((uint8_t)mode & SSD1315_CMD_MAM_MASK)
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_page_start_address_set(uint8_t addr)
{
uint8_t cmd[] = {
SSD1315_CMD,
(SSD1315_CMD_PSA | (addr & SSD1315_CMD_PSA_MASK))
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_contrast_set(uint8_t contrast)
{
uint8_t cmd[] = {
SSD1315_CMD,
SSD1315_CMD_CC,
contrast
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_segment_remap(bool remap)
{
uint8_t cmd[] = {
SSD1315_CMD,
SSD1315_CMD_SR | (uint8_t)remap
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_mux_ratio_set(uint8_t mux)
{
mux = (mux < SSD1315_MUX_MIN) ? SSD1315_MUX_MIN : mux;
mux = (mux > SSD1315_MUX_MAX) ? SSD1315_MUX_MAX : mux;
uint8_t cmd[] = {SSD1315_CMD, SSD1315_CMD_MR, mux};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_com_scan_invert(bool invert)
{
uint8_t cmd[] = {
SSD1315_CMD,
SSD1315_CMD_COSD | ((uint8_t)invert << SSD1315_CMD_COSD_POS)
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_display_offset_set(uint8_t offset)
{
uint8_t cmd[] = {SSD1315_CMD, SSD1315_CMD_DO, offset};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_clock_set(uint8_t freq, uint8_t divider)
{
uint8_t cmd[] = {
SSD1315_CMD,
SSD1315_CMD_DCDR,
(divider & SSD1315_CMD_DCDR_DIV_MASK) |
((freq << SSD1315_CMD_DCDR_CLK_POS) & SSD1315_CMD_DCDR_CLK_MASK)
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_precharge_period_set(uint8_t period)
{
uint8_t cmd[] = {SSD1315_CMD, SSD1315_CMD_PCP, period};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_com_pins_set(bool alternative, bool left_right_remap)
{
uint8_t cmd[] = {
SSD1315_CMD,
SSD1315_CMD_CPHC,
SSD1315_CMD_CPHC_VALUE
| (uint8_t)alternative << SSD1315_CMD_CPHC_CONF_POS
| (uint8_t)left_right_remap << SSD1315_CMD_CPHC_MAP_POS
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_vcomh_level_set(SSD1315VcomLevel level)
{
uint8_t cmd[] = {SSD1315_CMD, SSD1315_CMD_VCSL, (uint8_t)level};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_charge_pump_mode_set(SSD1315ChargePumpMode mode)
{
uint8_t cmd[] = {
SSD1315_CMD,
SSD1315_CMD_CPS,
(uint8_t)mode
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_display_invert(bool invert)
{
uint8_t cmd[] = {
SSD1315_CMD,
(SSD1315_CMD_DINV | (uint8_t)invert)
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_column_address_set(uint8_t begin, uint8_t end)
{
uint8_t cmd[] = {
SSD1315_CMD,
SSD1315_CMD_CA,
begin & SSD1315_CMD_CA_MASK,
end & SSD1315_CMD_CA_MASK
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}
void ssd1315_page_address_set(uint8_t begin, uint8_t end)
{
uint8_t cmd[] = {
SSD1315_CMD,
SSD1315_CMD_PA,
begin & SSD1315_CMD_PA_MASK,
end & SSD1315_CMD_PA_MASK
};
drv->i2c_write(SSD1315_I2C_ADDR, cmd, sizeof(cmd));
}