1 // SPDX-License-Identifier: GPL-2.0
3 * Filename: cfag12864b-example.c
5 * Description: cfag12864b LCD userspace example program
7 * Author: Copyright (C) Miguel Ojeda <ojeda@kernel.org>
12 * ------------------------
13 * start of cfag12864b code
14 * ------------------------
20 #include <sys/types.h>
24 #define CFAG12864B_WIDTH (128)
25 #define CFAG12864B_HEIGHT (64)
26 #define CFAG12864B_SIZE (128 * 64 / 8)
27 #define CFAG12864B_BPB (8)
28 #define CFAG12864B_ADDRESS(x, y) ((y) * CFAG12864B_WIDTH / \
29 CFAG12864B_BPB + (x) / CFAG12864B_BPB)
30 #define CFAG12864B_BIT(n) (((unsigned char) 1) << (n))
32 #undef CFAG12864B_DOCHECK
33 #ifdef CFAG12864B_DOCHECK
34 #define CFAG12864B_CHECK(x, y) ((x) < CFAG12864B_WIDTH && \
35 (y) < CFAG12864B_HEIGHT)
37 #define CFAG12864B_CHECK(x, y) (1)
41 unsigned char * cfag12864b_mem;
42 unsigned char cfag12864b_buffer[CFAG12864B_SIZE];
45 * init a cfag12864b framebuffer device
47 * No error: return = 0
48 * Unable to open: return = -1
49 * Unable to mmap: return = -2
51 static int cfag12864b_init(char *path)
53 cfag12864b_fd = open(path, O_RDWR);
54 if (cfag12864b_fd == -1)
57 cfag12864b_mem = mmap(0, CFAG12864B_SIZE, PROT_READ | PROT_WRITE,
58 MAP_SHARED, cfag12864b_fd, 0);
59 if (cfag12864b_mem == MAP_FAILED) {
68 * exit a cfag12864b framebuffer device
70 static void cfag12864b_exit(void)
72 munmap(cfag12864b_mem, CFAG12864B_SIZE);
79 static void cfag12864b_set(unsigned char x, unsigned char y)
81 if (CFAG12864B_CHECK(x, y))
82 cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] |=
83 CFAG12864B_BIT(x % CFAG12864B_BPB);
89 static void cfag12864b_unset(unsigned char x, unsigned char y)
91 if (CFAG12864B_CHECK(x, y))
92 cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &=
93 ~CFAG12864B_BIT(x % CFAG12864B_BPB);
97 * is set (x, y) pixel?
99 * Pixel off: return = 0
100 * Pixel on: return = 1
102 static unsigned char cfag12864b_isset(unsigned char x, unsigned char y)
104 if (CFAG12864B_CHECK(x, y))
105 if (cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &
106 CFAG12864B_BIT(x % CFAG12864B_BPB))
115 static void cfag12864b_not(unsigned char x, unsigned char y)
117 if (cfag12864b_isset(x, y))
118 cfag12864b_unset(x, y);
120 cfag12864b_set(x, y);
124 * fill (set all pixels)
126 static void cfag12864b_fill(void)
130 for (i = 0; i < CFAG12864B_SIZE; i++)
131 cfag12864b_buffer[i] = 0xFF;
135 * clear (unset all pixels)
137 static void cfag12864b_clear(void)
141 for (i = 0; i < CFAG12864B_SIZE; i++)
142 cfag12864b_buffer[i] = 0;
146 * format a [128*64] matrix
148 * Pixel off: src[i] = 0
149 * Pixel on: src[i] > 0
151 static void cfag12864b_format(unsigned char * matrix)
153 unsigned char i, j, n;
155 for (i = 0; i < CFAG12864B_HEIGHT; i++)
156 for (j = 0; j < CFAG12864B_WIDTH / CFAG12864B_BPB; j++) {
157 cfag12864b_buffer[i * CFAG12864B_WIDTH / CFAG12864B_BPB +
159 for (n = 0; n < CFAG12864B_BPB; n++)
160 if (matrix[i * CFAG12864B_WIDTH +
161 j * CFAG12864B_BPB + n])
162 cfag12864b_buffer[i * CFAG12864B_WIDTH /
163 CFAG12864B_BPB + j] |=
171 static void cfag12864b_blit(void)
173 memcpy(cfag12864b_mem, cfag12864b_buffer, CFAG12864B_SIZE);
177 * ----------------------
178 * end of cfag12864b code
179 * ----------------------
186 static void example(unsigned char n)
189 unsigned char matrix[CFAG12864B_WIDTH * CFAG12864B_HEIGHT];
194 printf("Example %i/%i - ", n, EXAMPLES);
198 printf("Draw points setting bits");
200 for (i = 0; i < CFAG12864B_WIDTH; i += 2)
201 for (j = 0; j < CFAG12864B_HEIGHT; j += 2)
202 cfag12864b_set(i, j);
206 printf("Clear the LCD");
211 printf("Draw rows formatting a [128*64] matrix");
212 memset(matrix, 0, CFAG12864B_WIDTH * CFAG12864B_HEIGHT);
213 for (i = 0; i < CFAG12864B_WIDTH; i++)
214 for (j = 0; j < CFAG12864B_HEIGHT; j += 2)
215 matrix[j * CFAG12864B_WIDTH + i] = 1;
216 cfag12864b_format(matrix);
220 printf("Fill the lcd");
225 printf("Draw columns unsetting bits");
226 for (i = 0; i < CFAG12864B_WIDTH; i += 2)
227 for (j = 0; j < CFAG12864B_HEIGHT; j++)
228 cfag12864b_unset(i, j);
232 printf("Do negative not-ing all bits");
233 for (i = 0; i < CFAG12864B_WIDTH; i++)
234 for (j = 0; j < CFAG12864B_HEIGHT; j ++)
235 cfag12864b_not(i, j);
239 puts(" - [Press Enter]");
242 int main(int argc, char *argv[])
249 "Usually: /dev/fb0, /dev/fb1...\n", argv[0]);
253 if (cfag12864b_init(argv[1])) {
254 printf("Can't init %s fbdev\n", argv[1]);
258 for (n = 1; n <= EXAMPLES; n++) {
261 while (getchar() != '\n');