阶段小项目1:循环间隔1秒lcd显示红绿蓝

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<error.h>
#include<errno.h>
#include<unistd.h>
#include<strings.h>
#include<stdbool.h>

#include<sys/stat.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<sys/mman.h>
int main(int argc,char **argv)
{
int fd = open("/dev/fb0",O_RDWR);
if(fd == -1)
{
fprintf(stderr,"open (%s) failed: %s" ,"/dev/fb0",strerror(errno));
exit(0);
}

char *p = mmap(NULL,800*480*4,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
if(p == MAP_FAILED)
{
perror("mmap failed!");
exit(0);
}

unsigned long buff[3] = {0x000000ff,0x00ff0000,0x0000ff00};
int i,j = 0;
for(j;;j++)  //主要变量j的使用,如果只有i循环,而没有j,则会出现雪花点,
{

for(i=0;i<800*480;i++)
{
memcpy(p+i*4,&buff[j%3],4);//此处少了求余,会出现段错误,内存溢出。
}

sleep(1);
}

return 0;
}

posted @ 2016-02-14 19:24  高傲的monkey  阅读(558)  评论(0编辑  收藏  举报