-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssd1306_font.c
115 lines (100 loc) · 2.12 KB
/
ssd1306_font.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
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <wiringPi.h>
#include "oled_lib.h"
#define OLED_SPI_CS 0
#define OLED_GPIO_RST 12
#define OLED_GPIO_DC 16
/* Show help */
void help(char *progname)
{
printf("Usage:\n\t%s [-u | -a] font string0 ... stringN\n\n", progname);
puts("Where:");
puts("\t-u\t - update screen - do not initialize and clear (optional)");
puts("\t-a\t - display all characters (optional)");
puts("\tfont\t - path to PSF font file - no Unicode, 256 chars (mandatory)");
puts("\tstringN\t - text to display on row N (optional)");
}
/* ********** */
/* * MAIN * */
/* ********** */
int main(int argc, char *argv[])
{
int fd;
int i, n, a, fontid, hb, noclr;
int allch, cw, x, row;
wiringPiSetupGpio();
fd = OLED_initSPI(ADAFRUIT_SSD1306_128_64, OLED_SPI_CS, OLED_GPIO_RST,
OLED_GPIO_DC);
if (fd < 0) {
fprintf(stderr, "Unable to open SPI device: %s\n",
strerror (errno));
exit(-1);
}
if (argc < 2 || !strcmp(argv[1], "--?")) {
help(argv[0]);
exit(0);
}
if (!strcmp(argv[1], "-u")) {
n = 2;
noclr = 1;
allch = 0;
} else if (!strcmp(argv[1], "-a")) {
n = 2;
allch = 1;
noclr = 0;
} else {
n = 1;
noclr = 0;
allch = 0;
}
fontid = OLED_loadPsf(argv[n]);
if (fontid < 0) {
fprintf(stderr, "Unable to open font file: %s\n", argv[n]);
exit(-1);
}
if (allch) {
/* all character dump mode */
OLED_powerOn(fd);
OLED_clearDisplay(fd);
OLED_getFontScreenSize(fontid, NULL, NULL, &cw, NULL, &hb);
x = 0;
row = 0;
for(i = 0; i < 256; i++) {
OLED_putChar(fd, fontid, x, row, 0, i);
x += cw;
if (x >= 128) {
x = 0;
row += hb;
}
if (row >= 8) {
x = 0;
row = 0;
sleep(8);
}
}
} else {
/* display mode */
a = argc - n - 1;
if (a) {
if (a > 8)
a = 8;
OLED_getFontScreenSize(fontid, NULL, NULL,
NULL, NULL, &hb);
if (!noclr) {
OLED_powerOn(fd);
OLED_clearDisplay(fd);
}
for(i = 0; i < a; i++) {
OLED_putString(fd, fontid, 0, i * hb, 0,
argv[i + n + 1]);
}
}
}
return 0;
}