Blame view

Documentation/auxdisplay/cfag12864b-example.c 5.81 KB
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
1
2
3
4
5
6
  /*
   *    Filename: cfag12864b-example.c
   *     Version: 0.1.0
   * Description: cfag12864b LCD userspace example program
   *     License: GPLv2
   *
450c622e9   Miguel Ojeda   Miguel Ojeda has ...
7
   *      Author: Copyright (C) Miguel Ojeda Sandonis
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
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
   *        Date: 2006-10-31
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License version 2 as
   *  published by the Free Software Foundation.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; if not, write to the Free Software
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   *
   */
  
  /*
   * ------------------------
   * start of cfag12864b code
   * ------------------------
   */
  
  #include <string.h>
  #include <fcntl.h>
  #include <unistd.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <sys/mman.h>
  
  #define CFAG12864B_WIDTH		(128)
  #define CFAG12864B_HEIGHT		(64)
  #define CFAG12864B_SIZE			(128 * 64 / 8)
  #define CFAG12864B_BPB			(8)
  #define CFAG12864B_ADDRESS(x, y)	((y) * CFAG12864B_WIDTH / \
  					CFAG12864B_BPB + (x) / CFAG12864B_BPB)
  #define CFAG12864B_BIT(n)		(((unsigned char) 1) << (n))
  
  #undef CFAG12864B_DOCHECK
  #ifdef CFAG12864B_DOCHECK
  	#define CFAG12864B_CHECK(x, y)		((x) < CFAG12864B_WIDTH && \
  						(y) < CFAG12864B_HEIGHT)
  #else
  	#define CFAG12864B_CHECK(x, y)		(1)
  #endif
  
  int cfag12864b_fd;
  unsigned char * cfag12864b_mem;
  unsigned char cfag12864b_buffer[CFAG12864B_SIZE];
  
  /*
   * init a cfag12864b framebuffer device
   *
   * No error:       return = 0
   * Unable to open: return = -1
   * Unable to mmap: return = -2
   */
b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
65
  static int cfag12864b_init(char *path)
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  {
  	cfag12864b_fd = open(path, O_RDWR);
  	if (cfag12864b_fd == -1)
  		return -1;
  
  	cfag12864b_mem = mmap(0, CFAG12864B_SIZE, PROT_READ | PROT_WRITE,
  		MAP_SHARED, cfag12864b_fd, 0);
  	if (cfag12864b_mem == MAP_FAILED) {
  		close(cfag12864b_fd);
  		return -2;
  	}
  
  	return 0;
  }
  
  /*
   * exit a cfag12864b framebuffer device
   */
b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
84
  static void cfag12864b_exit(void)
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
85
86
87
88
89
90
91
92
  {
  	munmap(cfag12864b_mem, CFAG12864B_SIZE);
  	close(cfag12864b_fd);
  }
  
  /*
   * set (x, y) pixel
   */
b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
93
  static void cfag12864b_set(unsigned char x, unsigned char y)
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
94
95
96
97
98
99
100
101
102
  {
  	if (CFAG12864B_CHECK(x, y))
  		cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] |=
  			CFAG12864B_BIT(x % CFAG12864B_BPB);
  }
  
  /*
   * unset (x, y) pixel
   */
b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
103
  static void cfag12864b_unset(unsigned char x, unsigned char y)
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
104
105
106
107
108
109
110
111
112
113
114
115
  {
  	if (CFAG12864B_CHECK(x, y))
  		cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &=
  			~CFAG12864B_BIT(x % CFAG12864B_BPB);
  }
  
  /*
   * is set (x, y) pixel?
   *
   * Pixel off: return = 0
   * Pixel on:  return = 1
   */
b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
116
  static unsigned char cfag12864b_isset(unsigned char x, unsigned char y)
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
117
118
119
120
121
122
123
124
125
126
127
128
  {
  	if (CFAG12864B_CHECK(x, y))
  		if (cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &
  			CFAG12864B_BIT(x % CFAG12864B_BPB))
  			return 1;
  
  	return 0;
  }
  
  /*
   * not (x, y) pixel
   */
b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
129
  static void cfag12864b_not(unsigned char x, unsigned char y)
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
130
131
132
133
134
135
136
137
138
139
  {
  	if (cfag12864b_isset(x, y))
  		cfag12864b_unset(x, y);
  	else
  		cfag12864b_set(x, y);
  }
  
  /*
   * fill (set all pixels)
   */
b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
140
  static void cfag12864b_fill(void)
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
141
142
143
144
145
146
147
148
149
150
  {
  	unsigned short i;
  
  	for (i = 0; i < CFAG12864B_SIZE; i++)
  		cfag12864b_buffer[i] = 0xFF;
  }
  
  /*
   * clear (unset all pixels)
   */
b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
151
  static void cfag12864b_clear(void)
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
152
153
154
155
156
157
158
159
160
161
162
163
164
  {
  	unsigned short i;
  
  	for (i = 0; i < CFAG12864B_SIZE; i++)
  		cfag12864b_buffer[i] = 0;
  }
  
  /*
   * format a [128*64] matrix
   *
   * Pixel off: src[i] = 0
   * Pixel on:  src[i] > 0
   */
b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
165
  static void cfag12864b_format(unsigned char * matrix)
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  {
  	unsigned char i, j, n;
  
  	for (i = 0; i < CFAG12864B_HEIGHT; i++)
  	for (j = 0; j < CFAG12864B_WIDTH / CFAG12864B_BPB; j++) {
  		cfag12864b_buffer[i * CFAG12864B_WIDTH / CFAG12864B_BPB +
  			j] = 0;
  		for (n = 0; n < CFAG12864B_BPB; n++)
  			if (matrix[i * CFAG12864B_WIDTH +
  				j * CFAG12864B_BPB + n])
  				cfag12864b_buffer[i * CFAG12864B_WIDTH /
  					CFAG12864B_BPB + j] |=
  					CFAG12864B_BIT(n);
  	}
  }
  
  /*
   * blit buffer to lcd
   */
b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
185
  static void cfag12864b_blit(void)
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
186
187
188
189
190
191
192
193
194
195
196
  {
  	memcpy(cfag12864b_mem, cfag12864b_buffer, CFAG12864B_SIZE);
  }
  
  /*
   * ----------------------
   * end of cfag12864b code
   * ----------------------
   */
  
  #include <stdio.h>
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
197
198
  
  #define EXAMPLES	6
b7ed698cc   Ladinu Chandrasinghe   Documentation/: f...
199
  static void example(unsigned char n)
70e840499   Miguel Ojeda Sandonis   [PATCH] drivers: ...
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
  {
  	unsigned short i, j;
  	unsigned char matrix[CFAG12864B_WIDTH * CFAG12864B_HEIGHT];
  
  	if (n > EXAMPLES)
  		return;
  
  	printf("Example %i/%i - ", n, EXAMPLES);
  
  	switch (n) {
  	case 1:
  		printf("Draw points setting bits");
  		cfag12864b_clear();
  		for (i = 0; i < CFAG12864B_WIDTH; i += 2)
  			for (j = 0; j < CFAG12864B_HEIGHT; j += 2)
  				cfag12864b_set(i, j);
  		break;
  
  	case 2:
  		printf("Clear the LCD");
  		cfag12864b_clear();
  		break;
  
  	case 3:
  		printf("Draw rows formatting a [128*64] matrix");
  		memset(matrix, 0, CFAG12864B_WIDTH * CFAG12864B_HEIGHT);
  		for (i = 0; i < CFAG12864B_WIDTH; i++)
  			for (j = 0; j < CFAG12864B_HEIGHT; j += 2)
  				matrix[j * CFAG12864B_WIDTH + i] = 1;
  		cfag12864b_format(matrix);
  		break;
  
  	case 4:
  		printf("Fill the lcd");
  		cfag12864b_fill();
  		break;
  
  	case 5:
  		printf("Draw columns unsetting bits");
  		for (i = 0; i < CFAG12864B_WIDTH; i += 2)
  			for (j = 0; j < CFAG12864B_HEIGHT; j++)
  				cfag12864b_unset(i, j);
  		break;
  
  	case 6:
  		printf("Do negative not-ing all bits");
  		for (i = 0; i < CFAG12864B_WIDTH; i++)
  			for (j = 0; j < CFAG12864B_HEIGHT; j ++)
  				cfag12864b_not(i, j);
  		break;
  	}
  
  	puts(" - [Press Enter]");
  }
  
  int main(int argc, char *argv[])
  {
  	unsigned char n;
  
  	if (argc != 2) {
  		printf(
  			"Sintax:  %s fbdev
  "
  			"Usually: /dev/fb0, /dev/fb1...
  ", argv[0]);
  		return -1;
  	}
  
  	if (cfag12864b_init(argv[1])) {
  		printf("Can't init %s fbdev
  ", argv[1]);
  		return -2;
  	}
  
  	for (n = 1; n <= EXAMPLES; n++) {
  		example(n);
  		cfag12864b_blit();
  		while (getchar() != '
  ');
  	}
  
  	cfag12864b_exit();
  
  	return 0;
  }