这是我上一年参与的开发的项目

主要是利用到了单片机和Linux,去制作出能播放音乐和视频的电子相册

以下是我的main源码,开源出来,给需要用到的人使用,下面的源码没有版权限制,可自由使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <sys/mman.h>

#include <unistd.h>

#include <linux/input.h>

#include <stdlib.h>



unsigned char *lcd_ptr;

int ts_fd;

int get_xy(int *x, int *y)
{
struct input_event ts;
int x_read = 0, y_read = 1;

*x = 0, *y = 0;

//2, 打印 触摸屏相关信息
while (1) {
read(ts_fd, &ts, sizeof(ts));

//绝对坐标事件 (x, y) (x, x) (y, y) (y, x)
if (ts.type == EV_ABS) {
if (ts.code == ABS_X && x_read == 0) {
*x = ts.value;
x_read = 1;
y_read = 0;
}
if (ts.code == ABS_Y && y_read == 0) {
*y = ts.value;
x_read = 0;
y_read = 1;
}
}

if (*x != 0 && *y != 0) {
break;
}

/* if (ts.type == EV_KEY) {
if (ts.code == BTN_TOUCH) {
if (ts.value == KEY_RESERVED) {
break;
}
}
}
*/
}


}



int lcd_draw_bmp(const char *pathname, int x, int y, int w, int h)

{

int i, j;



//a 打开图片文件



int bmp_fd = open(pathname, O_RDWR);



//错误处理



if (bmp_fd == -1) {

printf("open bmp file failed!\n");

return -1;

}



//2,将图片数据加载到lcd屏幕

char header[54];



char rgb_buf[w*h*3];
//char argb_buf[800*480*4];

//a 将图片颜色数据读取出来



read(bmp_fd, header, 54);


/* 1, 文件头 54字节 文件信息存储 宽,高,位深度,... */


int pad = (4-(w*3)%4)%4;


/* 2, 位深度:24位:红,绿,蓝 */


for (i = 0; i < h; i++) {

//读取图片颜色数据

read(bmp_fd, &rgb_buf[w*i*3], w*3);

//跳过无效字节

lseek(bmp_fd, pad, SEEK_CUR);

}

//b 加载数据到lcd屏幕





// int r = 0xef, g = 0xab, b = 0xcd;

// int color = 0xefabcd;



//int color = b;



// 遇1结果则为1

// b : 00000000 00000000 00000000 11001101

// g : 00000000 00000000 10101011 00000000

// color : 00000000 00000000 10101011 11001101



// 1000 = 800*1+200

// 1800 = 800*2+200



//24 --- 32

for (j = 0; j < h; j++) {

for (i = 0; i < w; i++) {

/* 24 --》 32 */

/* 800*j+i 提取像素点 */

/* 3, bmp 存储方式:从左往右,从下往上 */

*(lcd_ptr+(800*(h-1-j+y)+i+x)*4+0) = rgb_buf[(j*w+i)*3+0];

*(lcd_ptr+(800*(h-1-j+y)+i+x)*4+1) = rgb_buf[(j*w+i)*3+1];

*(lcd_ptr+(800*(h-1-j+y)+i+x)*4+2) = rgb_buf[(j*w+i)*3+2];

*(lcd_ptr+(800*(h-1-j+y)+i+x)*4+3) = 0;

/*
* argb_buf[(800*(h-1-j+y)+i+x)*4+0] = rgb_buf[(j*w+i)*3+0];
* argb_buf[(800*(h-1-j+y)+i+x)*4+1] = rgb_buf[(j*w+i)*3+1];
* argb_buf[(800*(h-1-j+y)+i+x)*4+2] = rgb_buf[(j*w+i)*3+2];
* argb_buf[(800*(h-1-j+y)+i+x)*4+3] = 0;
*/

}

}





//3,关闭文件

//a 关闭图片文件

close(bmp_fd);



return 0;

}



int main(void)

{

//1,打开设备文件



int lcd_fd = open("/dev/ubuntu_lcd", O_RDWR);



//错误处理

if (lcd_fd == -1) {

printf("open lcd device failed!\n");

return -1;

}

ts_fd = open("/dev/ubuntu_event", O_RDWR);



//错误处理

if (ts_fd == -1) {

printf("open ts device failed!\n");

return -1;

}



//2,为lcd设备建立内存映射关系:镜子

lcd_ptr = mmap(NULL, 800*480*4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd_fd, 0);



if (lcd_ptr == MAP_FAILED) {

printf("mmap failed!\n");

return -1;

}

//显示背景
lcd_draw_bmp("backgound.bmp", 0, 0, 800, 480);
lcd_draw_bmp("next.bmp", 20, 390, 80, 80);
lcd_draw_bmp("prev.bmp", 680, 390, 80, 80);

char music_buf[4096];

const char *pic_name[] = {"1.bmp", "2.bmp", "3.bmp", "4.bmp"};
const char *music_name[] = {"1.mp3", "2.mp3", "3.mp3", "4.mp3"};

int count = 0;

int x, y;

while (1) {
get_xy(&x, &y);
printf("(%d, %d)\n", x, y);

/* 上一张图片切换的同时并播放上一首歌 */
if (x >= 0 && x < 400 && y >= 0 && y < 480) {
// 让当前歌停止然后播放下一首
system("killall -SIGKILL mplayer");

count--;
if (count == -1) {count = 3;}

lcd_draw_bmp(pic_name[count], 20, 10, 760, 370);

sprintf(music_buf, "mplayer %s &", music_name[count]);
system(music_buf);
}else if (x >= 400 && x < 800 && y >= 0 && y < 480) {
system("killall -SIGKILL mplayer");
count++;
if(count==4){count=-1; }
lcd_draw_bmp(pic_name[count],20,10,760,370);
sprintf(music_buf,"mplayer %s &",music_name[count]);
system(music_buf);
}
else if(x>=400&&x<800&&y>=0&&y<480)
{system("killall -SIGKILL mplayer");
}
}

munmap(lcd_ptr, 800*480*4);

//b 关闭设备文件

close(lcd_fd);



return 0;

}

//上面有些源码的格式可能有所偏差,需要自己去换行以下