PixArt Optical Finger Navigation, OFN, library for PAW3007 sensor. Initial release v1.0.
Fork of Pixart_OFN by
Pixart_OFN.cpp@0:a4065043fd57, 2019-02-26 (annotated)
- Committer:
- PixArtHC
- Date:
- Tue Feb 26 18:57:35 2019 +0000
- Revision:
- 0:a4065043fd57
PixArt Optical Finger Navigation, OFN, library for PAW3007 sensor. Initial release v1.0.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PixArtHC | 0:a4065043fd57 | 1 | /* PixArt Optical Finger Navigation, OFN, sensor driver. |
PixArtHC | 0:a4065043fd57 | 2 | * By PixArt Imaging Inc. |
PixArtHC | 0:a4065043fd57 | 3 | * Primary Engineer: Hill Chen (PixArt USA) |
PixArtHC | 0:a4065043fd57 | 4 | * |
PixArtHC | 0:a4065043fd57 | 5 | * License: Apache-2.0; http://www.apache.org/licenses/LICENSE-2.0 |
PixArtHC | 0:a4065043fd57 | 6 | */ |
PixArtHC | 0:a4065043fd57 | 7 | |
PixArtHC | 0:a4065043fd57 | 8 | #include "mbed.h" |
PixArtHC | 0:a4065043fd57 | 9 | #include "Pixart_OFN.h" |
PixArtHC | 0:a4065043fd57 | 10 | |
PixArtHC | 0:a4065043fd57 | 11 | //Pixart_OFN::Pixart_OFN(I2C *i2c, float Period, bool &Result){ |
PixArtHC | 0:a4065043fd57 | 12 | Pixart_OFN::Pixart_OFN(I2C *i2c, Serial *pc, float Period, bool &Result){ |
PixArtHC | 0:a4065043fd57 | 13 | m_pc = pc; |
PixArtHC | 0:a4065043fd57 | 14 | pc->baud (115200); wait_ms(500); |
PixArtHC | 0:a4065043fd57 | 15 | |
PixArtHC | 0:a4065043fd57 | 16 | DEBUG_PRINT("\r\n >> Constructor"); |
PixArtHC | 0:a4065043fd57 | 17 | print_build_info(); |
PixArtHC | 0:a4065043fd57 | 18 | |
PixArtHC | 0:a4065043fd57 | 19 | m_i2c = i2c; |
PixArtHC | 0:a4065043fd57 | 20 | m_i2c->frequency(400000); |
PixArtHC | 0:a4065043fd57 | 21 | |
PixArtHC | 0:a4065043fd57 | 22 | m_Period = Period; |
PixArtHC | 0:a4065043fd57 | 23 | |
PixArtHC | 0:a4065043fd57 | 24 | Result = Sensor_Init(); |
PixArtHC | 0:a4065043fd57 | 25 | |
PixArtHC | 0:a4065043fd57 | 26 | DEBUG_PRINT("\r\n << Constructor"); |
PixArtHC | 0:a4065043fd57 | 27 | } |
PixArtHC | 0:a4065043fd57 | 28 | |
PixArtHC | 0:a4065043fd57 | 29 | bool Pixart_OFN::Sensor_Init(){ |
PixArtHC | 0:a4065043fd57 | 30 | DEBUG_PRINT("\r\n >> Sensor Init"); |
PixArtHC | 0:a4065043fd57 | 31 | |
PixArtHC | 0:a4065043fd57 | 32 | writeRegister(0x7F, 0x00); |
PixArtHC | 0:a4065043fd57 | 33 | writeRegister(0x06, 0x82); //Soft-reset the chip. |
PixArtHC | 0:a4065043fd57 | 34 | if(readRegister(0x00) != PXI_WMI){ |
PixArtHC | 0:a4065043fd57 | 35 | DEBUG_PRINT("\r\n << Sensor_Init (Fail)"); |
PixArtHC | 0:a4065043fd57 | 36 | return false; |
PixArtHC | 0:a4065043fd57 | 37 | } |
PixArtHC | 0:a4065043fd57 | 38 | |
PixArtHC | 0:a4065043fd57 | 39 | totalX = 0; totalY = 0; |
PixArtHC | 0:a4065043fd57 | 40 | load(initialize, initialize_size); |
PixArtHC | 0:a4065043fd57 | 41 | #ifdef USE_CALLBACK |
PixArtHC | 0:a4065043fd57 | 42 | m_ticker.attach(callback(this, &Pixart_OFN::periodicCallback), m_Period); |
PixArtHC | 0:a4065043fd57 | 43 | #endif |
PixArtHC | 0:a4065043fd57 | 44 | DEBUG_PRINT("\r\n << Sensor_Init (Success)"); |
PixArtHC | 0:a4065043fd57 | 45 | |
PixArtHC | 0:a4065043fd57 | 46 | return true; |
PixArtHC | 0:a4065043fd57 | 47 | } |
PixArtHC | 0:a4065043fd57 | 48 | |
PixArtHC | 0:a4065043fd57 | 49 | void Pixart_OFN::periodicCallback(){ |
PixArtHC | 0:a4065043fd57 | 50 | DEBUG_PRINT("\r\n >> Callback"); |
PixArtHC | 0:a4065043fd57 | 51 | |
PixArtHC | 0:a4065043fd57 | 52 | if(readRegister(0x02) & 0x80){ |
PixArtHC | 0:a4065043fd57 | 53 | grabData(); |
PixArtHC | 0:a4065043fd57 | 54 | printData(); |
PixArtHC | 0:a4065043fd57 | 55 | } |
PixArtHC | 0:a4065043fd57 | 56 | |
PixArtHC | 0:a4065043fd57 | 57 | DEBUG_PRINT("\r\n << Callback"); |
PixArtHC | 0:a4065043fd57 | 58 | } |
PixArtHC | 0:a4065043fd57 | 59 | |
PixArtHC | 0:a4065043fd57 | 60 | void Pixart_OFN::writeRegister(uint8_t addr, uint8_t data){ |
PixArtHC | 0:a4065043fd57 | 61 | char data_write[2]; |
PixArtHC | 0:a4065043fd57 | 62 | |
PixArtHC | 0:a4065043fd57 | 63 | data_write[0] = addr; |
PixArtHC | 0:a4065043fd57 | 64 | data_write[1] = data; |
PixArtHC | 0:a4065043fd57 | 65 | m_i2c->write(I2C_ADDR, data_write, 2, 0); |
PixArtHC | 0:a4065043fd57 | 66 | } |
PixArtHC | 0:a4065043fd57 | 67 | |
PixArtHC | 0:a4065043fd57 | 68 | uint8_t Pixart_OFN::readRegister(uint8_t addr){ |
PixArtHC | 0:a4065043fd57 | 69 | char data_write[2]; |
PixArtHC | 0:a4065043fd57 | 70 | char data_read[2]; |
PixArtHC | 0:a4065043fd57 | 71 | |
PixArtHC | 0:a4065043fd57 | 72 | data_write[0] = addr; |
PixArtHC | 0:a4065043fd57 | 73 | m_i2c->write(I2C_ADDR, data_write, 1, 0); |
PixArtHC | 0:a4065043fd57 | 74 | m_i2c->read(I2C_ADDR, data_read, 1, 0); |
PixArtHC | 0:a4065043fd57 | 75 | |
PixArtHC | 0:a4065043fd57 | 76 | return data_read[0]; |
PixArtHC | 0:a4065043fd57 | 77 | } |
PixArtHC | 0:a4065043fd57 | 78 | |
PixArtHC | 0:a4065043fd57 | 79 | void Pixart_OFN::load(const uint8_t array[][2], uint8_t arraySize){ |
PixArtHC | 0:a4065043fd57 | 80 | for(uint8_t q = 0; q < arraySize; q++) |
PixArtHC | 0:a4065043fd57 | 81 | { |
PixArtHC | 0:a4065043fd57 | 82 | writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data. |
PixArtHC | 0:a4065043fd57 | 83 | } |
PixArtHC | 0:a4065043fd57 | 84 | } |
PixArtHC | 0:a4065043fd57 | 85 | |
PixArtHC | 0:a4065043fd57 | 86 | void Pixart_OFN::grabData(void){ |
PixArtHC | 0:a4065043fd57 | 87 | deltaX = readRegister(0x03); //Grabs data from the proper registers. |
PixArtHC | 0:a4065043fd57 | 88 | deltaY = readRegister(0x04); |
PixArtHC | 0:a4065043fd57 | 89 | //writeRegister(0x02, 0x00); //Clear EVENT and motion registers. |
PixArtHC | 0:a4065043fd57 | 90 | } |
PixArtHC | 0:a4065043fd57 | 91 | |
PixArtHC | 0:a4065043fd57 | 92 | void Pixart_OFN::printData(void){ |
PixArtHC | 0:a4065043fd57 | 93 | if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data. |
PixArtHC | 0:a4065043fd57 | 94 | { |
PixArtHC | 0:a4065043fd57 | 95 | totalX += deltaX; |
PixArtHC | 0:a4065043fd57 | 96 | totalY += deltaY; |
PixArtHC | 0:a4065043fd57 | 97 | |
PixArtHC | 0:a4065043fd57 | 98 | m_pc->printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY. |
PixArtHC | 0:a4065043fd57 | 99 | m_pc->printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY); //Prints the total movement made during runtime. |
PixArtHC | 0:a4065043fd57 | 100 | } |
PixArtHC | 0:a4065043fd57 | 101 | |
PixArtHC | 0:a4065043fd57 | 102 | deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten. |
PixArtHC | 0:a4065043fd57 | 103 | deltaY = 0; |
PixArtHC | 0:a4065043fd57 | 104 | } |
PixArtHC | 0:a4065043fd57 | 105 | |
PixArtHC | 0:a4065043fd57 | 106 | void Pixart_OFN::print_build_info(){ |
PixArtHC | 0:a4065043fd57 | 107 | m_pc->printf("\r\n\n PixArt Mbed eval code, %s, %s", PRODUCT, MODEL); |
PixArtHC | 0:a4065043fd57 | 108 | m_pc->printf("\r\n Fw version: %s, Hw version: %s, mbed version: %d", FW_VERSION, HW_VERSION, MBED_VERSION); |
PixArtHC | 0:a4065043fd57 | 109 | m_pc->printf("\r\n Build time: %s %s\r\n", __TIME__, __DATE__); |
PixArtHC | 0:a4065043fd57 | 110 | } |