| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 436 人关注过本帖
标题:求大神解答,得分多多
只看楼主 加入收藏
q1392079125
Rank: 2
等 级:论坛游民
帖 子:19
专家分:11
注 册:2013-1-10
结帖率:75%
收藏
已结贴  问题点数:12 回复次数:9 
求大神解答,得分多多
求大神注释一下句子

程序代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include "font_8x16.h"

#define Width        320            //LCD屏的宽度
#define Heigth        240            //LCD屏的高度
#define Start_x     8            //屏-左上角的起始坐标  x   y
#define Start_y        8
#define End_x        (Width - 8)    //屏-左上角的起始坐标  x   y
#define End_y        (Heigth    - 8)    
    
int fk;                             //按键的文件描述符
char button_status[6];
int fp=0;                            //LCD屏的文件描述符
struct fb_var_screeninfo vinfo;     //LCD可变参数结构体
struct fb_fix_screeninfo finfo;        //LCD固定参数结构体
char *fbp = 0;
long screensize=0;                    //初始化屏幕大小为0
int s_x = 0, s_y = 0,id = 0;                 //屏幕的x y 坐标 




//清屏
void clear_screen()
{
    memset(fbp, 0xf8, screensize);
}

//绘制单个字符
void Draw_8X16(int x, int y, const unsigned char ch[])
{
    int i = 0, j = 0;
    unsigned char mask, buffer;
    long location_tt;
    location_tt = x * vinfo.bits_per_pixel / 8 + y *finfo.line_length;

    for (i = 0; i < 16; i++)
    {
        mask = 0x80;
        buffer = ch[i];
        location_tt +=  finfo.line_length;
        for (j = 0; j < 8; j ++)
        {
            if ((buffer & mask)) { 
                *(fbp +location_tt + j * vinfo.bits_per_pixel / 8) = 0; 
                *(fbp +location_tt + j * vinfo.bits_per_pixel / 8 + 1) = 0;
             }
             mask = mask >> 1;
        }
    }
    s_x += 8;
}

//绘制单个汉字    16 * 16
void Draw_16X16(int x, int y, const unsigned char ch[])
{
    int i = 0, j = 0;
    unsigned char mask, buffer;
    long location_tt;
    location_tt = x * vinfo.bits_per_pixel / 8 + y * finfo.line_length;

    for (i = 0; i < 16; i++)
    {
        mask = 0x80;
        buffer = ch[i * 2];
        location_tt +=  finfo.line_length;
        for (j = 0; j < 8; j++) { 
            if ((buffer & mask)) { 
                *(fbp +location_tt + j * vinfo.bits_per_pixel / 8) = 0; 
                *(fbp +location_tt + j * vinfo.bits_per_pixel / 8 + 1) = 0;
             }
             mask = mask >> 1;
         }

        mask = 0x80;
        buffer = ch[i * 2 + 1];
        for (j = 0; j < 8; j++)
        {
            if ((buffer & mask))
            {
                *(fbp +location_tt + (8 + j) * vinfo.bits_per_pixel / 8) = 0;
                *(fbp +location_tt + (8 + j) * vinfo.bits_per_pixel / 8 + 1) = 0;
            }
            mask = mask >> 1;
        }
    }
    s_x += 16;
}

//判断是否换行   x坐标
int Is_next_line(int x)    
{
    if (x >= End_x)
        return 1;
    return 0;
}

//跳到下一行
int Next_line(int y)
{
    //sleep(1);
    y = y + 16;
    s_y = y;
    s_x = Start_x;
    return y;
}

//判断是否换屏   y坐标
int Is_next_screen(int y)
{
    if (y >= End_y - 8)
        return 1;
    return 0;
}

//清屏,跳到下一屏
void   Next_screen()
{
    int ret;
int i;
    while (1)
    {
        ret = read(fk, button_status, 6);
        if (ret < 0)
        {
            printf("read button status err\n");
        }
        //按键控制翻到下一页
        if (button_status[0] == '1')
        {
            //清屏
            clear_screen();
            //s_x = Start_x;
            s_y = Start_y;
            break;
        }
        if (button_status[1] == '1'){
            for(i=0;i<255;i++) ;
            if (button_status[1] == '1'){
                clear_screen();
                printf("%d\n",id);
                id=id-532;
                    s_y = Start_y;
                break;}
            
        }
    }
//    sleep(2);

}

//清屏,跳到上一屏
void UP_screen(unsigned int zf_index)
{
    int ret;
    //int i=0;
//    unsigned char *buffer;
    //unsigned char buf[16];
    while (1)
    {
        ret = read(fk, button_status, 6);
        if (ret < 0)
        {
            printf("read button status err\n");
        }
        //按键控制翻到上一页
        if (button_status[1] == '1')
        {
            //清屏
            clear_screen();
            //s_x = Start_x;
            //zf_index = buffer[i] * 16;
        //    memcpy(buf, (const void *)&Assic[zf_index], 16);
            s_y=zf_index;
            break;
        }
    }
//    sleep(2);
}



//绘制一行字符    
void Put_text_ch(int x, int y, int size, const unsigned char ch[])
{
    int i = 0, j = 0;
    unsigned char buffer[16];
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < 16; j++)
        {
            buffer[j] = ch[i * 16 + j];
        }
        if (i != 0)
            x += 8;
        if(Is_next_line(x))
        {
            y = Next_line(y); 
            x = s_x;
            if (Is_next_screen(y))
            {
                Next_screen();
                y = s_y;
            }
        }
        Draw_8X16(x, y, buffer);
    }
}

//绘制一行汉字    
void Put_text_hz(int x, int y, int size, const unsigned char ch[])
{
    int i = 0, j = 0;
    unsigned char buffer[32];
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < 32; j++)
        {
            buffer[j] = ch[i * 32 + j];
        }
        if (i != 0)
            x += 16;
        if(Is_next_line(x))
        {
            y = Next_line(y); 
            x = s_x;
            if (Is_next_screen(y))
            {
                Next_screen();
                y = s_y;
            }
        }
        Draw_16X16(x, y, buffer);
    }
}

//打开LCD设备
void open_lcd()
{
    //以可读可写的方式打开LCD设备
    fp = open ("/dev/fb0",O_RDWR);
    //错误冗余
    if (fp < 0){
        printf("Error : Can not open framebuffer device\n");
        exit(1);
    }
    //在超级终端打印下面两句话
    puts("hello, world.\n");
    printf("open framebuffer device\n");
    //获取屏幕相关参数    
    if (ioctl(fp,FBIOGET_FSCREENINFO,&finfo)){
        printf("Error reading fixed information\n");
        exit(2);
    }
    if (ioctl(fp,FBIOGET_VSCREENINFO,&vinfo)){
        printf("Error reading variable information\n");
        exit(3);
    }

    //计算屏幕缓冲区大小
    screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;   //单帧画面空间

    printf("可见屏幕一行有多少个像素点:xres= %d\n", vinfo.xres);
    printf("可见屏幕一列有多少个像素点:yres = %d\n", vinfo.yres);
    printf("每个相似的位数即BPP: bits_per_pixel = %d\n", vinfo.bits_per_pixel);

    /*这就是把fp所指的文件中从开始到screensize大小的内容给映射出来,得到一个指向这块空间的指针*/
    fbp =(char *) mmap (0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fp,0);
    if ((int) fbp == -1)
    {
         printf ("Error: failed to map framebuffer device to memory.\n");
         exit (4);
    }
    printf("The Framebuffer device was mapped to memory successfully!\n");
}

//关闭LCD设备
void close_lcd()
{
    munmap (fbp, screensize); /*解除映射*/
    close (fp);    /*关闭文件*/
}

//打开文本文件
int open_txt()
{
    int fd_t;
    fd_t = open("./1.txt",0);
    if (fd_t < 0)
    {
        printf("txt can't open!\n");
        return (-1);
    }
    return fd_t;
}

//关闭文本文件
void close_txt(int fd_t)
{
    close(fd_t);
}

//计算文件总字节数    
off_t cal_filesize(fd_t)
{
    struct stat txt_stat;
    off_t file_size_t = 0;

    fstat(fd_t, &txt_stat);
    file_size_t = txt_stat.st_size;
    printf("file_size = %d\n", file_size_t);
    return file_size_t;    
}

int open_buttons()
{
    int fk_t;
    fk_t = open("/dev/buttons", O_RDWR);
    if (fk_t < 0)
    {
        printf("can't open /dev/buttons\n");
        return -1;
    }
    return fk_t;
}

void close_buttons(int fk_t)
{
    close(fk_t);    
}

int main(void)
{
    int fd;
    unsigned char *buffer;
    off_t file_size = 0;    
    int k = 0;
    unsigned int zf_index;
    unsigned char buf[16];
    
    fk = open_buttons();
    
    open_lcd();    
    clear_screen();
    
    s_x = Start_x;
    s_y = Start_y;

    //传过去的应该是文件路径
    fd = open_txt();
    if (fd < 0)
    {
        printf("txt can't open!\n");
        return (-1);
    }

    //计算文件总字节数    
    file_size = cal_filesize(fd);

    buffer = (unsigned char *)malloc(file_size);
    memset(buffer, 0, file_size);

    read(fd, buffer, file_size); 
    //printf("%s\n", buffer);
    
    for (id = 0; id < file_size - 1; id++)
    {
        
        //判断文本中的当前字是不是换行符
        if (buffer[id] == '\n')
        {
            //换行
            Next_line(s_y);
            //判断一下是不是又刚好是换屏
            if (Is_next_screen(s_y))
                Next_screen();
                
            continue;
        }
    
        
        zf_index = buffer[id] * 16;
        memcpy(buf, (const void *)&Assic[zf_index], 16);
        /*for (k = 0; k < 16; k++)
            printf("0x%x\t",buf[k]);
        printf("\n");
        */
        Put_text_ch(s_x, s_y, 1, buf);
        
    }
    
    close_txt(fd);
    close_lcd();
    close_buttons(fk);
    return 0;
}
2013-02-25 18:29
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:1 
好长啊
2013-02-25 18:36
Toolme
Rank: 1
等 级:新手上路
帖 子:11
专家分:5
注 册:2013-2-17
收藏
得分:1 
kan bu dong,,,
2013-02-25 18:48
疯丶领
Rank: 1
等 级:新手上路
帖 子:3
专家分:3
注 册:2013-2-25
收藏
得分:1 
楼主是大神。。。。
2013-02-25 19:38
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:1 
12分而已,不多

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-02-25 19:45
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:1 
以下是引用zklhp在2013-2-25 18:36:19的发言:

好长啊

是啊,好长啊
懒得看

而且本身的注释不是很多了么
再看不懂的,调一下也差不多了呀
我写代码都不喜欢写这么多注释

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-02-25 19:46
a646698961
Rank: 2
等 级:论坛游民
威 望:1
帖 子:49
专家分:94
注 册:2012-12-19
收藏
得分:1 
好遥远。。。。我该怎么办

欢迎来讨论和分享经验,QQ:646698961
2013-02-25 19:48
黑暗烧纸人
Rank: 4
来 自:甘肃
等 级:业余侠客
帖 子:113
专家分:239
注 册:2012-12-22
收藏
得分:1 
菜鸟飘过。。。

老衲闻到一股蛋蛋的忧伤......
2013-02-25 21:30
为你而来。
Rank: 2
等 级:论坛游民
帖 子:21
专家分:36
注 册:2013-2-18
收藏
得分:1 
那么长????12分?  精!
2013-02-25 21:53
logman1068
Rank: 1
等 级:新手上路
帖 子:1
专家分:1
注 册:2013-2-25
收藏
得分:1 
菜鸟求拜师
2013-02-25 23:24
快速回复:求大神解答,得分多多
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028982 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved