IPAB Neuromorphic / ADNS5020EN
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADNS5020EN.cpp Source File

ADNS5020EN.cpp

00001 #include "ADNS5020EN.h"
00002 
00003 ADNS5020EN::ADNS5020EN(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName cr, float freq)
00004         : _spi(mosi, miso, sclk), _cs(cs), _cr(cr) {
00005 
00006     // setup the spi connection to a given frequency (MHz)
00007     _spi.format(8,3);
00008     set_freq(freq);
00009 
00010     // Chip Reset
00011     creset();
00012     _cs = 1;
00013     wait_us(20);
00014 
00015     _false_readings = 0;
00016     _cpi = 500;
00017 
00018     int prod_id = cread(0x00); // read Product ID register
00019     int rev_id = cread(0x01); // read Revision ID register
00020 
00021     if ((prod_id!=18)||(rev_id!=1)) {
00022 
00023         error("The connection with the ADNS-5020-EN is not established\n");;
00024 
00025     }
00026 
00027 }
00028 
00029 // read the delta_X and delta_Y of the chip
00030 void ADNS5020EN::read_deltas(int* a_dx, int* a_dy) {
00031 
00032     int * dx, * dy;
00033     dx=(int*)a_dx;
00034     dy=(int*)a_dy;
00035     *dx = 0;
00036     *dy = 0;
00037 
00038     int prod_id = cread(0x00); // read Product ID register
00039     int rev_id = cread(0x01); // read Revision ID register
00040     int motion = cread(0x02); // read Motion register
00041 
00042     if ((prod_id==18)&&(rev_id==1)&&(motion==128)) {
00043 
00044         *dx = MChipMotion(cread(0x03));
00045         *dy = MChipMotion(cread(0x04));
00046 
00047     } else if ((prod_id!=18)||(rev_id!=1)) {
00048 
00049         _false_readings ++;
00050         printf("====  FALSE Reading ====\r\n");
00051 
00052     }
00053 }
00054 
00055 void ADNS5020EN::read_deltas_mm(float* a_dx, float* a_dy) {
00056 
00057     float * dx, * dy;
00058     dx=(float*)a_dx;
00059     dy=(float*)a_dy;
00060 
00061     int i_dx, i_dy;
00062 
00063     read_deltas(&i_dx,&i_dy);
00064 
00065     *dx = counts_to_mm(i_dx);
00066     *dy = counts_to_mm(i_dy);
00067 
00068 }
00069 
00070 // Set new working frequency to the chip
00071 void ADNS5020EN::set_freq(float freq) {
00072 
00073     _spi.frequency(freq*1000000);
00074 }
00075 
00076 // return the false readings value
00077 int ADNS5020EN::falser() {
00078 
00079     return _false_readings;
00080 }
00081 
00082 // mouse chip - end process
00083 void ADNS5020EN::end() {
00084 
00085     // Chip Reset
00086     creset();
00087     _cs = 1;
00088     wait_us(20);
00089 
00090     // Chip Power Down signal
00091     cwrite(0x8d,0x02);
00092     
00093 }
00094 
00095 // mouse chip - change resolurion
00096 int ADNS5020EN::changeCPI(bool cpi=0) {
00097 
00098     if (cpi) {
00099         // Change cpi to 1000
00100         cwrite(0x8d,0x01);
00101         _cpi = 1000;
00102     } else {
00103         // Change cpi to 500
00104         cwrite(0x8d,0x00);
00105         _cpi = 500;
00106     }
00107 
00108     return cread(0x0d);
00109 }
00110 
00111 // mouse chip reset
00112 void ADNS5020EN::creset() {
00113 
00114     _cr = 0;
00115     wait_us(2);
00116     _cr = 1;
00117     wait_ms(52);
00118 
00119 }
00120 
00121 // read a register
00122 int ADNS5020EN::cread(int cregister) {
00123 
00124     // select the device by setting pin4 high
00125     _cs=0;
00126 
00127     // send the command to read the register
00128     _spi.write(cregister);
00129     wait_us(10);
00130 
00131     // send dummy byte
00132     int reply = _spi.write(0x00);
00133 
00134     // deselect the device by setting pin4 high
00135     _cs=1;
00136     wait_us(10);
00137 
00138     return reply;
00139 }
00140 
00141 // write data to a register
00142 void ADNS5020EN::cwrite(int caddress, int cdata) {
00143 
00144     // select the device by setting pin4 high
00145     _cs=0;
00146 
00147     // send the command to read the register
00148     _spi.write(caddress);
00149     wait_us(10);
00150 
00151     // send dummy byte
00152     _spi.write(cdata);
00153 
00154     // deselect the device by setting pin4 high
00155     _cs=1;
00156     wait_us(40); // more than the minimum for safety
00157 }
00158 
00159 
00160 // Transform the reading of the mouse Delta register to actual motion
00161 int ADNS5020EN::MChipMotion(int reading) {
00162 
00163     int displacement;
00164     if (reading <= 128) {
00165         displacement = reading;
00166     } else {
00167         displacement = reading - 256;
00168     }
00169     return displacement;
00170 
00171 }
00172 
00173 // Translate the readed counts from the chip into mm
00174 float ADNS5020EN::counts_to_mm(int counts) {
00175 
00176     float INCHtoMM =  25.4;
00177     float result = (float) counts * INCHtoMM/_cpi;
00178     return result;
00179 
00180 }