Chinaunix首页 | 论坛 | 博客
  • 博客访问: 676334
  • 博文数量: 156
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 1201
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-05 20:08
文章分类

全部博文(156)

文章存档

2010年(13)

2008年(39)

2007年(104)

我的朋友

分类: LINUX

2007-05-23 19:47:18

//makefile



PROG=clock

all: $(PROG)

clock: main.o fb.o
    gcc -o $@ $^

main.o: main.c fb.h
    gcc -c -o $@ $<

fb.o: fb.c fb.h fbnum.h
    gcc -c -o $@ $<

clean:
    rm -f *.o $(PROG)

//fbnum.h

#ifndef _LOCAL_NUM_H_
#define _LOCAL_NUM_H_


static const int NUM_WIDTH = 20;
static const int NUM_HEIGHT = 40;

static const char num_map[40][20] = {
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"...aaaaaaaaaaaaa....",
"..f.aaaaaaaaaaa.b...",
"..ff.aaaaaaaaa.bb...",
"..fff.........bbb...",
"..fff.........bbb...",
"..fff.........bbb...",
"..fff...hhh...bbb...",
"..fff...hhh...bbb...",
"..fff...hhh...bbb...",
"..fff.........bbb...",
"..fff.........bbb...",
"..ff...........bb...",
"..f.ggggggggggg.b...",
"...ggggggggggggg....",
"..e.ggggggggggg.c...",
"..ee...........cc...",
"..eee.........ccc...",
"..eee.........ccc...",
"..eee...hhh...ccc...",
"..eee...hhh...ccc...",
"..eee...hhh...ccc...",
"..eee.........ccc...",
"..eee.........ccc...",
"..eee.........ccc...",
"..ee.ddddddddd.cc...",
"..e.ddddddddddd.c...",
"...ddddddddddddd....",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"...................."};


static const char *num_table[] = {
"0 abcdef",
"1 bc",
"2 abedg",
"3 abcdg",
"4 bcfg",
"5 acdfg",
"6 acdefg",
"7 abc",
"8 abcdefg",
"9 abcdfg",
": h",
0
};


#endif /* _LOCAL_NUM_H_ */

//fb.h

#ifndef _LOCAL_FB_H_
#define _LOCAL_FB_H_

typedef struct
{
    int r; /* Red */
    int g; /* Green */
    int b; /* Blue */
    int t; /* Transparency */
} fb_color_t;


void fb_open();
void fb_close();
void fb_set_text_color(fb_color_t *color);
void fb_set_back_color(fb_color_t *color);
void fb_pixel(int x, int y, fb_color_t *color);
int fb_putc(int x, int y, char c);
int fb_puts(int x, int y, const char *str);



#endif /* _LOCAL_FB_H_ */

//main.c

#include <stdio.h>
#include <time.h>
#include "fb.h"


extern int max_text_row;

int main ()
{
    time_t now;
    char *buf;
    fb_color_t color;

    fb_open();
    color.r = 255;
    color.g = 100;
    color.b = 100;
    color.t = 0;
    fb_set_text_color(&color);

    while (1)
    {
        now = time(NULL);
        buf = ctime(&now);
        buf = strchr(buf, ':') - 2;
        buf[8] = '\0';
        fb_puts(max_text_row-9, 0, buf);
        usleep(50);
    }

    fb_close();
    return 0;
}

//fb.c

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <assert.h>
#include <linux/fb.h>
#include <sys/mman.h>

#include "fb.h"
#include "fbnum.h"


int fb_fd = 0;
int max_text_line, max_text_row;
struct fb_var_screeninfo fb_vsinfo;
struct fb_fix_screeninfo fb_fsinfo;
long int fb_scrsize = 0;
char *fb_mp = 0;
fb_color_t fb_text_color = {255, 255, 255, 0};
fb_color_t fb_back_color = {0, 0, 0, 0};


void
fb_open()
{
    /* Open the file for reading and writing */
    fb_fd = open ("/dev/fb0", O_RDWR);
    if (!fb_fd)
    {
         printf ("Error: cannot open framebuffer device.\n");
         exit (1);
    }
    printf ("The framebuffer device was opened successfully.\n");

    /* Get fixed screen information */
    if (ioctl (fb_fd, FBIOGET_FSCREENINFO, &fb_fsinfo))
    {
         printf ("Error reading fixed information.\n");
         exit (2);
    }

    /* Get variable screen information */
    if (ioctl (fb_fd, FBIOGET_VSCREENINFO, &fb_vsinfo))
    {
         printf ("Error reading variable information.\n");
         exit (3);
    }

    /* Figure out the size of the screen in bytes */
    fb_scrsize = fb_vsinfo.xres * fb_vsinfo.yres * fb_vsinfo.bits_per_pixel / 8;

    /* Map the device to memory */
    fb_mp = (char *) mmap (0, fb_scrsize, PROT_READ | PROT_WRITE, MAP_SHARED,
             fb_fd, 0);
    if ((int) fb_mp == -1)
    {
         printf ("Error: failed to map framebuffer device to memory.\n");
         exit (4);
    }
    printf ("The framebuffer device was mapped to memory successfully.\n");

    /* Count the max line and row of texts */
    max_text_line = fb_vsinfo.yres / NUM_HEIGHT;
    max_text_row = fb_vsinfo.xres / NUM_WIDTH;

    return;
}


void
fb_close()
{
    munmap (fb_mp, fb_scrsize);
    close (fb_fd);

    return;
}


void
fb_pixel(int x, int y, fb_color_t *color)
{
    long location = 0;

    assert(color != NULL);

    /* Figure out where in memory to put the pixel */
    location = (x + fb_vsinfo.xoffset) * (fb_vsinfo.bits_per_pixel / 8) +
     (y + fb_vsinfo.yoffset) * fb_fsinfo.line_length;

    *(fb_mp + location + 3) = color->t; /* If transparency */
    *(fb_mp + location + 2) = color->r; /* Red */
    *(fb_mp + location + 1) = color->g; /* Green */
    *(fb_mp + location) = color->b; /* Blue */

    return;
}


void
fb_set_text_color(fb_color_t *color)
{
    memcpy(&fb_text_color, color, sizeof(fb_color_t));
    return;    
}


void
fb_set_back_color(fb_color_t *color)
{
    memcpy(&fb_back_color, color, sizeof(fb_color_t));
    return;    
}


static void
draw_char(int x, int y, const char *led_set)
{
    int i, j;

    for (i=0; i<NUM_WIDTH; i++)
    {
        for (j=0; j<NUM_HEIGHT; j++)
        {
            if (strchr(led_set, num_map[j][i]))
                fb_pixel(x+i, y+j, &fb_text_color);
            else
                fb_pixel(x+i, y+j, &fb_back_color);
        }
    }

    return;
}


int
fb_putc(int x, int y, char c)
{
    int i;
    int pixel_x = x * NUM_WIDTH;
    int pixel_y = y * NUM_HEIGHT;

    for (i=0; num_table[i]; i++)
    {
        if (*num_table[i] == c)
        {
            draw_char(pixel_x, pixel_y, num_table[i]+1);
            break;
        }
    }
    
    if (num_table[i])
        return (int)c;
    else
    {
        printf("fb_putc: no font infomation for '%c'.\n", c);
        return -1;
    }
}


int
fb_puts(int x, int y, const char *str)
{
    int i;
    int ret = 0;

    assert(str);
    for (i=0; str[i]; i++, x++)
        if (fb_putc(x, y, str[i]) == -1)
            ret = -1;

    if (ret = -1)
        return ret;
    else
        return i;
}

//num.xpm

/* XPM */
static char *num[]={
"20 40 9 1",
". c None",
"f c #0000ff",
"# c #00ff00",
"g c #00ffff",
"c c #303030",
"b c #ff0000",
"d c #ff00ff",
"e c #ffc0c0",
"a c #ffff00",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"...#############....",
"..a.###########.b...",
"..aa.#########.bb...",
"..aaa.........bbb...",
"..aaa.........bbb...",
"..aaa.........bbb...",
"..aaa...ccc...bbb...",
"..aaa...ccc...bbb...",
"..aaa...ccc...bbb...",
"..aaa.........bbb...",
"..aaa.........bbb...",
"..aa...........bb...",
"..a.ddddddddddd.b...",
"...ddddddddddddd....",
"..e.ddddddddddd.f...",
"..ee...........ff...",
"..eee.........fff...",
"..eee.........fff...",
"..eee...ccc...fff...",
"..eee...ccc...fff...",
"..eee...ccc...fff...",
"..eee.........fff...",
"..eee.........fff...",
"..eee.........fff...",
"..ee.ggggggggg.ff...",
"..e.ggggggggggg.f...",
"...ggggggggggggg....",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"...................."};

阅读(2145) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~