急!!!求教segmentation fault的问题,如何解决???

qq_23324889 2015-08-13 04:42:56
#include "/headsys.h"

#define SCREENNAME "/dev/fb0"
#define MICEDEV "/dev/input/mice"
//定义鼠标的大小
#define C_WIDTH 10
#define C_HEIGHT 17
//定义鼠标的空间颜色
#define BORD 0xffff
#define X___ 0x00ff
#define T___ 0x0

#define BTN_N 0 //无鼠标按键
#define BTN_L 1 //左
#define BTN_R 2 //右
#define BTN_L_R 3 //左右同时按下
#define BTN_M 4 //中间

struct screeninfo myscreeninfo; //像素结构体,在头文件里

typedef struct MICEINFO{ //鼠标结构体
char mousex;
char mousey;
int mousebtn;
}mouseinfo;

static u32_t cursor_bg[C_WIDTH*C_HEIGHT],cursor_pixel[C_WIDTH*C_HEIGHT]= //绘制鼠标图形
{
BORD,T___,T___,T___,T___,T___,T___,T___,T___,T___,
BORD,BORD,T___,T___,T___,T___,T___,T___,T___,T___,
BORD,X___,BORD,T___,T___,T___,T___,T___,T___,T___,
BORD,X___,X___,BORD,T___,T___,T___,T___,T___,T___,
BORD,X___,X___,X___,BORD,T___,T___,T___,T___,T___,
BORD,X___,X___,X___,X___,BORD,T___,T___,T___,T___,
BORD,X___,X___,X___,X___,X___,BORD,T___,T___,T___,
BORD,X___,X___,X___,X___,X___,X___,BORD,T___,T___,
BORD,X___,X___,X___,X___,X___,X___,X___,BORD,T___,
BORD,X___,X___,X___,X___,X___,X___,X___,X___,BORD,
BORD,X___,X___,X___,X___,X___,BORD,BORD,BORD,BORD,
BORD,X___,X___,BORD,X___,X___,BORD,T___,T___,T___,
BORD,X___,BORD,T___,BORD,X___,X___,BORD,T___,T___,
BORD,BORD,T___,T___,BORD,X___,X___,BORD,T___,T___,
T___,T___,T___,T___,T___,BORD,X___,X___,BORD,T___,
T___,T___,T___,T___,T___,BORD,X___,X___,BORD,T___,
T___,T___,T___,T___,T___,T___,BORD,BORD,T___,T___
};

void write_pixel(u32_t x, u32_t y, u32_t color) //给图片添加颜色
{
if(x > myscreeninfo.width || y > myscreeninfo.height)
{
printf("pixel position error.\n");
exit(-1);
}

*(myscreeninfo.start + y*myscreeninfo.width + x) = color;//添加颜色
}

void fb_mouse(int x,int y) //绘制鼠标, 同时将被覆盖的图像保存到cursor_bg数组中
{//问题出现在了这里
int i,j;
for(i=0;i<C_HEIGHT;i++)
{
for(j=0;j<C_WIDTH;j++)
{
if(x+j>0 && x+j < 1024 && y+i >0 && y+i < 768)
cursor_bg[j+i*C_WIDTH] = *(myscreeninfo.start+ (y+i)*myscreeninfo.width + x+j);
if(x + j >= 0 && x + j <= myscreeninfo.width && y + i >= i && y +i <= myscreeninfo.height)
if(cursor_pixel[j+i*C_WIDTH]!=T___ && x+j <= myscreeninfo.width && y+i <= myscreeninfo.height)
write_pixel(x+j,y+i,cursor_pixel[j+i*C_WIDTH]);

}
}
}

void fb_mouse_bg(int x,int y) //绘制鼠标图形,并且填充色
{
int i,j;
for(i=0;i<C_HEIGHT;i++)
{
for(j=0;j<C_WIDTH;j++)
{
write_pixel(x+j,y+i,cursor_bg[j+i*C_WIDTH]);
}
}
}

int getmouseinfo(int mousefd,mouseinfo *mouse) //记录鼠标的x,y,tn的坐标值
{
int n;
char buf[3];
if((n=read(mousefd,buf,3))<0)
{
if(errno==EAGAIN)
return -1;
fprintf(stderr,"[%s:%d]read mice failed:%s\n",__FILE__,__LINE__,strerror(errno));
exit(-1);
}
mouse->mousex = buf[1];
mouse->mousey = -buf[2];
mouse->mousebtn = buf[0] & 0x7; //屏蔽第四位,判断按下的是哪一个键值
return 0;
}


int main()
{
int mx,my;
int fd;
char buf[20];
char order = 0;
//1.创建文件
u32_t fd2 = open(SCREENNAME, O_RDWR);
if(fd2 < 0)
ERR();
//2.获取屏幕参数
struct fb_var_screeninfo info;
if(ioctl(fd2, FBIOGET_VSCREENINFO, &info) < 0)
ERR();
printf("Get success\n\n");
myscreeninfo.width = info.xres;
myscreeninfo.height = info.yres;
myscreeninfo.bpp = info.bits_per_pixel;

printf("screenInfo:\n");
printf("width = %d, height = %d, bpp = %d\n", myscreeninfo.width, myscreeninfo.height,\
myscreeninfo.bpp);
//3.内存映射
myscreeninfo.length = myscreeninfo.width*myscreeninfo.height*myscreeninfo.bpp/8;//字节
if((myscreeninfo.start = (u32_t *)mmap(0, myscreeninfo.length, PROT_READ|PROT_WRITE, MAP_SHARED, fd2, 0)) == (void *)-1)
ERR();

mx = myscreeninfo.width/2; //图片的1/2
my = myscreeninfo.height/2;
fb_mouse(mx,my); //将图片的大小存入鼠标数组
//1. 打开鼠标
if((fd = open(MICEDEV,O_RDONLY)) < 0)
{
ERR();
}

mouseinfo mouse;
int lastbtn = BTN_N; //无按键
// system("stty raw -echo"); //此函数不建议使用,返回值太多,不好控制
popen("stty raw -echo","r"); //代替system()函数

while(1)
{
if(getmouseinfo(fd,&mouse) >= 0) //记录鼠标值>0
{
switch(mouse.mousebtn) //mouse结构体
{
case BTN_N: //无鼠标按键
switch(lastbtn) //按键判断
{
case BTN_L:
order = 'L';break;
case BTN_R:
order = 'R';break;
default:
break;
}
lastbtn = BTN_N;
break;
default:
lastbtn = mouse.mousebtn;
break;
}
fb_mouse_bg(mx,my);//恢复被覆盖的图像
if(mx+mouse.mousex >= 0 && mx+mouse.mousex <= myscreeninfo.width && my+mouse.mousey >= 0 && my+mouse.mousey <= myscreeninfo.height) //鼠标在图像范围内的条件
{
mx += mouse.mousex;
my += mouse.mousey;
}
fb_mouse(mx,my); //在mx和my位置绘制鼠标
if(order == 'L' || order == 'R') //输出鼠标所在位置的坐标值
{
printf("@Pressed %c @:%d;%d",order,mx,my);
order = 0;
}
}
}
}

...全文
294 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
访问了非法内存地址,但是你的代码太长,有没打印信息,实在无法看下去。你自己慢慢调试吧。 注意指针什么之类的访问。
fly 100% 2015-08-14
  • 打赏
  • 举报
回复
一般这种情况都是访问不存在的地址 可以靠打印看看哪步出的问题

19,500

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧