fs: use kmem_cache_zalloc instead
[pv_ops_mirror.git] / Documentation / auxdisplay / cfag12864b-example.c
blob7bfac354d4c9f0181d311a0d80e798fdf502f75c
1 /*
2 * Filename: cfag12864b-example.c
3 * Version: 0.1.0
4 * Description: cfag12864b LCD userspace example program
5 * License: GPLv2
7 * Author: Copyright (C) Miguel Ojeda Sandonis <maxextreme@gmail.com>
8 * Date: 2006-10-31
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * ------------------------
27 * start of cfag12864b code
28 * ------------------------
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/mman.h>
38 #define CFAG12864B_WIDTH (128)
39 #define CFAG12864B_HEIGHT (64)
40 #define CFAG12864B_SIZE (128 * 64 / 8)
41 #define CFAG12864B_BPB (8)
42 #define CFAG12864B_ADDRESS(x, y) ((y) * CFAG12864B_WIDTH / \
43 CFAG12864B_BPB + (x) / CFAG12864B_BPB)
44 #define CFAG12864B_BIT(n) (((unsigned char) 1) << (n))
46 #undef CFAG12864B_DOCHECK
47 #ifdef CFAG12864B_DOCHECK
48 #define CFAG12864B_CHECK(x, y) ((x) < CFAG12864B_WIDTH && \
49 (y) < CFAG12864B_HEIGHT)
50 #else
51 #define CFAG12864B_CHECK(x, y) (1)
52 #endif
54 int cfag12864b_fd;
55 unsigned char * cfag12864b_mem;
56 unsigned char cfag12864b_buffer[CFAG12864B_SIZE];
59 * init a cfag12864b framebuffer device
61 * No error: return = 0
62 * Unable to open: return = -1
63 * Unable to mmap: return = -2
65 int cfag12864b_init(char *path)
67 cfag12864b_fd = open(path, O_RDWR);
68 if (cfag12864b_fd == -1)
69 return -1;
71 cfag12864b_mem = mmap(0, CFAG12864B_SIZE, PROT_READ | PROT_WRITE,
72 MAP_SHARED, cfag12864b_fd, 0);
73 if (cfag12864b_mem == MAP_FAILED) {
74 close(cfag12864b_fd);
75 return -2;
78 return 0;
82 * exit a cfag12864b framebuffer device
84 void cfag12864b_exit(void)
86 munmap(cfag12864b_mem, CFAG12864B_SIZE);
87 close(cfag12864b_fd);
91 * set (x, y) pixel
93 void cfag12864b_set(unsigned char x, unsigned char y)
95 if (CFAG12864B_CHECK(x, y))
96 cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] |=
97 CFAG12864B_BIT(x % CFAG12864B_BPB);
101 * unset (x, y) pixel
103 void cfag12864b_unset(unsigned char x, unsigned char y)
105 if (CFAG12864B_CHECK(x, y))
106 cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &=
107 ~CFAG12864B_BIT(x % CFAG12864B_BPB);
111 * is set (x, y) pixel?
113 * Pixel off: return = 0
114 * Pixel on: return = 1
116 unsigned char cfag12864b_isset(unsigned char x, unsigned char y)
118 if (CFAG12864B_CHECK(x, y))
119 if (cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &
120 CFAG12864B_BIT(x % CFAG12864B_BPB))
121 return 1;
123 return 0;
127 * not (x, y) pixel
129 void cfag12864b_not(unsigned char x, unsigned char y)
131 if (cfag12864b_isset(x, y))
132 cfag12864b_unset(x, y);
133 else
134 cfag12864b_set(x, y);
138 * fill (set all pixels)
140 void cfag12864b_fill(void)
142 unsigned short i;
144 for (i = 0; i < CFAG12864B_SIZE; i++)
145 cfag12864b_buffer[i] = 0xFF;
149 * clear (unset all pixels)
151 void cfag12864b_clear(void)
153 unsigned short i;
155 for (i = 0; i < CFAG12864B_SIZE; i++)
156 cfag12864b_buffer[i] = 0;
160 * format a [128*64] matrix
162 * Pixel off: src[i] = 0
163 * Pixel on: src[i] > 0
165 void cfag12864b_format(unsigned char * matrix)
167 unsigned char i, j, n;
169 for (i = 0; i < CFAG12864B_HEIGHT; i++)
170 for (j = 0; j < CFAG12864B_WIDTH / CFAG12864B_BPB; j++) {
171 cfag12864b_buffer[i * CFAG12864B_WIDTH / CFAG12864B_BPB +
172 j] = 0;
173 for (n = 0; n < CFAG12864B_BPB; n++)
174 if (matrix[i * CFAG12864B_WIDTH +
175 j * CFAG12864B_BPB + n])
176 cfag12864b_buffer[i * CFAG12864B_WIDTH /
177 CFAG12864B_BPB + j] |=
178 CFAG12864B_BIT(n);
183 * blit buffer to lcd
185 void cfag12864b_blit(void)
187 memcpy(cfag12864b_mem, cfag12864b_buffer, CFAG12864B_SIZE);
191 * ----------------------
192 * end of cfag12864b code
193 * ----------------------
196 #include <stdio.h>
197 #include <string.h>
199 #define EXAMPLES 6
201 void example(unsigned char n)
203 unsigned short i, j;
204 unsigned char matrix[CFAG12864B_WIDTH * CFAG12864B_HEIGHT];
206 if (n > EXAMPLES)
207 return;
209 printf("Example %i/%i - ", n, EXAMPLES);
211 switch (n) {
212 case 1:
213 printf("Draw points setting bits");
214 cfag12864b_clear();
215 for (i = 0; i < CFAG12864B_WIDTH; i += 2)
216 for (j = 0; j < CFAG12864B_HEIGHT; j += 2)
217 cfag12864b_set(i, j);
218 break;
220 case 2:
221 printf("Clear the LCD");
222 cfag12864b_clear();
223 break;
225 case 3:
226 printf("Draw rows formatting a [128*64] matrix");
227 memset(matrix, 0, CFAG12864B_WIDTH * CFAG12864B_HEIGHT);
228 for (i = 0; i < CFAG12864B_WIDTH; i++)
229 for (j = 0; j < CFAG12864B_HEIGHT; j += 2)
230 matrix[j * CFAG12864B_WIDTH + i] = 1;
231 cfag12864b_format(matrix);
232 break;
234 case 4:
235 printf("Fill the lcd");
236 cfag12864b_fill();
237 break;
239 case 5:
240 printf("Draw columns unsetting bits");
241 for (i = 0; i < CFAG12864B_WIDTH; i += 2)
242 for (j = 0; j < CFAG12864B_HEIGHT; j++)
243 cfag12864b_unset(i, j);
244 break;
246 case 6:
247 printf("Do negative not-ing all bits");
248 for (i = 0; i < CFAG12864B_WIDTH; i++)
249 for (j = 0; j < CFAG12864B_HEIGHT; j ++)
250 cfag12864b_not(i, j);
251 break;
254 puts(" - [Press Enter]");
257 int main(int argc, char *argv[])
259 unsigned char n;
261 if (argc != 2) {
262 printf(
263 "Sintax: %s fbdev\n"
264 "Usually: /dev/fb0, /dev/fb1...\n", argv[0]);
265 return -1;
268 if (cfag12864b_init(argv[1])) {
269 printf("Can't init %s fbdev\n", argv[1]);
270 return -2;
273 for (n = 1; n <= EXAMPLES; n++) {
274 example(n);
275 cfag12864b_blit();
276 while (getchar() != '\n');
279 cfag12864b_exit();
281 return 0;