forked from indev29/egis0570
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
197 lines (164 loc) · 5.11 KB
/
test.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
#include <stdio.h>
#include <stdlib.h>
#include "misc.h"
#include "init.h"
#include <libusb.h>
#define DEV_VID 0x1c7a
#define DEV_PID 0x0570
#define DEV_CONF 0x1
#define DEV_INTF 0x0
#define DEV_EPOUT 0x04
#define DEV_EPIN 0x83
int _num = 0;
void printData(unsigned char * data, int length)
{
for (int i = 0; i < length; ++i)
printf("%#04x ", data[i]);
printf("\n");
}
void finger_status(unsigned char * data)
{
}
void writeRaw(const char * filename, unsigned char * data, int length)
{
FILE * fp = fopen(filename, "w");
if (fp == NULL)
{
perror("Error opening file");
return;
}
fwrite(data, sizeof(unsigned char), length, fp);
fclose(fp);
}
void writeImg(const char * filename, unsigned char * data, int width, int height)
{
const char format_mark[] = "P5";
FILE * fp = fopen(filename, "w");
if (fp == NULL)
{
perror("Error opening file");
return;
}
fprintf(fp, "%s\n%d %d\n%d\n", format_mark, width, height, 255);
/*for (int i = 0; i < width * height / 2; ++i)
{
fputc(data[i] & 0x0F, fp);
fputc(' ', fp);
fputc(data[i] & 0xF0, fp);
fputc(' ', fp);
}*/
int i = 0;
for (int x = 0; x < height; ++x)
{
for (int y = 0; y < width-1; ++y, ++i)
{
fputc(data[i], fp);
/* if (y < width - 1)
fputc(' ', fp); */
}
fputc('\n', fp);
}
fclose(fp);
}
void imgInfo(unsigned char * data, int length)
{
int res = 0;
int min = data[0], max = data[0];
for (int i = 0; i < length; ++i)
{
res += data[i];
if (data[i] < min)
min = data[i];
if (data[i] > max)
max = data[i];
}
printf("min: %d | max: %d | avg: %d\n", min, max, res / length);
}
int main(int argc, char * argv[])
{
libusb_context * cntx = NULL;
if (libusb_init(&cntx) != 0)
perror_exit("Could not init context");
//libusb_set_debug(cntx, LIBUSB_LOG_LEVEL_DEBUG);
libusb_device_handle * handle = libusb_open_device_with_vid_pid(cntx, DEV_VID, DEV_PID);
if (handle == NULL)
perror_exit("Device not opened");
if (libusb_kernel_driver_active(handle, DEV_INTF) == 1)
libusb_detach_kernel_driver(handle, DEV_INTF);
if (libusb_set_configuration(handle, DEV_CONF) != 0)
perror_exit("Could not set configuration");
if (libusb_claim_interface(handle, DEV_INTF) != 0)
perror_exit("Could not claim device interface");
libusb_reset_device(handle);
// TRANSFER
int initLen = sizeof(init) / sizeof(init[0]);
int initPktSize = sizeof(init[0]);
unsigned char data[32512];
int length = sizeof(data);
int transferred;
for (int i = 0; i < initLen; ++i)
{
if (libusb_bulk_transfer(handle, DEV_EPOUT, init[i], initPktSize, &transferred, 0) != 0)
perror_exit("'Out' transfer error");
if (libusb_bulk_transfer(handle, DEV_EPIN, data, length, &transferred, 0) != 0)
perror_exit("'In' transfer error");
//printf("Read %d\n", transferred);
}
imgInfo(data, transferred);
writeRaw("scans/raw.bin", data, transferred);
// 1 bpp
writeImg("scans/254x128.pgm", data, 254, 128);
writeImg("scans/256x127.pgm", data, 256, 127);
writeImg("scans/508x64.pgm", data, 508, 64);
writeImg("scans/1016x32.pgm", data, 1016, 32);
writeImg("scans/2032x16.pgm", data, 2032, 16);
writeImg("scans/115x284.pgm", data, 115, 284);/* The proper image size is 114*57 and 5 image , the image below is have taken later about 0.0004 s */
writeImg("scans/128x254.pgm", data, 128, 254);
writeImg("scans/127x256.pgm", data, 127, 256);
writeImg("scans/64x508.pgm", data, 64, 508);
writeImg("scans/32x1016.pgm", data, 32, 1016);
writeImg("scans/16x2032.pgm", data, 16, 2032);
// 0.5 bpp
/*writeImg("scans/min/0_32x2032.pgm", data, 32, 2032);
writeImg("scans/min/0_64x1016.pgm", data, 64, 1016);
writeImg("scans/min/0_127x512.pgm", data, 127, 512);
writeImg("scans/min/0_128x508.pgm", data, 128, 508);
writeImg("scans/min/0_254x256.pgm", data, 254, 256);
writeImg("scans/min/0_2032x32.pgm", data, 2032, 32);
writeImg("scans/min/0_1016x64.pgm", data, 1016, 64);
writeImg("scans/min/0_512x127.pgm", data, 512, 127);
writeImg("scans/min/0_508x128pgm", data, 508, 128);
writeImg("scans/min/0_256x254.pgm", data, 256, 254);*/
/*
int repeatLen = sizeof(repeat) / sizeof(repeat[0]);
int repeatPktSize = sizeof(init[0]);
while (1)
{
for (int i = 0; i < repeatLen; ++i)
{
if (libusb_bulk_transfer(handle, DEV_EPOUT, repeat[i], repeatPktSize, &transferred, 0) != 0)
perror_exit("'Out' transfer error");
if (libusb_bulk_transfer(handle, DEV_EPIN, data, length, &transferred, 0) != 0)
perror_exit("'In' transfer error");
//printf("Read %d\n", transferred);
}
//imgInfo(data, transferred);
//printImg(data, 128, 254);
writeImg("scans/254x128.pgm", data, 254, 128);
writeImg("scans/256x127.pgm", data, 256, 127);
writeImg("scans/508x64.pgm", data, 508, 64);
writeImg("scans/1016x32.pgm", data, 1016, 32);
writeImg("scans/2032x16.pgm", data, 2032, 16);
writeImg("scans/128x254.pgm", data, 128, 254);
writeImg("scans/127x256.pgm", data, 127, 256);
writeImg("scans/64x508.pgm", data, 64, 508);
writeImg("scans/32x1016.pgm", data, 32, 1016);
writeImg("scans/16x2032.pgm", data, 16, 2032);
}
*/
libusb_release_interface(handle, DEV_INTF);
libusb_attach_kernel_driver(handle, DEV_INTF);
libusb_close(handle);
libusb_exit(cntx);
return 0;
}