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
commHeaders/SPIcommFunctions.h@0:a051df82fcdf, 2018-06-25 (annotated)
- Committer:
- PixArtVY
- Date:
- Mon Jun 25 21:14:10 2018 +0000
- Revision:
- 0:a051df82fcdf
First release.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PixArtVY | 0:a051df82fcdf | 1 | #define SPImode //Used to check if the sensor is in I2C mode or SPI mode. |
PixArtVY | 0:a051df82fcdf | 2 | |
PixArtVY | 0:a051df82fcdf | 3 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 4 | //Communication pinouts for serial COM port, SPI, and interrupts |
PixArtVY | 0:a051df82fcdf | 5 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 6 | static Serial pc(USBTX, USBRX); //PC comm |
PixArtVY | 0:a051df82fcdf | 7 | static SPI spi(p23, p24, p25); //mosi, miso, sclk |
PixArtVY | 0:a051df82fcdf | 8 | static DigitalOut cs(p22); //chip select |
PixArtVY | 0:a051df82fcdf | 9 | static DigitalOut shutdown(p20); //Shutdown pin |
PixArtVY | 0:a051df82fcdf | 10 | static DigitalOut IO_sel(p19); //IO interface selection pin (I2C and SPI) |
PixArtVY | 0:a051df82fcdf | 11 | |
PixArtVY | 0:a051df82fcdf | 12 | |
PixArtVY | 0:a051df82fcdf | 13 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 14 | //Variables and arrays used for communications and data storage |
PixArtVY | 0:a051df82fcdf | 15 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 16 | int8_t deltaX, deltaY; //Stores the value of one individual motion report. |
PixArtVY | 0:a051df82fcdf | 17 | int totalX, totalY = 0; //Stores the total deltaX and deltaY moved during runtime. |
PixArtVY | 0:a051df82fcdf | 18 | |
PixArtVY | 0:a051df82fcdf | 19 | |
PixArtVY | 0:a051df82fcdf | 20 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 21 | //Functions used to communicate with the sensor and grab/print data |
PixArtVY | 0:a051df82fcdf | 22 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 23 | uint8_t readRegister(uint8_t addr); |
PixArtVY | 0:a051df82fcdf | 24 | //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00. |
PixArtVY | 0:a051df82fcdf | 25 | |
PixArtVY | 0:a051df82fcdf | 26 | void writeRegister(uint8_t addr, uint8_t data); |
PixArtVY | 0:a051df82fcdf | 27 | //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address. |
PixArtVY | 0:a051df82fcdf | 28 | |
PixArtVY | 0:a051df82fcdf | 29 | void load(const uint8_t array[][2], uint8_t arraySize); |
PixArtVY | 0:a051df82fcdf | 30 | //Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values. |
PixArtVY | 0:a051df82fcdf | 31 | |
PixArtVY | 0:a051df82fcdf | 32 | void grabData(void); |
PixArtVY | 0:a051df82fcdf | 33 | //Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format. |
PixArtVY | 0:a051df82fcdf | 34 | |
PixArtVY | 0:a051df82fcdf | 35 | void printData(void); |
PixArtVY | 0:a051df82fcdf | 36 | //Prints the data out to a serial terminal. |
PixArtVY | 0:a051df82fcdf | 37 | |
PixArtVY | 0:a051df82fcdf | 38 | |
PixArtVY | 0:a051df82fcdf | 39 | |
PixArtVY | 0:a051df82fcdf | 40 | |
PixArtVY | 0:a051df82fcdf | 41 | |
PixArtVY | 0:a051df82fcdf | 42 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 43 | //Functions definitions |
PixArtVY | 0:a051df82fcdf | 44 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 45 | uint8_t readRegister(uint8_t addr) |
PixArtVY | 0:a051df82fcdf | 46 | { |
PixArtVY | 0:a051df82fcdf | 47 | cs = 0; //Set chip select low/active |
PixArtVY | 0:a051df82fcdf | 48 | addr = addr & 0x7F; //Set MSB to 0 to indicate read operation |
PixArtVY | 0:a051df82fcdf | 49 | spi.write(addr); //Write the given address |
PixArtVY | 0:a051df82fcdf | 50 | wait_us(5); |
PixArtVY | 0:a051df82fcdf | 51 | uint8_t data_read = spi.write(0x00); //Throw dummy byte after sending address to receieve data |
PixArtVY | 0:a051df82fcdf | 52 | wait_us(30); |
PixArtVY | 0:a051df82fcdf | 53 | cs = 1; //Set chip select back to high/inactive |
PixArtVY | 0:a051df82fcdf | 54 | return data_read; //Returns 8-bit data from register |
PixArtVY | 0:a051df82fcdf | 55 | } |
PixArtVY | 0:a051df82fcdf | 56 | |
PixArtVY | 0:a051df82fcdf | 57 | |
PixArtVY | 0:a051df82fcdf | 58 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 59 | void writeRegister(uint8_t addr, uint8_t data) |
PixArtVY | 0:a051df82fcdf | 60 | { |
PixArtVY | 0:a051df82fcdf | 61 | cs = 0; //Set chip select low/active |
PixArtVY | 0:a051df82fcdf | 62 | addr = addr | 0x80; //Set MSB to 1 to indicate write operation |
PixArtVY | 0:a051df82fcdf | 63 | spi.write(addr); //Write the given address |
PixArtVY | 0:a051df82fcdf | 64 | spi.write(data); //Write the given data |
PixArtVY | 0:a051df82fcdf | 65 | cs = 1; //Set chip select back to high/inactive |
PixArtVY | 0:a051df82fcdf | 66 | wait_us(30); //Wait time between write commands. |
PixArtVY | 0:a051df82fcdf | 67 | |
PixArtVY | 0:a051df82fcdf | 68 | //pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr)); |
PixArtVY | 0:a051df82fcdf | 69 | //Uncomment this line for debugging. Prints every register write operation. |
PixArtVY | 0:a051df82fcdf | 70 | } |
PixArtVY | 0:a051df82fcdf | 71 | |
PixArtVY | 0:a051df82fcdf | 72 | |
PixArtVY | 0:a051df82fcdf | 73 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 74 | void load(const uint8_t array[][2], uint8_t arraySize) |
PixArtVY | 0:a051df82fcdf | 75 | { |
PixArtVY | 0:a051df82fcdf | 76 | for(uint8_t q = 0; q < arraySize; q++) |
PixArtVY | 0:a051df82fcdf | 77 | { |
PixArtVY | 0:a051df82fcdf | 78 | writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data. |
PixArtVY | 0:a051df82fcdf | 79 | } |
PixArtVY | 0:a051df82fcdf | 80 | } |
PixArtVY | 0:a051df82fcdf | 81 | |
PixArtVY | 0:a051df82fcdf | 82 | |
PixArtVY | 0:a051df82fcdf | 83 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 84 | void grabData(void) |
PixArtVY | 0:a051df82fcdf | 85 | { |
PixArtVY | 0:a051df82fcdf | 86 | deltaX = readRegister(0x03); //Grabs data from the proper registers. |
PixArtVY | 0:a051df82fcdf | 87 | deltaY = readRegister(0x04); |
PixArtVY | 0:a051df82fcdf | 88 | writeRegister(0x02, 0x00); //Clear EVENT and motion registers. |
PixArtVY | 0:a051df82fcdf | 89 | } |
PixArtVY | 0:a051df82fcdf | 90 | |
PixArtVY | 0:a051df82fcdf | 91 | |
PixArtVY | 0:a051df82fcdf | 92 | //========================================================================= |
PixArtVY | 0:a051df82fcdf | 93 | void printData(void) |
PixArtVY | 0:a051df82fcdf | 94 | { |
PixArtVY | 0:a051df82fcdf | 95 | if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data. |
PixArtVY | 0:a051df82fcdf | 96 | { |
PixArtVY | 0:a051df82fcdf | 97 | totalX += deltaX; |
PixArtVY | 0:a051df82fcdf | 98 | totalY += deltaY; |
PixArtVY | 0:a051df82fcdf | 99 | |
PixArtVY | 0:a051df82fcdf | 100 | pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY. |
PixArtVY | 0:a051df82fcdf | 101 | pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY); //Prints the total movement made during runtime. |
PixArtVY | 0:a051df82fcdf | 102 | } |
PixArtVY | 0:a051df82fcdf | 103 | |
PixArtVY | 0:a051df82fcdf | 104 | deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten. |
PixArtVY | 0:a051df82fcdf | 105 | deltaY = 0; |
PixArtVY | 0:a051df82fcdf | 106 | } |