-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholed.c
More file actions
executable file
·289 lines (247 loc) · 6.26 KB
/
oled.c
File metadata and controls
executable file
·289 lines (247 loc) · 6.26 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "oled.h"
#include "font.h"
#include <stdarg.h>
#include <string.h>
static int pos_x = 0, pos_y = 0;
/* send command */
void oled_send_cmd(char cmd)
{
IIC_start();
IIC_writebyte(0x78); // Slave address, SA0=0
IIC_writebyte(0x00); // write command
IIC_writebyte(cmd);
IIC_stop();
}
/* send data to RAM */
void oled_send_data(char data)
{
IIC_start();
IIC_writebyte(0x78);
IIC_writebyte(0x40);
IIC_writebyte(data);
IIC_stop();
}
/* oled init */
void oled_init(void)
{
IIC_ioinit();
/* display off */
oled_send_cmd(0xAE);
/* set memory addressing mode */
oled_set_addr_mode(ADDR_MODE_PAGE);
/* Set Page Start Address for Page Addressing Mode */
oled_send_cmd(0xb0); // 0-7
/* Set Common Ouput Scan Direction */
oled_send_cmd(0xc8); // COM[N-1] ~ COM[0]
/* set start line address */
oled_send_cmd(0x40); // 0
/* contrast control register */
oled_send_cmd(0x81);
oled_send_cmd(0x7f);
/* set segment re-map */
oled_send_cmd(0xa1); // reverse direction
/* Set Normal/Reverse Display */
oled_send_cmd(0xa6); // normal
/* set multiplex ratio */
oled_send_cmd(0xa8);
oled_send_cmd(0x3F); // 64
/* Set Entire Display */
oled_send_cmd(0xa4); // (off) Output follows RAM content
/* (14) set display offset */
oled_send_cmd(0xd3);
oled_send_cmd(0x00); // 0
/* (15) set display clock divide ratio/oscillator frequency */
oled_send_cmd(0xd5);
oled_send_cmd(0xf0); // set divide ratio
/* (16) Set Dis-charge/Pre-charge Period */
oled_send_cmd(0xd9);
oled_send_cmd(0x22); // pre-charge: 2 DCLKs (POR) | dis-charge: 2 DCLKs (POR)
/* (17) Set Common pads hardware configuration */
oled_send_cmd(0xda);
oled_send_cmd(0x12); // H, Alternative. (POR)
/* (18) Set VCOM Deselect Level */
oled_send_cmd(0xdb);
oled_send_cmd(0x20); // 0x20, 0.77 x Vcc
// /* (10) set DC-DC enable */
// oled_send_cmd(0x8d);
// oled_send_cmd(0x14);
/* display on */
oled_send_cmd(0xaf);
oled_cls();
oled_set_pos(0, 0);
}
/* set current postion */
void oled_set_pos(int x, int y)
{
oled_send_cmd(0xb0 | y);
oled_send_cmd(((x & 0xf0) >> 4) | 0x10);
oled_send_cmd(((x + 2) & 0x0f)); // unknown bug: "+2" ?
}
/* set postion and (pos_x, pos_y) */
void oled_set_p_pos(int x, int y)
{
pos_x = x, pos_y = y;
oled_set_pos(pos_x, pos_y);
}
/* clear screen */
void oled_cls(void)
{
int y, x;
pos_x = 0, pos_y = 0;
for (y = 0; y < OLED_HEIGHT / 8; y++) {
oled_set_pos(0, y);
for (x = 0; x < OLED_WIDTH; x++)
oled_send_data(0);
}
oled_set_pos(0, 0);
}
/* clear one line */
void oled_clear_line(char line)
{
oled_set_pos(0, line);
for (pos_x = 0; pos_x < OLED_WIDTH; pos_x++)
oled_send_data(0);
oled_set_p_pos(0, line);
}
/* display an image */
void oled_display_img(char *img, int width, int height, int _x, int _y)
{
int x = 0, y = 0;
height /= 8;
for (y = 0; y < height; y++) {
oled_set_pos(_x, y + _y);
for (x = 0; x < width; x++) {
oled_send_data(img[x + y * width]);
}
}
oled_set_pos(pos_x, pos_y);
}
static void __out_char(char c)
{
int x = 0;
if (c < F6x8_OFFSET) return;
c -= F6x8_OFFSET;
for (; x < 6; x++)
oled_send_data(F6x8[c * 6 + x]);
}
/* display one char at (x, y) */
void oled_display_chr(char c, int _x, int _y)
{
oled_set_pos(_x, _y);
__out_char(c);
oled_set_pos(pos_x, pos_y);
}
void oled_display_short(unsigned short num, int x, int y)
{
int i = 4;
char buf[6] = {0};
while (num) {
buf[i--] = num % 10 + 0x30;
num /= 10;
}
while (i >= 0) buf[i--] = '0';
oled_set_pos(x, y);
for (i = 0; i < 5; i++)
__out_char(buf[i]);
oled_set_pos(pos_x, pos_y);
}
void oled_display_str(char *s, int n, int x, int y)
{
oled_set_pos(x, y);
while (n-- && *s) __out_char(*s++);
oled_set_pos(pos_x, pos_y);
}
/* put char */
void oled_putc(char c)
{
int x = 0;
__out_char(c);
pos_x += 6;
}
/* puts */
void oled_puts(char *s, int n)
{
while (n-- && *s)
oled_putc(*s++);
}
char* i16tostr(unsigned int num, char *buf)
{
/* buf -> char[6] */
int i = 4;
buf[5] = 0;
/* 0 */
if (!num) {
buf[0] = '0', buf[1] = 0;
return buf;
}
/* ! 0 */
while (num) {
buf[i--] = num % 10 + 0x30;
num /= 10;
}
return &buf[++i];
}
void __internal_printf(char *fmt, va_list args)
{
char buf[0x20] = {0}, op = 0;
char *p = 0;
union
{
unsigned short vunsigned;
} value;
while ( (op = *fmt++) ) {
/* char cannot be printed */
if (op < 0x20) {
switch (op) {
case '\n':
oled_set_pos(pos_x, ++pos_y);
break;
default:
break;
}
} else if (op == '%') {
op = *fmt++;
switch (op) {
case 'c':
oled_putc(va_arg(args, char));
break;
case 'd':
{
value.vunsigned = va_arg(args, short);
p = i16tostr(value.vunsigned, buf);
oled_puts(p, 6);
break;
}
case 's':
{
p = va_arg(args, char *);
oled_puts(p, 100);
break;
}
default:
break;
}
} else {
oled_putc(op);
}
}
}
void oled_printf(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
__internal_printf(fmt, args);
va_end(args);
}
void oled_printfln(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
__internal_printf(fmt, args);
va_end(args);
while (pos_x < OLED_WIDTH) {
oled_send_data(0);
pos_x++;
}
oled_set_p_pos(0, pos_y + 1);
}