Path: blob/master/Documentation/auxdisplay/cfag12864b-example.c
10821 views
/*1* Filename: cfag12864b-example.c2* Version: 0.1.03* Description: cfag12864b LCD userspace example program4* License: GPLv25*6* Author: Copyright (C) Miguel Ojeda Sandonis7* Date: 2006-10-318*9* This program is free software; you can redistribute it and/or modify10* it under the terms of the GNU General Public License version 2 as11* published by the Free Software Foundation.12*13* This program is distributed in the hope that it will be useful,14* but WITHOUT ANY WARRANTY; without even the implied warranty of15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16* GNU General Public License for more details.17*18* You should have received a copy of the GNU General Public License19* along with this program; if not, write to the Free Software20* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA21*22*/2324/*25* ------------------------26* start of cfag12864b code27* ------------------------28*/2930#include <string.h>31#include <fcntl.h>32#include <unistd.h>33#include <sys/types.h>34#include <sys/stat.h>35#include <sys/mman.h>3637#define CFAG12864B_WIDTH (128)38#define CFAG12864B_HEIGHT (64)39#define CFAG12864B_SIZE (128 * 64 / 8)40#define CFAG12864B_BPB (8)41#define CFAG12864B_ADDRESS(x, y) ((y) * CFAG12864B_WIDTH / \42CFAG12864B_BPB + (x) / CFAG12864B_BPB)43#define CFAG12864B_BIT(n) (((unsigned char) 1) << (n))4445#undef CFAG12864B_DOCHECK46#ifdef CFAG12864B_DOCHECK47#define CFAG12864B_CHECK(x, y) ((x) < CFAG12864B_WIDTH && \48(y) < CFAG12864B_HEIGHT)49#else50#define CFAG12864B_CHECK(x, y) (1)51#endif5253int cfag12864b_fd;54unsigned char * cfag12864b_mem;55unsigned char cfag12864b_buffer[CFAG12864B_SIZE];5657/*58* init a cfag12864b framebuffer device59*60* No error: return = 061* Unable to open: return = -162* Unable to mmap: return = -263*/64static int cfag12864b_init(char *path)65{66cfag12864b_fd = open(path, O_RDWR);67if (cfag12864b_fd == -1)68return -1;6970cfag12864b_mem = mmap(0, CFAG12864B_SIZE, PROT_READ | PROT_WRITE,71MAP_SHARED, cfag12864b_fd, 0);72if (cfag12864b_mem == MAP_FAILED) {73close(cfag12864b_fd);74return -2;75}7677return 0;78}7980/*81* exit a cfag12864b framebuffer device82*/83static void cfag12864b_exit(void)84{85munmap(cfag12864b_mem, CFAG12864B_SIZE);86close(cfag12864b_fd);87}8889/*90* set (x, y) pixel91*/92static void cfag12864b_set(unsigned char x, unsigned char y)93{94if (CFAG12864B_CHECK(x, y))95cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] |=96CFAG12864B_BIT(x % CFAG12864B_BPB);97}9899/*100* unset (x, y) pixel101*/102static void cfag12864b_unset(unsigned char x, unsigned char y)103{104if (CFAG12864B_CHECK(x, y))105cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &=106~CFAG12864B_BIT(x % CFAG12864B_BPB);107}108109/*110* is set (x, y) pixel?111*112* Pixel off: return = 0113* Pixel on: return = 1114*/115static unsigned char cfag12864b_isset(unsigned char x, unsigned char y)116{117if (CFAG12864B_CHECK(x, y))118if (cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &119CFAG12864B_BIT(x % CFAG12864B_BPB))120return 1;121122return 0;123}124125/*126* not (x, y) pixel127*/128static void cfag12864b_not(unsigned char x, unsigned char y)129{130if (cfag12864b_isset(x, y))131cfag12864b_unset(x, y);132else133cfag12864b_set(x, y);134}135136/*137* fill (set all pixels)138*/139static void cfag12864b_fill(void)140{141unsigned short i;142143for (i = 0; i < CFAG12864B_SIZE; i++)144cfag12864b_buffer[i] = 0xFF;145}146147/*148* clear (unset all pixels)149*/150static void cfag12864b_clear(void)151{152unsigned short i;153154for (i = 0; i < CFAG12864B_SIZE; i++)155cfag12864b_buffer[i] = 0;156}157158/*159* format a [128*64] matrix160*161* Pixel off: src[i] = 0162* Pixel on: src[i] > 0163*/164static void cfag12864b_format(unsigned char * matrix)165{166unsigned char i, j, n;167168for (i = 0; i < CFAG12864B_HEIGHT; i++)169for (j = 0; j < CFAG12864B_WIDTH / CFAG12864B_BPB; j++) {170cfag12864b_buffer[i * CFAG12864B_WIDTH / CFAG12864B_BPB +171j] = 0;172for (n = 0; n < CFAG12864B_BPB; n++)173if (matrix[i * CFAG12864B_WIDTH +174j * CFAG12864B_BPB + n])175cfag12864b_buffer[i * CFAG12864B_WIDTH /176CFAG12864B_BPB + j] |=177CFAG12864B_BIT(n);178}179}180181/*182* blit buffer to lcd183*/184static void cfag12864b_blit(void)185{186memcpy(cfag12864b_mem, cfag12864b_buffer, CFAG12864B_SIZE);187}188189/*190* ----------------------191* end of cfag12864b code192* ----------------------193*/194195#include <stdio.h>196197#define EXAMPLES 6198199static void example(unsigned char n)200{201unsigned short i, j;202unsigned char matrix[CFAG12864B_WIDTH * CFAG12864B_HEIGHT];203204if (n > EXAMPLES)205return;206207printf("Example %i/%i - ", n, EXAMPLES);208209switch (n) {210case 1:211printf("Draw points setting bits");212cfag12864b_clear();213for (i = 0; i < CFAG12864B_WIDTH; i += 2)214for (j = 0; j < CFAG12864B_HEIGHT; j += 2)215cfag12864b_set(i, j);216break;217218case 2:219printf("Clear the LCD");220cfag12864b_clear();221break;222223case 3:224printf("Draw rows formatting a [128*64] matrix");225memset(matrix, 0, CFAG12864B_WIDTH * CFAG12864B_HEIGHT);226for (i = 0; i < CFAG12864B_WIDTH; i++)227for (j = 0; j < CFAG12864B_HEIGHT; j += 2)228matrix[j * CFAG12864B_WIDTH + i] = 1;229cfag12864b_format(matrix);230break;231232case 4:233printf("Fill the lcd");234cfag12864b_fill();235break;236237case 5:238printf("Draw columns unsetting bits");239for (i = 0; i < CFAG12864B_WIDTH; i += 2)240for (j = 0; j < CFAG12864B_HEIGHT; j++)241cfag12864b_unset(i, j);242break;243244case 6:245printf("Do negative not-ing all bits");246for (i = 0; i < CFAG12864B_WIDTH; i++)247for (j = 0; j < CFAG12864B_HEIGHT; j ++)248cfag12864b_not(i, j);249break;250}251252puts(" - [Press Enter]");253}254255int main(int argc, char *argv[])256{257unsigned char n;258259if (argc != 2) {260printf(261"Sintax: %s fbdev\n"262"Usually: /dev/fb0, /dev/fb1...\n", argv[0]);263return -1;264}265266if (cfag12864b_init(argv[1])) {267printf("Can't init %s fbdev\n", argv[1]);268return -2;269}270271for (n = 1; n <= EXAMPLES; n++) {272example(n);273cfag12864b_blit();274while (getchar() != '\n');275}276277cfag12864b_exit();278279return 0;280}281282283