PixArt Optical Finger Navigation, OFN, demo program for A350 sensor with library. Alternative porting style in C++. Initial release v1.0.
Dependencies: Pixart_OFN_A
Fork of OFN_A350_Demo by
Revision 2:4c248212e354, committed 2019-02-27
- Comitter:
- PixArtHC
- Date:
- Wed Feb 27 21:16:38 2019 +0000
- Parent:
- 1:67d6484416a6
- Commit message:
- PixArt Optical Finger Navigation, OFN, demo program for A350 sensor with library. Alternative porting style in C++. Initial release v1.0.
Changed in this revision
diff -r 67d6484416a6 -r 4c248212e354 Pixart_OFN_A.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Pixart_OFN_A.lib Wed Feb 27 21:16:38 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/PixArtHC/code/Pixart_OFN_A/#4237dc1f43b4
diff -r 67d6484416a6 -r 4c248212e354 commHeaders/I2CcommFunctions.h --- a/commHeaders/I2CcommFunctions.h Tue Oct 30 21:14:15 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,102 +0,0 @@ -#define I2Cmode //Used to check if the sensor is in I2C mode or SPI mode. -#define I2C_Slave_ID 0x57 //Slave ID is 0x57 only if p22 and p23 are both HIGH. - -//========================================================================= -//Communication pinouts for serial COM port, I2C, and interrupts -//========================================================================= -static Serial pc(USBTX, USBRX); //PC comm -static I2C i2c(p26, p27); //SDA, SCL -static DigitalOut cs(p22); //CS pin for selecting slave address -static DigitalOut MOSI(p23); //MOSI pin for selecting slave address -static DigitalOut shutdown(p20); //Shutdown pin -static DigitalOut IO_sel(p19); //IO interface selection pin (I2C and SPI) - - -//========================================================================= -//Variables and arrays used for communications and data storage -//========================================================================= -int8_t deltaX, deltaY; //Stores the value of one individual motion report. -int totalX, totalY = 0; //Stores the total deltaX and deltaY moved during runtime. - - -//========================================================================= -//Functions used to communicate with the sensor and grab/print data -//========================================================================= -uint8_t readRegister(uint8_t addr); -//This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00. - -void writeRegister(uint8_t addr, uint8_t data); -//This function takes an 8-bit address and 8-bit data. Writes the given data to the given address. - -void load(const uint8_t array[][2], uint8_t arraySize); -//Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values. - -void grabData(void); -//Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format. - -void printData(void); -//Prints the data out to a serial terminal. - - - - - -//========================================================================= -//Functions definitions -//========================================================================= -uint8_t readRegister(uint8_t addr) -{ - uint8_t data; - i2c.write((I2C_Slave_ID << 1), (const char*)&addr, 1, 0); //Send the address to the chip - i2c.read((I2C_Slave_ID << 1), (char*)&data, 1, 0); //Send the memory address where you want to store the read data - return(data); -} - - -//========================================================================= -void writeRegister(uint8_t addr, uint8_t data) -{ - char data_write[2]; //Create an array to store the address/data to pass them at the same time. - data_write[0] = addr; //Store the address in the first byte - data_write[1] = data; //Store the data in the second byte - i2c.write((I2C_Slave_ID << 1), data_write, 2, 0); //Send both over at once - - pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr)); - //Uncomment this line for debugging. Prints every register write operation. -} - - -//========================================================================= -void load(const uint8_t array[][2], uint8_t arraySize) -{ - for(uint8_t q = 0; q < arraySize; q++) - { - writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data. - } -} - - -//========================================================================= -void grabData(void) -{ - deltaX = readRegister(0x03); //Grabs data from the proper registers. - deltaY = readRegister(0x04); - writeRegister(0x02, 0x00); //Clear EVENT and motion registers. -} - - -//========================================================================= -void printData(void) -{ - if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data. - { - totalX += deltaX; - totalY += deltaY; - - pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY. - pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY); //Prints the total movement made during runtime. - } - - deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten. - deltaY = 0; -}
diff -r 67d6484416a6 -r 4c248212e354 commHeaders/SPIcommFunctions.h --- a/commHeaders/SPIcommFunctions.h Tue Oct 30 21:14:15 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -#define SPImode //Used to check if the sensor is in I2C mode or SPI mode. - -//========================================================================= -//Communication pinouts for serial COM port, SPI, and interrupts -//========================================================================= -static Serial pc(USBTX, USBRX); //PC comm -static SPI spi(p23, p24, p25); //mosi, miso, sclk -static DigitalOut cs(p22); //chip select -static DigitalOut shutdown(p20); //Shutdown pin -static DigitalOut IO_sel(p19); //IO interface selection pin (I2C and SPI) - - -//========================================================================= -//Variables and arrays used for communications and data storage -//========================================================================= -int8_t deltaX, deltaY; //Stores the value of one individual motion report. -int totalX, totalY = 0; //Stores the total deltaX and deltaY moved during runtime. - - -//========================================================================= -//Functions used to communicate with the sensor and grab/print data -//========================================================================= -uint8_t readRegister(uint8_t addr); -//This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00. - -void writeRegister(uint8_t addr, uint8_t data); -//This function takes an 8-bit address and 8-bit data. Writes the given data to the given address. - -void load(const uint8_t array[][2], uint8_t arraySize); -//Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values. - -void grabData(void); -//Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format. - -void printData(void); -//Prints the data out to a serial terminal. - - - - - -//========================================================================= -//Functions definitions -//========================================================================= -uint8_t readRegister(uint8_t addr) -{ - cs = 0; //Set chip select low/active - addr = addr & 0x7F; //Set MSB to 0 to indicate read operation - spi.write(addr); //Write the given address - wait_us(5); - uint8_t data_read = spi.write(0x00); //Throw dummy byte after sending address to receieve data - wait_us(30); - cs = 1; //Set chip select back to high/inactive - return data_read; //Returns 8-bit data from register -} - - -//========================================================================= -void writeRegister(uint8_t addr, uint8_t data) -{ - cs = 0; //Set chip select low/active - addr = addr | 0x80; //Set MSB to 1 to indicate write operation - spi.write(addr); //Write the given address - spi.write(data); //Write the given data - cs = 1; //Set chip select back to high/inactive - wait_us(30); //Wait time between write commands. - - //pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr)); - //Uncomment this line for debugging. Prints every register write operation. -} - - -//========================================================================= -void load(const uint8_t array[][2], uint8_t arraySize) -{ - for(uint8_t q = 0; q < arraySize; q++) - { - writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data. - } -} - - -//========================================================================= -void grabData(void) -{ - deltaX = readRegister(0x03); //Grabs data from the proper registers. - deltaY = readRegister(0x04); - writeRegister(0x02, 0x00); //Clear EVENT and motion registers. -} - - -//========================================================================= -void printData(void) -{ - if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data. - { - totalX += deltaX; - totalY += deltaY; - - pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY. - pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY); //Prints the total movement made during runtime. - } - - deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten. - deltaY = 0; -}
diff -r 67d6484416a6 -r 4c248212e354 commHeaders/registerArrays.h --- a/commHeaders/registerArrays.h Tue Oct 30 21:14:15 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -const uint8_t initialize[][2] = { - { 0xC9, 0x61 }, //OFN Engine settings. - //{ 0x6C, 0xA0 }, //Set GPIO pin as button input. -}; -#define initialize_size (sizeof(initialize)/sizeof(initialize[0]))
diff -r 67d6484416a6 -r 4c248212e354 main.cpp --- a/main.cpp Tue Oct 30 21:14:15 2018 +0000 +++ b/main.cpp Wed Feb 27 21:16:38 2019 +0000 @@ -1,71 +1,41 @@ -// ADBM-A350: Finger navigation chip. -// Version: 1.1 -// Latest Revision Date: 18 July 2018 -// By PixArt Imaging Inc. -// Primary Engineer: Vincent Yeh (PixArt USA) +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + */ -// Copyright [2018] [Vincent Yeh] -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: -// http://www.apache.org/licenses/LICENSE-2.0 +/* ADBM-A350: Finger navigation chip. + * By PixArt Imaging Inc. + * Primary Engineer: Hill Chen (PixArt USA) + * + * License: Apache-2.0; http://www.apache.org/licenses/LICENSE-2.0 + */ + +/* Revision History + * V1.0: February 27, 2019 + * First release. + */ -/* -//======================= -//Revision History -//======================= -Version 1.1 -- 18 July 2018 -Added apache license notice. +#include "mbed.h" +#include "Pixart_OFN.h" -Version 1.0 -- 14 June 2018 -First release. -*/ +Serial pc(USBTX, USBRX); +I2C i2c(I2C_SDA0, I2C_SCL0); -#include "mbed.h" -#include "registerArrays.h" -//#include "I2CcommFunctions.h" -#include "SPIcommFunctions.h" -//Make sure you only have one of either SPIcommFunctions or I2CcommFunctions enabled. You cannot include both headers. - +// main() runs in its own thread in the OS int main() { - pc.baud(115200); // Set baud rate to 115200. Remember to sync serial terminal baud rate to the same value. + bool Result = false; + Pixart_OFN *m_Sensor = new Pixart_OFN(&i2c, &pc, 0.25f, Result); + if(Result) pc.printf("\r\n\n %s %s initialization successfully\r\n", PRODUCT, MODEL); + else{ pc.printf("\r\n\n %s %s fail on initialization", PRODUCT, MODEL); + while (true) ; + } - #ifdef SPImode - IO_sel = 1; // Set IO_select pin to be HIGH for SPI. - spi.format(8,3); // Set SPI to 8 bits with inverted polarity and phase-shifted to second edge. - spi.frequency(100000); // Set frequency for SPI communication. - cs = 1; // Initialize chip select as inactive. - #endif - - #ifdef I2Cmode - IO_sel = 0; // Set IO_select pin to be LOW for I2C. - i2c.frequency(400000); // Set frequency for I2C communication. - cs = 1; // These two pins are used to determine the device's slave ID. - MOSI = 1; - #endif - - shutdown = 0; - writeRegister(0x3A, 0x5A); //Soft-reset the chip. - - pc.printf("Program START\n\r"); - - pc.printf("ID Check: %2X\n\r", readRegister(0x00)); //Checks product ID to make sure communication protocol is working properly. - if(readRegister(0x00) != 0x88) - { - pc.printf("Communication protocol error! Terminating program.\n\r"); - return 0; - } - - load(initialize, initialize_size); //Load register settings from the "initialize" array (see registerArrays.h) - - while(1) - { - //pc.printf("MOTION bit: %2X\n\r", readRegister(0x02)); //Prints EVENT register for debugging. - - if(readRegister(0x02) & 0x80) - { - grabData(); - printData(); - } + while (true){ +#ifndef USE_CALLBACK + m_Sensor->periodicCallback(); + wait_ms(250); +#endif } }