找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 483|回复: 0
打印 上一主题 下一主题
收起左侧

FPM32C指纹模块源程序

[复制链接]
跳转到指定楼层
楼主
ID:334697 发表于 2023-7-20 19:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include <string.h>
  2. #include "fingerprint_action.h"
  3. #include "fingerprint_type.h"
  4. #include "fingerprint_protocol.h"

  5. static U8Bit s_recv_buffer[256];
  6. static U8Bit s_send_buffer[256];
  7. static U8Bit s_ftrDataBuffer[8196];        //待上传下载的指纹数据buffer
  8. static U32Bit s_ftrDataLength = 0;          //待上传下载的指纹数据buffer长度

  9. #define CHECK_BIT(value, x) ((value) & (1 << (x)))

  10. void Delay_ms(U32Bit delaytime)
  11. {
  12.      U32Bit i;
  13.      U32Bit j;

  14.      for (j = 0; j < delaytime; j++)
  15.      {
  16.          for (i = 0; i < 10000; i++)
  17.          {
  18.              //根据不同的开发环境实现延时
  19.          }
  20.      }
  21.      i=j;
  22. }


  23. void getID(void)
  24. {
  25.     U32Bit errorCode;
  26.         S32Bit ret;

  27.     FP_moduleID_t moduleID;

  28.     ret = FP_action_getID(&moduleID, &errorCode);
  29.     if(FP_OK == ret && 0 == errorCode)
  30.     {
  31.         //get ID ok
  32.     }
  33. }

  34. void enroll(void)
  35. {
  36.     U32Bit errorCode;
  37.         S32Bit ret;
  38.     U8Bit index = 1;
  39.     U8Bit isTouch;
  40.         U16Bit SaveID;

  41.     FP_enrollResult_t enrollResult;
  42.     enrollResult.fingerID = 0;
  43.     enrollResult.progress = 0;

  44.     //第一次直接enrollStart
  45.     goto EnrollStartLabel;

  46.     //
  47.     //每次enroll过后,确保手指要抬起来以后,再次按压
  48. FingerLeaveLabel:               
  49.     FP_action_check_finger_is_touch(&isTouch, &errorCode);
  50.     if(0 != isTouch || COMP_CODE_OK != errorCode)
  51.     {
  52.         // 当isTouch不是0的时候,意味着手指在上,
  53.         //提示用户手指离开sensor再次按压
  54.         //printf("lift your finger please !");
  55.         //延时 200 ms
  56.                 Delay_ms(200);
  57.         goto FingerLeaveLabel;
  58.     }

  59.     //开始注册
  60. EnrollStartLabel:
  61.     ret = FP_action_enroll_start(index, &errorCode);
  62.     if(COMP_CODE_OK != errorCode || FP_OK != ret)
  63.     {
  64.             Delay_ms(100);
  65.                 //可能上电初始化未完成或睡眠唤醒初始化中
  66.                 goto EnrollStartLabel;
  67.         }
  68.         else
  69.         {
  70.                 //可延时100ms后发送获取注册结果命令,采图后需要大概这么长时间处理
  71.                 Delay_ms(100);
  72.         }

  73.     //获取注册结果
  74. EnrollResultLabel:
  75.     ret = FP_action_get_enroll_result(&enrollResult, &errorCode);
  76.     if(FP_DEVICE_TIMEOUT_ERROR == ret)
  77.         {
  78.                 //接受数据超时,根据接受数据所设超时时间适当延时。
  79.                 Delay_ms(100);
  80.                 goto EnrollResultLabel;
  81.         }

  82.     if(COMP_CODE_OK == errorCode)
  83.     {
  84.         //如果errorCode 是COMP_CODE_OK,说明本次enroll执行完成,
  85.         //此时可以查看enrollResult.progress是否增加,如果增加,说明本次enroll成功

  86.         //如果progress >= 100 ,说明整个注册流程成功结束,开始根据enrollResult.fingerID保存指纹
  87.         if(enrollResult.progress >= 0x64)
  88.         {
  89.             goto SaveStartLabel;
  90.         }
  91.         else
  92.         {
  93.             //如果progress < 100, 手指离开sensor,再次按压,继续注册
  94.             index++;
  95.             //延时 100 ms
  96.             Delay_ms(100);
  97.             goto FingerLeaveLabel;
  98.         }
  99.     }
  100.     else if(COMP_CODE_CMD_NOT_FINISHED == errorCode)
  101.     {
  102.         //errorCode == COMP_CODE_CMD_NOT_FINISHED意味着sensor还没有处理完指纹数据或没有按压手指,
  103.         //适当延时,再次获取结果
  104.         //延时 100 ms
  105.         Delay_ms(100);
  106.         goto EnrollResultLabel;
  107.     }
  108.     else if(COMP_CODE_SAME_ID == errorCode)
  109.     {
  110.         //errorCode == COMP_CODE_SAME_ID意味着与已注册指纹重复,需换手指注册
  111.         //适当延时,再次获取结果
  112.         //延时 100 ms
  113.         Delay_ms(100);
  114.         goto EnrollResultLabel;
  115.     }
  116.         else if(COMP_CODE_NO_FINGER_DETECT == errorCode)
  117.         {
  118.                 //errorCode == COMP_CODE_NO_FINGER_DETECT意味着超时还没按压手指
  119.         //重新注册
  120.         }
  121.         else if(COMP_CODE_OTHER_ERROR == errorCode)
  122.         {
  123.          goto EnrollStartLabel;
  124.         }
  125.         else if(COMP_CODE_NO_REQ_CMD == errorCode)
  126.         {
  127.                 goto EnrollStartLabel;
  128.         }
  129.         else
  130.     {
  131.         //图像质量不好,手指可能按压过重、过轻、或者太过潮湿等,继续enrollStart即可,也可根据Elock用户手册细化处理
  132.         //延时 200 ms
  133.             Delay_ms(200);
  134.         goto EnrollStartLabel;
  135.     }

  136.     //保存指纹开始
  137.     //enrollResult.fingerID会根据模组回复的推荐id去保存,编号从00开始
  138. SaveStartLabel:
  139.     ret = FP_action_save_start(enrollResult.fingerID, &errorCode);
  140.     if(COMP_CODE_OK != errorCode || FP_OK != ret)
  141.     {
  142.             Delay_ms(100);
  143.                 goto SaveStartLabel;
  144.         }

  145.         Delay_ms(200);
  146.     //获取保存指纹结果
  147. SaveResultLabel://【保存指纹开始命令】发送后,模组需操作flash,这期间大概200ms发送【获取保存指纹结果】没有数据回复,可超时重发
  148.     ret = FP_action_get_save_result(&errorCode, &SaveID);
  149.         if(FP_DEVICE_TIMEOUT_ERROR == ret)
  150.         {
  151.                 //接受数据超时,根据接受数据所设超时时间适当延时。
  152.                 Delay_ms(100);
  153.                 goto SaveResultLabel;
  154.         }
  155.         
  156.     if(COMP_CODE_OK == errorCode)
  157.     {
  158.             //查看保存成功的SaveID
  159.         //保存完成
  160.     }
  161.     else if(COMP_CODE_CMD_NOT_FINISHED == errorCode)
  162.     {
  163.         //还未保存完成,延时适当时间,再次获取结果
  164.         //延时 100 ms
  165.         Delay_ms(100);
  166.         goto SaveResultLabel;
  167.     }
  168.     else if(COMP_CODE_STORAGE_IS_FULL == errorCode)
  169.     {
  170.         //flash存储已满,不能保存指纹
  171.     }
  172.     else
  173.     {
  174.         //其他错误,比如硬件错误等。
  175.     }

  176.     return;
  177. }

  178. void match(void)
  179. {
  180.     U32Bit errorCode;
  181.     S32Bit ret;
  182.     U8Bit isTouch;
  183.     FP_matchResult_t matchResult;

  184. /***********************
  185. 为了提速,也可以不先检查手指在位,
  186. 不同的模组回复时间不同(FPM08X系列模组大约耗时30ms左右)
  187. ************************/
  188. checkFingeronLabel:
  189.     FP_action_check_finger_is_touch(&isTouch, &errorCode);
  190.     if((0 != isTouch) && (COMP_CODE_OK == errorCode))
  191.     {
  192.         //当isTouch是1的时候,意味着手指在上,
  193.         //播放语音“滴”
  194.     }
  195.     else
  196.     {
  197.         //延时 50 ms
  198.         Delay_ms(50);
  199.         goto checkFingeronLabel;
  200.     }
  201. /**********************/   
  202. matchStartLabel:
  203.     //开始match
  204.     ret = FP_action_match_start(&errorCode);
  205.         if(COMP_CODE_OK != errorCode || FP_OK != ret)
  206.         {
  207.                 //延时 50 ms
  208.                 //可能上电初始化未完成或睡眠唤醒初始化中
  209.         Delay_ms(50);
  210.                 goto matchStartLabel;
  211.         }

  212.         //匹配处理固定大于300ms;
  213.     Delay_ms(300);
  214.     //获取注册结果
  215. matchResultLabel:
  216.     ret = FP_action_get_match_result(&matchResult,&errorCode);
  217.     if(FP_DEVICE_TIMEOUT_ERROR == ret)
  218.         {
  219.                 //接受数据超时,根据接受数据所设超时时间适当延时。
  220.                 Delay_ms(100);
  221.                 goto matchResultLabel;
  222.         }

  223.     if(COMP_CODE_OK == errorCode)
  224.     {
  225.         //match动作完成

  226.         /*  typedef struct {
  227.          *      U16Bit isPass;
  228.          *      U16Bit matchScore;
  229.          *      U16Bit matchID;
  230.          * } FP_matchResult_t, *FP_matchResult_p;
  231.          */

  232.         // isPass是1的话,代表已经匹配到了指纹,这时,matchID是匹配到的指纹ID
  233.         // matchScore是具体匹配的分数

  234.         if(1 == matchResult.isPass)
  235.         {
  236.             //匹配到指纹
  237.         }
  238.         else
  239.         {
  240.             //未匹配到指纹
  241.         }

  242.     }
  243.     else if(COMP_CODE_CMD_NOT_FINISHED == errorCode)
  244.     {
  245.         //还未匹配完成,延时适当时间,再次获取结果,处理指纹需要一些时间
  246.         //延时 30 ms
  247.             Delay_ms(30);
  248.         goto matchResultLabel;
  249.     }
  250.         else if(COMP_CODE_NO_FINGER_DETECT == errorCode)
  251.         {
  252.                 //errorCode == COMP_CODE_NO_FINGER_DETECT意味着超时还没按压手指
  253.         //重新匹配
  254.         }
  255.     else if(COMP_CODE_NO_REQ_CMD == errorCode)
  256.     {
  257.         goto matchStartLabel;
  258.         }
  259.     else if(COMP_CODE_HARDWARE_ERROR == errorCode)
  260.     {
  261.         //延时 200 ms
  262.         Delay_ms(200);
  263.         goto matchStartLabel;
  264.     }
  265.         else
  266.     {
  267.         return;
  268.     }

  269. }


  270. /***匹配成功后执行更新指纹特征(模组自学习),有利于优化体验*
  271.     该命令可在匹配成功以后直接发送,模组自身会判断是否要自学习,
  272.     如果不需要会返回错误码
  273. **/
  274. void updateFeature(void)
  275. {
  276.     U32Bit errorCode;
  277.     S32Bit ret;

  278.     S32Bit updateID = 0;//传入的ID必须与匹配成功后的ID一致

  279. updatestartLabel:
  280.     ret = FP_action_update_start(updateID, &errorCode);
  281.     if(COMP_CODE_OK == errorCode)
  282.     {        
  283.         //需要更新,延时100毫秒
  284.         Delay_ms(100);
  285.     }
  286.     else
  287.     {
  288.        //不需要更新,直接结束
  289.         return;
  290.     }

  291. updateResultLabel:
  292.     ret = FP_action_get_update_result(&errorCode);
  293.     if(ret == FP_DEVICE_TIMEOUT_ERROR)
  294.     {
  295.         Delay_ms(100);
  296.         goto updateResultLabel;
  297.     }

  298.     if(COMP_CODE_OK == errorCode)
  299.     {
  300.             //更新成功;
  301.     }
  302.     else if(COMP_CODE_CMD_NOT_FINISHED == errorCode)
  303.     {
  304.         //适当延时100ms,再次获取
  305.         Delay_ms(100);
  306.         goto updateResultLabel;
  307.     }
  308.     else
  309.     {
  310.         //更新失败,重新更新,或者退出
  311.         goto updatestartLabel;
  312.     }
  313.                
  314.     return;
  315. }


  316. void deleteFp(void)
  317. {
  318.     U32Bit errorCode;
  319.     S32Bit ret;
  320.     S16Bit delete_id = -1;

  321.     //开始删除,delete_id > 0 的时候,删除指定id的指纹。
  322.     //delete_id == -1的时候,删除所有指纹,
  323. deleteStartLabel:
  324.     ret = FP_action_delete_start(delete_id, &errorCode);
  325.         if(COMP_CODE_OK != errorCode || FP_OK != ret)
  326.         {
  327.                 //延时 100 ms
  328.                 //可能上电初始化未完成或睡眠唤醒初始化中
  329.                 Delay_ms(100);
  330.                 goto deleteStartLabel;
  331.         }

  332.     //获取删除结果
  333. deleteResultLabel:
  334.         Delay_ms(100);      //延时 100 ms
  335.     ret = FP_action_get_delete_result(&errorCode);
  336.     if(ret == FP_DEVICE_TIMEOUT_ERROR)
  337.     {
  338.         goto deleteResultLabel;
  339.     }
  340.         
  341.     if(COMP_CODE_OK == errorCode)
  342.     {
  343.         //删除成功
  344.     }
  345.     else if(COMP_CODE_CMD_NOT_FINISHED == errorCode)
  346.     {
  347.         //还未删除完成,再次获取结果
  348.         goto deleteResultLabel;
  349.     }
  350.     else
  351.     {
  352.         //其他错误,比如指纹ID不存在等、flash硬件错误等。
  353.     }

  354.     return;
  355. }

  356. void Sleep(void)
  357. {
  358.          U32Bit errorCode;
  359.      S32Bit ret;

  360. SleepStartLabel:
  361.     ret = FP_action_sleep(sleep_type_normal, &errorCode);
  362.     if(FP_OK == ret && 0 == errorCode)
  363.     {
  364.         //休眠成功,模组进入休眠
  365.     }
  366.     else
  367.     {
  368.         //延时 200 ms, 继续发送休眠命令,直到回复休眠成功为止
  369.                 Delay_ms(200);
  370.         goto SleepStartLabel;
  371.     }
  372. }


  373. void autoEnroll(void)
  374. {
  375.     U32Bit errorCode;
  376.         S32Bit ret;
  377.     FP_auto_enroll_t enrollPara;
  378.         FP_enrollResult_t EnrollResult;
  379.     U32Bit timeout = 10000; //建议超时时间设为10s

  380.         enrollPara.fingerID_high = 0xFF;  //可修改ID,若 ID 为 0xFFFF 则由系统自动分配指纹 ID 。
  381.         enrollPara.fingerID_low = 0xFF;  //可修改ID,若 ID 为 0xFFFF 则由系统自动分配指纹 ID 。
  382.         
  383.         enrollPara.enroll_mode = 0x01; //设置为 1则表示按压后需要等待手指抬起再次才可以进行下次注册,设置为0则不需要。
  384.         enrollPara.times = 3;                  //可修改注册次数,范围:1~6次

  385. SendEnrollStartLabel:
  386.         ret = FP_action_auto_enroll_send(enrollPara);
  387.     if(FP_OK != ret)
  388.     {
  389.         goto SendEnrollStartLabel;
  390.     }

  391. GetEnrollResultLabel:
  392.     ret = FP_action_auto_enroll_recv( &EnrollResult , &errorCode, timeout);  //处理返回码
  393.     if(0 != ret)
  394.     {
  395.         //可能在等待手指按压,或者正在处理注册,再重新查询是否接收到
  396.         goto GetEnrollResultLabel;
  397.     }

  398.     if(errorCode == COMP_CODE_CMD_DATA_LEN_ERROR || errorCode == COMP_CODE_CMD_DATA_ERROR)
  399.     {
  400.         //检查命令是否发错,重新开始注册
  401.         goto SendEnrollStartLabel;
  402.     }

  403.     if(errorCode == COMP_CODE_STORAGE_IS_FULL)
  404.     {
  405.         //指纹容量已满,退出
  406.         return;
  407.     }

  408.     if((100 == EnrollResult.progress) && (EnrollResult.state == 0xff))
  409.     {
  410.         if(COMP_CODE_OK == errorCode)
  411.         {
  412.             // 进度100,state=0XFF时才认为注册成功
  413.             // 注册成功
  414.         }
  415.         else
  416.         {
  417.             //存储出了问题,重新开始注册
  418.             goto SendEnrollStartLabel;
  419.         }
  420.     }
  421.     else
  422.     {
  423.         if(errorCode == COMP_CODE_OK)
  424.         {
  425.             //单次注册成功,继续按压
  426.         }
  427.         else if(errorCode == COMP_CODE_SAME_ID)
  428.         {
  429.             //重复指纹,可提示用户换手指按压
  430.         }
  431.         else if(errorCode == COMP_CODE_UNQUALIFIED_IMAGE_ERROR)
  432.         {
  433.             //采图质量不好,继续按压直到成功
  434.         }
  435.         else if(errorCode == COMP_CODE_NO_FINGER_DETECT)
  436.         {
  437.             //8S超时还没按压手指,继续按压直到注册成功;
  438.         }
  439.         else
  440.         {
  441.             //其他问题,继续按压
  442.         }

  443.         //继续获取下一次结果
  444.         goto GetEnrollResultLabel;
  445.     }

  446.     return;
  447. }


  448. void matchSyn(void)
  449. {
  450.     U32Bit errorCode;
  451.         S32Bit ret;
  452.     U32Bit timeout = 10000; //建议超时时间设为10s
  453.     FP_matchResult_t matchResult;

  454. matchSynSendLabel:
  455.     ret = FP_action_match_syn_send();
  456.     if(FP_OK != ret)
  457.     {
  458.         goto matchSynSendLabel;
  459.     }

  460. getMatchSynResultLabel:
  461.     ret = FP_action_match_syn_recv(&matchResult, &errorCode, timeout);
  462.     if(FP_DEVICE_TIMEOUT_ERROR == ret)
  463.     {
  464.         //可能在等待手指按压,或者正在处理匹配,再重新查询是否接收到
  465.         goto getMatchSynResultLabel;
  466.     }

  467.     if((FP_OK == ret) && (COMP_CODE_OK == errorCode))
  468.     {
  469.         if(1 == matchResult.isPass)
  470.         {
  471.             //匹配成功
  472.             //同步匹配会自带自学习功能,所以耗时会相对于分步匹配长一点
  473.         }
  474.         else
  475.         {
  476.             //匹配失败
  477.         }
  478.     }
  479.     else if(COMP_CODE_NO_FINGER_DETECT == errorCode)
  480.     {
  481.         //没有手指按压
  482.     }
  483.     else if(COMP_CODE_UNQUALIFIED_IMAGE_ERROR == errorCode)
  484.     {
  485.         //采图质量不佳
  486.         goto matchSynSendLabel;
  487.     }
  488.     else
  489.     {
  490.         //其他错误
  491.     }
  492. }


  493. void deleteSyn(void)
  494. {
  495.     U32Bit errorCode;
  496.     S32Bit ret;
  497.     U32Bit timeout = 1000;         //建议超时时间设为1s
  498.     S16Bit delete_id = -1;

  499.     //delete_id > 0 的时候,删除指定id的指纹。
  500.     //delete_id == -1的时候,删除所有指纹,

  501. SendDeleteStartLabel:
  502.     ret = FP_action_delete_syn_send(delete_id);
  503.     if(FP_OK != ret)
  504.     {
  505.         goto SendDeleteStartLabel;
  506.     }

  507. GetDeleteResultLabel:
  508.     ret = FP_action_delete_syn_recv(&errorCode, timeout);
  509.     if(ret == FP_DEVICE_TIMEOUT_ERROR)
  510.     {
  511.         goto GetDeleteResultLabel;
  512.     }

  513.     if((FP_OK == ret) && (COMP_CODE_OK == errorCode))
  514.     {
  515.         //删除成功
  516.     }
  517.     else
  518.     {
  519.         //其他错误,比如指纹ID不存在等、flash硬件错误等。
  520.     }

  521.     return;
  522. }


  523. /**********************************************************************************************
  524. 上传指纹特征:适用于FPM08X系列软件版本大于118的模组(如版本号为:FPM083-00-118);

  525. 一般的,分为三种应用场景:
  526.     1、上传模组内正在注册的id:替代保存指纹命令(0x13)+ 查询保存结果命令(0x14),
  527.         直接将指纹数据上传至上位机;

  528.     2、上传模组内存储的指定一个id:上传之前,需确认id是否存在,如果存在则继续上传;

  529.     3、上传模组内存储的所有id;上传之前,需获取指纹模板分布,然后根据分布指定id号,一个一个上传;

  530. *********************************************************************************************/

  531. void uploadFTR(void)
  532. {
  533.     FP_ftrData_t uploadftrData;
  534.     FP_ftrDistribution_t distribution;
  535.     U32Bit errorCode;
  536.     S32Bit ret;
  537.     U16Bit ftrLength;
  538.     S32Bit i = 0;
  539.     U16Bit frameCount;
  540.     U8Bit ftrReceiveBuffer[128];        //每帧命令回复的buffer
  541.     U8Bit err_num = 0;

  542. /*
  543.     typedef struct {
  544.         U16Bit id;          //待上传的指纹id
  545.         U16Bit length;      //待上传的指纹数据长度
  546.         U8Bit  *data;      
  547.     }FP_ftrData_t, *FP_ftrData_p;
  548. */

  549.     uploadftrData.data = s_ftrDataBuffer;
  550.     uploadftrData.id = 0x00;

  551. /******上传模组内正在注册的id*************
  552.     //注册进度100%
  553.     //保存指纹开始
  554.     uploadftrData.id = 0xFFFF;      //当id为0XFFFF时,可直接上传
  555.     goto UploadFtrGetInfoLabel;
  556. *****************************************/

  557. UploadFtrCheckFtrLabel:
  558.     ret = FP_action_get_ftr_distribution(&distribution, &errorCode);
  559.     if(FP_OK != ret || errorCode != FP_OK)
  560.     {
  561.         //错误,重新发送  
  562.         Delay_ms(100);
  563.         goto UploadFtrCheckFtrLabel;
  564.     }

  565. /*****************上传模组内存储的指定一个id**********/
  566. //  Printf("distribution[] = 0x%x\r\n", distribution.distributon[check_id >> 3]);

  567.     if(0 == CHECK_BIT(distribution.distributon[uploadftrData.id >> 3], (uploadftrData.id & 0x7)))      
  568.     {
  569.         //id不存在,重新输出正确id号
  570.         Delay_ms(100);
  571.         goto UploadFtrCheckFtrLabel;
  572.     }
  573. /****************************************************/

  574. /*********上传模组内存储的所有id********************
  575.     S16Bit upload_id[100] = 0;  //待上传的id,用于上传所有指纹
  576.     U8Bit currentByte = 0;
  577.     S16Bit *p = &upload_id[0];

  578.     for(int i = 0; i <= (distribution.ftrMaxCount >> 3); i++)
  579.     {
  580.         currentByte = distribution.distributon[i];
  581.         for(int j = 0; j < 8; j++)
  582.         {
  583.             if(CHECK_BIT(currentByte,j))
  584.             {
  585.                 *p = i*8 + j;     
  586.                 p++;
  587.             }
  588.         }
  589.     }
  590. **************************************************/
  591. UploadFtrGetInfoLabel:
  592.     ret= FP_action_get_ftr_info(&uploadftrData, &errorCode);
  593.     if( FP_OK != ret)
  594.     {
  595.         Delay_ms(100);
  596.         goto UploadFtrGetInfoLabel;
  597.     }

  598.     ftrLength = uploadftrData.length;
  599.     frameCount = ((ftrLength - 1) >> 7) + 1;   

  600. UploadFtrGetDataLabel:
  601.     for(; i < frameCount; i++)
  602.     {
  603.         ret = FP_action_get_ftr_data(i, ftrReceiveBuffer, &errorCode);
  604.         if(ret != FP_OK || errorCode != FP_OK)
  605.         {
  606.             err_num++;
  607.             if(err_num < 3)
  608.             {
  609.                 //数据接收有误重新发送该帧
  610.                 goto UploadFtrGetDataLabel;
  611.             }
  612.             else
  613.             {
  614.                 //如果超过3次,报错退出
  615.                 return;
  616.             }
  617.         }
  618.         else
  619.         {
  620.             err_num = 0;
  621.             memcpy(uploadftrData.data + 128*i, ftrReceiveBuffer, 128);    //模组每帧回复的数据长度其实是固定的128字节
  622.         }
  623.     }

  624.     s_ftrDataLength = ftrLength;

  625.     /*
  626.         FTR存储在上位机数据库前
  627.         需对ftrData的长度ftrLength进行记录,作为该指纹下载时的长度;
  628.         可把ftrData打包一下(加一个CRC或奇偶校验)
  629.     */
  630. }


  631. /*********************************************************************************************
  632. 下载指纹特征:适用于FPM08X系列软件版本大于118的模组(如版本号为:FPM083-00-118);
  633. 将指纹数据分发到模组中
  634. */
  635. void downloadFTR(void)
  636. {
  637.     FP_ftrData_t downloadftrData;
  638.     U8Bit ftrSentBuffer[128];          //每帧命令发送的buffer
  639.     U16Bit i = 0;
  640.     U16Bit remainLength;
  641.     U16Bit currentLength;
  642.     U16Bit frameCount;
  643.     U32Bit errorCode;
  644.     U32Bit ret;
  645.     U32Bit timeout = DEFAULT_TIME_OUT;
  646.     U8Bit err_num = 0;

  647. /*
  648.     typedef struct {
  649.         U16Bit id;          //待下载的指纹id
  650.         U16Bit length;      //待下载的指纹数据长度
  651.         U8Bit  *data;      
  652.     }FP_ftrData_t, *FP_ftrData_p;
  653. */
  654.     downloadftrData.data = s_ftrDataBuffer;
  655.     downloadftrData.length = s_ftrDataLength;
  656.     downloadftrData.id = 0;

  657. DownloadFtrSentInfoLabel:
  658.     ret = FP_action_write_ftr_info(&downloadftrData, &errorCode);
  659.     if(FP_OK != ret || errorCode != FP_OK)
  660.     {
  661.         Delay_ms(100);
  662.         goto DownloadFtrSentInfoLabel;
  663.     }

  664.     remainLength = downloadftrData.length;
  665.     currentLength = 128;
  666.     frameCount = ((downloadftrData.length - 1) >> 7) + 1;

  667. DownloadFtrSentDataLabel:
  668.     for(; i < frameCount; i++)
  669.     {
  670.         memcpy(ftrSentBuffer, downloadftrData.data + 128*i, currentLength);

  671.         //如果发最后一帧,由于模组需要存储指纹,命令超时时间需久一点, 1S比较合适
  672.         if (i == frameCount - 1)
  673.         {
  674.             timeout = 1000;
  675.         }
  676.         ret = FP_action_write_ftr_data(i, ftrSentBuffer, currentLength, &errorCode, timeout);
  677.         if(FP_OK != ret || errorCode != FP_OK)
  678.         {
  679.             err_num++;
  680.             if(err_num < 3)
  681.             {
  682.                 //数据发送有误,重新发送该帧
  683.                 Delay_ms(100);
  684.                 goto DownloadFtrSentDataLabel;

  685.             }
  686.             else
  687.             {
  688.                 //如果超过3次,报错退出
  689.                 return;
  690.             }
  691.         }
  692.         else
  693.         {
  694.             err_num = 0;
  695.             remainLength -= currentLength;
  696.             if(remainLength < 128)
  697.             {
  698.                 currentLength = remainLength;
  699.             }
  700.         }
  701.     }
  702. }

  703. /*********************************************************************************************
  704. 设置 LED 控制信息:适用于带LED灯的模组;
  705. */
  706. void setLed(void)
  707. {
  708.     U32Bit errorCode;
  709.     S32Bit ret;
  710.         FP_LED_CTRL_INFO stLEDCtrlInfo;

  711. setLedStartLabel:
  712. /**灯的模式与颜色按需求设置,以下为参考范例1:闪烁绿灯5次,亮的时间为200ms,灭的时间为200ms*/
  713.     stLEDCtrlInfo.ucLEDCtrlMode = EM_LED_CTRL_BLINK;
  714.     stLEDCtrlInfo.ucLEDColor = EM_LED_GREEN;
  715.     stLEDCtrlInfo.usLEDpara1 = 0x14;    //点亮时长
  716.     stLEDCtrlInfo.usLEDpara2 = 0x14;    //熄灭时长
  717.     stLEDCtrlInfo.usLEDpara3 = 0x5;     //闪烁次数
  718. /***/
  719. /**灯的模式与颜色按需求设置,以下为参考范例2:蓝灯呼吸,最大占空比100,最小占空比0,占空比每秒变化50%)*
  720.     stLEDCtrlInfo.ucLEDCtrlMode = EM_LED_CTRL_PWM;
  721.     stLEDCtrlInfo.ucLEDColor = EM_LED_BLUE;
  722.     stLEDCtrlInfo.usLEDpara1 = 0x64;    //最大占空比
  723.     stLEDCtrlInfo.usLEDpara2 = 0x00;    //熄灭时长
  724.     stLEDCtrlInfo.usLEDpara3 = 0x32;     //闪烁次数
  725. ***/
  726.     ret = FP_action_set_led(&stLEDCtrlInfo, &errorCode);
  727.     if(FP_OK == ret && 0 == errorCode)
  728.     {
  729.         //点灯成功
  730.     }
  731.     else
  732.     {
  733.         //延时 50 ms, 继续发送点灯命令,或者直接退出
  734.         Delay_ms(50);
  735.         goto setLedStartLabel;
  736.     }
  737. }


  738. /*********************************************************************************************
  739. 设置系统策略:用于设置模组默认的功能;如关闭重复指纹检查功能
  740. */
  741. void setSysPolicy(void)
  742. {
  743.     U32Bit errorCode;
  744.     S32Bit ret;
  745.     U32Bit SysPolicy_value;
  746.     U32Bit SysPolicy_bit_selflearn = 0x00000002;    //重复指纹检查功能的位
  747.     U32Bit timeout = 1000; //建议超时时间设为1s

  748. /**设置系统策略之前,先获取系统策略*/
  749. getSysPolicyLabel:

  750.     ret = FP_action_get_SysPolicy(&SysPolicy_value, &errorCode);
  751.     if(FP_OK == ret && 0 == errorCode)
  752.     {
  753.         //获取策略成功
  754.     }
  755.     else
  756.     {
  757.         //延时 50 ms, 继续发送点灯命令,或者直接退出
  758.         Delay_ms(50);
  759.         goto getSysPolicyLabel;
  760.     }

  761. setSysPolicyLabel:
  762.     SysPolicy_value &= ~SysPolicy_bit_selflearn;

  763.     ret = FP_action_set_SysPolicy(&SysPolicy_value, &errorCode, timeout);  //处理返回码
  764.     if(FP_OK == ret && 0 == errorCode)
  765.     {
  766.         //获取策略成功后,模组会自行复位
  767.         Delay_ms(200);
  768.         return;
  769.     }
  770.     else
  771.     {
  772.         //延时 50 ms, 继续发送
  773.         Delay_ms(50);
  774.         goto setSysPolicyLabel;
  775.     }
  776. }


  777. void demo_main(U8Bit command)
  778. {
  779.     //设置指纹收发的buffer
  780.     FP_action_set_send_buffer(s_send_buffer);
  781.     FP_action_set_recv_buffer(s_recv_buffer);

  782.     switch(command)
  783.     {
  784.         case 0:
  785.             getID();
  786.             break;
  787.         case 1:
  788.             enroll();
  789.             break;
  790.         case 2:
  791.             match();
  792.             break;
  793.         case 3:
  794.             deleteFp();
  795.             break;
  796.             case 4:
  797.             Sleep();
  798.             break;
  799.             case 5:
  800.             updateFeature();
  801.                     break;
  802.         case 6:
  803.             autoEnroll();
  804.             break;
  805.         case 7:
  806.             matchSyn();
  807.             break;
  808.         case 8:
  809.             deleteSyn();
  810.             break;
  811.         case 9:
  812.             uploadFTR();
  813.             break;
  814.         case 10:
  815.             downloadFTR();
  816.             break;
  817.         case 11:
  818.             setLed();
  819.             break;
  820.         case 12:
  821.             setSysPolicy();
  822.             break;

  823.         default:
  824.             break;
  825.     }

  826. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表