#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> //open
#include <unistd.h> //sleep close
#include <linux/input.h>
#include <sys/mman.h>
#define adr1 "./picture/image1.bmp"
#define adr2 "./picture/image2.bmp"
#define adr3 "./picture/image3.bmp"
#define adr4 "./picture/image4.bmp"
#define adr5 "./picture/image5.bmp"
#define adr6 "./picture/image6.bmp"
#define adr7 "./picture/image7.bmp"
#define adr8 "./picture/image8.bmp"
void init(); // 初始化按键图片函数
void upimage(int a);
void change(int a);
void init()
{
int fd_lcd = open("./dev/fb0", O_RDWR);
if (-1 == fd_lcd)
{
printf("lcd=-12\n");
return 0;
}
printf("open LED ok\n");
int lt = open("./picture/lt.bmp", O_RDWR);
if (-1 == lt)
{
printf("lt=-11\n");
return 0;
}
char buf[160 * 160 * 3];
lseek(lt, 54, SEEK_SET);
read(lt, buf, 160 * 160 * 3);
int *p = (int *)mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd_lcd, 0);
int i = 0;
int x = 0;
int y = 0;
int color;
int r, g, b;
for (y = 160; y < 320; y++)
{
for (x = 20; x < 180; x++)
{
b = buf[i++];
g = buf[i++];
r = buf[i++];
color = b | g <<8| r << 16;
p[y * 800 + x] = color;
}
}
printf(" lt ok\n");
int rt = open("./picture/rt.bmp", O_RDWR);
if (-1 == rt)
{
printf("rt=-11\n");
return 0;
}
lseek(rt, 54, SEEK_SET);
read(rt, buf, 160 * 160 * 3);
i = 0;
for (y = 160; y < 320; y++)
{
for (x = 620; x < 780; x++)
{
b = buf[i++];
g = buf[i++];
r = buf[i++];
color = b | g <<8| r << 16;
p[y * 800 + x] = color;
}
}
printf(" lt ok\n");
int sp = open("./picture/stop.bmp", O_RDWR);
if (-1 == sp)
{
printf("sp=-11\n");
return 0;
}
lseek(sp, 54, SEEK_SET);
read(sp, buf, 100 * 100 * 3);
i = 0;
for (y = 20; y < 120; y++)
{
for (x = 20; x < 120; x++)
{
b = buf[i++];
g = buf[i++];
r = buf[i++];
color = b | g << 8 | r << 16;
p[y * 800 + x] = color;
}
}
close(lt);
close(rt);
close(sp);
close(fd_lcd);
munmap(p, sizeof(p));
}
void upimage(int a)
{
printf("a=%d\n", a);
int img = 1;
if (a == 0)
{
img = open(adr1, O_RDWR);
}
if (a == 1)
{
img = open(adr2, O_RDWR);
}
if (a == 2)
{
img = open(adr3, O_RDWR);
}
if (a == 3)
{
img = open(adr4, O_RDWR);
}
if (a == 4)
{
img = open(adr5, O_RDWR);
}
if (a == 5)
{
img = open(adr6, O_RDWR);
}
if (a == 6)
{
img = open(adr7, O_RDWR);
}
if (a == 7)
{
img = open(adr8, O_RDWR);
}
if (-1 == img)
{
printf("img=-11\n");
return 0;
}
printf("open image ok\n");
char buf[400 * 400 * 3];
lseek(img, 54, SEEK_SET);
read(img, buf, 400 * 400 * 3);
int fd_lcd = open("./dev/fb0", O_RDWR);
if (-1 == fd_lcd)
{
printf("lcd=-12\n");
return 0;
}
printf("lcd=%d\n", 1);
int *lcd_p = (int *)mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd_lcd, 0);
int i = 0;
int x = 0;
int y = 0;
int color;
int r, g, b;
for (y = 40; y < 440; y++)
{
for (x = 200; x < 600; x++)
{
b = buf[i++];
g = buf[i++];
r = buf[i++];
color = b | g <<8| r << 16;
lcd_p[y * 800 + x] = color;
}
}
printf("main image ok \n");
close(img);
close(fd_lcd);
munmap(lcd_p, sizeof(lcd_p));
}
void change(int a)
{
printf("a1=%d",a);
// 1:打开我们的一个触摸屏文件
int fd_ts = open("/dev/input/event0", O_RDWR);
if (-1 == fd_ts)
{
printf("打开触摸屏失败\n");
}
printf("open tslcd ok\n");
int cnt = 0;
int x, y;
int b[2];
struct input_event init_eventStruct;
while (1)
{
memset(&init_eventStruct, 0, sizeof(struct input_event));
read(fd_ts, &init_eventStruct, sizeof(struct input_event));
// 触摸屏默认的一个xy的大小是1024*600
if (init_eventStruct.type == EV_ABS)
{
if (init_eventStruct.code == ABS_X)
{
x = init_eventStruct.value * 800 / 1024;
// x = (int)(800*1.0)/1024*init_eventStruct.value;
// printf("x0= %d\n", x);
cnt++;
// printf("cntx=%d\n",cnt);
}
if (init_eventStruct.code == ABS_Y)
{
y = init_eventStruct.value * 480 / 600;
// y = (int)(480*1.0)/600*init_eventStruct.value;
// printf("y0 = %d\n", y);
cnt++;
// printf("cnty=%d\n",cnt);
}
}
printf("cnt=%d\n",cnt);
if(cnt>2){
cnt=0;
}
if (cnt == 2)
{
if (x > 20 && x < 180 && y > 160 && y < 320)
{
a++;
printf("inc1 image ok\n");
upimage(a % 8);
printf("inc image ok\n");
cnt = 0;
}
if (x > 620 && x < 780 && y > 160 && y < 320)
{
if(a==0)
a=7;
a--;
printf("dec1 image ok\n");
upimage(a % 8);
printf("dec image ok\n");
cnt = 0;
}
if (x > 20 && x < 120 && y > 20 && y < 120)
{
break;
printf("stop image ok\n");
}
}
}
// printf("x= %d\n", x);
// printf("y= %d\n", y);
close(fd_ts);
}
int main(int argc, char const *argv[])
{
int i = 1;
int x=1000;
init();
printf("init ok \n");
upimage(i);
printf("upimage ok \n");
change(x);
printf("change ok \n");
return 0;
}