📄 fps_app.c
字号:
/* app example of fps200 driver */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <termios.h>#include <errno.h>#include <linux/ioctl.h>#include <sys/ioctl.h>#include <string.h>#define PRINT_VERSION printf("version 1.0\n");/* use for ioctl */#define FPS200_IOC_MAGIC 'k'/* * S means "Set" through a ptr * G means "Get": reply by setting through a pointer * C means "Check" */#define FPS200_IOCSDTR _IOC(_IOC_WRITE, FPS200_IOC_MAGIC, 1, 1)#define FPS200_IOCSDCR _IOC(_IOC_WRITE, FPS200_IOC_MAGIC, 2, 1)#define FPS200_IOCSPGC _IOC(_IOC_WRITE, FPS200_IOC_MAGIC, 3, 1)#define FPS200_IOCGDTR _IOC(_IOC_READ, FPS200_IOC_MAGIC, 4, 1)#define FPS200_IOCGDCR _IOC(_IOC_READ, FPS200_IOC_MAGIC, 5, 1)#define FPS200_IOCGPGC _IOC(_IOC_READ, FPS200_IOC_MAGIC, 6, 1)#define FPS200_IOCFCAP _IOC(_IOC_READ, FPS200_IOC_MAGIC, 7, 4) // force capture#define FPS200_IOCGDATA _IOC(_IOC_READ, FPS200_IOC_MAGIC, 8, 4) // get kernel data#define FPS200_IOCEINT _IOC(_IOC_NONE, FPS200_IOC_MAGIC, 9, 0) // enable irq#define FPS200_IOCDINT _IOC(_IOC_NONE, FPS200_IOC_MAGIC, 10, 0) // disable irq#define FPS200_IOCCINT _IOC(_IOC_READ, FPS200_IOC_MAGIC, 11, 1) // check irq#define FPS200_IOCCRDY _IOC(_IOC_READ, FPS200_IOC_MAGIC, 12, 1) // check whether finish capture#define FPS200_IOCCLR _IOC(_IOC_NONE, FPS200_IOC_MAGIC, 13, 0) // clear all data#define FPS200_IOC_MAXNR 13/* for bmp file */typedef struct tagBITMAPFILEHEADER { unsigned short bfType; unsigned int bfSize; unsigned short bfReserved1; unsigned short bfReserved2; unsigned int bfOffBits;} BITMAPFILEHEADER;typedef struct tagBITMAPINFOHEADER { unsigned int biSize; int biWidth; int biHeight; unsigned short biPlanes; unsigned short biBitCount; unsigned int biCompression; unsigned int biSizeImage; int biXPelsPerMeter; int biYPelsPerMeter; unsigned int biClrUsed; unsigned int biClrImportant;} BITMAPINFOHEADER;typedef struct tagRGBQUAD { unsigned char rgbBlue; unsigned char rgbGreen; unsigned char rgbRed; unsigned char rgbReserved;} RGBQUAD;void byte_hex_to_up_ascii(char *str, unsigned char hex) { unsigned char tmp; tmp = hex >> 4; tmp = tmp & 0x0f; if(tmp > 0x09) tmp += '0'+7; else tmp += '0'; *str = tmp; tmp = hex & 0x0f; if(tmp > 0x09) tmp += '0'+7; else tmp += '0'; *(str+1) = tmp; *(str+2) = 0;}unsigned charbytetohex(char *str) { int num = 0; int len = strlen(str); int i; char c; int tag = 0; if(len < 2) return(0); if(len > 4) return(0); if((str[0]!='0') || (str[1]!='x')) return(0); for(i=2; i<len; i++) { if((str[i]==0x30) && (tag==0)) continue; if((str[i]>='0') && (str[i]<='9')) { tag = 1; num = num<<4; c = str[i]&0x0f; num += c; } else if((str[i]>='A') && (str[i]<='F')) { tag = 1; num = num<<4; c = str[i]-55; num += c; } else if((str[i]>='a') && (str[i]<='f')) { tag = 1; num = num<<4; c = str[i]-87; num += c; } else return(0); } return(num);}voidprint_help() { printf("Example: download [options]... [parameter]...\n"); printf("Options: \n"); printf(" -d, --device [DEVICENAME] set your device file path, default is '/dev/fps200'\n"); printf(" -h, --help print help\n"); printf(" -v, --version print version\n"); printf("Finished.\n");}void make_image(char *data, unsigned char fps_dtr, unsigned char fps_dcr, unsigned char fps_pgc) { char bmpfile[128]; char tmp[128]; BITMAPFILEHEADER header; BITMAPINFOHEADER info; RGBQUAD palette[256]; int i = 0; int bmpfd = 0; /* READY BMP FILE *//* set bitmap file header */ header.bfType = 0x4d42; header.bfSize = 0x13036; /* 14+40+256x4+256x300 = 77878 = 0x13036 */ header.bfReserved1 = 0x0; header.bfReserved2 = 0x0; header.bfOffBits = 0x436; /* 14+40+256x4 = 1078 = 0x436 *//* set bitmap file info header */ info.biSize = 40; info.biWidth = 256; info.biHeight = 300; info.biPlanes = 1; info.biBitCount = 8; info.biCompression = 0; info.biSizeImage = 76800; /* 256x300 */ info.biXPelsPerMeter = 0x400b; info.biYPelsPerMeter = 0x400b; info.biClrUsed = 0; info.biClrImportant = 0;/* set bitmap file palette */ for(i=0; i<256; i++) { palette[i].rgbBlue = i; palette[i].rgbGreen = i; palette[i].rgbRed = i; palette[i].rgbReserved = 0; } byte_hex_to_up_ascii(tmp, fps_dtr); strcpy(bmpfile, tmp); byte_hex_to_up_ascii(tmp, fps_dcr); strcat(bmpfile, tmp); byte_hex_to_up_ascii(tmp, fps_pgc); strcat(bmpfile, tmp); sprintf(tmp, "_%d-%d.bmp", 1, 300); strcat(bmpfile, tmp); if((bmpfd=creat(bmpfile, 0666)) == -1) { printf("Can't access bmpfile.\n"); exit(0); } if(write(bmpfd, &header, sizeof(BITMAPFILEHEADER)) < sizeof(BITMAPFILEHEADER)) { printf("error!\n"); exit(0); } if(write(bmpfd, &info, sizeof(BITMAPINFOHEADER)) < sizeof(BITMAPINFOHEADER)) { printf("error!\n"); exit(0); } if(write(bmpfd, palette, sizeof(RGBQUAD)*256) < sizeof(RGBQUAD)*256) { printf("error!\n"); exit(0); } for(i=0; i<300; i++) if(write(bmpfd, (data+(299-i)*256), 256) < 256) { printf("error!\n"); exit(0); }}intmain(int argc, char **argv) { int fd = 0; char cmd[128]; char param[128]; int i = 1; unsigned char chk = 0; int readparam = 0; char device[128]; unsigned char dtr, dcr, pgc; char data[76800]; strcpy(device, "/dev/fps200"); dtr = 0x23; dcr = 0x1; pgc = 0; while(i < argc) { i++; if(strlen(argv[i-1]) > 127) { printf("Too long Parameter, Error.\n"); print_help(); exit(0); } if(readparam == 1) { strcpy(param, argv[i-1]); if(param[0] == '-') { printf("Parameter Error.\n"); print_help(); exit(0); } if((strcmp(cmd, "--device")==0) || (strcmp(cmd, "-d")==0)) { strcpy(device, "/dev/"); strcat(device, param); } else if(strcmp(cmd, "--setdtr") == 0) { dtr = bytetohex(param); if(dtr > 0x7f) dtr = 0x7f; } else if(strcmp(cmd, "--setdcr") == 0) { dcr = bytetohex(param); if(dcr > 0x1f) dcr = 0x1f; } else if(strcmp(cmd, "--setpgc") == 0) { pgc = bytetohex(param); if(pgc > 0x0f) pgc = 0x0f; } else { printf("Parameter Error.\n"); print_help(); exit(0); } readparam = 0; } else { strcpy(cmd, argv[i-1]); if(strlen(cmd) <= 1) { printf("Parameter Error.\n"); print_help(); exit(0); } if(cmd[0] != '-') { printf("Parameter Error.\n"); print_help(); exit(0); } if((strcmp(cmd, "--help")==0) || (strcmp(cmd, "-h")==0)) { print_help(); exit(0); } if((strcmp(cmd, "--version")==0) || (strcmp(cmd, "-v")==0)) { PRINT_VERSION exit(0); } readparam = 1; } } if((readparam==1) && (argc!=1)) { printf("Parameter Error.\n"); print_help(); exit(0); } fd = open(device, O_RDWR); if(fd <= 0) { printf("Can't Open FPS200 Device, Error.\n"); exit(0); } ioctl(fd, FPS200_IOCSDTR, &dtr); ioctl(fd, FPS200_IOCSDCR, &dcr); ioctl(fd, FPS200_IOCSPGC, &pgc); //ioctl(fd, FPS200_IOCFCAP, data); readparam = 0; while(1) { chk = 0; while(chk != 1) ioctl(fd, FPS200_IOCCRDY, &chk); sleep(1); ioctl(fd, FPS200_IOCFCAP, data); if(readparam == 0) { make_image(data, dtr, dcr, pgc); printf("move your finger now !\n"); } chk = 0; while(chk != 1) ioctl(fd, FPS200_IOCCINT, &chk); sleep(1); ioctl(fd, FPS200_IOCCLR); ioctl(fd, FPS200_IOCEINT); if(readparam == 0) printf("ok!\n"); readparam = (readparam)?0:1; } close(fd); printf("All Done Successful !\n"); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -