diff --git a/device/gxx-linux/imgproc/imageencode.cpp b/device/gxx-linux/imgproc/imageencode.cpp index da70500..2390c94 100644 --- a/device/gxx-linux/imgproc/imageencode.cpp +++ b/device/gxx-linux/imgproc/imageencode.cpp @@ -24,16 +24,16 @@ MemoryPtr BmpImageEncode::encode(cv::Mat &image) JpegImageEncode::JpegImageEncode(bool bwimg) { - if (!bwimg) - { - compression_params.push_back(cv::IMWRITE_JPEG_QUALITY); - compression_params.push_back(100); - } - else{ - compression_params.push_back(/*CV_IMWRITE_PNG_STRATEGY*/17); - compression_params.push_back(cv::IMWRITE_PNG_STRATEGY_FIXED); - } - m_bwimg = bwimg; + // if (!bwimg) + // { + // compression_params.push_back(cv::IMWRITE_JPEG_QUALITY); + // compression_params.push_back(100); + // } + // else{ + // compression_params.push_back(/*CV_IMWRITE_PNG_STRATEGY*/17); + // compression_params.push_back(cv::IMWRITE_PNG_STRATEGY_FIXED); + // } + // m_bwimg = bwimg; } JpegImageEncode::~JpegImageEncode() @@ -43,7 +43,11 @@ MemoryPtr JpegImageEncode::encode(cv::Mat &image) { VectorMemroyPtr mem = VectorMemroyPtr(new VectorMemroy()); StopWatch sw; - cv::imencode(m_bwimg ? ".png" : ".jpg", image, mem->buf(), compression_params); + printf("encode image(%d * %d): %p - %p\n", image.cols, image.rows, image.data, image.dataend); + // cv::imwrite("beforcompress.jpg",image); + // cv::imencode(m_bwimg ? ".png" : ".jpg", image, mem->buf(), compression_params); + cv::imencode(".png", image, mem->buf(), compression_params); + //printf("encode time = %0.2f \n", sw.elapsed_ms()); return mem; } diff --git a/device/gxx-linux/imgproc/imemory.h b/device/gxx-linux/imgproc/imemory.h index 65b8da7..9d93a48 100644 --- a/device/gxx-linux/imgproc/imemory.h +++ b/device/gxx-linux/imgproc/imemory.h @@ -3,6 +3,10 @@ #include #include + +#define ASYNC_EP + + class IMemory { public: diff --git a/device/gxx-linux/packages/common.pkg/include/commondef.h b/device/gxx-linux/packages/common.pkg/include/commondef.h index 773df32..386b2ea 100644 --- a/device/gxx-linux/packages/common.pkg/include/commondef.h +++ b/device/gxx-linux/packages/common.pkg/include/commondef.h @@ -1,32 +1,34 @@ #pragma once #include #include -#include - +#include #define FWVersionEN -#define G200 +#define G400 #ifdef G200 -#define FWVersion "G2393A1225" +#define FWVersion "G2393A1215" #define SERIALNUM "G2396021071101" +#elif defined G300 +#define FWVersion "G3393C0303" +#define SERIALNUM "G3396021071101" +#elif defined G400 +#define FWVersion "G4393C0303" +#define SERIALNUM "G4396021071101" #else -#define FWVersion "G1393A1225" +#define FWVersion "G1393A1215" #define SERIALNUM "G1396021071101" #endif #ifdef TEST_MODE #define HG_SAVEIMG cv::imwrite -#define HG_LOG printf #else -#define HG_LOG #define HG_SAVEIMG #endif //#ifdef FWVersionEN //#pragma warnning("WARNNING! current fareware version is :" FWVersion) //#endif typedef unsigned int u32; -typedef unsigned int uint_32; typedef unsigned char u8; typedef unsigned char byte; typedef unsigned short u16; @@ -42,6 +44,7 @@ typedef enum Error_Status : uint32_t NO_error = 0, Dog_error = 1, Size_error = 2, + Img_Detecting = 0xffff, }; typedef struct ImgStatus @@ -64,7 +67,10 @@ typedef union HG_ScanConfig unsigned int screw_detect_level : 3; //第十四位 unsigned int iscorrect_mode : 1; unsigned int is_autopaper : 1; - unsigned int unused_one : 4; + unsigned int is_textcorrect : 1; + unsigned int is_fixedpaper : 1; + unsigned int en_anlogic_key : 1; + unsigned int en_autosize : 1; unsigned int pc_correct : 1; unsigned int enable_sizecheck : 1; unsigned int enabledsp_cache : 1; @@ -130,7 +136,7 @@ struct MotorBoardGlue struct ScannerNativeParam { - u32 TotalScanned; //扫描总计? + u32 TotalScanned; //扫描总计数 u32 RollerNum; //滚轴计数 u32 H_ratio; //横向修正系数 u32 V_ratio; //纵向修正系数 @@ -138,13 +144,13 @@ struct ScannerNativeParam u32 FeedErrorTimes; //搓纸失败次数 u32 DoubleFeedTimes; //双张次数 u32 ScanSessionsCount; //总计扫描次数 - u16 gray_sp; //灰度sp? - u16 color_sp; //彩色sp? - int sleeptime; //休眠模式时间 -1 不休? - std::string SerialNum; //序列? - std::string Token; //识别? - u32 clr_maxbright; //彩色平场校正最大亮? - u32 gray_maxbright; //灰度平场校正最大亮? + u16 gray_sp; //灰度sp值 + u16 color_sp; //彩色sp值 + int sleeptime; //休眠模式时间 -1 不休眠 + std::string SerialNum; //序列号 + std::string Token; //识别码 + u32 clr_maxbright; //彩色平场校正最大亮度 + u32 gray_maxbright; //灰度平场校正最大亮度 u32 speedmode; //速度模式 G100 0->70 1->80 2->90 3->100 G200 0->100 1->110 2->120 3->130 u16 Vid; //USB vid u16 Pid; //USB Pid @@ -236,9 +242,9 @@ enum class PaperSize G400_LETTER, G400_LETTERR, G400_LONGLETTER = 16, - G400_MAXSIZE = 16, + G400_MAXSIZE = 17, G400_AUTO = 16, - G400_MAXAUTO = 16 + G400_MAXAUTO = 18 }; struct HGSize @@ -254,7 +260,7 @@ struct HGSize } }; -//usb int 端点 返回消息的生产�? +//usb int 端点 返回消息的生产者 enum HGType { MtBoard = 1, @@ -263,9 +269,11 @@ enum HGType IMG, AutoCorrect, STOPSCAN, + CMD_OUT, + MBEvent, }; -//usb int 端点通信结构? +//usb int 端点通信结构体 struct HGIntInfo { HGType From; @@ -275,6 +283,9 @@ struct HGIntInfo enum SpeedMode { + PPM40 = 40, + PPM50 = 50, + PPM60 = 60, PPM70 = 70, PPM80 = 80, PPM90 = 90, @@ -284,6 +295,18 @@ enum SpeedMode PPM130 = 130 }; + +typedef struct +{ + u32 colorSp_200; + u32 graySp_200; + u32 colorSp_300; + u32 graySp_300; + u32 colorSp_600; + u32 graySp_600; + u32 motorSpeed; +} DunNanCisSp; + typedef struct { u32 colorSp; @@ -291,21 +314,80 @@ typedef struct u32 motorSpeed; } CisSp; + +static std::map cameraparmSp = { +#ifdef G100 + {PPM70, {.colorSp_200 = 1341 , .graySp_200= 4030,.colorSp_300 = 1377 ,.graySp_300 = 4133,.colorSp_600 = 1701 ,.graySp_600 = 5105 , 1}}, + {PPM80, {.colorSp_200 = 1202 , .graySp_200= 3609,.colorSp_300 = 1230 ,.graySp_300 = 3691,.colorSp_600 = 1701 ,.graySp_600 = 5105 , 2}}, + {PPM90, {.colorSp_200 = 1024 , .graySp_200= 3072,.colorSp_300 = 1110 ,.graySp_300 = 3330,.colorSp_600 = 1701 ,.graySp_600 = 5105 , 0}}, + {PPM100, {.colorSp_200 = 847 , .graySp_200= 2541,.colorSp_300 = 1011 ,.graySp_300 = 3036,.colorSp_600 = 1701 ,.graySp_600 = 5105 , 3}},//G100 3399 暂无100 ppm + {PPM110, {.colorSp_200 = 847 , .graySp_200= 2541,.colorSp_300 = 1011 ,.graySp_300 = 3036,.colorSp_600 = 1701 ,.graySp_600 = 5105 , 3}}, +#elif defined G200 + {PPM100, {.colorSp_200 = 838 , .graySp_200= 2514,.colorSp_300 = 895 ,.graySp_300 = 2689,.colorSp_600 = 1705 ,.graySp_600 = 5117 , 0}}, + {PPM110, {.colorSp_200 = 838 , .graySp_200= 2514,.colorSp_300 = 895 ,.graySp_300 = 2689,.colorSp_600 = 1705 ,.graySp_600 = 5117 , 1}}, + {PPM120, {.colorSp_200 = 838 , .graySp_200= 2514,.colorSp_300 = 895 ,.graySp_300 = 2689,.colorSp_600 = 1705 ,.graySp_600 = 5117 , 3}}, + {PPM130, {.colorSp_200 = 838 , .graySp_200= 2514,.colorSp_300 = 895 ,.graySp_300 = 2689,.colorSp_600 = 1705 ,.graySp_600 = 5117 , 2}} //默认 140 +#elif defined G300 + {PPM40, {.colorSp_200 = 838 , .graySp_200= 2514,.colorSp_300 = 895 ,.graySp_300 = 2689,.colorSp_600 = 1705 ,.graySp_600 = 5117 , 0}}, + {PPM50, {.colorSp_200 = 838 , .graySp_200= 2514,.colorSp_300 = 895 ,.graySp_300 = 2689,.colorSp_600 = 1705 ,.graySp_600 = 5117 , 1}}, + {PPM60, {.colorSp_200 = 838 , .graySp_200= 2514,.colorSp_300 = 895 ,.graySp_300 = 2689,.colorSp_600 = 1705 ,.graySp_600 = 5117 , 2}}, + {PPM70, {.colorSp_200 = 838 , .graySp_200= 2514,.colorSp_300 = 895 ,.graySp_300 = 2689,.colorSp_600 = 1705 ,.graySp_600 = 5117 , 3}} //默认 140 +#else + {PPM40, {.colorSp_200 = 2426 , .graySp_200= 7268,.colorSp_300 = 1615 ,.graySp_300 = 4849,.colorSp_600 = 1567 ,.graySp_600 = 4701 , 0}}, + {PPM50, {.colorSp_200 = 1950 , .graySp_200= 5870,.colorSp_300 = 1303 ,.graySp_300 = 3909,.colorSp_600 = 1567 ,.graySp_600 = 4701 , 1}}, + {PPM60, {.colorSp_200 = 1668 , .graySp_200= 5004,.colorSp_300 = 1080 ,.graySp_300 = 3248,.colorSp_600 = 1567 ,.graySp_600 = 4701 , 2}}, + {PPM70, {.colorSp_200 = 1287 , .graySp_200= 3864,.colorSp_300 = 859 ,.graySp_300 = 2477,.colorSp_600 = 1567 ,.graySp_600 = 4701 , 3}}, + {PPM80, {.colorSp_200 = 1245 , .graySp_200= 3735,.colorSp_300 = 830 ,.graySp_300 = 2490,.colorSp_600 = 1567 ,.graySp_600 = 4701 , 4}} +#endif +}; + static std::map scannerSp = { #ifdef G100 {PPM70, {0x42d, 0xc88 , 1}}, {PPM80, {0x37f, 0xa7f, 2}}, {PPM90, {0x2B6, 0x822, 0}}, - {PPM100, {0x27C, 0x775, 0}},//G100 3399 暂无100 ppm + //{PPM100, {0x27C, 0x775, 0}},//G100 3399 暂无100 ppm {PPM110, {0x24e, 0x706, 3}}, -#else - {PPM100, {0x27C, 0x775, 0}}, +#elif defined G200 + {PPM100, {0x42d, 0xc88, 0}}, {PPM110, {0x27C, 0x775, 1}}, {PPM120, {0x27C, 0x775, 3}}, - {PPM130, {0x27C, 0x775, 2}} //默认 140 + {PPM130, {0x27C, 0x775, 2}}, //默认 140 +#elif defined G300 + {PPM40, {0x42d, 0xc88, 0}}, + {PPM50, {0x27C, 0x775, 1}}, + {PPM60, {0x27C, 0x775, 2}}, + {PPM70, {0x27C, 0x775, 3}}, +#else + {PPM40, {0x42d, 0xc88, 0}}, + {PPM50, {0x27C, 0x775, 1}}, + {PPM60, {0x27C, 0x775, 2}}, + {PPM70, {0x27C, 0x775, 3}}, + {PPM80, {0x42d, 0xc88, 4}}, #endif }; +#ifdef G300 +static std::map papersMap= { + {PaperSize::G400_A3, {1728, 11100}}, + {PaperSize::G400_A4, {1728, 8100}}, + {PaperSize::G400_A4R, {1728, 6000}}, + {PaperSize::G400_A5, {1728, 8100}}, + {PaperSize::G400_A5R, {1728, 8100}}, + {PaperSize::G400_A6, {1728, 8100}}, + {PaperSize::G400_A6R, {1728, 8100}}, + {PaperSize::G400_B4, {1728, 10800}}, + {PaperSize::G400_B5, {1728, 10800}}, + {PaperSize::G400_B5R, {1728, 6600}}, + {PaperSize::G400_B6, {1728, 6600}}, + {PaperSize::G400_B6R, {1728, 6600}}, + {PaperSize::G400_DOUBLELETTER, {1728, 10800}}, + {PaperSize::G400_B6R, {1728, 10800}}, + {PaperSize::G400_LEGAL, {1728, 11100}}, + {PaperSize::G400_LETTER, {1728, 11100}}, + {PaperSize::G400_MAXSIZE, {1728, 16002}}, + }; +#else static std::map papersMap = { {PaperSize::G400_A3, {1632, 11100}}, {PaperSize::G400_A4, {1632, 8400}}, @@ -327,29 +409,28 @@ static std::map papersMap = { {PaperSize::G400_AUTO, {1632, 11100}}, {PaperSize::G400_MAXAUTO, {1632, 16002}}, }; - -// static std::map papersMap = { -// {PaperSize::G400_A3, {1632, 16002}}, -// {PaperSize::G400_A4, {1632, (uint)(8400*1.5)}}, -// {PaperSize::G400_A4R, {1632, (uint)(6000*1.5)}},//6000 -// {PaperSize::G400_A5, {1632, (uint)(6600*1.5)}}, -// {PaperSize::G400_A5R, {1632, (uint)(4500*1.5)}}, -// {PaperSize::G400_A6, {1632, (uint)(4500*1.5)}}, -// {PaperSize::G400_A6R, {1632, (uint)(4500*1.5)}}, -// {PaperSize::G400_B4, {1632, (uint)16002}}, -// {PaperSize::G400_B5, {1632, (uint)(8400*1.5)}}, -// {PaperSize::G400_B5R, {1632, (uint)(6600*1.5)}}, -// {PaperSize::G400_B6, {1632, (uint)(6600*1.5)}}, -// {PaperSize::G400_DOUBLELETTER, {1632, (uint)11100}}, -// {PaperSize::G400_B6R, {1632, (uint)(6600*1.5)}}, -// {PaperSize::G400_LEGAL, {1632, 16002}}, -// {PaperSize::G400_LETTER, {1632, (uint)(8400*1.5)}}, -// {PaperSize::G400_LETTERR, {1632, (uint)16002}}, -// {PaperSize::G400_MAXSIZE, {1632, (uint)16002}}, -// {PaperSize::G400_AUTO, {1632, (uint)16002}}, -// {PaperSize::G400_MAXAUTO, {1632, (uint)16002}}, -// }; - +#endif +static std::map paperHeight={ + {PaperSize::G400_A3, 475}, + {PaperSize::G400_A4, 337}, + {PaperSize::G400_A4R, 247}, + {PaperSize::G400_A5, 247}, + {PaperSize::G400_A5R, 178}, + {PaperSize::G400_A6, 178 }, + {PaperSize::G400_A6R, 135}, + {PaperSize::G400_B4, 383}, + {PaperSize::G400_B5, 300}, + {PaperSize::G400_B5R, 226}, + {PaperSize::G400_B6, 226}, + {PaperSize::G400_DOUBLELETTER, 500}, + {PaperSize::G400_B6R, 175}, + {PaperSize::G400_LEGAL, 386}, + {PaperSize::G400_LETTER, 339}, + {PaperSize::G400_LETTERR, 276}, + {PaperSize::G400_MAXSIZE, 1040},//300 dpi max heigth + {PaperSize::G400_AUTO, 1040},//300 dpi max heigth + {PaperSize::G400_MAXAUTO, 1040}//300 dpi max heigth +}; enum Scanner_Reg_Defs { SR_CMD, @@ -414,7 +495,10 @@ enum Scanner_Reg_Defs SR_SET_TOKEN, SR_GET_TOKEN_LENGHT, SR_DECODE_TOKEN, - SR_CLEAN_PAPER_ROAD, + SR_CLEAN_PAPER_ROAD, + SR_EXECUTE_CMD, + SR_EXECUTE_CMD_OUT_LENGHT, + SR_GET_EXECUTE_CMD_OUT, SR_GET_CUO_ERROR = 0x50, SR_GET_DOU_ERROR, SR_GET_JAM_ERROR, @@ -422,6 +506,18 @@ enum Scanner_Reg_Defs SR_CLR_ROLLER_NUM, SR_GET_ROLLER_NUM, SR_CLR_SCANN_NUM, + SR_GET_H_200_RATIO, //非敦南cis需返回false + SR_SET_H_200_RATIO, + SR_GET_V_200_RATIO, + SR_SET_V_200_RATIO, + SR_GET_H_300_RATIO, + SR_SET_H_300_RATIO, + SR_GET_V_300_RATIO, + SR_SET_V_300_RATIO, + SR_GET_H_600_RATIO, + SR_SET_H_600_RATIO, + SR_GET_V_600_RATIO, + SR_SET_V_600_RATIO, SR_UPDATA_START = 0x100, SR_UPDATA_STAUTUS = 0x101, SR_UPDATA_MD5_RELUST = 0x102, @@ -441,6 +537,8 @@ enum Scanner_Reg_Defs SR_GET_MBVERSION, SR_GET_USBVIDPID, SR_SET_USBVIDPID, + SR_SET_STARTBACKUP,//开始备份 + SR_GET_BACKUPSTATUS,//备份状态 SR_SCAN_DPI = 0x600, SR_SCAN_CNT, @@ -665,8 +763,8 @@ typedef struct tagCrop_Rect int enable; int x; /*****自定义裁切区域左上角x坐标*/ int y; /*****自定义裁切区域左上角y坐标*/ - int width; /*****自定义裁切区域宽?******/ - int height; /*****自定义裁切区域高?******/ + int width; /*****自定义裁切区域宽度*******/ + int height; /*****自定义裁切区域高度*******/ }CropRect; typedef enum tagMulti_Output { @@ -693,59 +791,67 @@ typedef struct tagHhardware_Params_3399 struct GScanCap { - uint8_t papertype; - PaperAlign paperAlign; - uint8_t en_sizecheck; - float imageRotateDegree; - uint8_t is_duplex; - uint8_t en_fold; - int pixtype; - int automaticcolor; - int automaticcolortype; + uint8_t papertype; /**< the current paper source ADF or Flatbed*/ + PaperAlign paperAlign; /**< 纸张放置方向*/ + uint8_t en_sizecheck; /**< 尺寸检测*/ + float imageRotateDegree; /**< 旋转角度*/ + uint8_t is_duplex; /**< True to use duplex false for simplex, ignored if flatbed*/ + uint8_t en_fold; /**< 对折*/ + int pixtype; /**< type of pixels to transfer image as */ + int automaticcolor; /**< 顔色自動識別*/ + int automaticcolortype; /**< 顔色自動識別后非彩色上傳類型*/ //ScanRect scanrect; - float resolution_dst; - float resolution_native; - float gamma; - float contrast; - float brightness; - float threshold; - uint8_t is_autocontrast; - uint8_t is_autocrop; - uint8_t is_autodiscradblank_normal; - int discardblank_percent; - uint8_t is_autodiscradblank_vince; - uint8_t is_switchfrontback; - uint8_t autodescrew; - uint8_t multi_output_red; - uint8_t hsvcorrect; - uint8_t filter; - uint8_t sharpen; - uint8_t enhance_color; - uint8_t fillbackground; - bool is_convex; - int noise; - int indent; - int AutoCrop_threshold; - unsigned short scannum; - uint8_t is_backrotate180; - uint8_t is_dogeardetection; - HardwareCaps hardwarecaps; - FillHole fillhole; - DetachNoise detachnoise; - uint8_t is_autotext; - bool isfillcolor; - int refuseInflow; - int colorCorrection; - int removeMorr; - int errorExtention; - int textureRemove; - int splitImage; - CropRect cropRect; - MultiOutput multiOutput; - bool normalCrop; - uint32_t dogeardistabce; - bool fadeback; - int fadebackrange; - uint32_t reserve[1024]; + float resolution_dst; /**< horizontal resolution */ + float resolution_native; /**< 实际扫描DPI*/ + float gamma; /**< Gamma */ + float contrast; /**< Contrast */ + float brightness; /**< Brightness */ + float threshold; /**< Threshold */ + uint8_t is_autocontrast; /**< 自动对比度*/ + uint8_t is_autocrop; /**< 自动裁切*/ + uint8_t is_autodiscradblank_normal; /**< 自动丢弃空白页通用*/ + int discardblank_percent; /**< 跳过空白页阀值*/ + uint8_t is_autodiscradblank_vince; /**< 自动丢弃空白页发票*/ + uint8_t is_switchfrontback; /**< 交换正反面*/ + uint8_t autodescrew; /**< 自动纠偏*/ + uint8_t multi_output_red; /**< 多流输出*/ + uint8_t hsvcorrect; /**< 答题卡除红*/ + uint8_t filter; /**< 除色*/ + uint8_t sharpen; /**< 锐化模糊*/ + uint8_t enhance_color; /**< 颜色增强*/ + uint8_t fillbackground; /**< 填黑框*/ + bool is_convex; /**< 填黑框模式,true为凸多边形填充,false为凹多边形填充,默认true*/ + int noise; /**< 除噪像素,能够消除noise宽度的背景竖条纹干扰,默认40*/ + int indent; /**< 轮廓缩进,裁剪、纠偏或者黑底填充时,对探索到的纸张轮廓进行缩进indent像素,默认5*/ + int AutoCrop_threshold; /**< 自动裁剪二值化阈值,取值范围(0, 255),默认40*/ + unsigned short scannum; /**< 扫描张数*/ + uint8_t is_backrotate180; /**< 背面旋转180*/ + uint8_t is_dogeardetection; /**< 折角检测*/ + HardwareCaps hardwarecaps; /**< 硬件扫描参数*/ + FillHole fillhole; /**< 穿孔移除>*/ + DetachNoise detachnoise; /**< 黑白降噪*/ + uint8_t is_autotext; /**< 自动文本方向识别 此处大小116字节*/ + bool isfillcolor; /**< 自动裁切颜色填充>*/ + int refuseInflow; /**< 防止渗透>*/ + int colorCorrection; /**< 色彩校正>*/ + int removeMorr; /**< 去除摩尔纹>*/ + int errorExtention; /**< 错误扩散>*/ + int textureRemove; /**< 除网纹>*/ + int splitImage; /**< 图像拆分>*/ + CropRect cropRect; /**< 自定义裁切>*/ + MultiOutput multiOutput; /**< 多流输出>*/ + bool normalCrop; /**< 自动裁切深色样张>*/ + uint32_t dogeardistabce; /**< 折角检测理论顶点到实际轮廓最新距离>*/ + bool fadeback; /**< 除底色>*/ + int fadebackrange; /**< 除底色范围>*/ + bool isuploadexceptionimage; /**< 是否上传双张报错后的异常图像>*/ + int fillholeratio_up; /**< 穿孔移除上下左右范围>*/ + int fillholeratio_down; + int fillholeratio_left; + int fillholeratio_right; + std::uint8_t fold_concatmode; /*拼接模式*/ + int HsvFilterType; /**< 答题卡留红除杂色功能类型 暂定0 为关闭;1为留红除杂色>*/ + bool is_colorcast; /**< 色偏校正>*/ + uint32_t reserve[1024]; /**< 预留4096字节做协议扩展*/ }; #pragma pack(pop) \ No newline at end of file diff --git a/device/gxx-linux/scanner/imageusbhandler.cpp b/device/gxx-linux/scanner/imageusbhandler.cpp index 2e42689..b6f103b 100644 --- a/device/gxx-linux/scanner/imageusbhandler.cpp +++ b/device/gxx-linux/scanner/imageusbhandler.cpp @@ -11,44 +11,74 @@ #include #include "ImageApplyAutoCrop.h" #include "hgutils.h" +#include "correct_ultis.h" static const std::string loggername = "imageusbhandler"; ImageUsbHandler::ImageUsbHandler(std::shared_ptr images) - : pool(2), encodepools(4) + : pool(1), encodepools(6),pushpool(1), que_len_(0) { LOG_INIT(); this->images = images; m_dog.reset(new CImageApplyDogEarDetection(40,1.0,50)); + // m_colorcast.reset(new CImageApplyColorCastCorrect()); m_sizedetect.reset(new CSizedetect(1)); initLut(); auto info= jsonconfig().getscannerinfo(); H_ratio =*((float*)(&info.H_ratio)); V_ratio =*((float*)(&info.V_ratio)); } -static int num = 0; -void ImageUsbHandler::add_image(void *data, int width, int height, int type, int scannnum) +ImageUsbHandler::~ImageUsbHandler() { - StopWatch addsw; + +} +static int num = 0; +void ImageUsbHandler::add_image(void *data, int width, int height, int type, int scannnum,std::uint32_t fpga_vs,CISVendor cistype) +{ + printf("add_image(%u * %u)\n", width, height); + std::string ext = ".jpg"; { - if( m_imgstatus.status!=NO_error) - return; - cv::Mat tmp; - if(type == CV_8UC3) - tmp = cv::Mat(height / 3 -1 , width * 9, CV_8UC1, data); + if (m_imgstatus.status != NO_error) + return; + cv::Mat mat; + if(m_scanconfig.g200params.dpi == 3) + mat = cv::Mat(height, width, CV_8UC1, data); else - tmp = cv::Mat(height, width * 3, CV_8UC1, data); - capture_data.push(tmp.clone()); + mat = cv::Mat(height, width, CV_8UC1, data).clone(); + capture_data.Put(mat); StopWatch checktime; - if(m_hgimgconfig.is_dogeardetection || m_hgimgconfig.en_sizecheck) + if (m_hgimgconfig.is_dogeardetection || m_hgimgconfig.en_sizecheck) { - if(tmp.empty()) - return ; - auto mergemat = GetMergeMat(tmp, width, height, type); - if(m_hgimgconfig.is_dogeardetection) + cv::Mat tmp = cv::Mat(height, width, CV_8UC1, data); + if (tmp.empty()) + return; + m_imgstatus.status = Img_Detecting; +#ifdef G300 + auto mergemat = GetStitchMat(type == 16?1:0,width,height,tmp); +#else + auto mergemat = GetMergeMat(type == CV_8UC1 ? width : width / 3, height, type, tmp,0x90001); +#endif + if (m_scanconfig.g200params.dpi == 1) + cv::resize(mergemat, mergemat, cv::Size(0, 0), 200.0 / 300.0, 1.0); + else if (m_scanconfig.g200params.dpi == 2) + cv::resize(mergemat, mergemat, cv::Size(0, 0), 200.0 / 300.0, 200.0 / 300.0); + else { - printf("is_dogeardetection"); +#ifdef G200 + cv::resize(mergemat,mergemat,cv::Size(0,0),200.0 / 600.0,1.43434/3); // 600 dpi ��������ʵ600�ɼ� +#elif defined G300 + cv::resize(mergemat,mergemat,cv::Size(0,0),200.0 / 600.0,1.432323/3); +#elif defined G400 + cv::resize(mergemat,mergemat,cv::Size(0,0),200.0 / 600.0,1.5/3); +#else + cv::resize(mergemat,mergemat,cv::Size(0,0),200.0 / 600.0,1.432323/3); +#endif + } + + if (m_hgimgconfig.is_dogeardetection) + { + printf("\n is_dogeardetection"); if(!m_scanconfig.g200params.pc_correct) correctColor(mergemat, false); auto dogmat=mergemat(cv::Rect(mergemat.cols/2, 0, mergemat.cols/2, mergemat.rows)); @@ -57,197 +87,282 @@ void ImageUsbHandler::add_image(void *data, int width, int height, int type, int { m_imgstatus.status=Dog_error; m_imgstatus.sannum=scannnum; - add_scanevent({.From=IMG,.Code=m_imgstatus.status,.Img_Index=m_imgstatus.sannum+1}); + add_scanevent({.From=IMG,.Code=m_imgstatus.status,.Img_Index=m_imgstatus.sannum}); return; } } if(m_hgimgconfig.en_sizecheck) { auto sizemat=mergemat(cv::Rect(mergemat.cols/2, 0, mergemat.cols/2, mergemat.rows)); - printf("en_sizecheck %d",m_scanconfig.g200params.paper); + printf("\n en_sizecheck %d",m_scanconfig.g200params.paper); m_sizedetect->SetPapertype(m_scanconfig.g200params.paper); if(m_sizedetect->preprocess(sizemat,NULL)) { m_imgstatus.status=Size_error; m_imgstatus.sannum=scannnum; - add_scanevent({.From=IMG,.Code=m_imgstatus.status,.Img_Index=m_imgstatus.sannum+1}); + add_scanevent({.From=IMG,.Code=m_imgstatus.status,.Img_Index=m_imgstatus.sannum}); return; } } - + m_imgstatus.status = NO_error; } - //printf("\n dogear or sizecheck time %f", checktime.elapsed_ms()); - std::lock_guard guard(mtx); + //tmp.release(); + + //std::lock_guard guard(mtx); + add_que_count(); results.emplace( - pool.enqueue([this, width, height, type, ext,scannnum] + pool.enqueue([this, width, height, type, ext, scannnum, data,cistype] { + printf("enqueue image index %d \n",scannnum); StopWatch sw; - auto param = m_hgimgconfig; - auto ials = m_ials; - cv::Mat mat =capture_data.front(); - capture_data.pop(); - cv::Mat saveMat = GetMergeMat(mat, width, height, type); - sw.reset(); + cv::Mat mat = capture_data.Take(); + if(m_scanconfig.g200params.dpi == 3) + mat = cv::Mat(height, width, CV_8UC1, data); + else + mat = cv::Mat(height, width, CV_8UC1, data).clone(); + capture_data.Put(mat); +#ifdef G300 + cv::Mat saveMat = GetStitchMat(type == 16?1:0,width,height,mat); +#else + int dstwidth = type==CV_8UC1?width:width/3; + cv::Mat saveMat =GetMergeMat(dstwidth, height, type,mat,0x90001); +#endif + printf("\n GetMergeMat time %f \n",sw.elapsed_ms()); + //cv::imwrite("/home/"+to_string(scannnum)+".png",saveMat); + int dpi = saveMat.cols==7344?2:3; if(!m_scanconfig.g200params.pc_correct) - correctColor(saveMat, false); - LOG_TRACE(string_format("correct time = %f\n", sw.elapsed_ms())); - auto paper = (param.papertype == TwSS::A4 && param.paperAlign == PaperAlign::Rot270) ? PaperSize::G400_A4R : PaperSize::G400_AUTO; - saveMat = flipRightHalf(saveMat, (int)paper); - LOG_TRACE(string_format("correct flip time = %f\n", sw.elapsed_ms())); - sw.reset(); - if(H_ratio>1.2f ||H_ratio<0.8f) - H_ratio=1.0f; - if(V_ratio>1.2f || V_ratio <0.8f) - V_ratio=1.0f; - if((H_ratio != 1.0f) || (V_ratio != 1.0f)) - cv::resize(saveMat,saveMat,cv::Size(),H_ratio,V_ratio); - cv::Mat imageMat; - std::vector imgs; - int actwidth = saveMat.cols / 2; - int actheight = saveMat.rows; - for (int i = 0; i < 2; i++) - { - imageMat = saveMat(cv::Rect(i * actwidth, 0, actwidth, actheight)); - if (!imageMat.empty()) - { - imgs.push_back(imageMat); - //HG_SAVEIMG("/home/linaro/"+to_string(num++)+".jpg",imageMat); - } - } - sw.reset(); - auto rets = encodepools.enqueue([this, param ,&imgs, &ials]() -> std::vector + correctColor(saveMat,dpi,saveMat.channels()==3?1:0,!m_scanconfig.g200params.is_textcorrect); + printf("\n correctColor time %f \n",sw.elapsed_ms()); + encode_data.Put(saveMat); + add_que_count(); + encodeimgs.push(encodepools.enqueue([this,width,height,type,cistype]() -> std::vector { + auto saveMat = encode_data.Take(); + if(H_ratio>1.2f ||H_ratio<0.8f) + H_ratio=1.0f; + if(V_ratio>1.2f || V_ratio <0.8f) + V_ratio=1.0f; + if(m_scanconfig.g200params.dpi==0x01) + { + cv::resize(saveMat,saveMat,cv::Size(0,0),200.0/300.0*H_ratio,1.0*V_ratio); + } + if(m_scanconfig.g200params.dpi == 2) + { + if((H_ratio != 1.0f) || (V_ratio != 1.0f)) + cv::resize(saveMat,saveMat,cv::Size(),H_ratio,V_ratio); + } + if(m_scanconfig.g200params.dpi == 3) + #ifdef G200 + cv::resize(saveMat,saveMat,cv::Size(0,0),1.0*H_ratio,1.43434*V_ratio); // 600 dpi ��������ʵ600�ɼ� + #elif defined G300 + cv::resize(saveMat,saveMat,cv::Size(0,0),1.0*H_ratio,1.432323*V_ratio); + #elif defined G400 + cv::resize(saveMat,saveMat,cv::Size(0,0),1.0*H_ratio,1.5*V_ratio); + #else + cv::resize(saveMat,saveMat,cv::Size(0,0),1.0*H_ratio,1.432323*V_ratio); + #endif + cv::Mat imageMat; + std::vector imgs; + int actwidth = saveMat.cols / 2; + int actheight = saveMat.rows; + for (int i = 0; i < 2; i++) + { + imageMat = saveMat(cv::Rect(i * actwidth, 0, actwidth, actheight)); + if (!imageMat.empty()) + { + imgs.push_back(imageMat); + } + } + // cv::imwrite("/home/proc.jpg",saveMat); + imgs.push_back(saveMat); std::shared_ptr imageencode; //(new BmpImageEncode()); std::vector encodedata; - if (!m_scanconfig.g200params.iscorrect_mode) + bool iscorrect_mode =true; + if(!iscorrect_mode) + //if (!m_scanconfig.g200params.iscorrect_mode) { - LOG_TRACE(string_format("ials size =%d", ials.size())); - if (param.imageRotateDegree != 0.0 && param.imageRotateDegree != 180.0 && imgs.size()>1) { - cv::flip(imgs[1], imgs[1], 0); - cv::flip(imgs[1], imgs[1], 1); - } - - if (param.is_switchfrontback && (imgs.size() > 1)) + if (!m_hgimgconfig.is_switchfrontback && (imgs.size() > 1)) std::swap(imgs[0], imgs[1]); - - for (auto &ialsnode : ials) + CImageApply* ptr = m_ials[0].get(); + if (typeid(*ptr) != typeid(CImageApplyOutHole)){ + if(m_hgimgconfig.imageRotateDegree != 0.0 && m_hgimgconfig.imageRotateDegree != 180.0 && (imgs.size() > 1)) + { + cv::flip(imgs[1], imgs[1], 0); + cv::flip(imgs[1], imgs[1], 1); + } + } + for (auto &ialsnode : m_ials) { - ialsnode->apply(imgs, bool(param.is_duplex)); + ialsnode->apply(imgs, bool(m_hgimgconfig.is_duplex)); } - if ((!param.is_duplex) && (imgs.size() > 1)) + if ((!m_hgimgconfig.is_duplex) && (imgs.size() > 1)) imgs.pop_back(); - + else if((imgs.size() > 1) && (cistype == CISVendor::HUALIN_CIS_V0)) + cv::flip(imgs[1], imgs[1], 0); } for (auto &img : imgs) { cv::Mat enMat = img; - if (!(enMat.empty() && (param.is_autodiscradblank_normal || param.is_autodiscradblank_vince))) + if (!(enMat.empty() && (m_hgimgconfig.is_autodiscradblank_normal || m_hgimgconfig.is_autodiscradblank_vince))) { - //cv::resize(enMat,enMat,cv::Size(),1.0,0.666666666667); + if(m_hgimgconfig.fadeback!=0) + { + if(enMat.channels()==3&&m_hgimgconfig.pixtype==1) + cv::cvtColor(enMat,enMat,cv::COLOR_BGR2GRAY); + } if(m_scanconfig.g200params.iscorrect_mode) imageencode.reset(new JpegImageEncode(false)); else - imageencode.reset(new JpegImageEncode(param.pixtype == 0)); + imageencode.reset(new JpegImageEncode(false)); encodedata.push_back(imageencode->encode(enMat)); } } return encodedata; - }); - - auto mem = rets.get(); - if (!mem.empty()) - { - for (auto &data : mem) - { - if (data.get()) - images->push(data, true); - else - add_scanevent({.From = V4L2, .Code = 1}); - } - } - //printf("imgproce time = %f\n", sw.elapsed_ms()); - LOG_TRACE(string_format("imgproce time = %f\n", sw.elapsed_ms())); - })); + })); + pushpool.enqueue([this]{ + auto mem = encodeimgs.front().get(); + encodeimgs.pop(); + if (!mem.empty()) + { + for (auto &data : mem) + { + if (data.get()) + images->push(data, true); + else + add_scanevent({.From = V4L2, .Code = 1}); + } + } + add_que_count(-1); + }); + printf("imgproce time = %f \n", sw.elapsed_ms()); + LOG_TRACE(string_format("imgproce time = %f\n", sw.elapsed_ms())); })); + add_que_count(-1); } - if(m_hgimgconfig.resolution_dst>200.0 || m_hgimgconfig.papertype==52 || m_hgimgconfig.papertype == 54|| - m_hgimgconfig.papertype==131) - { - while (results.size() >= 2) - { - results.front().get(); - { - std::lock_guard guard(mtx); - results.pop(); - } - } - } - else{ - while (results.size() >= 8) - { - results.front().get(); - { - std::lock_guard guard(mtx); - results.pop(); - } - } - } - } -void ImageUsbHandler::Set_ratio(u32 h_ratio,u32 v_ratio) +bool ImageUsbHandler::is_limit(){ + if (m_hgimgconfig.resolution_dst > 200.0 || m_hgimgconfig.papertype == 52 || m_hgimgconfig.papertype == 54 || + m_hgimgconfig.papertype == 131) + { + while (results.size() >= (m_scanconfig.g200params.dpi == 3 ? 1 : 2)) + { + results.front().get(); + { + //std::lock_guard guard(mtx); + results.pop(); + } + } + if (encodeimgs.size() >= (m_scanconfig.g200params.dpi == 3 ? 1 : 2)) + { + return true; + } + } + else + { + while (results.size() >= 20) + { + results.front().get(); + { + //std::lock_guard guard(mtx); + results.pop(); + } + } + if (encodeimgs.size() >= 20) + { + return true; + } + } + return false; +} + + +void ImageUsbHandler::thread_push_stop_event(HGIntInfo ev) { - H_ratio =*((float*)(&h_ratio)); - V_ratio =*((float*)(&v_ratio)); + printf("Received STOPSCAN event, but image-process is busying, wait real stop in thread ...\n"); + while(!done()) + { + std::this_thread::sleep_for(std::chrono::milliseconds(3)); + } + push_event(ev); +} +void ImageUsbHandler::push_event(HGIntInfo ev) +{ + VectorMemroyPtr mem = VectorMemroyPtr(new VectorMemroy()); + HGIntInfo info = ev; + mem->resize(sizeof(info)); + memcpy(&mem->buf()[0], &info, sizeof(info)); + images->push(mem, false); +} +int32_t ImageUsbHandler::add_que_count(int adden) +{ + std::lock_guard lock(proc_que_); + + que_len_ += adden; + + return que_len_; } void ImageUsbHandler::add_scanevent(HGIntInfo status) { - // if(status.From != IMG) - // { - // VectorMemroyPtr mem = VectorMemroyPtr(new VectorMemroy()); - // HGIntInfo info = status; - // mem->resize(sizeof(info)); - // memcpy(&mem->buf()[0], &info, sizeof(info)); - // images->push(mem, false); - // std::this_thread::sleep_for(std::chrono::milliseconds(500)); - // } - VectorMemroyPtr mem = VectorMemroyPtr(new VectorMemroy()); - HGIntInfo info = status; - mem->resize(sizeof(info)); - memcpy(&mem->buf()[0], &info, sizeof(info)); - images->push(mem, false); + printf("add_scanevent(%d)\n", status.From); + if(status.From == STOPSCAN && !done()) + { + if(stop_event_thread_.get() && stop_event_thread_->joinable()) + stop_event_thread_->join(); + stop_event_thread_.reset(new std::thread(&ImageUsbHandler::thread_push_stop_event, this, status)); + } + else + { + push_event(status); + } } void ImageUsbHandler::clear() { if (images.get()) - images->clear(); - while(results.size()>0) + images->clear(); + while (results.size() > 0) results.pop(); - while(capture_data.size()>0) - capture_data.pop(); + while (encodeimgs.size() > 0) + encodeimgs.pop(); + capture_data.Clear(); +} + +void ImageUsbHandler::Set_ratio(u32 h_ratio,u32 v_ratio) +{ + H_ratio =*((float*)(&h_ratio)); + //V_ratio =*((float*)(&v_ratio)); + V_ratio = 1.0f; } bool ImageUsbHandler::done() { - std::lock_guard guard(mtx); - //printf("\n results.size() %d ",results.size()); - while (results.size() >= 1) + return add_que_count(0) == 0; + + bool over = true; { - auto &fu_run = results.back(); - // if(!(fu_run.valid() && (fu_run.wait_for(std::chrono::seconds(0)) != std::future_status::ready))) - // { - // //results.pop(); - // //printf("\n results pop"); - // continue; - // } - //return !(fu_run.valid() && (fu_run.wait_for(std::chrono::seconds(0)) != std::future_status::ready)); - return !(fu_run.wait_for(std::chrono::seconds(0)) != std::future_status::ready); + std::lock_guard guard(mtx); + if(results.size() >= 1){ + auto &fu_run = results.back(); + if((fu_run.valid() && (fu_run.wait_for(std::chrono::seconds(0)) != std::future_status::ready))) + over = false; + } + if(over && encodeimgs.size()>=1) + { + auto &fu_encode = encodeimgs.back(); + over = !fu_encode.valid() || encodeimgs.size() == 0; + // if((!fu_encode.valid()) && encodeimgs.size() == 1) + // over = true; + // else + // over = false; + // return !(fu_encode.wait_for(std::chrono::seconds(0)) != std::future_status::ready); + //return (!(fu_run.wait_for(std::chrono::seconds(0)) != std::future_status::ready))&&(!(fu_encode.wait_for(std::chrono::seconds(0)) != std::future_status::ready)); + } } - //return true; - return true; + + return over; } void ImageUsbHandler::config_procparams(HGImgProcParms params) @@ -260,4 +375,4 @@ void ImageUsbHandler::config_procparams(HGImgProcParms params) LOG_TRACE(string_format("HGImgProcParms.filter = %d HGImgProcParms.enhancecolor = %d \n", params.imgprocparams.filter, params.imgprocparams.enhancecolor)); LOG_TRACE(string_format("HGImgProcParms.fillhole = %d \n", params.imgprocparams.fillhole)); m_procparams = params; -} \ No newline at end of file +} diff --git a/device/gxx-linux/scanner/imageusbhandler.h b/device/gxx-linux/scanner/imageusbhandler.h index 390ebc6..b424955 100644 --- a/device/gxx-linux/scanner/imageusbhandler.h +++ b/device/gxx-linux/scanner/imageusbhandler.h @@ -12,25 +12,37 @@ class UsbImageProcQueue; class ImageUsbHandler: public IImageHandler { + void push_event(HGIntInfo ev); + void thread_push_stop_event(HGIntInfo ev); + int32_t add_que_count(int adden = 1); + + std::shared_ptr stop_event_thread_; + std::mutex proc_que_; + volatile int32_t que_len_; + public: ImageUsbHandler(std::shared_ptr images = nullptr); - + virtual ~ImageUsbHandler(); virtual void config_procparams(HGImgProcParms params); - virtual void add_image(void* data, int width, int height, int type,int scannnum); + virtual void add_image(void* data, int width, int height, int type,int scannnum,std::uint32_t fpga_vs,CISVendor cistype); virtual void add_scanevent(HGIntInfo status); virtual bool done(); virtual void clear(); + virtual bool is_limit() override; virtual void Set_ratio(u32 h_ratio,u32 v_ratio) override; protected: std::shared_ptr images; - std::queue capture_data; + BlockingQueue capture_data; + BlockingQueue encode_data; ThreadPool pool; ThreadPool encodepools; + ThreadPool pushpool; volatile float H_ratio; volatile float V_ratio; - std::queue< std::future> results; + std::queue> results; + std::queue>> encodeimgs; BlockingQueue scanEvents; std::mutex mtx; HGImgProcParms m_procparams; diff --git a/device/gxx-linux/scanner/scannerregs.cpp b/device/gxx-linux/scanner/scannerregs.cpp index 46cac25..e044821 100644 --- a/device/gxx-linux/scanner/scannerregs.cpp +++ b/device/gxx-linux/scanner/scannerregs.cpp @@ -1,7 +1,7 @@ #include "scannerregs.h" #include #include -#include "scanner.h" +#include "iscanner.h" #include "usbimageprocqueue.h" #include "filetools.h" #include "stringex.hpp" @@ -9,9 +9,12 @@ #include #include "memoryex.h" #include "jsonconfig.h" +#include "CorrectParam.h" #include "hgutils.h" -#include "base64.hpp" +#include "MotorConfig.h" +#include "dailex.hpp" #include "deviceconfig.h" +#include "base64.hpp" unsigned long getfilesize(const char *filename) { @@ -36,6 +39,24 @@ void get_system_output(char *cmd, char *output, int size) } } +std::string get_system_output(const char *cmd) +{ + FILE *fp = NULL; + char output[100]; + std::string str =""; + fp = popen(cmd, "r"); + if (fp) + { + while (fgets(output, sizeof(output), fp) != NULL) + { + str += output; + memset(output,0,sizeof(output)); + } + pclose(fp); + } + return str; +} + static std::string load_mini_file(const char* path) { // /root/.scanner/log @@ -58,7 +79,7 @@ static std::string load_mini_file(const char* path) } static const std::string loggername = "ScannerRegAccess"; -ScannerRegAccess::ScannerRegAccess(std::shared_ptr scanner, std::shared_ptr usbimages, std::shared_ptr transmit, std::shared_ptr receiv) +ScannerRegAccess::ScannerRegAccess(std::shared_ptr scanner, std::shared_ptr usbimages, std::shared_ptr transmit, std::shared_ptr receiv) { LOG_INIT(); this->scanner = scanner; @@ -67,6 +88,7 @@ ScannerRegAccess::ScannerRegAccess(std::shared_ptr scanner, std::shared this->recieve = receiv; this->m_upstautus = Start_updata; this->scanner->set_paprent(this); + file_pos = 0; } ScannerRegAccess::~ScannerRegAccess() @@ -122,6 +144,10 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) return true; case SR_IM_TYPE: return true; + case SR_NOTIFY_SLEEP: + if(scanner->get_sleepstatus() == 0) + scanner->set_sleepstatus(true); + return true; case SR_IM_TX: { // if (val != 1 && (transmit->is_writing() || usbimages->empty())) @@ -140,10 +166,6 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) } return true; } - case SR_NOTIFY_SLEEP: - if(scanner->get_sleepstatus() == 0) - scanner->set_sleepstatus(true); - return true; case SR_IM_POP: usbimages->pop(); return true; @@ -161,7 +183,7 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) return true; case SR_CONFIF_IMGPROCPARAM: { - printf("\nSR_CONFIF_IMGPROCPARAM size =%d",val); + printf("\nSR_CONFIF_IMGPROCPARAM size =%d\n",val); #ifdef ASYNC_EP std::string cfg(load_mini_file("/root/.scanner/log/cfg_4308.bin")); GScanCap cap = *(GScanCap*)&cfg[0]; @@ -177,7 +199,7 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) char capdata[sizeof(GScanCap)]{0}; printf("\n gcap size = %d \n",sizeof(GScanCap)); memcpy(capdata,data.data(),std::min(sizeof(GScanCap),data.size())); - GScanCap cap = *(GScanCap *)capdata; + GScanCap cap = *(GScanCap*)capdata; #endif scanner->set_hgimgconfig(cap); return true; @@ -185,9 +207,7 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) case SR_GET_FWVERSION: { std::string fw(FWVersion); - printf("Firmware version(%p): %s\n", transmit.get(), fw.c_str()); - int sent = transmit->write_bulk(&fw[0], fw.length()); - printf("Firmware version sent: %d!\n", sent); + transmit->write_bulk(&fw[0], fw.length()); //printf((fw + "%d\n").c_str(), fw.length()); return true; } @@ -296,7 +316,7 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) m_upstautus = Download_file_finish; } else - m_upstautus = Download_file_finish; + m_upstautus = Download_file_error; } if (m_upstautus = Download_file_finish) { @@ -308,7 +328,10 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) } case SR_REBOOT: { - system("reboot"); + if(val >0) + system("reboot loader"); + else + system("reboot"); return true; } case SR_POWEROFF: @@ -318,6 +341,12 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) } case SR_SET_H_RATIO: case SR_SET_V_RATIO: + case SR_SET_H_200_RATIO: + case SR_SET_H_300_RATIO: + case SR_SET_H_600_RATIO: + case SR_SET_V_200_RATIO: + case SR_SET_V_300_RATIO: + case SR_SET_V_600_RATIO: case SR_SET_SERIALNUM: case SR_GET_SERIALNUM: { @@ -381,13 +410,23 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) case SR_SET_SPEEDMODE: { auto info = getscannerinfo(); + if(scannerSp.find((SpeedMode)info.speedmode)== scannerSp.end()) + return true; + printf("\nSR_SET_SPEEDMODE %d",val); #ifdef G200 - if (val == 100 || val == 140 || val == 110 || val == 120 || val == 130) + if(val == 100 || val == 110 || val == 120 || val == 130) + info.speedmode= val; +#elif defined G100 + if(val == 70 || val == 80 || val == 90 || val == 110) + info.speedmode = val; +#elif defined G400 + if(val == 40 || val == 50 || val == 60 || val == 70 ||val == 80) info.speedmode = val; #else - if (val == 100 || val == 70 || val == 80 || val == 90 || val == 110) + if(val == 40 || val == 50 || val == 60 || val == 70) info.speedmode = val; #endif + setcameraparmSp(val); savescannerinfo(info); return true; } @@ -429,7 +468,7 @@ bool ScannerRegAccess::write(unsigned int addr, const unsigned int val) printf("error vid pid value\n"); break; } -case SR_SET_JSON_PATH: + case SR_SET_JSON_PATH: { std::string path; path.resize(val); @@ -524,9 +563,6 @@ case SR_SET_JSON_PATH: token.resize(val); recieve->read_bulk(&token[0],val); m_token = token; - printf("\n token = "); - for(int i =0;iwrite_bulk(&tmp[0],val); return true; } + case SR_SET_STARTBACKUP: + { + printf("start Back up system\n"); + system("touch /etc/back_status"); + system("echo 0 > /etc/back_status"); + system("/etc/hgbackup_auto.sh &"); + return true; + } + case SR_CLEAN_PAPER_ROAD: + { + if(!scanner->is_runscan()) + scanner->clean_paper_road(); + return true; + } + + case SR_EXECUTE_CMD: + { + std::string cmd; + cmd.resize(val); + recieve->read_bulk(&cmd[0],val); + printf("SR_EXECUTE_CMD = %s \n",cmd.c_str()); + std::thread cmd_thread([this,cmd]{ + cmd_out = get_system_output(cmd.c_str()); + printf("lens = %d \n %s \n",cmd_out.length(), cmd_out.c_str()); + scanner->add_scansever({.From = HGType::CMD_OUT, .Code = static_cast(cmd_out.length())}); + // transmit->write_bulk(&str[0],str.length()); + }); + cmd_thread.detach(); + return true; + } + case SR_GET_EXECUTE_CMD_OUT: + { + if(val > cmd_out.size()) + cmd_out.resize(val); + transmit->write_bulk(&cmd_out[0],val); + cmd_out = ""; + cmd_out.resize(10); + return true; + } default: break; } @@ -544,8 +619,6 @@ case SR_SET_JSON_PATH: bool ScannerRegAccess::read(unsigned int addr, unsigned int &val) { - printf("ScannerRegAccess::read(%d, %d)\n", addr, val); - switch (addr) { case SR_STATUS: @@ -665,13 +738,19 @@ bool ScannerRegAccess::read(unsigned int addr, unsigned int &val) { jsonconfig js; auto info = js.getscannerinfo(); - info.RollerNum = 0; + info.TotalScanned = 0; js.savescannerinfo(info); return true; } case SR_GET_SERIAL_LEN: case SR_GET_H_RATIO: case SR_GET_V_RATIO: + case SR_GET_H_200_RATIO: + case SR_GET_H_300_RATIO: + case SR_GET_H_600_RATIO: + case SR_GET_V_200_RATIO: + case SR_GET_V_300_RATIO: + case SR_GET_V_600_RATIO: { return procscannerinfo(addr, val); } @@ -777,7 +856,17 @@ bool ScannerRegAccess::read(unsigned int addr, unsigned int &val) val = tmp.length(); return true; } - break; + case SR_GET_BACKUPSTATUS: + { + std::ifstream i("/etc/back_status"); + i>> val; + return true; + } + case SR_EXECUTE_CMD_OUT_LENGHT: + { + val = cmd_out.size(); + return true; + } default: break; } @@ -789,7 +878,8 @@ bool ScannerRegAccess::cmd(const unsigned int val) switch (val) { case SC_START: - scanner->try_scan(m_token); + if(scanner->check_scan(m_token)) + scanner->start_scan(); m_token = "null"; return true; case SC_STOP: @@ -852,6 +942,44 @@ bool ScannerRegAccess::procscannerinfo(unsigned int addr, unsigned int &value) info.V_ratio = value; break; } + case SR_GET_H_200_RATIO: + case SR_GET_H_300_RATIO: + case SR_GET_H_600_RATIO: + value = CorrectParam().GetFpgaparam(addr == SR_GET_H_200_RATIO? 1:(addr == SR_GET_H_300_RATIO? 2:3),1).HRatio; + break; + case SR_SET_H_200_RATIO: + case SR_SET_H_300_RATIO: + case SR_SET_H_600_RATIO: + { + float fx = *((float *)&value); + if (fx > 1.2f || fx < 0.8f) + break; + for(int x = 0;x<2;x++){ + auto tmp = CorrectParam().GetFpgaparam(addr == SR_SET_H_200_RATIO? 1:(addr == SR_SET_H_300_RATIO? 2:3),x); + tmp.HRatio = value; + CorrectParam().SaveCorrectParam(tmp); + } + break; + } + case SR_GET_V_200_RATIO: + case SR_GET_V_300_RATIO: + case SR_GET_V_600_RATIO: + value = CorrectParam().GetFpgaparam(addr == SR_GET_V_200_RATIO? 1:(addr == SR_GET_V_300_RATIO? 2:3),1).VRatio; + break; + case SR_SET_V_200_RATIO: + case SR_SET_V_300_RATIO: + case SR_SET_V_600_RATIO: + { + float fy = *((float *)&value); + if (fy > 1.2f || fy < 0.8f) + break; + for(int x = 0;x<2;x++){ + auto tmp = CorrectParam().GetFpgaparam(addr == SR_SET_V_200_RATIO? 1:(addr == SR_SET_V_300_RATIO? 2:3),x); + tmp.VRatio = value; + CorrectParam().SaveCorrectParam(tmp); + } + break; + } default: ret = false; break; @@ -868,3 +996,78 @@ void ScannerRegAccess::write_info(std::string info) LOG_TRACE(string_format("write info error !with info =%s ", info.c_str())); } } + +void ScannerRegAccess::set_image_callback(void(*cb)(MemoryPtr, bool, void*), void* para) +{ + if(usbimages) + usbimages->set_image_keeper(cb, para); +} + +void ScannerRegAccess::setcameraparmSp(int speedmode){ + if(cameraparmSp.find(SpeedMode(speedmode)) == cameraparmSp.end()) + return ; + printf("\nSetcameraparmSp %d",speedmode); +#if defined (G100) ||defined (G200) + CorrectParam cparam; + for(int i=1;i<4;i++) + { + for(int j =0; j<2;j++) + { + auto param = cparam.GetFpgaparam(i,j); + if((j==1)&& (i==1)) + param.Sp = cameraparmSp[(SpeedMode)speedmode].colorSp_200; + if((j==0)&& (i==1)) + param.Sp = cameraparmSp[(SpeedMode)speedmode].graySp_200; + if((j==1)&& (i==2)) + param.Sp = cameraparmSp[(SpeedMode)speedmode].colorSp_300; + if((j==0)&& (i==2)) + param.Sp = cameraparmSp[(SpeedMode)speedmode].graySp_300; + if((j==1)&& (i==3)) + param.Sp = cameraparmSp[(SpeedMode)speedmode].colorSp_600; + if((j==0)&& (i==3)) + param.Sp = cameraparmSp[(SpeedMode)speedmode].graySp_600; + printf("\n param.Sp = %d", param.Sp); + cparam.SaveCorrectParam(param); + } + } +#elif defined (G300) || defined (G400) + MotorConfig().reset_json(); + MotorConfig::MTBDType m_mttype; + int index = 0; + if(Dail().GetValue().dails.in_voltage4 != 0) + index = 60; + switch (speedmode) + { + case 40: index += 0 ;break; + case 50: index += 6 ;break; + case 60: index += 12 ;break; + case 70: index += 18 ;break; + case 80: index += 24 ;break; + default: + break; + } + CorrectParam cparam; + for(int i=1;i<4;i++) + { + for(int j =0; j<2;j++) + { + auto param = cparam.GetFpgaparam(i,j); + if((j==1)&& (i==1)) + param.Sp = m_motor_params[index+1].sp; + if((j==0)&& (i==1)) + param.Sp = m_motor_params[index].sp; + if((j==1)&& (i==2)) + param.Sp = m_motor_params[index+3].sp; + if((j==0)&& (i==2)) + param.Sp = m_motor_params[index+2].sp; + if((j==1)&& (i==3)) + param.Sp = m_motor_params[index+5].sp; + if((j==0)&& (i==3)) + param.Sp = m_motor_params[index+4].sp; + printf("\n param.Sp = %d", param.Sp); + cparam.SaveCorrectParam(param); + } + } +#endif + +} diff --git a/device/gxx-linux/scanner/scannerregs.h b/device/gxx-linux/scanner/scannerregs.h index 784c536..a6da03d 100644 --- a/device/gxx-linux/scanner/scannerregs.h +++ b/device/gxx-linux/scanner/scannerregs.h @@ -5,28 +5,35 @@ #include #include +#include + class UsbImageProcQueue; -class Scanner; +class IScanner; class ITransmit; class IReceive; class ScannerRegAccess : public IRegsAccess { public: - ScannerRegAccess(std::shared_ptr scanner, std::shared_ptr usbimages = nullptr, std::shared_ptr transmit = nullptr,std::shared_ptr receiv=nullptr); + ScannerRegAccess(std::shared_ptr scanner, std::shared_ptr usbimages = nullptr, std::shared_ptr transmit = nullptr,std::shared_ptr receiv=nullptr); virtual ~ScannerRegAccess(); virtual bool read(unsigned int addr, unsigned int& val); virtual bool write(unsigned int addr , const unsigned int val); void write_info(std::string info); + + void set_image_callback(void(*cb)(MemoryPtr, bool, void*), void* para); + private: + void setcameraparmSp(int speedmode); bool cmd(const unsigned int val); bool procscannerinfo(unsigned int addr,unsigned int& value); Updata_Stautus m_upstautus; std::string jsonpath; std::string m_token; + std::string cmd_out; volatile std::uint32_t file_pos; - std::shared_ptr scanner; + std::shared_ptr scanner; std::shared_ptr usbimages; std::shared_ptr transmit; std::shared_ptr recieve; diff --git a/device/gxx-linux/testusb/usb-rk3399/usbservice.cpp b/device/gxx-linux/testusb/usb-rk3399/usbservice.cpp index 89cf767..7496ecf 100644 --- a/device/gxx-linux/testusb/usb-rk3399/usbservice.cpp +++ b/device/gxx-linux/testusb/usb-rk3399/usbservice.cpp @@ -12,6 +12,7 @@ #include "usbreceive.h" #include "usbnotify.h" #include "applog.h" +#include "../../scanner/scannerregs.h" static const std::string loggername = "UsbService"; @@ -76,12 +77,12 @@ static bool write_regs(unsigned int *regs, int regindex, int creg, std::shared_p return ret; } -UsbService::UsbService(std::shared_ptr fgparegs, std::shared_ptr motorregs, std::shared_ptr scannerregs) +UsbService::UsbService(std::shared_ptr fgparegs, std::shared_ptr motorregs, std::shared_ptr scannerregs) : scanner_(nullptr) { LOG_INIT(); this->fgparegs = fgparegs; this->motorregs = motorregs; - this->devregs = scannerregs; + this->devregs = scannerregs.get(); auto ctrl_handle = [&](int fd, struct usb_ctrlrequest *setup, unsigned char *buffer) -> bool { printf("req = %x, type = %x, ind = %x, len = %x, val = %x\n", setup->bRequest, setup->bRequestType, setup->wIndex, setup->wLength, setup->wValue); @@ -221,9 +222,10 @@ std::shared_ptr UsbService::receiver() { return receiv ? receiv : (receiv = std::shared_ptr(new UsbReceive(usb))); } -void UsbService::set_scannerregs(std::shared_ptr scannerregs) +void UsbService::set_scannerregs(std::shared_ptr scannerregs) { - this->devregs = scannerregs; + scanner_ = scannerregs.get(); + this->devregs = dynamic_cast(scanner_); } std::shared_ptr UsbService::notify() diff --git a/device/gxx-linux/testusb/usb-rk3399/usbservice.h b/device/gxx-linux/testusb/usb-rk3399/usbservice.h index 3762ddc..2040aff 100644 --- a/device/gxx-linux/testusb/usb-rk3399/usbservice.h +++ b/device/gxx-linux/testusb/usb-rk3399/usbservice.h @@ -10,9 +10,12 @@ class UsbDevice; class INotify; class ITransmit; class IReceive; +class ScannerRegAccess; class UsbService { + ScannerRegAccess* scanner_; + public: UsbService(std::shared_ptr fgparegs, std::shared_ptr motorregs, std::shared_ptr scannerregs = nullptr); virtual ~UsbService(); @@ -22,13 +25,13 @@ public: std::shared_ptr notify(); std::shared_ptr receiver(); void set_onconnect_call(std::function usbhotplugcall); - void set_scannerregs(std::shared_ptr scannerregs); + void set_scannerregs(std::shared_ptr scannerregs); private: std::shared_ptr usb; std::shared_ptr fgparegs; std::shared_ptr motorregs; - std::shared_ptr devregs; + IRegsAccess* devregs; std::shared_ptr transmit; std::shared_ptr noti; std::shared_ptr receiv; diff --git a/device/gxx-linux/usb/inc/usbdevice.h b/device/gxx-linux/usb/inc/usbdevice.h index a252df3..8fbd4d2 100644 --- a/device/gxx-linux/usb/inc/usbdevice.h +++ b/device/gxx-linux/usb/inc/usbdevice.h @@ -26,6 +26,7 @@ class UsbDevice uint32_t scan_id_; uint32_t dpi_; uint32_t scan_cnt_; + uint32_t scan_err_; std::string cfg_text_; dyn_mem_ptr scan_over_pack_; diff --git a/device/gxx-linux/usb/src/common/ipc_util.h b/device/gxx-linux/usb/src/common/ipc_util.h index 3949d58..12d93ee 100644 --- a/device/gxx-linux/usb/src/common/ipc_util.h +++ b/device/gxx-linux/usb/src/common/ipc_util.h @@ -73,13 +73,16 @@ public: } public: - void save(const T& t, bool notify = false) + size_t save(const T& t, bool notify = false) { LOCKER lock(lock_); + size_t cnt = que_.size(); que_.push_back(std::move(t)); if (notify) wait_->trigger(); + + return cnt + 1; } bool take(T& t, bool wait = false) { diff --git a/device/gxx-linux/usb/src/common/log_util.cpp b/device/gxx-linux/usb/src/common/log_util.cpp index a90f48f..345df2f 100644 --- a/device/gxx-linux/usb/src/common/log_util.cpp +++ b/device/gxx-linux/usb/src/common/log_util.cpp @@ -1,5 +1,6 @@ #include "log_util.h" + #include #include #include @@ -118,6 +119,37 @@ void log_cls::initialize(const char* path_file, log_level level, int32_t max_siz } log_cls::inst_ = new log_cls(path_file, level, max_size); } + const char* log_cls::str_scanner_status(scanner_status s, char unk[]) + { + if(s == 0) + return "SCANNER_STATUS_READY"; + + RETURN_STR_ENUM(s, SCANNER_STATUS_READY); + RETURN_STR_ENUM(s, SCANNER_STATUS_NOT_OPEN); + RETURN_STR_ENUM(s, SCANNER_STATUS_LOST_CONNECT); + RETURN_STR_ENUM(s, SCANNER_STATUS_RESET_BULK); + RETURN_STR_ENUM(s, SCANNER_STATUS_START_SCANNING); + RETURN_STR_ENUM(s, SCANNER_STATUS_SCANNING); + RETURN_STR_ENUM(s, SCANNER_STATUS_SCAN_FINISHED); + RETURN_STR_ENUM(s, SCANNER_STATUS_BUSY); + RETURN_STR_ENUM(s, SCANNER_STATUS_COVER_OPENNED); + RETURN_STR_ENUM(s, SCANNER_STATUS_SLEEPING); + RETURN_STR_ENUM(s, SCANNER_STATUS_COUNT_MODE); + RETURN_STR_ENUM(s, SCANNER_STATUS_DOUBLE_FEEDED); + RETURN_STR_ENUM(s, SCANNER_STATUS_PAPER_JAMMED); + RETURN_STR_ENUM(s, SCANNER_STATUS_PAPER_ASKEW); + RETURN_STR_ENUM(s, SCANNER_STATUS_FEED_FAILED); + RETURN_STR_ENUM(s, SCANNER_STATUS_NO_PAPER); + RETURN_STR_ENUM(s, SCANNER_STATUS_STAPLE_ON); + RETURN_STR_ENUM(s, SCANNER_STATUS_SIZE_ERR); + RETURN_STR_ENUM(s, SCANNER_STATUS_DOGEAR); + RETURN_STR_ENUM(s, SCANNER_STATUS_CFG_CHANGED); + + sprintf(unk, "0x%x", (uint32_t)s); + + return unk; + } + int32_t log_cls::log_when_err(int32_t err, const char* oper_desc, log_level level) { if(err == -1) diff --git a/device/gxx-linux/usb/src/common/log_util.h b/device/gxx-linux/usb/src/common/log_util.h index 3778ba0..b298dc9 100644 --- a/device/gxx-linux/usb/src/common/log_util.h +++ b/device/gxx-linux/usb/src/common/log_util.h @@ -7,6 +7,7 @@ #include "referer.h" +#include "packet.h" #include #include @@ -43,6 +44,7 @@ protected: public: static void initialize(const char* path_file, log_level level = LOG_LEVEL_ALL, int32_t max_size = SIZE_MB(10)); + static const char* str_scanner_status(scanner_status s, char unk[20]); template static void log(log_level level, const char* fmt, Args ... args) diff --git a/device/gxx-linux/usb/src/common/packet.h b/device/gxx-linux/usb/src/common/packet.h index 3337380..839abd0 100644 --- a/device/gxx-linux/usb/src/common/packet.h +++ b/device/gxx-linux/usb/src/common/packet.h @@ -18,6 +18,10 @@ cmd, \ cmd##_ROGER +#define RETURN_STR_ENUM(v, e) \ + if(v == e) \ + return #e; + #define STRUCT_CONSTRUCTOR(st_name) \ st_name() \ { \ @@ -113,13 +117,16 @@ enum scanner_status SCANNER_STATUS_SCAN_FINISHED, // not a persistance status SCANNER_STATUS_BUSY, // doing task exclude scanning SCANNER_STATUS_COVER_OPENNED, + SCANNER_STATUS_COVER_CLOSED, SCANNER_STATUS_SLEEPING, + SCANNER_STATUS_WAKED_UP, SCANNER_STATUS_COUNT_MODE, SCANNER_STATUS_DOUBLE_FEEDED, SCANNER_STATUS_PAPER_JAMMED, SCANNER_STATUS_PAPER_ASKEW, SCANNER_STATUS_FEED_FAILED, SCANNER_STATUS_NO_PAPER, + SCANNER_STATUS_PAPER_ON, SCANNER_STATUS_STAPLE_ON, SCANNER_STATUS_SIZE_ERR, SCANNER_STATUS_DOGEAR, diff --git a/device/gxx-linux/usb/src/common/sys_util.cpp b/device/gxx-linux/usb/src/common/sys_util.cpp index 3bcd0a1..66f91dc 100644 --- a/device/gxx-linux/usb/src/common/sys_util.cpp +++ b/device/gxx-linux/usb/src/common/sys_util.cpp @@ -112,6 +112,37 @@ namespace sys_util return cb->on_found(off, m.c_str(), cb->param); } + static std::string pick_str_separate_by(const char* val, int divider_cnt = 0, char separator = ' ') + { + bool sep = false; + std::string ret(""); + + for(int i = 0; val[i]; ++i) + { + if(val[i] == separator) + { + if(!sep) + { + sep = true; + divider_cnt--; + } + } + else + { + sep = false; + if(divider_cnt <= 0) + { + ret = val + i; + divider_cnt = ret.find(" "); + if((size_t)divider_cnt != std::string::npos) + ret.erase(divider_cnt); + break; + } + } + } + + return ret; + } int32_t enum_modules(bool(*on_found)(const char* path_module_name, bool is_dir, void* param),// return false to stop enumeratin void* param, // user defined data, passed into callback on_found @@ -323,34 +354,18 @@ namespace sys_util } std::string get_memory_usage(const char* prog_name) { - std::string val(get_command_output((std::string("ps -Al | grep ") + prog_name).c_str())); + std::string val(get_command_output((std::string("top -b -n 1 | grep ") + prog_name).c_str())); if(val.length()) { - int cnt = 0; - bool space = false; - for(int i = 0; i < val.length(); ++i) + val = pick_str_separate_by(val.c_str(), 4); + } + else + { + val = get_command_output((std::string("ps -Al | grep ") + prog_name).c_str()); + if(val.length()) { - if(val[i] == ' ') - { - if(!space) - { - space = true; - cnt++; - } - } - else - { - space = false; - if(cnt == 9) // this value is unreliable ... - { - val.erase(0, i); - cnt = val.find(" "); - if((size_t)cnt != std::string::npos) - val.erase(cnt); - break; - } - } + val = pick_str_separate_by(val.c_str(), 9); } } diff --git a/device/gxx-linux/usb/src/common/usb_io.cpp b/device/gxx-linux/usb/src/common/usb_io.cpp index 1917405..0f69206 100644 --- a/device/gxx-linux/usb/src/common/usb_io.cpp +++ b/device/gxx-linux/usb/src/common/usb_io.cpp @@ -885,11 +885,13 @@ int async_usb_gadget::stop(void) } int async_usb_gadget::write_bulk(data_source_ptr data) { + int quel = 0; + if(!reset_bulk_) { data->add_ref(); - sent_que_.save(data, true); + quel = sent_que_.save(data, true); } - return 0; + return quel; } diff --git a/device/gxx-linux/usb/src/common/usb_io.h b/device/gxx-linux/usb/src/common/usb_io.h index 01180d6..4f30379 100644 --- a/device/gxx-linux/usb/src/common/usb_io.h +++ b/device/gxx-linux/usb/src/common/usb_io.h @@ -135,6 +135,6 @@ protected: public: int stop(void); - int write_bulk(data_source_ptr data); + int write_bulk(data_source_ptr data); // return sent-que length }; diff --git a/device/gxx-linux/usb/src/usbdevice.cpp b/device/gxx-linux/usb/src/usbdevice.cpp index 36d2f47..4658777 100644 --- a/device/gxx-linux/usb/src/usbdevice.cpp +++ b/device/gxx-linux/usb/src/usbdevice.cpp @@ -321,12 +321,16 @@ static void print_mem_usage(const char* desc) printf("\n--Memory usage of %s: %s--\n", desc, size.c_str()); } +static void image_receiver(MemoryPtr data, bool img, void* param) +{ + ((UsbDevice*)param)->save_image(data, img); +} #endif UsbDevice::UsbDevice(std::function handler, std::function call_back) : b_connected(false) , connect_call(call_back) #ifdef ASYNC_EP -, usb_(nullptr), cfg_(nullptr), scan_over_pack_(nullptr) +, usb_(nullptr), cfg_(nullptr), scan_over_pack_(nullptr), scan_id_(0) #endif { camtp_context.reset(new camtp_ctx); @@ -357,6 +361,21 @@ UsbDevice::UsbDevice(std::function void + { + static bool reg_cb = true; + + if(reg_cb) + { + uint64_t cb[] = {(uint64_t)image_receiver, (uint64_t)this}; + ctrl_handler(-1, (usb_ctrlrequest*)-1, (unsigned char*)cb); + + reg_cb = false; + } + + connect_call(conn); + }; + thread_pool *t = new thread_pool(this); print_mem_usage("new thread_pool"); t->thread_new(&UsbDevice::do_system_command, R"(echo linaro | sudo -S sh -c "echo fe900000.dwc3 > /opt/cfg/usb_gadget/g1/UDC")"); @@ -370,7 +389,7 @@ UsbDevice::UsbDevice(std::functionusb_cfg.usb_functionfs_mode, false); print_mem_usage("init_eps"); - usb_ = new async_usb_gadget(usb_ctx, ctrl_unhandle, bulk_handle, call_back); + usb_ = new async_usb_gadget(usb_ctx, ctrl_unhandle, bulk_handle, on_connect); print_mem_usage("new async_usb_gadget"); } t->thread_stop(); @@ -1416,7 +1435,7 @@ public: readonly_cfg() : jsn_(nullptr) { jsn_ = new json(); - jsn_->attach_text((char*)"{\"dev-sn\":{\"category\":\"base\",\"readonly\":true,\"affect\":0,\"group\":\"base\",\"visible\":true,\"field\":\"Common\",\"pos\":0,\"unit\":\"None\",\"title\":\"\\u8bbe\\u5907\\u5e8f\\u5217\\u53f7\",\"desc\":\"\\u8bbe\\u5907\\u552f\\u4e00\\u7f16\\u7801\",\"type\":\"string\",\"cur\":\"G100S20230001\",\"size\":20,\"ver\":1},\"fmw-ver\":{\"category\":\"base\",\"readonly\":true,\"affect\":0,\"group\":\"base\",\"visible\":true,\"field\":\"Common\",\"pos\":0,\"unit\":\"None\",\"title\":\"\\u56fa\\u4ef6\\u7248\\u672c\\u53f7\",\"desc\":\"\\u8bbe\\u5907\\u9a71\\u52a8\\u7248\\u672c\\u53f7\",\"type\":\"string\",\"cur\":\"FMWV1001\",\"size\":10,\"ver\":1},\"ip-addr\":{\"category\":\"base\",\"readonly\":true,\"affect\":0,\"group\":\"base\",\"visible\":true,\"field\":\"Common\",\"pos\":0,\"unit\":\"None\",\"title\":\"\\u8bbe\\u5907IP\\u5730\\u5740\",\"desc\":\"\\u5f53\\u8bbe\\u5907\\u8fde\\u63a5\\u7f51\\u7edc\\u65f6\\u5206\\u914d\\u7684IP\\u5730\\u5740\",\"type\":\"string\",\"cur\":\"0.0.0.0\",\"size\":60,\"ver\":1},\"mac-addr\":{\"category\":\"base\",\"readonly\":true,\"affect\":0,\"group\":\"base\",\"visible\":true,\"field\":\"Common\",\"pos\":0,\"unit\":\"None\",\"title\":\"\\u8bbe\\u5907MAC\\u5730\\u5740\",\"desc\":\"\\u8bbe\\u5907MAC\\u5730\\u5740\",\"type\":\"string\",\"cur\":\"00:11:22:33:44:55\",\"size\":30,\"ver\":1}}"); + jsn_->attach_text((char*)"{\"dev-sn\":{\"category\":\"base\",\"readonly\":true,\"affect\":0,\"group\":\"\\u8BBE\\u5907\\u5C5E\\u6027\",\"visible\":true,\"field\":\"Common\",\"pos\":0,\"unit\":\"None\",\"title\":\"\\u8bbe\\u5907\\u5e8f\\u5217\\u53f7\",\"desc\":\"\\u8bbe\\u5907\\u552f\\u4e00\\u7f16\\u7801\",\"type\":\"string\",\"cur\":\"G100S20230001\",\"size\":20,\"ver\":1},\"fmw-ver\":{\"category\":\"base\",\"readonly\":true,\"affect\":0,\"group\":\"\\u8BBE\\u5907\\u5C5E\\u6027\",\"visible\":true,\"field\":\"Common\",\"pos\":0,\"unit\":\"None\",\"title\":\"\\u56fa\\u4ef6\\u7248\\u672c\\u53f7\",\"desc\":\"\\u8bbe\\u5907\\u9a71\\u52a8\\u7248\\u672c\\u53f7\",\"type\":\"string\",\"cur\":\"FMWV1001\",\"size\":10,\"ver\":1},\"ip-addr\":{\"category\":\"base\",\"readonly\":true,\"affect\":0,\"group\":\"\\u8BBE\\u5907\\u5C5E\\u6027\",\"visible\":true,\"field\":\"Common\",\"pos\":0,\"unit\":\"None\",\"title\":\"\\u8bbe\\u5907IP\\u5730\\u5740\",\"desc\":\"\\u5f53\\u8bbe\\u5907\\u8fde\\u63a5\\u7f51\\u7edc\\u65f6\\u5206\\u914d\\u7684IP\\u5730\\u5740\",\"type\":\"string\",\"cur\":\"0.0.0.0\",\"size\":60,\"ver\":1},\"mac-addr\":{\"category\":\"base\",\"readonly\":true,\"affect\":0,\"group\":\"\\u8BBE\\u5907\\u5C5E\\u6027\",\"visible\":true,\"field\":\"Common\",\"pos\":0,\"unit\":\"None\",\"title\":\"\\u8bbe\\u5907MAC\\u5730\\u5740\",\"desc\":\"\\u8bbe\\u5907MAC\\u5730\\u5740\",\"type\":\"string\",\"cur\":\"00:11:22:33:44:55\",\"size\":30,\"ver\":1}}"); } @@ -1650,10 +1669,6 @@ std::string load_mini_file(const char* path) return cont; } -static void image_receiver(MemoryPtr data, bool img, void* param) -{ - ((UsbDevice*)param)->save_image(data, img); -} dyn_mem_ptr UsbDevice::unhandled_ep0(struct usb_functionfs_event* pev) { @@ -1819,13 +1834,11 @@ dyn_mem_ptr UsbDevice::handle_bulk_cmd(LPPACK_BASE pack, uint32_t* used, packet_ } break; case PACK_CMD_SCAN_START: - { - uint64_t cb[] = {(uint64_t)image_receiver, (uint64_t)this}; - ctrl_handler(-1, (usb_ctrlrequest*)-1, (unsigned char*)cb); - + { int err = 0; img_cnt_ = 0; scan_id_ = pack->pack_id; + scan_err_ = 0; if(scan_over_pack_) { scan_over_pack_->release(); @@ -1838,7 +1851,7 @@ dyn_mem_ptr UsbDevice::handle_bulk_cmd(LPPACK_BASE pack, uint32_t* used, packet_ ctrl_handler(-1, (usb_ctrlrequest*)SR_SCAN_CNT, (unsigned char*)scan_cnt_); ctrl_handler(-1, (usb_ctrlrequest*)SR_SCAN_DPI, (unsigned char*)dpi_); ctrl_handler(-1, (usb_ctrlrequest*)SR_CONFIF_IMGPROCPARAM, (unsigned char*)0); - log_cls::log(LOG_LEVEL_ALL, "Start scanning with %d DPI ...\n", dpi_); + log_cls::log(LOG_LEVEL_ALL, "Start scanning %d papers and %d DPI ...\n", scan_cnt_, dpi_); ret = ctrl_handler(-1, nullptr, nullptr); log_cls::log(LOG_LEVEL_ALL, "Start scanning %s\n", ret ? "OK" : "Failed"); } @@ -1886,6 +1899,7 @@ void UsbDevice::init(void) cfg_text_ = "{\"resolution\":{\"category\":\"base\",\"readonly\":false,\"affect\":2,\"ver\":1,\"group\":\"base\",\"visible\":true,\"field\":\"Common\",\"pos\":0,\"unit\":\"None\",\"title\":\"\\u5206\\u8fa8\\u7387\",\"desc\":\"\\u8bbe\\u7f6e\\u626b\\u63cf\\u56fe\\u50cf\\u7684\\u5206\\u8fa8\\u7387\",\"type\":\"int\",\"cur\":200,\"default\":200,\"size\":4,\"range\":[100,150,200,300,600]},\"count\":{\"category\":\"base\",\"readonly\":false,\"affect\":0,\"ver\":1,\"group\":\"base\",\"visible\":true,\"field\":\"Common\",\"pos\":0,\"unit\":\"None\",\"title\":\"\\u626b\\u63cf\\u5f20\\u6570\",\"desc\":\"\\u8bbe\\u7f6e\\u626b\\u63cf\\u7684\\u7eb8\\u5f20\\u6570\\u91cf\",\"type\":\"int\",\"cur\":-1,\"default\":-1,\"size\":4,\"range\":[-1,1]}}"; l = get_config(&dpi_, &l, "resolution"); + l = sizeof(scan_cnt_); l = get_config(&scan_cnt_, &l, "count"); cfg_ = new sane_cfg_mgr(); cfg_->reg_sane_provider(dynamic_cast(r)); @@ -1900,14 +1914,14 @@ void UsbDevice::save_image(MemoryPtr data, bool img) { image_packet *ip = new image_packet(data, ++img_cnt_, scan_id_); std::string mem(sys_util::get_memory_usage("scan")); - uint32_t n = atoi(mem.c_str()); + uint32_t n = atoi(mem.c_str()), que = 0; if(max_mem < n) max_mem = n; printf("image arrived with %u bytes!\n", data->size()); - log_cls::log(LOG_LEVEL_ALL, "New image with bytes: %u\n", data->size()); - usb_->write_bulk(ip); + que = usb_->write_bulk(ip); + log_cls::log(LOG_LEVEL_ALL, "New image with bytes: %u, memory usage: %s, sent-que length: %d\n", data->size(), sys_util::format_readable_bytes((uint64_t)n * 1024).c_str(), que); ip->release(); // check has completed ? @@ -1929,9 +1943,8 @@ void UsbDevice::save_image(MemoryPtr data, bool img) int cmd = /*PACK_CMD_SCAN_FINISHED_ROGER*/0, err = 0; - max_mem *= 1024; - log_cls::log(LOG_LEVEL_ALL, "--Max memory usage: %s--\n", sys_util::format_readable_bytes(max_mem).c_str()); - max_mem = 0; + if(info->From != HGType::MBEvent || scan_id_ == 0) + log_cls::log(LOG_LEVEL_ALL, "Scanner event: From = %d, Code = %d, Img_Index = %d\n", info->From, info->Code, info->Img_Index); if(info->From == HGType::MtBoard) { @@ -1980,21 +1993,39 @@ void UsbDevice::save_image(MemoryPtr data, bool img) { cmd = PACK_CMD_SCAN_FINISHED_ROGER; } - if(scan_id_ && cmd == PACK_CMD_SCAN_FINISHED_ROGER && scan_over_pack_ == nullptr) + + if(scan_id_ == 0 && info->From == HGType::MBEvent) { - log_cls::log(LOG_LEVEL_ALL, "Scan over with error: %d\n", err); - BASE_PACKET_REPLY(*((LPPACK_BASE)reply->ptr()), cmd, scan_id_, err); + BASE_PACKET_REPLY(*((LPPACK_BASE)reply->ptr()), PACK_CMD_STATUS_ROGER, 0, info->Code); reply->set_len(sizeof(PACK_BASE)); - scan_id_ = 0; - - // check if the image-proc-queue has completed ... - ctrl_handler(-2, (usb_ctrlrequest*)SR_GET_IMAGEPROCESSDONE, (unsigned char*)&err); - if(err) - usb_->write_bulk(reply); - else + usb_->write_bulk(reply); + } + else + { + if(err && scan_err_ == 0) + scan_err_ = err; + if(scan_id_ && /*cmd == PACK_CMD_SCAN_FINISHED_ROGER*/info->From == HGType::STOPSCAN && scan_over_pack_ == nullptr) { - scan_over_pack_ = reply; - scan_over_pack_->add_ref(); + char ebuf[20] = {0}; + + max_mem *= 1024; + err = scan_err_; + log_cls::log(LOG_LEVEL_ALL, "Scan over with error: %s; Max memory usage: %s\n", log_cls::str_scanner_status((scanner_status)scan_err_, ebuf), sys_util::format_readable_bytes(max_mem).c_str()); + max_mem = 0; + BASE_PACKET_REPLY(*((LPPACK_BASE)reply->ptr()), cmd, scan_id_, err); + reply->set_len(sizeof(PACK_BASE)); + scan_id_ = 0; + + // check if the image-proc-queue has completed ... + // ctrl_handler(-2, (usb_ctrlrequest*)SR_GET_IMAGEPROCESSDONE, (unsigned char*)&err); + err = 1; + if(err) + usb_->write_bulk(reply); + else + { + scan_over_pack_ = reply; + scan_over_pack_->add_ref(); + } } } reply->release(); diff --git a/pc/code_twain/sln/usb_tools/Debug/usb_tools.exe b/pc/code_twain/sln/usb_tools/Debug/usb_tools.exe index 4bf4a45..5738fad 100644 Binary files a/pc/code_twain/sln/usb_tools/Debug/usb_tools.exe and b/pc/code_twain/sln/usb_tools/Debug/usb_tools.exe differ diff --git a/pc/code_twain/sln/usb_tools/DlgScanner.cpp b/pc/code_twain/sln/usb_tools/DlgScanner.cpp index 3dbfab9..a954876 100644 --- a/pc/code_twain/sln/usb_tools/DlgScanner.cpp +++ b/pc/code_twain/sln/usb_tools/DlgScanner.cpp @@ -604,13 +604,16 @@ const wchar_t* scanner_status(int s, wchar_t unk[20]) RETURN_EQUAL(s, SCANNER_STATUS_SCAN_FINISHED); RETURN_EQUAL(s, SCANNER_STATUS_BUSY); RETURN_EQUAL(s, SCANNER_STATUS_COVER_OPENNED); + RETURN_EQUAL(s, SCANNER_STATUS_COVER_CLOSED); RETURN_EQUAL(s, SCANNER_STATUS_SLEEPING); + RETURN_EQUAL(s, SCANNER_STATUS_WAKED_UP); RETURN_EQUAL(s, SCANNER_STATUS_COUNT_MODE); RETURN_EQUAL(s, SCANNER_STATUS_DOUBLE_FEEDED); RETURN_EQUAL(s, SCANNER_STATUS_PAPER_JAMMED); RETURN_EQUAL(s, SCANNER_STATUS_PAPER_ASKEW); RETURN_EQUAL(s, SCANNER_STATUS_FEED_FAILED); RETURN_EQUAL(s, SCANNER_STATUS_NO_PAPER); + RETURN_EQUAL(s, SCANNER_STATUS_PAPER_ON); RETURN_EQUAL(s, SCANNER_STATUS_STAPLE_ON); RETURN_EQUAL(s, SCANNER_STATUS_SIZE_ERR); RETURN_EQUAL(s, SCANNER_STATUS_DOGEAR); @@ -687,7 +690,8 @@ void CDlgScanner::set_device(usb::LPUSBPNP pnp) { CDlgScanner* dlg = (CDlgScanner*)user_data; - log_cls::log(LOG_LEVEL_DEBUG, "Finished in receiving new image = %d\r\n", err); + if (cur >= total) + log_cls::log(LOG_LEVEL_DEBUG, "Finished in receiving new image = %d\r\n", err); if (err) //::SetDlgItemTextW(m_hWnd, IDC_EDIT_COUNT, (L"Receive image " + std::to_wstring(img_cnt_) + L" error: " + std::to_wstring(err)).c_str()); @@ -740,18 +744,24 @@ void CDlgScanner::set_device(usb::LPUSBPNP pnp) log_cls::log(LOG_LEVEL_DEBUG, "Scan stopped with error %s\r\n", usb::u2a(scanner_status(size, buf)).c_str()); - if (size) - set_text(IDC_EDIT_COUNT, (std::to_wstring(img_cnt_) + L" (Error: " + scanner_status(size, buf) + L")").c_str()); - else - set_text(IDC_EDIT_COUNT, (std::to_wstring(img_cnt_) + L" (Good)").c_str()); + set_text(IDC_EDIT_COUNT, std::to_wstring(img_cnt_).c_str()); + if (size == 0) + size = SCANNER_STATUS_READY; + ::PostMessage(m_hWnd, WM_DEVICE_STATTUS, 0, (LPARAM)size); + return NULL; } }; + auto status_cb = [&](uint32_t status) -> void + { + ::PostMessage(m_hWnd, WM_DEVICE_STATTUS, 0, (LPARAM)status); + }; if (pnp) { int err = 0; + OnDeviceStatus(0, (LPARAM)SCANNER_STATUS_READY); scanner_ = new scanner_handler(); err = scanner_->open_usb_scanner(pnp->device); if (err) @@ -760,6 +770,7 @@ void CDlgScanner::set_device(usb::LPUSBPNP pnp) scanner_ = NULL; msg_box(m_hWnd, MB_OK, L"Error", L"Open %04X:%04X failed with error %d.", pnp->vid, pnp->pid, err); enable_buttons(false); + OnDeviceStatus(0, (LPARAM)SCANNER_STATUS_NOT_OPEN); } else { @@ -779,6 +790,7 @@ void CDlgScanner::set_device(usb::LPUSBPNP pnp) scanner_->release(); scanner_ = NULL; enable_buttons(false); + OnDeviceStatus(0, (LPARAM)SCANNER_STATUS_NOT_OPEN); } else { @@ -792,6 +804,7 @@ void CDlgScanner::set_device(usb::LPUSBPNP pnp) RECT r = { 0 }; std::string all(""); + scanner_->set_status_notifyer(status_cb); if (scanner_->option_get_all(all) == ETIMEDOUT) { // reset it ... @@ -813,6 +826,7 @@ void CDlgScanner::set_device(usb::LPUSBPNP pnp) } else { + OnDeviceStatus(0, (LPARAM)SCANNER_STATUS_LOST_CONNECT); ::SetDlgItemTextW(m_hWnd, IDC_BUTTON_SCAN, L"Scan"); } GetDlgItem(IDC_CHECK_AUTO)->EnableWindow(scanner_ != NULL); @@ -1012,6 +1026,7 @@ BEGIN_MESSAGE_MAP(CDlgScanner, CDialogEx) ON_MESSAGE(WM_IS_BUTTON_CHECKED, &CDlgScanner::OnIsButtonChecked) ON_MESSAGE(WM_SET_BUTTON_CHECK, &CDlgScanner::OnSetButtonChecked) ON_MESSAGE(WM_ENABLE_CTRLS, &CDlgScanner::OnEnableCtrls) + ON_MESSAGE(WM_DEVICE_STATTUS, &CDlgScanner::OnDeviceStatus) END_MESSAGE_MAP() @@ -1191,7 +1206,10 @@ void CDlgScanner::OnBnClickedButtonScan() if (err) msg_box(m_hWnd, MB_OK, L"Error", L"Failed in startin scanning with code %d", err); else + { ::SetDlgItemTextW(m_hWnd, IDC_BUTTON_SCAN, L"Stop"); + OnDeviceStatus(0, (LPARAM)SCANNER_STATUS_SCANNING); + } } } else @@ -1335,3 +1353,11 @@ LRESULT CDlgScanner::OnEnableCtrls(WPARAM wp, LPARAM lp) return 0; } +LRESULT CDlgScanner::OnDeviceStatus(WPARAM wp, LPARAM lp) +{ + wchar_t unk[20] = { 0 }; + + ::SetDlgItemTextW(m_hWnd, IDC_EDIT_STATUS, scanner_status((int)lp, unk)); + + return 0; +} diff --git a/pc/code_twain/sln/usb_tools/DlgScanner.h b/pc/code_twain/sln/usb_tools/DlgScanner.h index 4cbffd1..0c1dc1d 100644 --- a/pc/code_twain/sln/usb_tools/DlgScanner.h +++ b/pc/code_twain/sln/usb_tools/DlgScanner.h @@ -112,4 +112,5 @@ public: afx_msg LRESULT OnIsButtonChecked(WPARAM wp, LPARAM lp); afx_msg LRESULT OnSetButtonChecked(WPARAM wp, LPARAM lp); afx_msg LRESULT OnEnableCtrls(WPARAM wp, LPARAM lp); + afx_msg LRESULT OnDeviceStatus(WPARAM wp, LPARAM lp); }; diff --git a/pc/code_twain/sln/usb_tools/resource.h b/pc/code_twain/sln/usb_tools/resource.h index 53a5877..77d9b2c 100644 --- a/pc/code_twain/sln/usb_tools/resource.h +++ b/pc/code_twain/sln/usb_tools/resource.h @@ -72,6 +72,8 @@ #define IDC_STATIC_CMD 1043 #define IDC_STATIC_PARAM 1044 #define IDC_STATIC_COUNT 1045 +#define IDC_EDIT_PROTOCOL_VER2 1046 +#define IDC_EDIT_STATUS 1046 #define IDC_EDIT_CMD_QUE 1047 #define IDC_EDIT_SENT_QUE 1048 #define IDC_EDIT_IND 1049 @@ -89,7 +91,6 @@ #define IDC_STATIC_SENT_QUE 1062 #define IDC_STATIC_OPTS 1063 #define IDC_CHECK_AUTO_OPEN_IMG 1064 -#define IDC_EDIT1 1065 #define IDC_EDIT_LOG_FILE 1065 #define ID_TRAY_EXIT 32771 diff --git a/pc/code_twain/sln/usb_tools/scanner/opt_ui/DlgPage.h b/pc/code_twain/sln/usb_tools/scanner/opt_ui/DlgPage.h index f57d21d..074e002 100644 --- a/pc/code_twain/sln/usb_tools/scanner/opt_ui/DlgPage.h +++ b/pc/code_twain/sln/usb_tools/scanner/opt_ui/DlgPage.h @@ -31,6 +31,7 @@ #define WM_IS_BUTTON_CHECKED WM_USER + 321 // WPARAM: id; LPARAM: bool* #define WM_SET_BUTTON_CHECK WM_USER + 322 // WPARAM: id; LPARAM: bool #define WM_ENABLE_CTRLS WM_USER + 323 // WPARAM: id (0 is click repeat button); LPARAM: bool +#define WM_DEVICE_STATTUS WM_USER + 324 // WPARAM: unused; LPARAM: scanner_status extern HMODULE g_my_inst; diff --git a/pc/code_twain/sln/usb_tools/scanner/opt_ui/DlgSetting.cpp b/pc/code_twain/sln/usb_tools/scanner/opt_ui/DlgSetting.cpp index faeb79b..a5eeb73 100644 --- a/pc/code_twain/sln/usb_tools/scanner/opt_ui/DlgSetting.cpp +++ b/pc/code_twain/sln/usb_tools/scanner/opt_ui/DlgSetting.cpp @@ -313,7 +313,10 @@ dlg_page* dlg_setting::add_tab(const char* utf8_title) GetTextExtentPointW(hdc, title.c_str(), title.length(), &text); ReleaseDC(hwnd(), hdc); tab_ = CreateWindowW(L"SysTabControl32", L"pages", WS_CHILD | WS_VISIBLE, 0, 0, 100, text.cy + 6, hwnd(), NULL, g_my_inst, NULL); - SendMessage(tab_, WM_SETFONT, (WPARAM)SendMessage(get_item(IDOK), WM_GETFONT, 0, 0), 1); + wcscpy(lf.lfFaceName, L"微软雅黑"); + lf.lfHeight = 18; + font = CreateFontIndirectW(&lf); + SendMessage(tab_, WM_SETFONT, (WPARAM)font, 1); SetWindowLong(tab_, GWL_ID, 1234); ShowWindow(tab_, SW_SHOW); } diff --git a/pc/code_twain/sln/usb_tools/usbtools.rc b/pc/code_twain/sln/usb_tools/usbtools.rc index 62cd8bc..345ae8d 100644 Binary files a/pc/code_twain/sln/usb_tools/usbtools.rc and b/pc/code_twain/sln/usb_tools/usbtools.rc differ