ATK-PAJ7620是一款高性能手势识别传感器模块。该模块采用原相科技(Pixart)公司的 PAJ7620U2
芯片,芯片内部集成了光学数组式传感器,以使复杂的手势和光标模式输出,自带九种手势
识别,支持上、下、左、右、前、后、顺时针旋转、逆时针旋转和挥动的手势动作识别,以
及支持物体接近检测等功能
代码示例
//手势传感器读取函数
//参数 手势结果
/*
#define GES_UP BIT(0) //向上
#define GES_DOWM BIT(1) //向下
#define GES_LEFT BIT(2) //向左
#define GES_RIGHT BIT(3) //向右
#define GES_FORWARD BIT(4) //向前
#define GES_BACKWARD BIT(5) //向后
#define GES_CLOCKWISE BIT(6) //顺时针
#define GES_COUNT_CLOCKWISE BIT(7) //逆时针
#define GES_WAVE BIT(8) //挥动
*/
//返回 0
//定义硬件接口
int pja7620(unsigned char *ges)
{
static char state = 0;
unsigned char tmp = 0;
if(state == 0)
{
tmp = paj7620u2_init();
if(tmp)
state = 1;
}
Gesture_test(ges);
return 0;
}
用法,用到i2c,和oled共用一个
unsigned char ges;
pja7620(&ges);
paj7620u2.c
#include "paj7620u2.h"
#include "paj7620u2_cfg.h"
#include "delay.h"
#include "i2c.h"
#define u8 unsigned char
#define u16 unsigned short
u8 GS_Write_Byte(u8 REG_Address,u8 REG_data)
{
i2c_1_write(0x73, 1, REG_Address, ®_data, 1);
}
u8 GS_Read_Byte(u8 REG_Address)
{
unsigned char tmp = 0;
i2c_1_read(0x73, 1, REG_Address, &tmp, 1);
return tmp;
}
u8 GS_Read_nByte(u8 REG_Address,u16 len,u8 *buf)
{
unsigned char tmp = 0,ret = 0;
ret = i2c_1_read(0x73, 1, REG_Address, buf, len);
if(ret) return 0;
else return 1;
}
void GS_i2c_init(void)
{
i2c_Init(I2C1,U32BIT( GPIO_12),U32BIT( GPIO_13));
}
//PAJ7620唤醒
void GS_WakeUp(void)
{
i2c_1_write(0x73, 0, 0, 0, 0);
}
//选择PAJ7620U2 BANK区域
void paj7620u2_selectBank(bank_e bank)
{
switch(bank)
{
case BANK0: GS_Write_Byte(PAJ_REGITER_BANK_SEL,PAJ_BANK0);break;//BANK0寄存器区域
case BANK1: GS_Write_Byte(PAJ_REGITER_BANK_SEL,PAJ_BANK1);break;//BANK1寄存器区域
}
}
//PAJ7620U2唤醒
u8 paj7620u2_wakeup(void)
{
u8 data=0x0a;
GS_WakeUp();//唤醒PAJ7620U2
delay_ms(5);//唤醒时间>400us
GS_WakeUp();//唤醒PAJ7620U2
delay_ms(5);//唤醒时间>400us
paj7620u2_selectBank(BANK0);//进入BANK0寄存器区域
data = GS_Read_Byte(0x00);//读取状态
if(data!=0x20) return 0; //唤醒失败
return 1;
}
//PAJ7620U2初始化
//返回值:0:失败 1:成功
u8 paj7620u2_init(void)
{
u8 i;
u8 status;
GS_i2c_init();//IIC初始化
status = paj7620u2_wakeup();//唤醒PAJ7620U2
if(!status) return 0;
paj7620u2_selectBank(BANK0);//进入BANK0寄存器区域
for(i=0;i<INIT_SIZE;i++)
{
GS_Write_Byte(init_Array[i][0],init_Array[i][1]);//初始化PAJ7620U2
}
paj7620u2_selectBank(BANK0);//切换回BANK0寄存器区域
return 1;
}
//手势识别测试
u8 Gesture_test(u8 *ges)
{
u8 i;
u8 status;
u8 data[2]={0x00};
u16 gesture_data;
static u8 state = 0;
if(state == 0)
{
paj7620u2_selectBank(BANK0);//进入BANK0寄存器区域
for(i=0;i<GESTURE_SIZE;i++)
{
GS_Write_Byte(gesture_arry[i][0],gesture_arry[i][1]);//手势识别模式初始化
}
paj7620u2_selectBank(BANK0);//切换回BANK0寄存器区域
state = 1;
}
else
{
status = GS_Read_nByte(PAJ_GET_INT_FLAG1,2,&data[0]);//读取手势状态
if(!status)
{
gesture_data =(u16)data[1]<<8 | data[0];
if(gesture_data)
*ges = gesture_data;
}
}
return 0;
}
//接近检测测试
u8 Ps_test(u8 *brightness, u16 *size)
{
u8 i;
u8 data[2]={0x00};
u8 obj_brightness=0;
u16 obj_size=0;
static u8 state = 0;
if(state == 0)
{
paj7620u2_selectBank(BANK0);//进入BANK0寄存器区域
for(i=0;i<PROXIM_SIZE;i++)
{
GS_Write_Byte(proximity_arry[i][0],proximity_arry[i][1]);//接近检测模式初始化
}
paj7620u2_selectBank(BANK0);//返回BANK0寄存器区域
}
else
{
obj_brightness = GS_Read_Byte(PAJ_GET_OBJECT_BRIGHTNESS);//读取物体亮度
data[0] = GS_Read_Byte(PAJ_GET_OBJECT_SIZE_1);//读取物体大小
data[1] = GS_Read_Byte(PAJ_GET_OBJECT_SIZE_2);
obj_size = ((u16)data[1] & 0x0f)<<8 | data[0];
}
return 0;
}
paj7620u2.h
#ifndef __PAJ7620U2_H
#define __PAJ7620U2_H
///#include "paj7620u2_iic.h"
#define u8 unsigned char
#define u16 unsigned short
//BANK寄存器枚举
typedef enum{
BANK0 = 0, //BANK0寄存器
BANK1, //BANK1寄存器
}bank_e;
#define PAJ7620_ID 0x73<<1 //设备地址
#define PAJ_REGITER_BANK_SEL 0XEF //BANK选择寄存器
#define PAJ_BANK0 0X00 //BANK0
#define PAJ_BANK1 0X01 //BANK1
//BANK0 寄存器组
#define PAJ_SUSPEND_CMD 0X03 //设置设备挂起
#define PAJ_SET_INT_FLAG1 0X41 //设置手势检测中断寄存器1
#define PAJ_SET_INT_FLAG2 0X42 //设置手势检测中断寄存器2
#define PAJ_GET_INT_FLAG1 0X43 //获取手势检测中断标志寄存器1(获取手势结果)
#define PAJ_GET_INT_FLAG2 0X44 //获取手势检测中断标志寄存器2(获取手势结果)
#define PAJ_GET_STATE 0X45 //获取手势检测工作状态
#define PAJ_SET_HIGH_THRESHOLD 0x69 //设置滞后高阀值(仅在接近检测模式下)
#define PAJ_SET_LOW_THRESEHOLD 0X6A //设置滞后低阀值
#define PAJ_GET_APPROACH_STATE 0X6B //获取接近状态 (1:PS data>= PS threshold ,0:PS data<= Low threshold)
#define PAJ_GET_GESTURE_DATA 0X6C //获取接近数据
#define PAJ_GET_OBJECT_BRIGHTNESS 0XB0 //获取被照物体亮度(最大255)
#define PAJ_GET_OBJECT_SIZE_1 0XB1 //获取被照物体大小低八位(bit7:0)(最大900)
#define PAJ_GET_OBJECT_SIZE_2 0XB2 //获取被照物体大小高四位(bit3:0)
//BANK1 寄存器组
#define PAJ_SET_PS_GAIN 0X44 //设置检测增益大小 (0:1x gain 1:2x gain)
#define PAJ_SET_IDLE_S1_STEP_0 0x67 //设置S1的响应因子
#define PAJ_SET_IDLE_S1_STEP_1 0x68
#define PAJ_SET_IDLE_S2_STEP_0 0X69 //设置S2的响应因子
#define PAJ_SET_IDLE_S2_STEP_1 0X6A
#define PAJ_SET_OP_TO_S1_STEP_0 0X6B //设置OP到S1的过度时间
#define PAJ_SET_OP_TO_S1_STEP_1 0X6C
#define PAJ_SET_S1_TO_S2_STEP_0 0X6D //设置S1到S2的过度时间
#define PAJ_SET_S1_TO_S2_STEP_1 0X6E
#define PAJ_OPERATION_ENABLE 0X72 //设置PAJ7620U2使能寄存器
//手势识别效果
#define BIT(x) 1<<(x)
#define GES_UP BIT(0) //向上
#define GES_DOWM BIT(1) //向下
#define GES_LEFT BIT(2) //向左
#define GES_RIGHT BIT(3) //向右
#define GES_FORWARD BIT(4) //向前
#define GES_BACKWARD BIT(5) //向后
#define GES_CLOCKWISE BIT(6) //顺时针
#define GES_COUNT_CLOCKWISE BIT(7) //逆时针
#define GES_WAVE BIT(8) //挥动
u8 paj7620u2_init(void);
u8 Gesture_test(u8 *ges);
u8 Ps_test(u8 *brightness, u16 *size);
#endif
paj7620u2_cfg.h
#ifndef __PAJ7620U2_CFG_H
#define __PAJ7620U2_CFG_H
#define INIT_SIZE sizeof(init_Array)/2
//上电初始化数组
const unsigned char init_Array[][2] = {
{0xEF,0x00},
{0x37,0x07},
{0x38,0x17},
{0x39,0x06},
{0x41,0x00},
{0x42,0x00},
{0x46,0x2D},
{0x47,0x0F},
{0x48,0x3C},
{0x49,0x00},
{0x4A,0x1E},
{0x4C,0x20},
{0x51,0x10},
{0x5E,0x10},
{0x60,0x27},
{0x80,0x42},
{0x81,0x44},
{0x82,0x04},
{0x8B,0x01},
{0x90,0x06},
{0x95,0x0A},
{0x96,0x0C},
{0x97,0x05},
{0x9A,0x14},
{0x9C,0x3F},
{0xA5,0x19},
{0xCC,0x19},
{0xCD,0x0B},
{0xCE,0x13},
{0xCF,0x64},
{0xD0,0x21},
{0xEF,0x01},
{0x02,0x0F},
{0x03,0x10},
{0x04,0x02},
{0x25,0x01},
{0x27,0x39},
{0x28,0x7F},
{0x29,0x08},
{0x3E,0xFF},
{0x5E,0x3D},
{0x65,0x96},
{0x67,0x97},
{0x69,0xCD},
{0x6A,0x01},
{0x6D,0x2C},
{0x6E,0x01},
{0x72,0x01},
{0x73,0x35},
{0x74,0x00},
{0x77,0x01},
};
#define PROXIM_SIZE sizeof(proximity_arry)/2
//接近检测初始化数组
const unsigned char proximity_arry[][2]={
{0xEF,0x00},
{0x41,0x00},
{0x42,0x00},
{0x48,0x3C},
{0x49,0x00},
{0x51,0x13},
{0x83,0x20},
{0x84,0x20},
{0x85,0x00},
{0x86,0x10},
{0x87,0x00},
{0x88,0x05},
{0x89,0x18},
{0x8A,0x10},
{0x9f,0xf8},
{0x69,0x96},
{0x6A,0x02},
{0xEF,0x01},
{0x01,0x1E},
{0x02,0x0F},
{0x03,0x10},
{0x04,0x02},
{0x41,0x50},
{0x43,0x34},
{0x65,0xCE},
{0x66,0x0B},
{0x67,0xCE},
{0x68,0x0B},
{0x69,0xE9},
{0x6A,0x05},
{0x6B,0x50},
{0x6C,0xC3},
{0x6D,0x50},
{0x6E,0xC3},
{0x74,0x05},
};
#define GESTURE_SIZE sizeof(gesture_arry)/2
//手势识别初始化数组
const unsigned char gesture_arry[][2]={
{0xEF,0x00},
{0x41,0x00},
{0x42,0x00},
{0xEF,0x00},
{0x48,0x3C},
{0x49,0x00},
{0x51,0x10},
{0x83,0x20},
{0x9F,0xF9},
{0xEF,0x01},
{0x01,0x1E},
{0x02,0x0F},
{0x03,0x10},
{0x04,0x02},
{0x41,0x40},
{0x43,0x30},
{0x65,0x96},
{0x66,0x00},
{0x67,0x97},
{0x68,0x01},
{0x69,0xCD},
{0x6A,0x01},
{0x6B,0xB0},
{0x6C,0x04},
{0x6D,0x2C},
{0x6E,0x01},
{0x74,0x00},
{0xEF,0x00},
{0x41,0xFF},
{0x42,0x01},
};
#endif