Interface to access to Avago ADNS-9500 laser mouse sensors.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sample.cpp Source File

sample.cpp

00001 /*
00002  * sample.hpp - Example of access to Avago ADNS-9500 laser mouse sensors
00003  *
00004  *   Copyright 2012 Jesus Torres <jmtorres@ull.es>
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 #include <mbed.h>
00020 #include <stdint.h>
00021 
00022 #include "adns9500.hpp"
00023 
00024 #define USE_MOTION_BURST
00025 //#define FRAME_CAPTURE
00026 
00027 const char* FIRMWARE_FILENAME = "/local/adns9500.fw";
00028 LocalFileSystem local("local");
00029 
00030 Ticker printData;
00031 adns9500::ADNS9500 sensor(p11, p12, p13, p15, adns9500::MAX_SPI_FREQUENCY, p14);
00032 
00033 bool motionTriggered = false;
00034 bool printDataTriggered = false;
00035 
00036 int motionCallbackCounter = 0;
00037 
00038 void printDataCallback()
00039 {
00040     printDataTriggered = true;
00041 }
00042 
00043 void motionCallback()
00044 {
00045     motionTriggered = true;
00046     motionCallbackCounter++;
00047 }
00048 
00049 int main()
00050 {
00051     int dataReadCounter = 0;
00052     float totalMotionDx = 0.0;
00053     float totalMotionDy = 0.0;
00054 
00055 #if defined (USE_MOTION_BURST)
00056     adns9500::MotionData data;
00057     sensor.attach(&motionCallback);
00058 #elif defined (FRAME_CAPTURE)
00059     uint8_t frame[adns9500::NUMBER_OF_PIXELS_PER_FRAME];
00060 #else
00061     sensor.attach(&motionCallback);
00062 #endif
00063 
00064     sensor.reset();
00065 
00066 #if ! defined (FRAME_CAPTURE)
00067     // Firmware upload
00068     int crc = sensor.sromDownload(FIRMWARE_FILENAME);
00069     printf("Firmware CRC: 0x%x (%s)\r\n", crc, FIRMWARE_FILENAME);
00070 #endif
00071 
00072     // Enable laser
00073     sensor.enableLaser();
00074     printf("Laser enabled\r\n");
00075 
00076 #if ! defined (FRAME_CAPTURE)
00077     printData.attach_us(&printDataCallback, 500);
00078 #endif
00079 
00080     while(true) {
00081         if (motionTriggered) {
00082             motionTriggered = false;
00083             
00084 #if defined (USE_MOTION_BURST)
00085             sensor.getMotionData(data);
00086             totalMotionDx += data.dxMM;
00087             totalMotionDy += data.dyMM;
00088 #else
00089             float dx, dy;
00090             sensor.getMotionDeltaMM(dx, dy);
00091             totalMotionDx += dx;
00092             totalMotionDy += dy;
00093 #endif
00094             dataReadCounter++;
00095         }
00096 
00097         if (printDataTriggered) {
00098             printDataTriggered = false;
00099 #if defined (USE_MOTION_BURST)
00100             printf("Motion burst: %f, %f, quality=%d, average=%f, maximum=%d, minimum=%d, "
00101                    "shutter=%d, periodo=%d, read=%d, irq=%d\r\n",
00102                     totalMotionDx, totalMotionDy,
00103                     data.surfaceQuality, data.averagePixel, data.maximumPixel, data.minimumPixel,
00104                     data.shutter, data.framePeriod, dataReadCounter,
00105                     motionCallbackCounter);
00106 #else
00107             printf("Motion delta: %f, %f, read=%d, irq=%d\r\n",
00108                 totalMotionDx, totalMotionDy, dataReadCounter, motionCallbackCounter);
00109 #endif
00110         }
00111 
00112 #if defined (FRAME_CAPTURE)
00113         printf("FRAME:%d:", dataReadCounter);
00114         sensor.captureFrame(frame);
00115         for(uint8_t *p = frame; p != frame + sizeof(frame); ++p)
00116             printf("%x", *p);
00117         printf("\r\n");
00118         dataReadCounter++;
00119 #endif
00120     }
00121 }