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