ADNS5020EN library example code.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <stdio.h>
00003 #include <math.h>
00004 #include "ADNS5020EN.h"
00005 
00006 ADNS5020EN adns(p5,p6,p7,p21,p22,0.1);
00007 Serial PC(USBTX,USBRX);
00008 
00009 DigitalIn button(p25);
00010 
00011 // Timers
00012 Timer ticktok;  // Runtime Timer
00013 Timer dtimer;   // Timer for data manipulation
00014 Timer flash;    // LED timer
00015 
00016 #define PI     3.14159265    // Pi (math)
00017 
00018 float accum_dx = 0.00, accum_dy = 0.00;
00019 
00020 // output function
00021 void action_data() {
00022     if (dtimer.read_ms()>=100) {
00023         float phi = 0, d = 0;
00024         float y = -1*accum_dy;
00025         float x = accum_dx;
00026 
00027         if ((y != 0) || (x != 0)) {
00028             phi = atan2(x,y);
00029             d = sqrt(pow(y,2) + pow(x,2));
00030         }
00031         PC.printf(" P = %+.4f rad\t", phi);
00032         PC.printf(" D = %+.4f mm\t", d);
00033         if (d<10.00)
00034             PC.printf("\t");
00035         PC.printf("|   deltaX = %+.4f mm\t", x);
00036         PC.printf(" deltaY = %+.4f mm\r\n", y);
00037         accum_dx = 0;
00038         accum_dy = 0;
00039         dtimer.reset();
00040     }
00041 }
00042 
00043 int main() {
00044     PC.baud(38400);
00045 
00046     PC.printf("\r\nADNS-5020EN mouse chip\r\n");
00047     PC.printf("=================================\r\n");
00048     PC.printf("Initialising...\r\n");
00049 
00050     float a_dx = 0, a_dy = 0;
00051     dtimer.start();
00052     while (button) {
00053 
00054         // read the mouse chip data
00055         adns.read_deltas_mm(&a_dx,&a_dy);
00056         accum_dx += a_dx;
00057         accum_dy += a_dy;
00058         
00059         // act
00060         action_data();
00061 
00062         wait_us(100);
00063 
00064     }
00065     
00066     PC.printf("False Readings: %d\r\n",adns.falser());    
00067     
00068     wait(1);
00069     
00070     int reply = adns.changeCPI(1);
00071     
00072     PC.printf("CPI = 0x%X\r\n",reply);    
00073     wait(1);
00074     adns.end();
00075 }