完成电子相册的功能,最开始进入程序的时候,显示功能选择按钮,可以是自动播放或手动播放 自动播放:会自动播放指定的图片(播放1轮后自动回到主界面) 手动播放:左滑播放下一张,右滑播放上一张,有一个退出按钮,点击退出可以回到主界面
时间: 2025-06-09 08:04:10 浏览: 27
为了实现电子相册的功能,并在最开始进入程序时显示功能选择按钮(自动播放或手动播放),可以在现有代码基础上进行扩展。以下是具体的实现方案:
### 1. 功能选择界面
首先,需要在程序启动时显示一个功能选择界面,用户可以选择自动播放或手动播放。
### 2. 自动播放功能
自动播放功能会在指定的时间间隔内自动切换图片,播放一轮后自动返回主界面。
### 3. 手动播放功能
手动播放功能允许用户通过触摸屏幕的左右区域来切换图片,并提供一个退出按钮返回主界面。
### 代码实现
#### 1. 功能选择界面
在 `main` 函数中添加功能选择界面的绘制和处理逻辑。
```c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/input.h>
// 全局变量
int fd = 0; // 屏幕的文件描述符
int *plcd = NULL; // 映射后的地址
char *name[5] = {"6.bmp", "7.bmp", "8.bmp", "9.bmp", "10.bmp"};
int current_index = 0; // 记录当前显示图片的索引
// 初始化屏幕
void lcd_init() {
fd = open("/dev/fb0", O_RDWR);
if (fd == -1) {
printf("open lcd error!\n");
return;
}
plcd = (int *)mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (plcd == MAP_FAILED) {
printf("mmap failed!\n");
return;
}
}
// 关闭屏幕
void lcd_close() {
munmap(plcd, 800 * 480 * 4);
close(fd);
}
// 在指定位置显示一个指定颜色的点
void lcd_draw_point(int x, int y, int color) {
if (x < 0 || x >= 800 || y < 0 || y >= 480) {
printf("bro, you point out of the screen!\n");
return;
}
*(plcd + 800 * y + x) = color;
}
// 刷屏
void lcd_clear(int color) {
for (int y = 0; y < 480; y++) {
for (int x = 0; x < 800; x++) {
lcd_draw_point(x, y, color);
}
}
}
// 在指定位置显示一个指定大小和颜色的矩形
void lcd_draw_rect(int x0, int y0, int w, int h, int color) {
for (int y = y0; y < y0 + h; y++) {
for (int x = x0; x < x0 + w; x++) {
lcd_draw_point(x, y, color);
}
}
}
// 显示指定的BMP图片
void lcd_draw_bmp(char *name, int x0, int y0) {
int fd_bmp = open(name, O_RDWR);
if (fd_bmp == -1) {
printf("open bmp error!\n");
return;
}
int w, h, dep = 24;
lseek(fd_bmp, 0x12, SEEK_SET);
read(fd_bmp, &w, 4);
read(fd_bmp, &h, 4);
lseek(fd_bmp, 0x1C, SEEK_SET);
read(fd_bmp, &dep, 2);
lseek(fd_bmp, 0, SEEK_SET);
char buf[800 * 480 * 4 + 54] = {0};
read(fd_bmp, buf, 800 * 480 * 4 + 54);
close(fd_bmp);
int x, y, p = 54;
for (y = h - 1; y >= 0; y--) {
for (x = 0; x < w; x++) {
char b = buf[p++];
char g = buf[p++];
char r = buf[p++];
int color = r << 16 | g << 8 | b;
lcd_draw_point(x + x0, y + y0, color);
}
if (w * (dep / 8) % 4 != 0) {
p += 4 - w * (dep / 8) % 4;
}
}
}
// 判断手指是否点击了指定的位置
int get_touch() {
int fd_touch = open("/dev/input/event0", O_RDWR);
if (fd_touch == -1) {
printf("open touch failed!\n");
return -1;
}
struct input_event ev;
int x = -1, y = -1;
while (1) {
read(fd_touch, &ev, sizeof(ev));
if (ev.type == EV_ABS) {
if (ev.code == ABS_X) {
x = ev.value / 1.28;
} else if (ev.code == ABS_Y) {
y = ev.value / 1.25;
}
}
if (ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value == 0) {
break;
}
}
close(fd_touch);
return x * 1000 + y;
}
// 显示功能选择界面
void show_menu() {
lcd_clear(0x00FFFFFF);
lcd_draw_rect(0, 0, 400, 480, 0x0000FF00); // 自动播放按钮
lcd_draw_rect(400, 0, 400, 480, 0x00FF0000); // 手动播放按钮
}
// 自动播放功能
void auto_play() {
for (int i = 0; i < 5; i++) {
lcd_clear(0x00FFFFFF);
lcd_draw_bmp(name[i], 0, 0);
sleep(3); // 每张图片显示3秒
}
show_menu(); // 返回主界面
}
// 手动播放功能
void manual_play() {
while (1) {
int touch = get_touch();
if (touch >= 0 && touch <= 400 * 1000) { // 左滑
current_index = (current_index + 1) % 5;
lcd_clear(0x00FFFFFF);
lcd_draw_bmp(name[current_index], 0, 0);
} else if (touch >= 400 * 1000 && touch <= 800 * 1000) { // 右滑
current_index = (current_index + 4) % 5;
lcd_clear(0x00FFFFFF);
lcd_draw_bmp(name[current_index], 0, 0);
} else if (touch >= 800 * 1000) { // 退出
show_menu();
break;
}
}
}
int main() {
lcd_init();
show_menu();
while (1) {
int touch = get_touch();
if (touch >= 0 && touch <= 400 * 1000) { // 自动播放
auto_play();
} else if (touch >= 400 * 1000 && touch <= 800 * 1000) { // 手动播放
manual_play();
}
}
lcd_close();
return 0;
}
```
### 说明
1. **功能选择界面**:在 `main` 函数中调用 `show_menu` 函数显示功能选择界面,用户可以通过触摸屏幕的左右区域选择自动播放或手动播放。
2. **自动播放功能**:在 `auto_play` 函数中,程序会自动切换图片,每张图片显示3秒,播放一轮后返回主界面。
3. **手动播放功能**:在 `manual_play` 函数中,用户可以通过触摸屏幕的左右区域切换图片,触摸屏幕的右下角区域可以退出手动播放并返回主界面。
这样,程序就能实现用户选择自动播放或手动播放的功能,并在手动播放模式下提供退出按钮。
阅读全文
相关推荐















