Path: blob/master/samples/auxdisplay/cfag12864b-example.c
26285 views
// SPDX-License-Identifier: GPL-2.01/*2* Filename: cfag12864b-example.c3* Version: 0.1.04* Description: cfag12864b LCD userspace example program5*6* Author: Copyright (C) Miguel Ojeda <[email protected]>7* Date: 2006-10-318*/910/*11* ------------------------12* start of cfag12864b code13* ------------------------14*/1516#include <string.h>17#include <fcntl.h>18#include <unistd.h>19#include <sys/types.h>20#include <sys/stat.h>21#include <sys/mman.h>2223#define CFAG12864B_WIDTH (128)24#define CFAG12864B_HEIGHT (64)25#define CFAG12864B_SIZE (128 * 64 / 8)26#define CFAG12864B_BPB (8)27#define CFAG12864B_ADDRESS(x, y) ((y) * CFAG12864B_WIDTH / \28CFAG12864B_BPB + (x) / CFAG12864B_BPB)29#define CFAG12864B_BIT(n) (((unsigned char) 1) << (n))3031#undef CFAG12864B_DOCHECK32#ifdef CFAG12864B_DOCHECK33#define CFAG12864B_CHECK(x, y) ((x) < CFAG12864B_WIDTH && \34(y) < CFAG12864B_HEIGHT)35#else36#define CFAG12864B_CHECK(x, y) (1)37#endif3839int cfag12864b_fd;40unsigned char * cfag12864b_mem;41unsigned char cfag12864b_buffer[CFAG12864B_SIZE];4243/*44* init a cfag12864b framebuffer device45*46* No error: return = 047* Unable to open: return = -148* Unable to mmap: return = -249*/50static int cfag12864b_init(char *path)51{52cfag12864b_fd = open(path, O_RDWR);53if (cfag12864b_fd == -1)54return -1;5556cfag12864b_mem = mmap(0, CFAG12864B_SIZE, PROT_READ | PROT_WRITE,57MAP_SHARED, cfag12864b_fd, 0);58if (cfag12864b_mem == MAP_FAILED) {59close(cfag12864b_fd);60return -2;61}6263return 0;64}6566/*67* exit a cfag12864b framebuffer device68*/69static void cfag12864b_exit(void)70{71munmap(cfag12864b_mem, CFAG12864B_SIZE);72close(cfag12864b_fd);73}7475/*76* set (x, y) pixel77*/78static void cfag12864b_set(unsigned char x, unsigned char y)79{80if (CFAG12864B_CHECK(x, y))81cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] |=82CFAG12864B_BIT(x % CFAG12864B_BPB);83}8485/*86* unset (x, y) pixel87*/88static void cfag12864b_unset(unsigned char x, unsigned char y)89{90if (CFAG12864B_CHECK(x, y))91cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &=92~CFAG12864B_BIT(x % CFAG12864B_BPB);93}9495/*96* is set (x, y) pixel?97*98* Pixel off: return = 099* Pixel on: return = 1100*/101static unsigned char cfag12864b_isset(unsigned char x, unsigned char y)102{103if (CFAG12864B_CHECK(x, y))104if (cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &105CFAG12864B_BIT(x % CFAG12864B_BPB))106return 1;107108return 0;109}110111/*112* not (x, y) pixel113*/114static void cfag12864b_not(unsigned char x, unsigned char y)115{116if (cfag12864b_isset(x, y))117cfag12864b_unset(x, y);118else119cfag12864b_set(x, y);120}121122/*123* fill (set all pixels)124*/125static void cfag12864b_fill(void)126{127unsigned short i;128129for (i = 0; i < CFAG12864B_SIZE; i++)130cfag12864b_buffer[i] = 0xFF;131}132133/*134* clear (unset all pixels)135*/136static void cfag12864b_clear(void)137{138unsigned short i;139140for (i = 0; i < CFAG12864B_SIZE; i++)141cfag12864b_buffer[i] = 0;142}143144/*145* format a [128*64] matrix146*147* Pixel off: src[i] = 0148* Pixel on: src[i] > 0149*/150static void cfag12864b_format(unsigned char * matrix)151{152unsigned char i, j, n;153154for (i = 0; i < CFAG12864B_HEIGHT; i++)155for (j = 0; j < CFAG12864B_WIDTH / CFAG12864B_BPB; j++) {156cfag12864b_buffer[i * CFAG12864B_WIDTH / CFAG12864B_BPB +157j] = 0;158for (n = 0; n < CFAG12864B_BPB; n++)159if (matrix[i * CFAG12864B_WIDTH +160j * CFAG12864B_BPB + n])161cfag12864b_buffer[i * CFAG12864B_WIDTH /162CFAG12864B_BPB + j] |=163CFAG12864B_BIT(n);164}165}166167/*168* blit buffer to lcd169*/170static void cfag12864b_blit(void)171{172memcpy(cfag12864b_mem, cfag12864b_buffer, CFAG12864B_SIZE);173}174175/*176* ----------------------177* end of cfag12864b code178* ----------------------179*/180181#include <stdio.h>182183#define EXAMPLES 6184185static void example(unsigned char n)186{187unsigned short i, j;188unsigned char matrix[CFAG12864B_WIDTH * CFAG12864B_HEIGHT];189190if (n > EXAMPLES)191return;192193printf("Example %i/%i - ", n, EXAMPLES);194195switch (n) {196case 1:197printf("Draw points setting bits");198cfag12864b_clear();199for (i = 0; i < CFAG12864B_WIDTH; i += 2)200for (j = 0; j < CFAG12864B_HEIGHT; j += 2)201cfag12864b_set(i, j);202break;203204case 2:205printf("Clear the LCD");206cfag12864b_clear();207break;208209case 3:210printf("Draw rows formatting a [128*64] matrix");211memset(matrix, 0, CFAG12864B_WIDTH * CFAG12864B_HEIGHT);212for (i = 0; i < CFAG12864B_WIDTH; i++)213for (j = 0; j < CFAG12864B_HEIGHT; j += 2)214matrix[j * CFAG12864B_WIDTH + i] = 1;215cfag12864b_format(matrix);216break;217218case 4:219printf("Fill the lcd");220cfag12864b_fill();221break;222223case 5:224printf("Draw columns unsetting bits");225for (i = 0; i < CFAG12864B_WIDTH; i += 2)226for (j = 0; j < CFAG12864B_HEIGHT; j++)227cfag12864b_unset(i, j);228break;229230case 6:231printf("Do negative not-ing all bits");232for (i = 0; i < CFAG12864B_WIDTH; i++)233for (j = 0; j < CFAG12864B_HEIGHT; j ++)234cfag12864b_not(i, j);235break;236}237238puts(" - [Press Enter]");239}240241int main(int argc, char *argv[])242{243unsigned char n;244245if (argc != 2) {246printf(247"Syntax: %s fbdev\n"248"Usually: /dev/fb0, /dev/fb1...\n", argv[0]);249return -1;250}251252if (cfag12864b_init(argv[1])) {253printf("Can't init %s fbdev\n", argv[1]);254return -2;255}256257for (n = 1; n <= EXAMPLES; n++) {258example(n);259cfag12864b_blit();260while (getchar() != '\n');261}262263cfag12864b_exit();264265return 0;266}267268269