本帖最后由 mmt 于 2024-4-29 18:27 编辑
ZW101.c文件。实现了基本的功能,还有待优化。- #include "ZW101.h"
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #include "bflb_mtimer.h"
- #include "bflb_dma.h"
- #include "bflb_uart.h"
- #include "bflb_gpio.h"
- #include "log.h"
- #include "matrix_key.h"
- extern uint16_t key_out();
- //缓冲区号
- #define Buffer1 0x01
- #define Buffer2 0x02
- //模块包头
- uint8_t PS_Head[2] = {0xEF,0x01};
- //模块地址
- uint8_t PS_Addr[4] = {0xFF,0xFF,0xFF,0xFF};
- //控制模块LED灯颜色
- uint8_t PS_BlueLEDBuf[16] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x07,0x3C,0x03,0x01,0x01,0x00,0x00,0x49};
- uint8_t PS_RedLEDBuf[16] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x07,0x3C,0x02,0x04,0x04,0x02,0x00,0x50};
- uint8_t PS_GreenLEDBuf[16] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x07,0x3C,0x02,0x02,0x02,0x02,0x00,0x4C};
- //休眠指令-设置传感器进入休眠模式
- uint8_t PS_SleepBuf[12] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x03,0x33,0x00,0x37};
- //清空指纹库-删除 flash 数据库中所有指纹模板。
- uint8_t PS_EmptyBuf[12] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x03,0x0D,0x00,0x11};
- //取消指令-取消自动注册模板和自动验证指纹。如表 2-1 中加密等级设置为 0 或 1 情况下支持此功能
- uint8_t PS_CancelBuf[12] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x03,0x30,0x00,0x34};
- //自动注册模板-一站式注册指纹,包含采集指纹、生成特征、组合模板、存储模板等功能。加密等级设置为 0 或 1 情况下支持此功能。
- uint8_t PS_AutoEnrollBuf[17] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF, 0x01,0x00,0x08,0x31,'\0','\0',0x04,0x00,0x14,'\0','\0'};
- //验证用获取图像-验证指纹时,探测手指,探测到后录入指纹图像存于图像缓冲区。返回确认码表示:录入成功、无手指等。
- uint8_t PS_GetImageBuf[12] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x03,0x01,0x00,0x05};
- //生成特征值-将图像缓冲区中的原始图像生成指纹特征文件存于模板缓冲区
- uint8_t PS_GetCharBuf[13] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x04,0x02,'\0','\0','\0'};
- //搜索指纹-以模板缓冲区中的特征文件搜索整个或部分指纹库。若搜索到,则返回页码。加密等级设置为 0 或 1 情况下支持
- uint8_t PS_SearchBuf[17] = {0xEF,0x01, 0xFF,0xFF,0xFF,0xFF, 0x01, 0x00,0x08, 0x04, 0x01, 0x00,0x00, 0xFF,0xFF, 0x02,0x0C};
- //删除模板-删除 flash 数据库中指定 ID 号开始的N 个指纹模板
- uint8_t PS_DeleteBuf[16] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x07,0x0C,'\0','\0',0x00,0x01,'\0','\0'};
- //精确对比两枚指纹特征
- uint8_t PS_MatchBuf[12] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x03, 0x00, 0x07};
- //合并特征-将特征文件融合后生成一个模板,结果存于模板缓冲区中
- uint8_t PS_RegModelBuf[12] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x05, 0x00, 0x09};
- //储存模板-将模板缓冲区中的模板文件存储到OageID号flash数据库位置
- uint8_t PS_StoreBuf[15] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x06, 0x06, 0x01, 0x00, '\0', '\0', '\0'};
- //读出模板-将flash数据库中指定ID号的指纹模板读入到模板缓冲区中
- uint8_t PS_LoadCharBuf[15] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x06, 0x07, 0x01, '\0', '\0', '\0', '\0'};
- //上传模板-将保存在模板缓冲区中的模板文件上传给主控
- uint8_t PS_UpCharBuf[13] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x04, 0x08, '\0', '\0', '\0'};
- //下载模板-主控下载模板到模组的一个模板缓冲区
- uint8_t PS_DownCharBuf[13] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x04, 0x09, '\0', '\0', '\0'};
- //写系统寄存器-写模组寄存器
- uint8_t PS_WriteRegBuf[14] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x05, 0x0E, '\0', '\0', '\0', '\0'};
- //读模组基本参数-读取模组的基本参数(波特率、包大小等),参数表前16个字节存放了模组的基本通讯和配置信息,称为模组的基本参数
- uint8_t PS_ReadSysParaBuf[12] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x0F, 0x00, 0x13};
- //读参数页-读取flash information page 所在的参数页(512bytes)
- uint8_t PS_ReadINFpageBuf[12] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x16, 0x00, 0x1A};
- //读有效模板个数
- uint8_t PS_ValidTempleteNumBuf[12] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x1D, 0x00, 0x21};
- //读索引表-读取录入模板的索引表
- uint8_t PS_ReadIndexTableBuf[13] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x04, 0x1F, '\0', '\0', '\0'};
- //注册用获取图像-注册指纹时,探测手指,探测到后录入指纹图像缓冲区
- uint8_t PS_GetEnrollImageBuf[12] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x29, 0x00, 0x2D};
- //获取模组附加参数
- uint8_t PS_ReadAddParaBuf[13] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x62, 0x00, 0x66};
- //自动验证指纹-自动采集指纹包括获取图像、生成特征、搜索指纹等功能
- uint8_t PS_AutoIdentifyBuf[17] = { 0xEF, 0x01,
- 0xFF, 0xFF, 0xFF, 0xFF,
- 0x01,
- 0x00, 0x08,
- 0x32,
- 0x00,
- 0x00, 0x01,
- 0x00, 0x00,
- 0x00, 0x3C };
- //设置名为zw101_uart的外设句柄,用来执行串口指令的发送
- struct bflb_device_s *zw101_uart;
- //USART串口接收长度以及标志位
- uint8_t USART2_STA = 0;
- //指纹ID和验证指纹的分数
- uint16_t pageID,score;
- //USART串口接收缓冲数组
- uint8_t USART_ReceiveBuf[30];
- //主循环状态标志位
- uint8_t ScanStatus = 0;
- /// @brief uart中断触发方法
- /// @param irq
- /// @param arg
- void uart_isr(int irq, void *arg)
- {
- uint32_t intstatus = bflb_uart_get_intstatus(zw101_uart);
- //接收超时中断,当一段时间内数据没有接收后便会停止,在触发中断后
- uint8_t i = 0;
- if (intstatus & UART_INTSTS_RTO) {
- // 检测缓冲区是否有数据
- while (bflb_uart_rxavailable(zw101_uart)) {
- // 轮询fpm383c_uart接收到的字符
- uint8_t data = bflb_uart_getchar(zw101_uart);
- // 获取数据并放入缓冲数组
- USART_ReceiveBuf[i++] = data;
- }
- //停止接收后清空中断标志等待下一次发送
- bflb_uart_int_clear(zw101_uart, UART_INTCLR_RTO);
- USART2_STA = 1;
- }
-
- }
- /// @brief 初始化ZW101指纹模块
- void ZW101_Init(){
- // 声明 gpio句柄
- struct bflb_device_s* gpio;
- // ZW101 模块默认波特率为 57600
- struct bflb_uart_config_s cfg = {
- .baudrate = 57600,
- .data_bits = UART_DATA_BITS_8,
- .stop_bits = UART_STOP_BITS_1,
- .parity = UART_PARITY_NONE,
- .flow_ctrl = 0,
- .tx_fifo_threshold = 4,
- .rx_fifo_threshold = 4,
- };
- // 初始化ZW101指纹模块UART
- gpio = bflb_device_get_by_name("gpio");
- zw101_uart = bflb_device_get_by_name("uart1");
- //将GPIO_23和GPIO_24设置为TX和RX
- bflb_gpio_uart_init(gpio, GPIO_PIN_23, GPIO_UART_FUNC_UART1_TX);
- bflb_gpio_uart_init(gpio, GPIO_PIN_24, GPIO_UART_FUNC_UART1_RX);
- bflb_uart_init(zw101_uart, &cfg);
- // uart tx fifo 阈值中断屏蔽开关,开启后超过设定阈值则触发中断。
- bflb_uart_txint_mask(zw101_uart, true);
- // uart rx fifo 阈值中断和超时屏蔽开关这里用不上关闭即可。
- bflb_uart_rxint_mask(zw101_uart, false);
- // 注册中断入口函数
- bflb_irq_attach(zw101_uart->irq_num, uart_isr, NULL);
- // 使能 GPIO 中断
- bflb_irq_enable(zw101_uart->irq_num);
- }
- /// @brief USART串口发送数据
- /// @param length 发送数组长度
- /// @param ZW101_DataBuf 需要发送的功能数组
- void ZW101_SendData(int length,uint8_t ZW101_DataBuf[])
- {
- for(int i = 0;i<length;i++)
- {
- bflb_uart_put(zw101_uart,(uint8_t *)&ZW101_DataBuf[i], 1);
- }
- USART2_STA = 0;
- }
- /// @brief 写系统寄存器
- /// @param
- uint8_t ZW101_WriteReg(uint16_t Reg_Num, uint16_t Content,uint16_t timeout)
- {
- PS_WriteRegBuf[10] = Reg_Num;
- PS_WriteRegBuf[11] = Content;
- PS_WriteRegBuf[12] = (0x14 + PS_WriteRegBuf[10] + PS_WriteRegBuf[11]) >> 8;
- PS_WriteRegBuf[13] = (0x14 + PS_WriteRegBuf[10] + PS_WriteRegBuf[11]);
- for (size_t i = 0; i < 14; i++)
- {
- printf("PS_WriteRegBuf[%d]:,%x\r\n",i,PS_WriteRegBuf[i] );
- }
- ZW101_SendData(14, PS_WriteRegBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- uint8_t temp = USART_ReceiveBuf[9];
- if (temp == 0)
- {
- printf("\r\n设置完成");
- }
- else
- {
- printf("\r\n设置错误");
- }
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return temp;
- }
- /// @brief 储存模板
- /// @param
- uint8_t ZW101_StoreChar(uint8_t BufferID,uint16_t PageID,uint16_t timeout)
- {
- PS_StoreBuf[10] = (BufferID);
- PS_StoreBuf[11] = (PageID>>8);
- PS_StoreBuf[12] = (PageID);
- PS_StoreBuf[13] = (0x0D + PS_StoreBuf[10] + PS_StoreBuf[11] + PS_StoreBuf[12]) >> 8;
- PS_StoreBuf[14] = (0x0D + PS_StoreBuf[10] + PS_StoreBuf[11] + PS_StoreBuf[12]);
- ZW101_SendData(15, PS_StoreBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- uint8_t temp = USART_ReceiveBuf[9];
- if (temp == 0)
- {
- ZW101_ShowErrMessage(temp);
- printf("\r\n保存完毕,ID号为:%d",PageID);
- }
- else
- {
- ZW101_ShowErrMessage(temp);
- printf("\r\n保存失败");
- }
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return temp;
- }
- /// @brief 读取模组的基本参数
- /// @param
- uint16_t ZW101_ReadSysPara(SysPara *p, uint16_t timeout)
- {
- uint8_t ensure;
- ZW101_SendData(12, PS_ReadSysParaBuf);
- while(!USART2_STA && (--timeout))
- {
- printf("\r\n接收到信息");
- ensure = USART_ReceiveBuf[9];
- p->PS_max = (USART_ReceiveBuf[14]<<8)+USART_ReceiveBuf[15];
- p->PS_level = (USART_ReceiveBuf[16]<<8)+USART_ReceiveBuf[17];
- p->PS_addr = (USART_ReceiveBuf[18]<<24)+(USART_ReceiveBuf[19]<<16)+(USART_ReceiveBuf[20]<<8)+USART_ReceiveBuf[21];
- p->PS_size = (USART_ReceiveBuf[22]<<8)+USART_ReceiveBuf[23];
- p->PS_N = (USART_ReceiveBuf[24]<<8)+USART_ReceiveBuf[25];
- }
- ensure = (USART_ReceiveBuf[9] == 0x00 ? USART_ReceiveBuf[9] : 0xFF);
- if(ensure==0x00)
- {
- printf("\r\n模块最大指纹容量=%d",p->PS_max);
- printf("\r\n对比等级=%d",p->PS_level);
- printf("\r\n地址=%x",p->PS_addr);
- printf("\r\n波特率=%d",p->PS_N*9600);
- }
- else
- printf("\r\n错误");
- return USART_ReceiveBuf[9];
- }
- /// @brief 读有效模板个数
- /// @param
- uint16_t ZW101_ValidTempNum(uint16_t timeout)
- {
- uint16_t ValidTempNum;
- ZW101_SendData(12, PS_ValidTempleteNumBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- ValidTempNum = (int)((USART_ReceiveBuf[10] << 8) + USART_ReceiveBuf[11]);
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return ValidTempNum;
- }
- /// @brief 发送休眠指令 确认码=00H 表示休眠设置成功。确认码=01H 表示休眠设置失败。
- /// @param
- void ZW101_Sleep(void)
- {
- ZW101_SendData(12,PS_SleepBuf);
- }
- /// @brief 验证用获取图像
- /// @param timeout 接收数据的超时时间
- /// @return 确认码
- uint8_t ZW101_GetImage(uint32_t timeout)
- {
- uint8_t tmp;
- ZW101_SendData(12,PS_GetImageBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- tmp = (USART_ReceiveBuf[6] == 0x07 ? USART_ReceiveBuf[9] : 0xFF);
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return tmp;
- }
- /// @brief 精确对比模版缓冲区中的特征文件。
- /// @param timeout 接收数据的超时时间
- /// @return 确认码
- uint8_t ZW101_Match(uint32_t timeout)
- {
- uint8_t ensure;
- ZW101_SendData(12,PS_MatchBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- ensure = (USART_ReceiveBuf[6] == 0x07 ? USART_ReceiveBuf[9] : 0xFF);
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return ensure;
- }
- /// @brief 将图像缓冲区中的原始图像生成指纹特征文件存于模板缓冲区。
- /// @param timeout 接收数据的超时时间
- /// @return 确认码
- uint8_t ZW101_GenChar(uint8_t BufferID, uint32_t timeout)
- {
- uint8_t tmp;
- PS_GetCharBuf[10] = BufferID;
- PS_GetCharBuf[11] = (0x07+PS_GetCharBuf[10])>>8;
- PS_GetCharBuf[12] = (0x07+PS_GetCharBuf[10]);
- ZW101_SendData(13,PS_GetCharBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- tmp = (USART_ReceiveBuf[6] == 0x07 ? USART_ReceiveBuf[9] : 0xFF);
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return tmp;
- }
- /// @brief 合并特征(生成模板)
- /// @param timeout 接收数据的超时时间
- /// @return 确认码
- uint8_t ZW101_RegModel(uint32_t timeout)
- {
- uint8_t ensure;
- ZW101_SendData(12, PS_RegModelBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- ensure = (USART_ReceiveBuf[6] == 0x07 ? USART_ReceiveBuf[9] : 0xFF);
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return ensure;
- }
- /// @brief 发送搜索指纹指令
- /// @param timeout 接收数据的超时时间
- /// @return 确认码
- uint8_t ZW101_Search(uint32_t timeout)
- {
- ZW101_SendData(17,PS_SearchBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- return (USART_ReceiveBuf[6] == 0x07 ? USART_ReceiveBuf[9] : 0xFF);
- }
- ///@brief 上传模板
- ///@param BufferID 模板缓冲区
- ///@return 确认码
- uint8_t ZW101_UpChar(uint8_t BufferID,uint16_t timeout)
- {
- PS_UpCharBuf[10] = (BufferID);
- PS_UpCharBuf[11] = ((0x0D + PS_UpCharBuf[10])>>8);
- PS_UpCharBuf[12] = ((0x0D + PS_UpCharBuf[10]));
- ZW101_SendData(13, PS_UpCharBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- return (USART_ReceiveBuf[6] == 0x07 ? USART_ReceiveBuf[9] : 0xFF);
- }
- ///@brief 下载模板
- ///@param BufferID 模板缓冲区
- ///@return 确认码
- uint8_t ZW101_DownChar(uint8_t BufferID,uint16_t timeout)
- {
- PS_DownCharBuf[10] = (BufferID);
- PS_DownCharBuf[11] = ((0x0E + PS_DownCharBuf[10])>>8);
- PS_DownCharBuf[12] = ((0x0E + PS_DownCharBuf[10]));
- ZW101_SendData(13, PS_DownCharBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- return (USART_ReceiveBuf[6] == 0x07 ? USART_ReceiveBuf[9] : 0xFF);
- }
- /// @brief 删除指定指纹指令
- /// @param pageID 需要删除的指纹ID号
- /// @param timeout 接收数据的超时时间
- /// @return 确认码
- uint8_t ZW101_Delete(uint16_t pageID,uint32_t timeout)
- {
- uint8_t tmp;
- PS_DeleteBuf[10] = (pageID>>8);
- PS_DeleteBuf[11] = (pageID);
- PS_DeleteBuf[14] = (0x15+PS_DeleteBuf[10]+PS_DeleteBuf[11])>>8;
- PS_DeleteBuf[15] = (0x15+PS_DeleteBuf[10]+PS_DeleteBuf[11]);
- ZW101_SendData(16,PS_DeleteBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- tmp = (USART_ReceiveBuf[6] == 0x07 ? USART_ReceiveBuf[9] : 0xFF);
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return tmp;
- }
- /// @brief 清空指纹库
- /// @param timeout 接收数据的超时时间
- /// @return 确认码
- uint8_t ZW101_Empty(uint32_t timeout)
- {
- uint8_t tmp;
- ZW101_SendData(12,PS_EmptyBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- tmp = (USART_ReceiveBuf[6] == 0x07 ? USART_ReceiveBuf[9] : 0xFF);
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return tmp;
- }
- /// @brief 发送控制灯光指令
- /// @param PS_ControlLEDBuf 不同颜色的协议数据
- /// @param timeout 接收数据的超时时间
- /// @return 确认码
- uint8_t ZW101_ControlLED(uint8_t PS_ControlLEDBuf[],uint32_t timeout)
- {
- uint8_t tmp;
- ZW101_SendData(16,PS_ControlLEDBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- tmp = (USART_ReceiveBuf[6] == 0x07 ? USART_ReceiveBuf[9] : 0xFF);
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return tmp;
- }
- /// @brief 自动注册
- /// @param pageID 输入需要注册的指纹ID号,取值范围0—59
- /// @param timeout 设置注册指纹超时时间,因为需要按压四次手指,建议大于10000(即10s)
- void ZW101_Enroll(uint16_t pageID, uint16_t timeout)
- {
- LOG_E("注册指纹ID: %d\r\n", pageID);
- PS_AutoEnrollBuf[10] = (pageID>>8);
- PS_AutoEnrollBuf[11] = (pageID);
- PS_AutoEnrollBuf[15] = (0x52+PS_AutoEnrollBuf[10]+PS_AutoEnrollBuf[11])>>8;
- PS_AutoEnrollBuf[16] = (0x52+PS_AutoEnrollBuf[10]+PS_AutoEnrollBuf[11]);
- ZW101_SendData(17,PS_AutoEnrollBuf);
- while(!USART2_STA && (--timeout))
- {
- bflb_mtimer_delay_ms(1);
- }
- if(USART_ReceiveBuf[9] == 0x00)
- {
- LOG_E("指纹注册完成\r\n");
- // 亮绿灯2秒
- ZW101_ControlLED(PS_GreenLEDBuf,2000);
- // 重置接收数据缓存
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return;
- }
- else if(timeout == 0)
- {
- // 超时取消注册
- ZW101_SendData(12,PS_CancelBuf);
- bflb_mtimer_delay_ms(50);
- // 重置接收数据缓存
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- }
- // 亮红灯2秒
- ZW101_ControlLED(PS_RedLEDBuf,2000);
- }
- /// @brief 验证指纹是否注册
- /// @param
- void ZW101_Identify(void)
- {
- uint8_t ensure;
- ensure = ZW101_GetImage(2000);
- if(ensure == 0x00)
- {
- ensure = ZW101_GenChar(Buffer1, 2000);
- if(ensure == 0x00)
- {
- struct bflb_device_s *led = bflb_device_get_by_name("gpio");
- if(ZW101_Search(2000) == 0x00)
- {
- score = (int)((USART_ReceiveBuf[10] << 8) + USART_ReceiveBuf[11]);
- LOG_E("解锁成功 指纹ID:%d\r\n",(int)score);
- ZW101_ControlLED(PS_GreenLEDBuf,1000);
- bflb_gpio_init(led, GPIO_PIN_14, GPIO_OUTPUT);
- bflb_gpio_set(led, GPIO_PIN_14);
- bflb_mtimer_delay_ms(1000);
- bflb_gpio_reset(led, GPIO_PIN_14);
- // 重置接收数据缓存
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return;
- }else{
- LOG_E("解锁失败\r\n");
- bflb_gpio_init(led, GPIO_PIN_12, GPIO_OUTPUT);
- bflb_gpio_set(led, GPIO_PIN_12);
- bflb_mtimer_delay_ms(1000);
- bflb_gpio_reset(led, GPIO_PIN_12);
- // 重置接收数据缓存
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return;
- }
- }
- }
- }
- /// @brief 自动验证指纹是否注册
- /// @param
- void ZW101_AutoIdentify(void){
- if(ZW101_GetImage(2000) == 0x00)
- {
- if(ZW101_GenChar(Buffer1, 2000) == 0x00)
- {
- struct bflb_device_s *led = bflb_device_get_by_name("gpio");
- if(ZW101_Search(2000) == 0x00)
- {
- ZW101_SendData(17, PS_AutoIdentifyBuf);
- score = (int)((USART_ReceiveBuf[11] << 8) + USART_ReceiveBuf[12]);
- LOG_E("解锁成功 指纹ID:%d\r\n",(int)score);
- ZW101_ControlLED(PS_GreenLEDBuf,1000);
- bflb_gpio_init(led, GPIO_PIN_14, GPIO_OUTPUT);
- bflb_gpio_set(led, GPIO_PIN_14);
- bflb_mtimer_delay_ms(1000);
- bflb_gpio_reset(led, GPIO_PIN_14);
- // 重置接收数据缓存
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return;
- }else{
- LOG_E("解锁失败\r\n");
- bflb_gpio_init(led, GPIO_PIN_12, GPIO_OUTPUT);
- bflb_gpio_set(led, GPIO_PIN_12);
- bflb_mtimer_delay_ms(1000);
- bflb_gpio_reset(led, GPIO_PIN_12);
- // 重置接收数据缓存
- memset(USART_ReceiveBuf,0xFF,sizeof(USART_ReceiveBuf));
- return;
- }
- }
- }
- }
- /// @brief 录入指纹
- /// @param
- void ZW101_Add(void)
- {
- uint8_t ensure, processnum = 0;
- uint16_t PageID = 0;
- struct bflb_device_s *led = bflb_device_get_by_name("gpio");
- printf("\r\n开始录入指纹");
- while (1)
- {
- switch (processnum)
- {
- case 0:
- ensure = ZW101_GetImage(2000);
- if (ensure == 0x00)
- {
- ensure = ZW101_GenChar(Buffer1, 2000);
- if (ensure == 0x00)
- {
- printf("\r\n指纹一正常");
- ZW101_ControlLED(PS_GreenLEDBuf,1000);
- bflb_gpio_init(led, GPIO_PIN_14, GPIO_OUTPUT);
- bflb_gpio_set(led, GPIO_PIN_14);
- bflb_mtimer_delay_ms(1000);
- bflb_gpio_reset(led, GPIO_PIN_14);
- processnum = 1;
- }
- }
- break;
- case 1:
- ensure = ZW101_GetImage(2000);
- if (ensure == 0x00)
- {
- ensure = ZW101_GenChar(Buffer2, 2000);
- if (ensure == 0x00)
- {
- printf("\r\n指纹二正常");
- ZW101_ControlLED(PS_GreenLEDBuf,1000);
- bflb_gpio_init(led, GPIO_PIN_14, GPIO_OUTPUT);
- bflb_gpio_set(led, GPIO_PIN_14);
- bflb_mtimer_delay_ms(1000);
- bflb_gpio_reset(led, GPIO_PIN_14);
- processnum = 3;
- }
- }
- break;
- case 2:
- ensure = ZW101_Match(2000);
- if (ensure == 0x00)
- {
- printf("\r\n对比成功,两次指纹一样");
- processnum = 3;
- }
- else
- {
- ZW101_ShowErrMessage(ensure);
- processnum = 0;
- }
- bflb_mtimer_delay_ms(1200);
- break;
- case 3:
- ensure = ZW101_RegModel(2000);
- if (ensure == 0x00)
- {
- printf("\r\n生成指纹模板成功");
- processnum = 4;
- }
- else
- {
- ZW101_ShowErrMessage(ensure);
- processnum = 0;
- }
- bflb_mtimer_delay_ms(1200);
- break;
- case 4:
- do
- {
- PageID = key_out();
- } while (PageID == 0);
- ensure = ZW101_StoreChar(Buffer2,PageID,2000);
- if (ensure == 0x00)
- {
- printf("\r\n录入指纹成功");
- ZW101_ValidTempNum(2000);
- return ;
- }
- else
- {
- ZW101_ShowErrMessage(ensure);
- processnum = 0;
- }
- break;
- }
- }
- }
- ///模板应答包确认码解析
- /// @brief 解析确认码错误信息
- /// @param ensure 确认码
- /// @return 返回确认码信息
- const char *EnsureMessage(uint8_t ensure)
- {
- const char *p;
- switch(ensure)
- {
- case 0x00:
- p="OK";break;
- case 0x01:
- p="数据包接受错误";break;
- case 0x02:
- p="传感器上没有手指";break;
- case 0x03:
- p="录入指纹图像失败";break;
- case 0x04:
- p="指纹图像太干、太淡而生不成特征";break;
- case 0x05:
- p="指纹图像太湿、太糊而生不成特征";break;
- case 0x06:
- p="指纹图像太乱而生不成特征";break;
- case 0x07:
- p="指纹图像正常,但特征点太少(面积太小)而生不成特征";break;
- case 0x08:
- p="指纹不匹配";break;
- case 0x09:
- p="没搜索到指纹";break;
- case 0x0a:
- p="特征合并失败";break;
- case 0x0b:
- p="访问指纹库时地址序号超出指纹库范围";break;
- case 0x0c:
- p="从指纹库读模板出错或无效";break;
- case 0x0d:
- p="上传特征失败";break;
- case 0x0e:
- p="模组不能接收后续数据包";break;
- case 0x0f:
- p="上传图像失败";break;
- case 0x10:
- p="删除模板失败";break;
- case 0x11:
- p="清空指纹库失败";break;
- case 0x12:
- p="不能进入低功耗状态";break;
- case 0x13:
- p="口令不正确";break;
- case 0x14:
- p="系统复位失败";break;
- case 0x15:
- p="缓冲区内没有有效原始图像而生不成图像";break;
- case 0x17:
- p="残留指纹或两次采集之间手指没有移动过";break;
- case 0x18:
- p="读写FLASH出错";break;
- case 0x1a:
- p="无效寄存器号";break;
- case 0x1b:
- p="寄存器设定内容错误号";break;
- case 0x1c:
- p="记事本页码指定错误";break;
- case 0x1d:
- p="端口操作失败";break;
- case 0x1e:
- p="自动注册失败";break;
- case 0x1f:
- p="指纹库满";break;
- case 0x20:
- p="设备地址错误";break;
- case 0x21:
- p="密码有误";break;
- case 0x22:
- p="指纹模板非空";break;
- case 0x23:
- p="指纹模板为空";break;
- case 0x24:
- p="指纹库为空";break;
- case 0x25:
- p="录入次数设置错误";break;
- case 0x26:
- p="超时";break;
- case 0x27:
- p="指纹已存在";break;
- case 0x28:
- p="指纹模板有关联";break;
- case 0x29:
- p="传感器初始化失败";break;
- case 0x2A:
- p="模组信息非空";break;
- case 0x2B:
- p="模组信息为空";break;
- case 0x33:
- p="图像面积小";break;
- case 0x34:
- p="图像不可用";break;
- case 0x35:
- p="非法数据";break;
- default :
- p="注册次数少于规定次数";break;
- }
- return p;
- }
- ///显示确认码错误信息
- /// @param ensure 确认码
- void ZW101_ShowErrMessage(uint8_t ensure)
- {
- char *p = EnsureMessage(ensure);
- printf("\r\n%s",p);
- }
复制代码
|