Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *    Filename: cfag12864b-example.c
0004  *     Version: 0.1.0
0005  * Description: cfag12864b LCD userspace example program
0006  *
0007  *      Author: Copyright (C) Miguel Ojeda <ojeda@kernel.org>
0008  *        Date: 2006-10-31
0009  */
0010 
0011 /*
0012  * ------------------------
0013  * start of cfag12864b code
0014  * ------------------------
0015  */
0016 
0017 #include <string.h>
0018 #include <fcntl.h>
0019 #include <unistd.h>
0020 #include <sys/types.h>
0021 #include <sys/stat.h>
0022 #include <sys/mman.h>
0023 
0024 #define CFAG12864B_WIDTH        (128)
0025 #define CFAG12864B_HEIGHT       (64)
0026 #define CFAG12864B_SIZE         (128 * 64 / 8)
0027 #define CFAG12864B_BPB          (8)
0028 #define CFAG12864B_ADDRESS(x, y)    ((y) * CFAG12864B_WIDTH / \
0029                     CFAG12864B_BPB + (x) / CFAG12864B_BPB)
0030 #define CFAG12864B_BIT(n)       (((unsigned char) 1) << (n))
0031 
0032 #undef CFAG12864B_DOCHECK
0033 #ifdef CFAG12864B_DOCHECK
0034     #define CFAG12864B_CHECK(x, y)      ((x) < CFAG12864B_WIDTH && \
0035                         (y) < CFAG12864B_HEIGHT)
0036 #else
0037     #define CFAG12864B_CHECK(x, y)      (1)
0038 #endif
0039 
0040 int cfag12864b_fd;
0041 unsigned char * cfag12864b_mem;
0042 unsigned char cfag12864b_buffer[CFAG12864B_SIZE];
0043 
0044 /*
0045  * init a cfag12864b framebuffer device
0046  *
0047  * No error:       return = 0
0048  * Unable to open: return = -1
0049  * Unable to mmap: return = -2
0050  */
0051 static int cfag12864b_init(char *path)
0052 {
0053     cfag12864b_fd = open(path, O_RDWR);
0054     if (cfag12864b_fd == -1)
0055         return -1;
0056 
0057     cfag12864b_mem = mmap(0, CFAG12864B_SIZE, PROT_READ | PROT_WRITE,
0058         MAP_SHARED, cfag12864b_fd, 0);
0059     if (cfag12864b_mem == MAP_FAILED) {
0060         close(cfag12864b_fd);
0061         return -2;
0062     }
0063 
0064     return 0;
0065 }
0066 
0067 /*
0068  * exit a cfag12864b framebuffer device
0069  */
0070 static void cfag12864b_exit(void)
0071 {
0072     munmap(cfag12864b_mem, CFAG12864B_SIZE);
0073     close(cfag12864b_fd);
0074 }
0075 
0076 /*
0077  * set (x, y) pixel
0078  */
0079 static void cfag12864b_set(unsigned char x, unsigned char y)
0080 {
0081     if (CFAG12864B_CHECK(x, y))
0082         cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] |=
0083             CFAG12864B_BIT(x % CFAG12864B_BPB);
0084 }
0085 
0086 /*
0087  * unset (x, y) pixel
0088  */
0089 static void cfag12864b_unset(unsigned char x, unsigned char y)
0090 {
0091     if (CFAG12864B_CHECK(x, y))
0092         cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &=
0093             ~CFAG12864B_BIT(x % CFAG12864B_BPB);
0094 }
0095 
0096 /*
0097  * is set (x, y) pixel?
0098  *
0099  * Pixel off: return = 0
0100  * Pixel on:  return = 1
0101  */
0102 static unsigned char cfag12864b_isset(unsigned char x, unsigned char y)
0103 {
0104     if (CFAG12864B_CHECK(x, y))
0105         if (cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &
0106             CFAG12864B_BIT(x % CFAG12864B_BPB))
0107             return 1;
0108 
0109     return 0;
0110 }
0111 
0112 /*
0113  * not (x, y) pixel
0114  */
0115 static void cfag12864b_not(unsigned char x, unsigned char y)
0116 {
0117     if (cfag12864b_isset(x, y))
0118         cfag12864b_unset(x, y);
0119     else
0120         cfag12864b_set(x, y);
0121 }
0122 
0123 /*
0124  * fill (set all pixels)
0125  */
0126 static void cfag12864b_fill(void)
0127 {
0128     unsigned short i;
0129 
0130     for (i = 0; i < CFAG12864B_SIZE; i++)
0131         cfag12864b_buffer[i] = 0xFF;
0132 }
0133 
0134 /*
0135  * clear (unset all pixels)
0136  */
0137 static void cfag12864b_clear(void)
0138 {
0139     unsigned short i;
0140 
0141     for (i = 0; i < CFAG12864B_SIZE; i++)
0142         cfag12864b_buffer[i] = 0;
0143 }
0144 
0145 /*
0146  * format a [128*64] matrix
0147  *
0148  * Pixel off: src[i] = 0
0149  * Pixel on:  src[i] > 0
0150  */
0151 static void cfag12864b_format(unsigned char * matrix)
0152 {
0153     unsigned char i, j, n;
0154 
0155     for (i = 0; i < CFAG12864B_HEIGHT; i++)
0156     for (j = 0; j < CFAG12864B_WIDTH / CFAG12864B_BPB; j++) {
0157         cfag12864b_buffer[i * CFAG12864B_WIDTH / CFAG12864B_BPB +
0158             j] = 0;
0159         for (n = 0; n < CFAG12864B_BPB; n++)
0160             if (matrix[i * CFAG12864B_WIDTH +
0161                 j * CFAG12864B_BPB + n])
0162                 cfag12864b_buffer[i * CFAG12864B_WIDTH /
0163                     CFAG12864B_BPB + j] |=
0164                     CFAG12864B_BIT(n);
0165     }
0166 }
0167 
0168 /*
0169  * blit buffer to lcd
0170  */
0171 static void cfag12864b_blit(void)
0172 {
0173     memcpy(cfag12864b_mem, cfag12864b_buffer, CFAG12864B_SIZE);
0174 }
0175 
0176 /*
0177  * ----------------------
0178  * end of cfag12864b code
0179  * ----------------------
0180  */
0181 
0182 #include <stdio.h>
0183 
0184 #define EXAMPLES    6
0185 
0186 static void example(unsigned char n)
0187 {
0188     unsigned short i, j;
0189     unsigned char matrix[CFAG12864B_WIDTH * CFAG12864B_HEIGHT];
0190 
0191     if (n > EXAMPLES)
0192         return;
0193 
0194     printf("Example %i/%i - ", n, EXAMPLES);
0195 
0196     switch (n) {
0197     case 1:
0198         printf("Draw points setting bits");
0199         cfag12864b_clear();
0200         for (i = 0; i < CFAG12864B_WIDTH; i += 2)
0201             for (j = 0; j < CFAG12864B_HEIGHT; j += 2)
0202                 cfag12864b_set(i, j);
0203         break;
0204 
0205     case 2:
0206         printf("Clear the LCD");
0207         cfag12864b_clear();
0208         break;
0209 
0210     case 3:
0211         printf("Draw rows formatting a [128*64] matrix");
0212         memset(matrix, 0, CFAG12864B_WIDTH * CFAG12864B_HEIGHT);
0213         for (i = 0; i < CFAG12864B_WIDTH; i++)
0214             for (j = 0; j < CFAG12864B_HEIGHT; j += 2)
0215                 matrix[j * CFAG12864B_WIDTH + i] = 1;
0216         cfag12864b_format(matrix);
0217         break;
0218 
0219     case 4:
0220         printf("Fill the lcd");
0221         cfag12864b_fill();
0222         break;
0223 
0224     case 5:
0225         printf("Draw columns unsetting bits");
0226         for (i = 0; i < CFAG12864B_WIDTH; i += 2)
0227             for (j = 0; j < CFAG12864B_HEIGHT; j++)
0228                 cfag12864b_unset(i, j);
0229         break;
0230 
0231     case 6:
0232         printf("Do negative not-ing all bits");
0233         for (i = 0; i < CFAG12864B_WIDTH; i++)
0234             for (j = 0; j < CFAG12864B_HEIGHT; j ++)
0235                 cfag12864b_not(i, j);
0236         break;
0237     }
0238 
0239     puts(" - [Press Enter]");
0240 }
0241 
0242 int main(int argc, char *argv[])
0243 {
0244     unsigned char n;
0245 
0246     if (argc != 2) {
0247         printf(
0248             "Syntax:  %s fbdev\n"
0249             "Usually: /dev/fb0, /dev/fb1...\n", argv[0]);
0250         return -1;
0251     }
0252 
0253     if (cfag12864b_init(argv[1])) {
0254         printf("Can't init %s fbdev\n", argv[1]);
0255         return -2;
0256     }
0257 
0258     for (n = 1; n <= EXAMPLES; n++) {
0259         example(n);
0260         cfag12864b_blit();
0261         while (getchar() != '\n');
0262     }
0263 
0264     cfag12864b_exit();
0265 
0266     return 0;
0267 }