PixArt Gesture Library for nRF051
Fork of Pixart_Gesture by
Revision 0:726fdf0062c6, committed 2016-09-15
- Comitter:
- pixus_mbed
- Date:
- Thu Sep 15 18:07:48 2016 +0000
- Commit message:
- PixArt Gesture main.cpp
Changed in this revision
diff -r 000000000000 -r 726fdf0062c6 Gesture.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Gesture.cpp Thu Sep 15 18:07:48 2016 +0000 @@ -0,0 +1,165 @@ +#include "mbed.h" +#include "PAC7620_30Set.h" +#include "Gesture.h" + + + +#define PAC7620_30_ADDR (0xE6) //PAC7620 address shift 1-bit +#define I2C_ADDR PAC7620_30_ADDR +#define PXI_WMI 0x20 +#define ID_7620 0x01 +#define ID_7630 0x00 +#define GestureRegAddrL 0x43 +#define GestureRegAddrH 0x44 +#define DebounceT 300 //debounce time, unit:ms + + +Pixart_Gesture::Pixart_Gesture(I2C *i2c, int Period,GestureCallback callback,bool &Result) +{ + m_i2c = i2c; + m_GestureCallback = callback; + //m_pc = pc; + m_Period = Period; + + + Result=PAC7620_30_init(); +} + + + +bool Pixart_Gesture::PAC7620_30_init() +{ uint8_t q; + + readRegister(0x00); + wait_ms(50); + + if(readRegister(0x00) == PXI_WMI) + ;//m_pc->printf("I2C Link Successful!\n\r"); + else{ + //m_pc->printf(" I2C Link Fail!\n\r"); + return false; + } + + if(readRegister(0x02) == ID_7620) + { + // m_pc->printf("PAC7620 detected.\n\r"); + + writeRegister(0xff, 0x00); //wakeup + wait_ms(10); //make a delay + for(q=0;q<INIT_Ges7620_REG_ARRAY_SIZE;q++) + writeRegister(init_ges7620_register_array[q][0], init_ges7620_register_array[q][1]); + + wait_ms(DebounceT); //debounce, clean data + readRegister(GestureRegAddrH); readRegister(GestureRegAddrL); + Flag_7630 = 0; + } + else if(readRegister(0x02) == ID_7630) + { + //m_pc->printf("PAC7630 detected.\n\r"); + + writeRegister(0xff, 0x00); //wakeup + wait_ms(10); //make a delay + for(q=0;q<INIT_Ges7630_REG_ARRAY_SIZE;q++) + writeRegister(init_ges7630_register_array[q][0], init_ges7630_register_array[q][1]); + + wait_ms(DebounceT); //debounce, clean data + readRegister(GestureRegAddrH); readRegister(GestureRegAddrL); + + Flag_7630 = 1; + } + else + ;// m_pc->printf("Device not support.\n\r"); + + m_ticker.attach_us(this,&Pixart_Gesture::periodicCallback, m_Period*1000); + + //m_pc->printf("\n\r~~~Start Real-time Gesture Demo~~~ \n\r"); + + return true; +} + +void Pixart_Gesture::periodicCallback(void) +{ + uint16_t gesture_code; + + gesture_code = (readRegister(GestureRegAddrH)<<8) + readRegister(GestureRegAddrL); + //pc.printf("Gesture Code: 0x %4X\n\r", gesture_code); + //UP,DOWN,LEFT,RIGHT,PUSH,POLL,CLOCKWISE,COUNTER_CLOCKWISE,WAVE}; + if(gesture_code != 0) + { + switch (gesture_code) + { + case 0x01: // up + if(Flag_7630 == 0) + m_GestureCallback(DOWN); + else + m_GestureCallback(LEFT); + + //m_pc->printf("up 0x%4X\n\r", gesture_code); + break; + case 0x02: // down + if(Flag_7630 == 0) + m_GestureCallback(UP); + else + m_GestureCallback(RIGHT); + + //m_pc->printf("down 0x%4X\n\r", gesture_code); + break; + case 0x04: // left + if(Flag_7630 == 0) + m_GestureCallback(RIGHT); + else + m_GestureCallback(DOWN); + //m_pc->printf("left 0x%4X\n\r", gesture_code); + break; + case 0x08: // right + if(Flag_7630 == 0) + m_GestureCallback(LEFT); + else + m_GestureCallback(UP); + //m_pc->printf("right 0x%4X\n\r", gesture_code); + break; + case 0x10: // push + m_GestureCallback(PUSH); + //m_pc->printf("push 0x%4X\n\r", gesture_code); + break; + case 0x20: // pop + m_GestureCallback(POLL); + //m_pc->printf("pop 0x%4X\n\r", gesture_code); + break; + case 0x40: // rotate right + m_GestureCallback(CLOCKWISE); + //m_pc->printf("rotate right 0x%4X\n\r", gesture_code); + break; + case 0x80: // rotate left + m_GestureCallback(COUNTER_CLOCKWISE); + //m_pc->printf("rotate left 0x%4X\n\r", gesture_code); + break; + case 0x100:// wave + m_GestureCallback(WAVE); + //m_pc->printf("wave 0x%4X\n\r", gesture_code); + break; + } + readRegister(GestureRegAddrH); readRegister(GestureRegAddrL); + } +} + + + +void Pixart_Gesture::writeRegister(uint8_t addr, uint8_t data) +{ + char data_write[2]; + + data_write[0] = addr; + data_write[1] = data; + m_i2c->write(I2C_ADDR, data_write, 2, 0); +} +uint8_t Pixart_Gesture::readRegister(uint8_t addr) +{ + char data_write[2]; + char data_read[2]; + + data_write[0] = addr; + m_i2c->write(I2C_ADDR, data_write, 1, 0); + m_i2c->read(I2C_ADDR, data_read, 1, 0); + return data_read[0]; +} \ No newline at end of file
diff -r 000000000000 -r 726fdf0062c6 Gesture.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Gesture.h Thu Sep 15 18:07:48 2016 +0000 @@ -0,0 +1,38 @@ +/* + * Pixart support 9 gesture type + * + */ +enum PIXART_GESTURE_TYPE{UP,DOWN,LEFT,RIGHT,PUSH,POLL,CLOCKWISE,COUNTER_CLOCKWISE,WAVE}; + +/* The callback function when Gesture happen + Type ==> The Gesture type recognize by Pixart's Gesture class + */ +typedef void (*GestureCallback)(PIXART_GESTURE_TYPE Type); + +class Pixart_Gesture +{ + private: + Ticker m_ticker; + I2C *m_i2c; + //Serial *m_pc; + int m_Period; + uint8_t Flag_7630 ; + + GestureCallback m_GestureCallback; + void periodicCallback(void); + bool PAC7620_30_init(); + + void writeRegister(uint8_t addr, uint8_t data); + uint8_t readRegister(uint8_t addr); + + + public: + /* + * The initial function for Gesture class + * i2c ==> The I2C object from outside, the clock rate should be 400k + * Period ==> The polling rate for gesture, in ms + * callback ==> The call back function for gesture status + * Result ==> The result for initialize + */ + Pixart_Gesture(I2C *i2c, int Period,GestureCallback callback,bool &Result); +}; \ No newline at end of file
diff -r 000000000000 -r 726fdf0062c6 PAC7620_30Set.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PAC7620_30Set.h Thu Sep 15 18:07:48 2016 +0000 @@ -0,0 +1,207 @@ +const uint8_t init_ges7620_register_array[][2] = { + {0xEF,0x00}, + {0x37,0x07}, + {0x38,0x17}, + {0x39,0x06}, + {0x42,0x01}, + {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}, + //{0x91,0x0A} + {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}, + {0x77,0x01}, + {0xEF,0x00}, +}; + +#define INIT_Ges7620_REG_ARRAY_SIZE (sizeof(init_ges7620_register_array)/sizeof(init_ges7620_register_array[0])) + +const uint8_t init_ges7630_register_array[][2] = { + {0xEF,0x00}, + {0x07,0x01}, + {0x10,0x01}, + {0x11,0x31}, + {0x13,0x07}, + {0x14,0x17}, + {0x15,0x06}, + {0x42,0x01}, + {0x46,0x2D}, + {0x47,0x0F}, + {0x48,0x4B}, + {0x49,0x00}, + {0x4A,0x4B}, + {0x4C,0x12}, + {0x4D,0x14}, + {0x4E,0x30}, + {0x4F,0x50}, + {0x51,0x10}, + {0x54,0x01}, + {0x5F,0xFF}, + {0x61,0x4C}, + {0x80,0xCA}, + {0x81,0xCC}, + {0x85,0x00}, + {0x88,0x05}, + {0x89,0x18}, + {0x8A,0x00}, + {0x8B,0x01}, + {0x90,0x06}, + {0x93,0x0F}, + {0x94,0x0F}, + {0x95,0x0A}, + {0x96,0x0C}, + {0x97,0x05}, + {0x99,0x20}, + {0x9A,0x14}, + {0x9C,0x3F}, + {0xA5,0x11}, + {0xCA,0x64}, + {0xCF,0x01}, + {0xE0,0x01}, + {0xE1,0x04}, + {0xE2,0x1E}, + {0xE3,0xD6}, + {0xE4,0x00}, + {0xE5,0x0C}, + {0xE6,0x0A}, + {0xE7,0x00}, + {0xE8,0x00}, + {0xE9,0x00}, + {0xEF,0x01}, + {0x04,0x03}, + {0x25,0x01}, + {0x26,0x1C}, + {0x27,0x0A}, + {0x28,0x78}, + {0x29,0x07}, + {0x32,0x1C}, + {0x33,0x1C}, + {0x42,0x80}, + {0x44,0x20}, + {0x5E,0x74}, + {0x5F,0x19}, + {0x64,0x0A}, + {0x65,0xA3}, + {0x74,0x09}, + {0x77,0x31}, + {0x7E,0x01}, + {0xEF,0x00}, +}; + +#define INIT_Ges7630_REG_ARRAY_SIZE (sizeof(init_ges7630_register_array)/sizeof(init_ges7630_register_array[0])) + +//default, 15cm operation distance settings +const uint8_t set1_ges7630_register_array[][2] = { + {0xEF,0x00}, + {0x48,0x4B}, + {0x49,0x00}, + {0x4A,0x4B}, + {0x4B,0x00}, + {0xAA,0x1E}, + {0xAB,0x1E}, + {0xCA,0x63}, + {0xEF,0x01}, + {0x00,0x1D}, + {0x01,0x1D}, + {0x02,0x00}, + {0x03,0x00}, + {0x4A,0x1D}, + {0x4B,0x1D}, + {0x4C,0x00}, + {0x4D,0x00}, + {0xEF,0x00}, + {0x93,0x0F}, + {0x94,0x0F}, +}; + +#define SET1_Ges7630_REG_ARRAY_SIZE (sizeof(set1_ges7630_register_array)/sizeof(set1_ges7630_register_array[0])) + +//20cm operation distance settings +const uint8_t set2_ges7630_register_array[][2] = { + {0xEF,0x00}, + {0x48,0x6B}, + {0x49,0x00}, + {0x4A,0x6B}, + {0x4B,0x00}, + {0xAA,0x1E}, + {0xAB,0x1E}, + {0xCA,0x63}, + {0xEF,0x01}, + {0x00,0x1D}, + {0x01,0x1D}, + {0x02,0x00}, + {0x03,0x00}, + {0x4A,0x1D}, + {0x4B,0x1D}, + {0x4C,0x00}, + {0x4D,0x00}, + {0xEF,0x00}, + {0x93,0x0F}, + {0x94,0x0F}, +}; + +#define SET2_Ges7630_REG_ARRAY_SIZE (sizeof(set2_ges7630_register_array)/sizeof(set2_ges7630_register_array[0])) + +//30cm operation distance settings +const uint8_t set3_ges7630_register_array[][2] = { + {0xEF,0x00}, + {0x48,0x00}, + {0x49,0x02}, + {0x4A,0x00}, + {0x4B,0x02}, + {0xAA,0x10}, + {0xAB,0x10}, + {0xCA,0x62}, + {0xEF,0x01}, + {0x00,0x0F}, + {0x01,0x0F}, + {0x02,0x08}, + {0x03,0x08}, + {0x4A,0x0F}, + {0x4B,0x0F}, + {0x4C,0x00}, + {0x4D,0x00}, + {0xEF,0x00}, + {0x93,0x07}, + {0x94,0x07}, +}; + +#define SET3_Ges7630_REG_ARRAY_SIZE (sizeof(set3_ges7630_register_array)/sizeof(set3_ges7630_register_array[0])) \ No newline at end of file