本设计根据tslib中提供的ts_test程序修改而来,本设计中ts_test.c还依赖于tslib中的其他文件,若想直接编译,可置于tslib中的test文件夹中,替换到原来的文件。本设计根据tslib中提供的API取得X,Y坐标,根据得到的X,Y,打开fb0,映射进入内存(出于速度的考虑),进行划线,对线条进行了加粗处理,并随机生成颜色,最终形成灿烂的笔画。
ts_test.c
-
#include "config.h"
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <signal.h>
-
#include <sys/fcntl.h>
-
#include <sys/ioctl.h>
-
#include <sys/mman.h>
-
#include <sys/time.h>
-
#include<linux/fb.h>
-
-
-
#include "tslib.h"
-
#include "fbutils.h"
-
-
unsigned char *fbp;
-
struct fb_var_screeninfo vinfo;
-
struct fb_fix_screeninfo finfo;
-
-
-
void drawpoint(unsigned int x,unsigned int y,unsigned int *color)
-
{
-
unsigned char *fbp0;
-
fbp0=fbp;
-
fbp0+=y*vinfo.xres*vinfo.bits_per_pixel/8+x*vinfo.bits_per_pixel/8;
-
memcpy(fbp0,color,vinfo.bits_per_pixel/8);
-
}
-
-
-
void drawline(unsigned int x0,unsigned int y0,unsigned int x1,unsigned int y1,unsigned int *color)
-
{
-
unsigned int x=x0;
-
unsigned int y=y0;
-
int s0,s1,s2;
-
while(x!=x1||y!=y1)
-
{
-
drawpoint(x,y,color);//以下为加粗直线
-
drawpoint(x+1,y,color);
-
drawpoint(x,y+1,color);
-
drawpoint(x+1,y+1,color);
-
drawpoint(x+2,y,color);
-
drawpoint(x,y+2,color);
-
drawpoint(x+2,y+2,color);
-
-
if(x0>=x1&&y0>=y1) //求直线下一点的算法。
-
{
-
s0=abs((x-x0-1)*(y0-y1)-(y-y0)*(x0-x1));
-
s1=abs((x-x0)*(y0-y1)-(y-y0-1)*(x0-x1));
-
s2=abs((x-x0-1)*(y0-y1)-(y-y0-1)*(x0-x1));
-
s0<=s1?(s0<=s2?x=x-1:(x=x-1,y=y-1)):(s1<=s2?y=y-1:(x=x-1,y=y-1));
-
}
-
else if(x0>=x1&&y0<=y1)
-
{
-
s0=abs((x-x0-1)*(y0-y1)-(y-y0)*(x0-x1));
-
s1=abs((x-x0)*(y0-y1)-(y-y0+1)*(x0-x1));
-
s2=abs((x-x0-1)*(y0-y1)-(y-y0+1)*(x0-x1));
-
s0<=s1?(s0<=s2?x=x-1:(x=x-1,y=y+1)):(s1<=s2?y=y+1:(x=x-1,y=y+1));
-
}
-
else if(x0<=x1&&y0>=y1)
-
{
-
s0=abs((x-x0+1)*(y0-y1)-(y-y0)*(x0-x1));
-
s1=abs((x-x0)*(y0-y1)-(y-y0-1)*(x0-x1));
-
s2=abs((x-x0+1)*(y0-y1)-(y-y0-1)*(x0-x1));
-
s0<=s1?(s0<=s2?x=x+1:(x=x+1,y=y-1)):(s1<=s2?y=y-1:(x=x+1,y=y-1));
-
}
-
else if(x0<=x1&&y0<=y1)
-
{
-
s0=abs((x-x0+1)*(y0-y1)-(y-y0)*(x0-x1));
-
s1=abs((x-x0)*(y0-y1)-(y-y0+1)*(x0-x1));
-
s2=abs((x-x0+1)*(y0-y1)-(y-y0+1)*(x0-x1));
-
s0<=s1?(s0<=s2?x=x+1:(x=x+1,y=y+1)):(s1<=s2?y=y+1:(x=x+1,y=y+1));
-
}
-
}
-
drawpoint(x,y,color);
-
}
-
-
int mrand(int x,int y) //生成x到y之间的随机数。
-
{
-
int i;
-
struct timeval tpstart;
-
gettimeofday(&tpstart,NULL);
-
srand(tpstart.tv_usec); //微秒级产生随机数种子
-
return (rand()%(y-x+1)+x);
-
}
-
-
-
-
static int palette [] =
-
{
-
0x000000, 0xffe080, 0xffffff, 0xe0c0a0, 0x304050, 0x80b8c0
-
};
-
#define NR_COLORS (sizeof (palette) / sizeof (palette [0]))
-
-
struct ts_button {
-
int x, y, w, h;
-
char *text;
-
int flags;
-
#define BUTTON_ACTIVE 0x00000001
-
};
-
-
/* [inactive] border fill text [active] border fill text */
-
static int button_palette [6] =
-
{
-
1, 4, 2,
-
1, 5, 0
-
};
-
-
#define NR_BUTTONS 2
-
static struct ts_button buttons [NR_BUTTONS];
-
-
static void sig(int sig)
-
{
-
close_framebuffer();
-
fflush(stderr);
-
printf("signal %d caught\n", sig);
-
fflush(stdout);
-
exit(1);
-
}
-
-
static void button_draw (struct ts_button *button)
-
{
-
int s = (button->flags & BUTTON_ACTIVE) ? 3 : 0;
-
rect (button->x, button->y, button->x + button->w - 1,
-
button->y + button->h - 1, button_palette [s]);
-
fillrect (button->x + 1, button->y + 1,
-
button->x + button->w - 2,
-
button->y + button->h - 2, button_palette [s + 1]);
-
put_string_center (button->x + button->w / 2,
-
button->y + button->h / 2,
-
button->text, button_palette [s + 2]);
-
}
-
-
static int button_handle (struct ts_button *button, struct ts_sample *samp)
-
{
-
int inside = (samp->x >= button->x) && (samp->y >= button->y) &&
-
(samp->x < button->x + button->w) &&
-
(samp->y < button->y + button->h);
-
-
if (samp->pressure > 0) {
-
if (inside) {
-
if (!(button->flags & BUTTON_ACTIVE)) {
-
button->flags |= BUTTON_ACTIVE;
-
button_draw (button);
-
}
-
} else if (button->flags & BUTTON_ACTIVE) {
-
button->flags &= ~BUTTON_ACTIVE;
-
button_draw (button);
-
}
-
} else if (button->flags & BUTTON_ACTIVE) {
-
button->flags &= ~BUTTON_ACTIVE;
-
button_draw (button);
-
return 1;
-
}
-
-
return 0;
-
}
-
-
static void refresh_screen ()
-
{
-
int i;
-
-
fillrect (0, 0, xres - 1, yres - 1, 0);
-
put_string_center (xres/2, yres/4, "Enjoy the writing! ", 1);
-
put_string_center (xres/2, yres/4+20,"Touch screen to move crosshair", 2);
-
-
for (i = 0; i < NR_BUTTONS; i++)
-
button_draw (&buttons [i]);
-
}
-
-
int main()
-
{
-
int fd;
-
long int screensize;
-
int color=0x00ff0000;//榛樿?red
-
-
if((fd=open("/dev/fb0",O_RDWR))<0)
-
{
-
perror("fail to open");
-
return 0;
-
}
-
if (ioctl(fd,FBIOGET_FSCREENINFO, &finfo))
-
{
-
perror("fail to get");
-
return 0;
-
}
-
if (ioctl(fd,FBIOGET_VSCREENINFO, &vinfo))
-
{
-
perror("fail to get");
-
return 0;
-
}
-
screensize=vinfo.xres*vinfo.yres*vinfo.bits_per_pixel/8;
-
fbp=(unsigned char *)mmap(0,screensize,PROT_READ | PROT_WRITE,MAP_SHARED,fd, 0);
-
-
-
struct tsdev *ts;
-
int x, y;
-
unsigned int i;
-
unsigned int mode = 0;
-
-
char *tsdevice=NULL;
-
-
signal(SIGSEGV, sig);
-
signal(SIGINT, sig);
-
signal(SIGTERM, sig);
-
-
if ((tsdevice = getenv("TSLIB_TSDEVICE")) == NULL) {
-
#ifdef USE_INPUT_API
-
tsdevice = strdup ("/dev/input/event0");
-
#else
-
tsdevice = strdup ("/dev/touchscreen/ucb1x00");
-
#endif /* USE_INPUT_API */
-
}
-
-
ts = ts_open (tsdevice, 0);
-
-
if (!ts) {
-
perror (tsdevice);
-
exit(1);
-
}
-
-
if (ts_config(ts)) {
-
perror("ts_config");
-
exit(1);
-
}
-
-
if (open_framebuffer()) {
-
close_framebuffer();
-
exit(1);
-
}
-
-
x = xres/2;
-
y = yres/2;
-
-
for (i = 0; i < NR_COLORS; i++)
-
setcolor (i, palette [i]);
-
-
/* Initialize buttons */
-
memset (&buttons, 0, sizeof (buttons));
-
buttons [0].w = buttons [1].w = xres / 4;
-
buttons [0].h = buttons [1].h = 20;
-
buttons [0].x = xres / 4 - buttons [0].w / 2;
-
buttons [1].x = (3 * xres) / 4 - buttons [0].w / 2;
-
buttons [0].y = buttons [1].y = 10;
-
buttons [0].text = "Drag";
-
buttons [1].text = "Draw";
-
-
refresh_screen ();
-
-
while (1) {
-
struct ts_sample samp;
-
int ret;
-
-
/* Show the cross */
-
if ((mode & 15) != 1)
-
put_cross(x, y, 2 | XORMODE);
-
-
ret = ts_read(ts, &samp, 1); //璇诲叆
-
-
/* Hide it */
-
if ((mode & 15) != 1)
-
put_cross(x, y, 2 | XORMODE);
-
-
if (ret < 0) {
-
perror("ts_read");
-
close_framebuffer();
-
exit(1);
-
}
-
-
if (ret != 1)
-
{
-
continue;
-
}
-
for (i = 0; i < NR_BUTTONS; i++)
-
if (button_handle (&buttons [i], &samp))
-
switch (i) {
-
case 0:
-
mode = 0;
-
refresh_screen ();
-
break;
-
case 1:
-
mode = 1;
-
refresh_screen ();
-
break;
-
}
-
-
printf("%ld.%06ld: %6d %6d %6d\n", samp.tv.tv_sec, samp.tv.tv_usec,
-
samp.x, samp.y, samp.pressure);
-
-
-
-
if (samp.pressure > 0)
-
{
-
-
if (mode == 0x80000001)
-
-
-
{
-
color=mrand(0x00000000,0x00ffffff);//随机生成颜色,可能因屏幕不同而范围不同。
-
drawline(x,y,samp.x,samp.y,&color);
-
}
-
x = samp.x;
-
y = samp.y;
-
-
mode |= 0x80000000;
-
}
-
else
-
{
-
-
mode &= ~0x80000000;
-
}
-
}
-
close_framebuffer();
-
}
阅读(1333) | 评论(0) | 转发(0) |