-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathulk_photo_frame.c~
496 lines (394 loc) · 10.8 KB
/
ulk_photo_frame.c~
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/dir.h>
#include <linux/fb.h>
#include <ctype.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <signal.h>
#include <linux/input.h>
#include <sys/ioctl.h>
#include <string.h>
// For CLCD
#define NODE_EXP "/dev/fpga_char_lcd"
#define LCD_ON 1
#define LCD_CLEAR 2
#define LCD_SET_HOME 3
#define LCD_CURSOR_BLINK 4
#define LCD_DATA_WRITE 5
#define LCD_OFF 6
//For 7_segment_LED_display
#define NODE_EXP1 "/dev/7seg_led"
#define SEG_7_LED_ON 1
#define SEG_7_LED_WRITE 2
#define SEG_7_LED_CLEAR 3
#define SEG_7_LED_OFF 4
unsigned short Type;
unsigned long Size; /* file size in bytes */
struct FileHeader {
unsigned short Reserved1; /* 0 */
unsigned short Reserved2; /* 0 */
unsigned long OffBits; /* offset to bitmap */
unsigned long StructSize; /* size of this struct (40) */
unsigned long Width; /* bmap width in pixels */
unsigned long Height; /* bmap height in pixels */
unsigned short Planes; /* num planes - always 1 */
unsigned short BitCount; /* bits per pixel */
unsigned long Compression; /* compression flag */
unsigned long SizeImage; /* image size in bytes */
long XPelsPerMeter; /* horz resolution */
long YPelsPerMeter; /* vert resolution */
unsigned long ClrUsed; /* 0 -> color table size */
unsigned long ClrImportant; /* important color count */
};
struct RGBQUAD {
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
};
extern int alphasort();
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
char *fbp = 0;
int fbfd = 0;
long int screensize;
char *fname[100];
int pid;
int xpos,ypos;
int exp_dev1= 0,exp_dev= 0;
int res=0;
void ex_program(int sig)
{
munmap(fbp, screensize);
close(fbfd);
system("pkill -9 -f X");
exit(0);
}
/******************************************************************************
*
******************************************************************************/
void touch()
{
int fd;
char buff[256] = "Name";
int i,rb;
struct input_event ev;
struct input_absinfo ab;
if ((fd = open("/dev/input/touchscreen0", O_RDONLY)) < 0)
{
printf ("Error Opening the Device\n");
}
ioctl(fd, EVIOCGNAME(sizeof(buff)), buff);
printf("Touch Input device name: \"%s\"\n", buff);
for (i = 0; i < 5; i++)
{
rb=read(fd,&ev,sizeof(struct input_event));
printf ("After read\n");
if ((ev.type == 1) && (ev.code == 330))
{
printf ("Touch Event = %d\n",ev.code);
}
if ((ev.type == 3) && (ev.code == 0))
{
printf ("X Value = %d\n",ev.value);
xpos=ev.value;
}
if ((ev.type == 3) && (ev.code == 1))
{
printf ("Y Value = %d\n",ev.value);
ypos=ev.value;
}
if ((ev.type == 3) && (ev.code == 24))
{
printf ("Touch Pressure = %d\n",ev.value);
}
}
}
void openclcd()
{
/* open as blocking mode*/
exp_dev = open(NODE_EXP, O_RDWR);
if (exp_dev < 0)
{
fprintf(stderr, "Open error: %s\n", NODE_EXP);
}
if (ioctl(exp_dev,LCD_ON, NULL) < 0)
{
printf("Error in Writing the LCD \r\n");
close(exp_dev);
}
if (ioctl(exp_dev,LCD_CLEAR, NULL) < 0)
{
printf("Error in Clearing the LCD \r\n");
close(exp_dev);
}
}
void clcd(char *str)
{
if (ioctl(exp_dev,LCD_SET_HOME, NULL) < 0 )
{
printf("Error in setting the cursor in home\
position \r\n");
close(exp_dev);
}
if (ioctl(exp_dev,LCD_CURSOR_BLINK, NULL) < 0 )
{
printf("Error in Curosr blinking \r\n");
close(exp_dev);
}
if (ioctl(exp_dev,LCD_DATA_WRITE,str) < 0 )
{
printf("Error in writing the LCD \r\n");
close(exp_dev);
}
}
void closeclcd(){
if (ioctl(exp_dev,LCD_OFF, NULL) < 0 )
{
printf("Error in Switching OFF the LCD \r\n");
close(exp_dev);
}
close(exp_dev);
}
void seg(int i)
{
/* open as blocking mode */
exp_dev1 = open(NODE_EXP1, O_RDWR);
if (exp_dev1 < 0){
fprintf(stderr, "Open error: %s\n", NODE_EXP1);
}
if (res = ioctl(exp_dev1,SEG_7_LED_ON, NULL) < 0 ){
printf("%d---> Error in switching OFF the LED \r\n",res);
close(exp_dev1);
}
if (res=ioctl(exp_dev1,SEG_7_LED_WRITE,i) < 0 )
{
printf("%d---> Error in writing to the 7-Seg LED \r\n",res);
close(exp_dev1);
}
}
void closeled()
{
if (res=ioctl(exp_dev1,SEG_7_LED_CLEAR, NULL) < 0 ){
printf("%d---> Error in Clearing the LED \r\n",res);
close(exp_dev1);
}
if (res=ioctl(exp_dev1,SEG_7_LED_OFF, NULL) < 0 ){
printf("%d---> Error in switching off the LED \r\n",res);
close(exp_dev1);
}
close(exp_dev1);
}
int show_bmp(int count,int delay,int q)
{
FILE *fp;
unsigned long int location = 0, BytesPerLine = 0;
unsigned long pixel, p1;
struct FileHeader *Header;
unsigned int t,x,y;
unsigned long size, bytes_read;
unsigned char Bmp, dummy, red, blue, green;
int i,hindex,index,j;
struct RGBQUAD Palette[256];
unsigned long *bgr_buff;
char buff[50];
j=0;
(void) signal(SIGINT, ex_program);
while(j < 1) {
fp = fopen(fname[q],"rb");
Header = (struct FileHeader *) malloc (sizeof(struct FileHeader));
if (!Header) {
perror("Error:");
exit(1);
}
if (!fp) {
printf ("Error opening source file\r\n");
perror ("Error");
exit (1);
}
printf("\nfilename: %s\n\n",fname[q]);
fread(&Type, sizeof(Type), 1, fp);
bytes_read = sizeof(Type);
fread(&Size, sizeof(Size), 1, fp);
bytes_read += sizeof(Size);
if ((fread(Header, sizeof(struct FileHeader), 1, fp)) == -1) {
printf ("Error: Unable to read File header.\r\n");
exit (1);
}
bytes_read += sizeof(struct FileHeader);
while (bytes_read < Header->OffBits) {
if (fread(&dummy,sizeof(dummy),1,fp)!=1) {
printf("Error seeking to bitmap data\n");
exit(-1);
}
++bytes_read;
}
size = Header->Width * Header->Height;
printf ("BMP Width = %d\tBMP Height = %d\n", Header->Width, Header->Height);
printf ("Bit Count = %d\n", Header->BitCount);
index=0;
if (Header->BitCount == 24) {
bgr_buff = (unsigned long *) malloc (size * sizeof(unsigned long));
for (i = 0; i < size; i++) {
blue = fgetc(fp);
green = fgetc(fp);
red = fgetc(fp);
p1 = 0;
p1 |= red;
p1 = p1 << 16;
pixel = p1;
p1 = 0;
p1 |= green;
p1 = p1 << 8;
pixel |= p1;
p1 = 0;
p1 |= blue;
pixel |= p1;
bgr_buff [index] = pixel;
index ++;
}
// At this point bgr_buff contains the RGB values for the pixels defined by height & width of the BMP file.
hindex=0;
y = Header->Height-1;
while(y > 0) {
for(x=0; x < Header->Width ; x++) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +(y+vinfo.yoffset) * finfo.line_length;
*((unsigned long *)(fbp + location)) = bgr_buff [hindex] ;
hindex++;
}
--y;
}
free(bgr_buff);
}
free(Header);
printf ("\n[Done]\n\r");
j++;
fclose (fp);
sleep(delay);
}
return( 0 );
}
/******************************************************************************
*
******************************************************************************/
int file_select(const struct direct *entry)
{
char *ptr;
if ((strcmp(entry->d_name, ".")== 0) ||
(strcmp(entry->d_name, "..") == 0))
return (0);
/* Check for filename extensions */
ptr = rindex(entry->d_name, '.');
if ((ptr != NULL) &&((strcmp(ptr, ".bmp")==0)))
return (1);
else
return(0);
}
/******************************************************************************
*
******************************************************************************/
int main ( int argc, char *argv[] )
{
unsigned long size, bytes_read;
struct direct **files;
char *pathname;
int count=0,delay =0,i;
pid=getpid();
if (argc <= 2) {
printf ("Usage:./ulk_photo_frame <source bitmap directory> <delay>\r\n");
exit (2);
}
pathname=argv[1];
delay=atoi(argv[2]);
size =pathconf(pathname, _PC_PATH_MAX);
if (!(getcwd(pathname,(size_t)size))) {
printf("Error getting path\n");
return 0;
}
if(!(chdir(pathname)))
count = scandir(pathname, &files, file_select, alphasort);
if (count <= 0) {
printf("No files in this directory\n");
return 0;
}
printf("Number of files = %d\n",count);
printf("*****Files are*****\n");
for (i=1; i<count+1; ++i) {
printf("%s\n",files[i-1]->d_name);
fname[i-1]=files[i-1]->d_name;
}
printf("\n");
// Open the file for reading and writing
fbfd = open("/dev/fb0", O_RDWR);
if (!fbfd) {
printf("Error cannot open framebuffer device.\n");
exit(1);
}
// Get fixed screen information
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
printf("Error reading fixed information.\n");
exit(2);
}
// Get variable screen information
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
printf("Error reading variable information.\n");
exit(3);
}
printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );
screensize = 0;
// Figure out the size of the screen in bytes
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
// Map the device to memory
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,fbfd, 0);
if ((int)fbp == -1) {
printf("Error failed to map framebuffer device to memory.\n");
exit(4);
}
printf("into show_bmp \n");
show_bmp(count,delay,0);
touch();
show_bmp(count,delay,1);
touch();
show_bmp(count,delay,2);
touch();
openclcd();
if(xpos<2000&&ypos<2000)
{ show_bmp(count,delay,3);
seg(0x500);
clcd("destination: Tanjore");
}
else if(xpos>2000&&ypos<2000)
{
show_bmp(count,delay,4);
seg(0x700);
clcd("destination: Madurai");
}
else if(xpos<2000&&ypos>2000)
{
show_bmp(count,delay,5);
seg(0x1000);
clcd("destination: Kanniyakumari");
}
else
{
show_bmp(count,delay,6);
seg(0x900);
clcd("destination: Tuticorin");
}
touch();
closeclcd();
closeled();
show_bmp(count,delay,7);
openclcd();
clcd("You Have paid");
sleep(2);
touch();
closeclcd();
show_bmp(count,delay,8);
munmap(fbp, screensize);
close(fbfd);
return 0;
}