Jesús Torres
/
ADNS9500
Interface to access to Avago ADNS-9500 laser mouse sensors.
sample.cpp@16:0f8e730f3272, 2013-04-20 (annotated)
- Committer:
- aplatanado
- Date:
- Sat Apr 20 12:50:42 2013 +0000
- Revision:
- 16:0f8e730f3272
- Parent:
- 6:1969574dee8b
apply some coding style rules to macros
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aplatanado | 6:1969574dee8b | 1 | /* |
aplatanado | 6:1969574dee8b | 2 | * sample.hpp - Example of access to Avago ADNS-9500 laser mouse sensors |
aplatanado | 6:1969574dee8b | 3 | * |
aplatanado | 6:1969574dee8b | 4 | * Copyright 2012 Jesus Torres <jmtorres@ull.es> |
aplatanado | 6:1969574dee8b | 5 | * |
aplatanado | 6:1969574dee8b | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
aplatanado | 6:1969574dee8b | 7 | * you may not use this file except in compliance with the License. |
aplatanado | 6:1969574dee8b | 8 | * You may obtain a copy of the License at |
aplatanado | 6:1969574dee8b | 9 | * |
aplatanado | 6:1969574dee8b | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
aplatanado | 6:1969574dee8b | 11 | * |
aplatanado | 6:1969574dee8b | 12 | * Unless required by applicable law or agreed to in writing, software |
aplatanado | 6:1969574dee8b | 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
aplatanado | 6:1969574dee8b | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
aplatanado | 6:1969574dee8b | 15 | * See the License for the specific language governing permissions and |
aplatanado | 6:1969574dee8b | 16 | * limitations under the License. |
aplatanado | 6:1969574dee8b | 17 | */ |
aplatanado | 6:1969574dee8b | 18 | |
aplatanado | 6:1969574dee8b | 19 | #include <mbed.h> |
aplatanado | 6:1969574dee8b | 20 | #include <stdint.h> |
aplatanado | 6:1969574dee8b | 21 | |
aplatanado | 6:1969574dee8b | 22 | #include "adns9500.hpp" |
aplatanado | 6:1969574dee8b | 23 | |
aplatanado | 6:1969574dee8b | 24 | #define USE_MOTION_BURST |
aplatanado | 6:1969574dee8b | 25 | //#define FRAME_CAPTURE |
aplatanado | 6:1969574dee8b | 26 | |
aplatanado | 6:1969574dee8b | 27 | const char* FIRMWARE_FILENAME = "/local/adns9500.fw"; |
aplatanado | 6:1969574dee8b | 28 | LocalFileSystem local("local"); |
aplatanado | 6:1969574dee8b | 29 | |
aplatanado | 6:1969574dee8b | 30 | Ticker printData; |
aplatanado | 6:1969574dee8b | 31 | adns9500::ADNS9500 sensor(p11, p12, p13, p15, adns9500::MAX_SPI_FREQUENCY, p14); |
aplatanado | 6:1969574dee8b | 32 | |
aplatanado | 6:1969574dee8b | 33 | bool motionTriggered = false; |
aplatanado | 6:1969574dee8b | 34 | bool printDataTriggered = false; |
aplatanado | 6:1969574dee8b | 35 | |
aplatanado | 6:1969574dee8b | 36 | int motionCallbackCounter = 0; |
aplatanado | 6:1969574dee8b | 37 | |
aplatanado | 6:1969574dee8b | 38 | void printDataCallback() |
aplatanado | 6:1969574dee8b | 39 | { |
aplatanado | 6:1969574dee8b | 40 | printDataTriggered = true; |
aplatanado | 6:1969574dee8b | 41 | } |
aplatanado | 6:1969574dee8b | 42 | |
aplatanado | 6:1969574dee8b | 43 | void motionCallback() |
aplatanado | 6:1969574dee8b | 44 | { |
aplatanado | 6:1969574dee8b | 45 | motionTriggered = true; |
aplatanado | 6:1969574dee8b | 46 | motionCallbackCounter++; |
aplatanado | 6:1969574dee8b | 47 | } |
aplatanado | 6:1969574dee8b | 48 | |
aplatanado | 6:1969574dee8b | 49 | int main() |
aplatanado | 6:1969574dee8b | 50 | { |
aplatanado | 6:1969574dee8b | 51 | int dataReadCounter = 0; |
aplatanado | 6:1969574dee8b | 52 | float totalMotionDx = 0.0; |
aplatanado | 6:1969574dee8b | 53 | float totalMotionDy = 0.0; |
aplatanado | 6:1969574dee8b | 54 | |
aplatanado | 6:1969574dee8b | 55 | #if defined (USE_MOTION_BURST) |
aplatanado | 6:1969574dee8b | 56 | adns9500::MotionData data; |
aplatanado | 6:1969574dee8b | 57 | sensor.attach(&motionCallback); |
aplatanado | 6:1969574dee8b | 58 | #elif defined (FRAME_CAPTURE) |
aplatanado | 6:1969574dee8b | 59 | uint8_t frame[adns9500::NUMBER_OF_PIXELS_PER_FRAME]; |
aplatanado | 6:1969574dee8b | 60 | #else |
aplatanado | 6:1969574dee8b | 61 | sensor.attach(&motionCallback); |
aplatanado | 6:1969574dee8b | 62 | #endif |
aplatanado | 6:1969574dee8b | 63 | |
aplatanado | 6:1969574dee8b | 64 | sensor.reset(); |
aplatanado | 6:1969574dee8b | 65 | |
aplatanado | 6:1969574dee8b | 66 | #if ! defined (FRAME_CAPTURE) |
aplatanado | 6:1969574dee8b | 67 | // Firmware upload |
aplatanado | 6:1969574dee8b | 68 | int crc = sensor.sromDownload(FIRMWARE_FILENAME); |
aplatanado | 6:1969574dee8b | 69 | printf("Firmware CRC: 0x%x (%s)\r\n", crc, FIRMWARE_FILENAME); |
aplatanado | 6:1969574dee8b | 70 | #endif |
aplatanado | 6:1969574dee8b | 71 | |
aplatanado | 6:1969574dee8b | 72 | // Enable laser |
aplatanado | 6:1969574dee8b | 73 | sensor.enableLaser(); |
aplatanado | 6:1969574dee8b | 74 | printf("Laser enabled\r\n"); |
aplatanado | 6:1969574dee8b | 75 | |
aplatanado | 6:1969574dee8b | 76 | #if ! defined (FRAME_CAPTURE) |
aplatanado | 6:1969574dee8b | 77 | printData.attach_us(&printDataCallback, 500); |
aplatanado | 6:1969574dee8b | 78 | #endif |
aplatanado | 6:1969574dee8b | 79 | |
aplatanado | 6:1969574dee8b | 80 | while(true) { |
aplatanado | 6:1969574dee8b | 81 | if (motionTriggered) { |
aplatanado | 6:1969574dee8b | 82 | motionTriggered = false; |
aplatanado | 6:1969574dee8b | 83 | |
aplatanado | 6:1969574dee8b | 84 | #if defined (USE_MOTION_BURST) |
aplatanado | 6:1969574dee8b | 85 | sensor.getMotionData(data); |
aplatanado | 6:1969574dee8b | 86 | totalMotionDx += data.dxMM; |
aplatanado | 6:1969574dee8b | 87 | totalMotionDy += data.dyMM; |
aplatanado | 6:1969574dee8b | 88 | #else |
aplatanado | 6:1969574dee8b | 89 | float dx, dy; |
aplatanado | 6:1969574dee8b | 90 | sensor.getMotionDeltaMM(dx, dy); |
aplatanado | 6:1969574dee8b | 91 | totalMotionDx += dx; |
aplatanado | 6:1969574dee8b | 92 | totalMotionDy += dy; |
aplatanado | 6:1969574dee8b | 93 | #endif |
aplatanado | 6:1969574dee8b | 94 | dataReadCounter++; |
aplatanado | 6:1969574dee8b | 95 | } |
aplatanado | 6:1969574dee8b | 96 | |
aplatanado | 6:1969574dee8b | 97 | if (printDataTriggered) { |
aplatanado | 6:1969574dee8b | 98 | printDataTriggered = false; |
aplatanado | 6:1969574dee8b | 99 | #if defined (USE_MOTION_BURST) |
aplatanado | 6:1969574dee8b | 100 | printf("Motion burst: %f, %f, quality=%d, average=%f, maximum=%d, minimum=%d, " |
aplatanado | 6:1969574dee8b | 101 | "shutter=%d, periodo=%d, read=%d, irq=%d\r\n", |
aplatanado | 6:1969574dee8b | 102 | totalMotionDx, totalMotionDy, |
aplatanado | 6:1969574dee8b | 103 | data.surfaceQuality, data.averagePixel, data.maximumPixel, data.minimumPixel, |
aplatanado | 6:1969574dee8b | 104 | data.shutter, data.framePeriod, dataReadCounter, |
aplatanado | 6:1969574dee8b | 105 | motionCallbackCounter); |
aplatanado | 6:1969574dee8b | 106 | #else |
aplatanado | 6:1969574dee8b | 107 | printf("Motion delta: %f, %f, read=%d, irq=%d\r\n", |
aplatanado | 6:1969574dee8b | 108 | totalMotionDx, totalMotionDy, dataReadCounter, motionCallbackCounter); |
aplatanado | 6:1969574dee8b | 109 | #endif |
aplatanado | 6:1969574dee8b | 110 | } |
aplatanado | 6:1969574dee8b | 111 | |
aplatanado | 6:1969574dee8b | 112 | #if defined (FRAME_CAPTURE) |
aplatanado | 6:1969574dee8b | 113 | printf("FRAME:%d:", dataReadCounter); |
aplatanado | 6:1969574dee8b | 114 | sensor.captureFrame(frame); |
aplatanado | 6:1969574dee8b | 115 | for(uint8_t *p = frame; p != frame + sizeof(frame); ++p) |
aplatanado | 6:1969574dee8b | 116 | printf("%x", *p); |
aplatanado | 6:1969574dee8b | 117 | printf("\r\n"); |
aplatanado | 6:1969574dee8b | 118 | dataReadCounter++; |
aplatanado | 6:1969574dee8b | 119 | #endif |
aplatanado | 6:1969574dee8b | 120 | } |
aplatanado | 6:1969574dee8b | 121 | } |