1 // SPDX-License-Identifier: GPL-2.0 << 2 /* 1 /* 3 * Filename: cfag12864b-example.c 2 * Filename: cfag12864b-example.c 4 * Version: 0.1.0 3 * Version: 0.1.0 5 * Description: cfag12864b LCD userspace examp 4 * Description: cfag12864b LCD userspace example program >> 5 * License: GPLv2 6 * 6 * 7 * Author: Copyright (C) Miguel Ojeda <oj !! 7 * Author: Copyright (C) Miguel Ojeda Sandonis 8 * Date: 2006-10-31 8 * Date: 2006-10-31 >> 9 * >> 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. >> 13 * >> 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. >> 18 * >> 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 >> 22 * 9 */ 23 */ 10 24 11 /* 25 /* 12 * ------------------------ 26 * ------------------------ 13 * start of cfag12864b code 27 * start of cfag12864b code 14 * ------------------------ 28 * ------------------------ 15 */ 29 */ 16 30 17 #include <string.h> 31 #include <string.h> 18 #include <fcntl.h> 32 #include <fcntl.h> 19 #include <unistd.h> 33 #include <unistd.h> 20 #include <sys/types.h> 34 #include <sys/types.h> 21 #include <sys/stat.h> 35 #include <sys/stat.h> 22 #include <sys/mman.h> 36 #include <sys/mman.h> 23 37 24 #define CFAG12864B_WIDTH (128) 38 #define CFAG12864B_WIDTH (128) 25 #define CFAG12864B_HEIGHT (64) 39 #define CFAG12864B_HEIGHT (64) 26 #define CFAG12864B_SIZE (128 * 40 #define CFAG12864B_SIZE (128 * 64 / 8) 27 #define CFAG12864B_BPB (8) 41 #define CFAG12864B_BPB (8) 28 #define CFAG12864B_ADDRESS(x, y) ((y) * 42 #define CFAG12864B_ADDRESS(x, y) ((y) * CFAG12864B_WIDTH / \ 29 CFAG12 43 CFAG12864B_BPB + (x) / CFAG12864B_BPB) 30 #define CFAG12864B_BIT(n) (((uns 44 #define CFAG12864B_BIT(n) (((unsigned char) 1) << (n)) 31 45 32 #undef CFAG12864B_DOCHECK 46 #undef CFAG12864B_DOCHECK 33 #ifdef CFAG12864B_DOCHECK 47 #ifdef CFAG12864B_DOCHECK 34 #define CFAG12864B_CHECK(x, y) 48 #define CFAG12864B_CHECK(x, y) ((x) < CFAG12864B_WIDTH && \ 35 49 (y) < CFAG12864B_HEIGHT) 36 #else 50 #else 37 #define CFAG12864B_CHECK(x, y) 51 #define CFAG12864B_CHECK(x, y) (1) 38 #endif 52 #endif 39 53 40 int cfag12864b_fd; 54 int cfag12864b_fd; 41 unsigned char * cfag12864b_mem; 55 unsigned char * cfag12864b_mem; 42 unsigned char cfag12864b_buffer[CFAG12864B_SIZ 56 unsigned char cfag12864b_buffer[CFAG12864B_SIZE]; 43 57 44 /* 58 /* 45 * init a cfag12864b framebuffer device 59 * init a cfag12864b framebuffer device 46 * 60 * 47 * No error: return = 0 61 * No error: return = 0 48 * Unable to open: return = -1 62 * Unable to open: return = -1 49 * Unable to mmap: return = -2 63 * Unable to mmap: return = -2 50 */ 64 */ 51 static int cfag12864b_init(char *path) 65 static int cfag12864b_init(char *path) 52 { 66 { 53 cfag12864b_fd = open(path, O_RDWR); 67 cfag12864b_fd = open(path, O_RDWR); 54 if (cfag12864b_fd == -1) 68 if (cfag12864b_fd == -1) 55 return -1; 69 return -1; 56 70 57 cfag12864b_mem = mmap(0, CFAG12864B_SI 71 cfag12864b_mem = mmap(0, CFAG12864B_SIZE, PROT_READ | PROT_WRITE, 58 MAP_SHARED, cfag12864b_fd, 0); 72 MAP_SHARED, cfag12864b_fd, 0); 59 if (cfag12864b_mem == MAP_FAILED) { 73 if (cfag12864b_mem == MAP_FAILED) { 60 close(cfag12864b_fd); 74 close(cfag12864b_fd); 61 return -2; 75 return -2; 62 } 76 } 63 77 64 return 0; 78 return 0; 65 } 79 } 66 80 67 /* 81 /* 68 * exit a cfag12864b framebuffer device 82 * exit a cfag12864b framebuffer device 69 */ 83 */ 70 static void cfag12864b_exit(void) 84 static void cfag12864b_exit(void) 71 { 85 { 72 munmap(cfag12864b_mem, CFAG12864B_SIZE 86 munmap(cfag12864b_mem, CFAG12864B_SIZE); 73 close(cfag12864b_fd); 87 close(cfag12864b_fd); 74 } 88 } 75 89 76 /* 90 /* 77 * set (x, y) pixel 91 * set (x, y) pixel 78 */ 92 */ 79 static void cfag12864b_set(unsigned char x, un 93 static void cfag12864b_set(unsigned char x, unsigned char y) 80 { 94 { 81 if (CFAG12864B_CHECK(x, y)) 95 if (CFAG12864B_CHECK(x, y)) 82 cfag12864b_buffer[CFAG12864B_A 96 cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] |= 83 CFAG12864B_BIT(x % CFA 97 CFAG12864B_BIT(x % CFAG12864B_BPB); 84 } 98 } 85 99 86 /* 100 /* 87 * unset (x, y) pixel 101 * unset (x, y) pixel 88 */ 102 */ 89 static void cfag12864b_unset(unsigned char x, 103 static void cfag12864b_unset(unsigned char x, unsigned char y) 90 { 104 { 91 if (CFAG12864B_CHECK(x, y)) 105 if (CFAG12864B_CHECK(x, y)) 92 cfag12864b_buffer[CFAG12864B_A 106 cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &= 93 ~CFAG12864B_BIT(x % CF 107 ~CFAG12864B_BIT(x % CFAG12864B_BPB); 94 } 108 } 95 109 96 /* 110 /* 97 * is set (x, y) pixel? 111 * is set (x, y) pixel? 98 * 112 * 99 * Pixel off: return = 0 113 * Pixel off: return = 0 100 * Pixel on: return = 1 114 * Pixel on: return = 1 101 */ 115 */ 102 static unsigned char cfag12864b_isset(unsigned 116 static unsigned char cfag12864b_isset(unsigned char x, unsigned char y) 103 { 117 { 104 if (CFAG12864B_CHECK(x, y)) 118 if (CFAG12864B_CHECK(x, y)) 105 if (cfag12864b_buffer[CFAG1286 119 if (cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] & 106 CFAG12864B_BIT(x % CFA 120 CFAG12864B_BIT(x % CFAG12864B_BPB)) 107 return 1; 121 return 1; 108 122 109 return 0; 123 return 0; 110 } 124 } 111 125 112 /* 126 /* 113 * not (x, y) pixel 127 * not (x, y) pixel 114 */ 128 */ 115 static void cfag12864b_not(unsigned char x, un 129 static void cfag12864b_not(unsigned char x, unsigned char y) 116 { 130 { 117 if (cfag12864b_isset(x, y)) 131 if (cfag12864b_isset(x, y)) 118 cfag12864b_unset(x, y); 132 cfag12864b_unset(x, y); 119 else 133 else 120 cfag12864b_set(x, y); 134 cfag12864b_set(x, y); 121 } 135 } 122 136 123 /* 137 /* 124 * fill (set all pixels) 138 * fill (set all pixels) 125 */ 139 */ 126 static void cfag12864b_fill(void) 140 static void cfag12864b_fill(void) 127 { 141 { 128 unsigned short i; 142 unsigned short i; 129 143 130 for (i = 0; i < CFAG12864B_SIZE; i++) 144 for (i = 0; i < CFAG12864B_SIZE; i++) 131 cfag12864b_buffer[i] = 0xFF; 145 cfag12864b_buffer[i] = 0xFF; 132 } 146 } 133 147 134 /* 148 /* 135 * clear (unset all pixels) 149 * clear (unset all pixels) 136 */ 150 */ 137 static void cfag12864b_clear(void) 151 static void cfag12864b_clear(void) 138 { 152 { 139 unsigned short i; 153 unsigned short i; 140 154 141 for (i = 0; i < CFAG12864B_SIZE; i++) 155 for (i = 0; i < CFAG12864B_SIZE; i++) 142 cfag12864b_buffer[i] = 0; 156 cfag12864b_buffer[i] = 0; 143 } 157 } 144 158 145 /* 159 /* 146 * format a [128*64] matrix 160 * format a [128*64] matrix 147 * 161 * 148 * Pixel off: src[i] = 0 162 * Pixel off: src[i] = 0 149 * Pixel on: src[i] > 0 163 * Pixel on: src[i] > 0 150 */ 164 */ 151 static void cfag12864b_format(unsigned char * 165 static void cfag12864b_format(unsigned char * matrix) 152 { 166 { 153 unsigned char i, j, n; 167 unsigned char i, j, n; 154 168 155 for (i = 0; i < CFAG12864B_HEIGHT; i++ 169 for (i = 0; i < CFAG12864B_HEIGHT; i++) 156 for (j = 0; j < CFAG12864B_WIDTH / CFA 170 for (j = 0; j < CFAG12864B_WIDTH / CFAG12864B_BPB; j++) { 157 cfag12864b_buffer[i * CFAG1286 171 cfag12864b_buffer[i * CFAG12864B_WIDTH / CFAG12864B_BPB + 158 j] = 0; 172 j] = 0; 159 for (n = 0; n < CFAG12864B_BPB 173 for (n = 0; n < CFAG12864B_BPB; n++) 160 if (matrix[i * CFAG128 174 if (matrix[i * CFAG12864B_WIDTH + 161 j * CFAG12864B 175 j * CFAG12864B_BPB + n]) 162 cfag12864b_buf 176 cfag12864b_buffer[i * CFAG12864B_WIDTH / 163 CFAG12 177 CFAG12864B_BPB + j] |= 164 CFAG12 178 CFAG12864B_BIT(n); 165 } 179 } 166 } 180 } 167 181 168 /* 182 /* 169 * blit buffer to lcd 183 * blit buffer to lcd 170 */ 184 */ 171 static void cfag12864b_blit(void) 185 static void cfag12864b_blit(void) 172 { 186 { 173 memcpy(cfag12864b_mem, cfag12864b_buff 187 memcpy(cfag12864b_mem, cfag12864b_buffer, CFAG12864B_SIZE); 174 } 188 } 175 189 176 /* 190 /* 177 * ---------------------- 191 * ---------------------- 178 * end of cfag12864b code 192 * end of cfag12864b code 179 * ---------------------- 193 * ---------------------- 180 */ 194 */ 181 195 182 #include <stdio.h> 196 #include <stdio.h> 183 197 184 #define EXAMPLES 6 198 #define EXAMPLES 6 185 199 186 static void example(unsigned char n) 200 static void example(unsigned char n) 187 { 201 { 188 unsigned short i, j; 202 unsigned short i, j; 189 unsigned char matrix[CFAG12864B_WIDTH 203 unsigned char matrix[CFAG12864B_WIDTH * CFAG12864B_HEIGHT]; 190 204 191 if (n > EXAMPLES) 205 if (n > EXAMPLES) 192 return; 206 return; 193 207 194 printf("Example %i/%i - ", n, EXAMPLES 208 printf("Example %i/%i - ", n, EXAMPLES); 195 209 196 switch (n) { 210 switch (n) { 197 case 1: 211 case 1: 198 printf("Draw points setting bi 212 printf("Draw points setting bits"); 199 cfag12864b_clear(); 213 cfag12864b_clear(); 200 for (i = 0; i < CFAG12864B_WID 214 for (i = 0; i < CFAG12864B_WIDTH; i += 2) 201 for (j = 0; j < CFAG12 215 for (j = 0; j < CFAG12864B_HEIGHT; j += 2) 202 cfag12864b_set 216 cfag12864b_set(i, j); 203 break; 217 break; 204 218 205 case 2: 219 case 2: 206 printf("Clear the LCD"); 220 printf("Clear the LCD"); 207 cfag12864b_clear(); 221 cfag12864b_clear(); 208 break; 222 break; 209 223 210 case 3: 224 case 3: 211 printf("Draw rows formatting a 225 printf("Draw rows formatting a [128*64] matrix"); 212 memset(matrix, 0, CFAG12864B_W 226 memset(matrix, 0, CFAG12864B_WIDTH * CFAG12864B_HEIGHT); 213 for (i = 0; i < CFAG12864B_WID 227 for (i = 0; i < CFAG12864B_WIDTH; i++) 214 for (j = 0; j < CFAG12 228 for (j = 0; j < CFAG12864B_HEIGHT; j += 2) 215 matrix[j * CFA 229 matrix[j * CFAG12864B_WIDTH + i] = 1; 216 cfag12864b_format(matrix); 230 cfag12864b_format(matrix); 217 break; 231 break; 218 232 219 case 4: 233 case 4: 220 printf("Fill the lcd"); 234 printf("Fill the lcd"); 221 cfag12864b_fill(); 235 cfag12864b_fill(); 222 break; 236 break; 223 237 224 case 5: 238 case 5: 225 printf("Draw columns unsetting 239 printf("Draw columns unsetting bits"); 226 for (i = 0; i < CFAG12864B_WID 240 for (i = 0; i < CFAG12864B_WIDTH; i += 2) 227 for (j = 0; j < CFAG12 241 for (j = 0; j < CFAG12864B_HEIGHT; j++) 228 cfag12864b_uns 242 cfag12864b_unset(i, j); 229 break; 243 break; 230 244 231 case 6: 245 case 6: 232 printf("Do negative not-ing al 246 printf("Do negative not-ing all bits"); 233 for (i = 0; i < CFAG12864B_WID 247 for (i = 0; i < CFAG12864B_WIDTH; i++) 234 for (j = 0; j < CFAG12 248 for (j = 0; j < CFAG12864B_HEIGHT; j ++) 235 cfag12864b_not 249 cfag12864b_not(i, j); 236 break; 250 break; 237 } 251 } 238 252 239 puts(" - [Press Enter]"); 253 puts(" - [Press Enter]"); 240 } 254 } 241 255 242 int main(int argc, char *argv[]) 256 int main(int argc, char *argv[]) 243 { 257 { 244 unsigned char n; 258 unsigned char n; 245 259 246 if (argc != 2) { 260 if (argc != 2) { 247 printf( 261 printf( 248 "Syntax: %s fbdev\n" !! 262 "Sintax: %s fbdev\n" 249 "Usually: /dev/fb0, /d 263 "Usually: /dev/fb0, /dev/fb1...\n", argv[0]); 250 return -1; 264 return -1; 251 } 265 } 252 266 253 if (cfag12864b_init(argv[1])) { 267 if (cfag12864b_init(argv[1])) { 254 printf("Can't init %s fbdev\n" 268 printf("Can't init %s fbdev\n", argv[1]); 255 return -2; 269 return -2; 256 } 270 } 257 271 258 for (n = 1; n <= EXAMPLES; n++) { 272 for (n = 1; n <= EXAMPLES; n++) { 259 example(n); 273 example(n); 260 cfag12864b_blit(); 274 cfag12864b_blit(); 261 while (getchar() != '\n'); 275 while (getchar() != '\n'); 262 } 276 } 263 277 264 cfag12864b_exit(); 278 cfag12864b_exit(); 265 279 266 return 0; 280 return 0; 267 } 281 } 268 282
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.