ADNS2051 Optical Sensor library

Committer:
clemente
Date:
Wed May 23 06:56:50 2012 +0000
Revision:
0:462017ee2a5b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
clemente 0:462017ee2a5b 1 /* mbed ADNS2051 Optical Sensor library.
clemente 0:462017ee2a5b 2
clemente 0:462017ee2a5b 3 Copyright (c) 2011 NXP 3787
clemente 0:462017ee2a5b 4
clemente 0:462017ee2a5b 5 Permission is hereby granted, free of charge, to any person obtaining a copy
clemente 0:462017ee2a5b 6 of this software and associated documentation files (the "Software"), to deal
clemente 0:462017ee2a5b 7 in the Software without restriction, including without limitation the rights
clemente 0:462017ee2a5b 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
clemente 0:462017ee2a5b 9 copies of the Software, and to permit persons to whom the Software is
clemente 0:462017ee2a5b 10 furnished to do so, subject to the following conditions:
clemente 0:462017ee2a5b 11
clemente 0:462017ee2a5b 12 The above copyright notice and this permission notice shall be included in
clemente 0:462017ee2a5b 13 all copies or substantial portions of the Software.
clemente 0:462017ee2a5b 14
clemente 0:462017ee2a5b 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
clemente 0:462017ee2a5b 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
clemente 0:462017ee2a5b 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
clemente 0:462017ee2a5b 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
clemente 0:462017ee2a5b 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
clemente 0:462017ee2a5b 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
clemente 0:462017ee2a5b 21 THE SOFTWARE.
clemente 0:462017ee2a5b 22 */
clemente 0:462017ee2a5b 23
clemente 0:462017ee2a5b 24 #ifndef __ADNS2051_H
clemente 0:462017ee2a5b 25 #define __ADNS2051_H
clemente 0:462017ee2a5b 26
clemente 0:462017ee2a5b 27 #include "mbed.h"
clemente 0:462017ee2a5b 28
clemente 0:462017ee2a5b 29
clemente 0:462017ee2a5b 30 //
clemente 0:462017ee2a5b 31 #define PRODUCTID_REG 0x00 // Product ID = 2 (Fixed value)
clemente 0:462017ee2a5b 32 #define REVISIONID_REG 0x01 // Revision of IC [0xNN]
clemente 0:462017ee2a5b 33 #define MOTION_REG 0x02 // Register 0x02 allows the user to determine if motion has occurred since the last time it was read
clemente 0:462017ee2a5b 34 #define DELTAX_REG 0x03 // X movement is counts since last report. Absolute value is determined by resolution. Reading clears the register.
clemente 0:462017ee2a5b 35 #define DELTAY_REG 0x04 // Y movement is counts since last report. Absolute value is determined by resolution. Reading clears the register.
clemente 0:462017ee2a5b 36 #define SQUAL_REG 0x05 // Surface Quality. SQUAL is a measure of the number of features visible by the sensor in the
clemente 0:462017ee2a5b 37 // current frame. The maximum value is 255. Since small changes in the current frame
clemente 0:462017ee2a5b 38 // can result in changes in SQUAL, variations in SQUAL when looking at a surface are expected.
clemente 0:462017ee2a5b 39 #define AVRPXL_REG 0x06 // Average Pixel value in current frame. Minimum value = 0, maximum = 63. The average pixel value can be adjusted every frame.
clemente 0:462017ee2a5b 40 #define MAXPXL_REG 0x07 // Maximum Pixel value in current frame. Minimum value = 0, maximum value = 63. The maximum pixel value can be adjusted every frame.
clemente 0:462017ee2a5b 41 #define CONFIGBIT_REG 0x0A // Configuration bits. Register 0x0a allows the user to change the configuration of the sensor.
clemente 0:462017ee2a5b 42 #define DTOUTLW_REG 0x0C
clemente 0:462017ee2a5b 43 #define DTOUTUP_REG 0x0D // Once the pixel dump command is given, the sensor
clemente 0:462017ee2a5b 44 // writes the address and the value for the first pixel into
clemente 0:462017ee2a5b 45 // the Data_Out_Upper and Data_Out_Lower registers.
clemente 0:462017ee2a5b 46 #define SHUTTERLW_REG 0x0E
clemente 0:462017ee2a5b 47 #define SHUTTERUP_REG 0x0F // Shutter register.
clemente 0:462017ee2a5b 48 #define FRMPRDLW_REG 0x10
clemente 0:462017ee2a5b 49 #define FRMPRDUP_REG 0x11 // The frame period counter counts up until it overflows. Units are clock cycles.
clemente 0:462017ee2a5b 50
clemente 0:462017ee2a5b 51 // Motion register bits
clemente 0:462017ee2a5b 52 #define MOTION_BIT 0x07 // Motion since last report: 0=no motion, 1=motion occurred, data ready on deltaX and Y regs.
clemente 0:462017ee2a5b 53 #define LEDFAULT_BIT 0x05 // LED Fault detected: 0=no, 1=Fault detected
clemente 0:462017ee2a5b 54 #define OVERFY_BIT 0x04 // Motion overflow Y, dY buffer has overflowed since last report: 0=no, 1=overflow
clemente 0:462017ee2a5b 55 #define OVERFX_BIT 0x03 // Motion overflow X, dX buffer has overflowed since last report: 0=no, 1=overflow
clemente 0:462017ee2a5b 56 #define RESOLUTION_BIT 0x00 // Resolution in counts x inch: 0=400, 1=800
clemente 0:462017ee2a5b 57
clemente 0:462017ee2a5b 58 // Confiuration register bits
clemente 0:462017ee2a5b 59 #define RESET_BIT 0x07
clemente 0:462017ee2a5b 60 #define LEDMODE_BIT 0x06
clemente 0:462017ee2a5b 61 #define RESOLMODE_BIT 0x04
clemente 0:462017ee2a5b 62 #define PIXDUMP_BIT 0x03
clemente 0:462017ee2a5b 63 #define SLEEP_BIT 0x00
clemente 0:462017ee2a5b 64
clemente 0:462017ee2a5b 65 //
clemente 0:462017ee2a5b 66 #define PIXDATAVALID_BIT 0x07
clemente 0:462017ee2a5b 67
clemente 0:462017ee2a5b 68 // Product ID value
clemente 0:462017ee2a5b 69 #define PRODUCTID_VAL 0x02 // The value in this register does not change, it can be used to verify that the
clemente 0:462017ee2a5b 70 // serial communications link is OK.
clemente 0:462017ee2a5b 71
clemente 0:462017ee2a5b 72 /** ADNS2051 Optical Sensor
clemente 0:462017ee2a5b 73 * For this project I used an old Apple mouse.
clemente 0:462017ee2a5b 74 * Obviously modified for this project has made it unusable as a mouse for the PC.
clemente 0:462017ee2a5b 75 *
clemente 0:462017ee2a5b 76 * From the datasheet:
clemente 0:462017ee2a5b 77 * The ADNS-2051 is a low cost optical sensor used to
clemente 0:462017ee2a5b 78 * implement a non-mechanical tracking engine for
clemente 0:462017ee2a5b 79 * computer mice.
clemente 0:462017ee2a5b 80 * It is based on optical navigation technology, which
clemente 0:462017ee2a5b 81 * measures changes in position by optically acquiring
clemente 0:462017ee2a5b 82 * sequential surface images (frames) and mathematically
clemente 0:462017ee2a5b 83 * determining the direction and magnitude of
clemente 0:462017ee2a5b 84 * movement.
clemente 0:462017ee2a5b 85 * The sensor is housed in a 16-pin staggered dual inline
clemente 0:462017ee2a5b 86 * package (DIP).
clemente 0:462017ee2a5b 87 *
clemente 0:462017ee2a5b 88 * Features
clemente 0:462017ee2a5b 89 * . Precise optical navigation technology
clemente 0:462017ee2a5b 90 * . No mechanical moving parts
clemente 0:462017ee2a5b 91 * . Complete 2D motion sensor
clemente 0:462017ee2a5b 92 * . Serial interface and/or quadrature interface
clemente 0:462017ee2a5b 93 * . Smooth surface navigation
clemente 0:462017ee2a5b 94 * . Programmable frame speed up to 2300 frames per sec (fps)
clemente 0:462017ee2a5b 95 * . Accurate motion up to 14 ips
clemente 0:462017ee2a5b 96 * . 800 cpi resolution
clemente 0:462017ee2a5b 97 * . High reliability
clemente 0:462017ee2a5b 98 * . High speed motion detector
clemente 0:462017ee2a5b 99 * . No precision optical alignment
clemente 0:462017ee2a5b 100 * . On chip LED drive with regulated current
clemente 0:462017ee2a5b 101 * . Serial port registers
clemente 0:462017ee2a5b 102 * – Programming
clemente 0:462017ee2a5b 103 * – Data transfer
clemente 0:462017ee2a5b 104 *
clemente 0:462017ee2a5b 105 * Example code
clemente 0:462017ee2a5b 106 *
clemente 0:462017ee2a5b 107 * @code
clemente 0:462017ee2a5b 108 * #include "ADNS2051.h"
clemente 0:462017ee2a5b 109 *
clemente 0:462017ee2a5b 110 * // pin configuration: p19: sout, p20: sclk
clemente 0:462017ee2a5b 111 * ADNS2051 optical( p19, p20);
clemente 0:462017ee2a5b 112 *
clemente 0:462017ee2a5b 113 * // image buffer
clemente 0:462017ee2a5b 114 * unsigned char tmpmap[256];
clemente 0:462017ee2a5b 115 *
clemente 0:462017ee2a5b 116 * int main( void)
clemente 0:462017ee2a5b 117 * {
clemente 0:462017ee2a5b 118 * printf( "Images from optical mouse: Init");
clemente 0:462017ee2a5b 119 *
clemente 0:462017ee2a5b 120 * // wait until the ADNS chip become ready...
clemente 0:462017ee2a5b 121 * while( optical.init() );
clemente 0:462017ee2a5b 122 *
clemente 0:462017ee2a5b 123 * optical.setframerate();
clemente 0:462017ee2a5b 124 *
clemente 0:462017ee2a5b 125 * printf("Images from optical mouse: PixelDump [Chip rev: 0x%X]", optical.get_revisionID());
clemente 0:462017ee2a5b 126 * wait( 1.0);
clemente 0:462017ee2a5b 127 *
clemente 0:462017ee2a5b 128 * optical.pixeldump( &tmpmap[0]);
clemente 0:462017ee2a5b 129 * // may be... do something with tmpmap...
clemente 0:462017ee2a5b 130 * while(1);
clemente 0:462017ee2a5b 131 *
clemente 0:462017ee2a5b 132 * }
clemente 0:462017ee2a5b 133 */
clemente 0:462017ee2a5b 134
clemente 0:462017ee2a5b 135 class ADNS2051 {
clemente 0:462017ee2a5b 136
clemente 0:462017ee2a5b 137 public:
clemente 0:462017ee2a5b 138 /** ADNS2051 pin assignement.
clemente 0:462017ee2a5b 139 *
clemente 0:462017ee2a5b 140 * @param sout This pin is used as data in and data out.
clemente 0:462017ee2a5b 141 * @param sclk This is the serial clock.
clemente 0:462017ee2a5b 142 */
clemente 0:462017ee2a5b 143 ADNS2051( PinName sout, PinName sclk);
clemente 0:462017ee2a5b 144
clemente 0:462017ee2a5b 145 /** Initialize the ADNS2051 chip.
clemente 0:462017ee2a5b 146 *
clemente 0:462017ee2a5b 147 * @param none
clemente 0:462017ee2a5b 148 * @return 0 for OK, 1 for bad.
clemente 0:462017ee2a5b 149 */
clemente 0:462017ee2a5b 150 unsigned char init( void);
clemente 0:462017ee2a5b 151
clemente 0:462017ee2a5b 152 /** Read the motion register and update the deltaX and deltaY variable with the register value.
clemente 0:462017ee2a5b 153 *
clemente 0:462017ee2a5b 154 * @param none
clemente 0:462017ee2a5b 155 * @return a byte 0 = no motion, no led fault, no overflow
clemente 0:462017ee2a5b 156 * 1 = motion
clemente 0:462017ee2a5b 157 * 2 = led fault
clemente 0:462017ee2a5b 158 * 4 = overflow
clemente 0:462017ee2a5b 159 */
clemente 0:462017ee2a5b 160 unsigned char readmotion( void);
clemente 0:462017ee2a5b 161
clemente 0:462017ee2a5b 162 /** Get revision ID
clemente 0:462017ee2a5b 163 */
clemente 0:462017ee2a5b 164 unsigned char get_revisionID( void);
clemente 0:462017ee2a5b 165
clemente 0:462017ee2a5b 166 /** Get product ID
clemente 0:462017ee2a5b 167 */
clemente 0:462017ee2a5b 168 unsigned char get_productID( void);
clemente 0:462017ee2a5b 169
clemente 0:462017ee2a5b 170 /** SQUAL is a measure of the number of features visible by the sensor in the
clemente 0:462017ee2a5b 171 * current frame. The maximum value is 255. Since small changes in the current frame
clemente 0:462017ee2a5b 172 * can result in changes in SQUAL, variations in SQUAL when looking at a surface are
clemente 0:462017ee2a5b 173 * expected. SQUAL is nearly equal to zero, if there is no surface below the sensor.
clemente 0:462017ee2a5b 174 *
clemente 0:462017ee2a5b 175 * @param the SQUAL value.
clemente 0:462017ee2a5b 176 */
clemente 0:462017ee2a5b 177 unsigned char surfacequality( void);
clemente 0:462017ee2a5b 178
clemente 0:462017ee2a5b 179 /** Average Pixel value in current frame. Minimum value = 0,
clemente 0:462017ee2a5b 180 * maximum = 63. The average pixel value can be adjusted every frame.
clemente 0:462017ee2a5b 181 *
clemente 0:462017ee2a5b 182 * @param none
clemente 0:462017ee2a5b 183 */
clemente 0:462017ee2a5b 184 unsigned char averagepixel( void);
clemente 0:462017ee2a5b 185
clemente 0:462017ee2a5b 186 /** Maximum Pixel value in current frame. Minimum value = 0,
clemente 0:462017ee2a5b 187 * maximum value = 63. The maximum pixel value can be adjusted every frame.
clemente 0:462017ee2a5b 188 *
clemente 0:462017ee2a5b 189 * @param none
clemente 0:462017ee2a5b 190 */
clemente 0:462017ee2a5b 191 unsigned char maxpixelval( void);
clemente 0:462017ee2a5b 192
clemente 0:462017ee2a5b 193 /** Change the default frame rate (1500 f/s) to 2300 f/s
clemente 0:462017ee2a5b 194 *
clemente 0:462017ee2a5b 195 * @param none
clemente 0:462017ee2a5b 196 */
clemente 0:462017ee2a5b 197 void setframerate( void);
clemente 0:462017ee2a5b 198
clemente 0:462017ee2a5b 199 /** Verify the ADNS 2051 connection.
clemente 0:462017ee2a5b 200 *
clemente 0:462017ee2a5b 201 * @param none
clemente 0:462017ee2a5b 202 * @return 0 OK, 1 Bad connection
clemente 0:462017ee2a5b 203 */
clemente 0:462017ee2a5b 204 unsigned char chkconn( void);
clemente 0:462017ee2a5b 205
clemente 0:462017ee2a5b 206 /** Start the dump of the pixel map. Store the value to the array pixmap
clemente 0:462017ee2a5b 207 *
clemente 0:462017ee2a5b 208 * @param buffer array of 256 chars
clemente 0:462017ee2a5b 209 */
clemente 0:462017ee2a5b 210 void pixeldump( unsigned char *buffer);
clemente 0:462017ee2a5b 211
clemente 0:462017ee2a5b 212 /** this values are updates every readmotion call.
clemente 0:462017ee2a5b 213 */
clemente 0:462017ee2a5b 214 signed char deltaX, deltaY;
clemente 0:462017ee2a5b 215
clemente 0:462017ee2a5b 216 /** This value is the current resolution in count per inch. Can be 400 or 800 cpi
clemente 0:462017ee2a5b 217 */
clemente 0:462017ee2a5b 218 unsigned int currentresolution;
clemente 0:462017ee2a5b 219
clemente 0:462017ee2a5b 220 private:
clemente 0:462017ee2a5b 221 void sync( void);
clemente 0:462017ee2a5b 222 unsigned char read(unsigned char address);
clemente 0:462017ee2a5b 223 void write(unsigned char address, unsigned char value);
clemente 0:462017ee2a5b 224 //
clemente 0:462017ee2a5b 225 DigitalInOut _sout;
clemente 0:462017ee2a5b 226 DigitalOut _sclk;
clemente 0:462017ee2a5b 227
clemente 0:462017ee2a5b 228 };
clemente 0:462017ee2a5b 229
clemente 0:462017ee2a5b 230 #endif