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

Dependencies:   mbed

Revision:
13:6d0642367fc1
Parent:
12:61b07ad45dc1
Child:
14:dc9badf3c6e9
--- a/adns9500.cpp	Sat Sep 29 22:36:29 2012 +0000
+++ b/adns9500.cpp	Tue Oct 16 16:59:53 2012 +0000
@@ -16,9 +16,8 @@
  * limitations under the License.
  */
 
+#include <cstdio>
 #include <cstdlib>
-#include <fstream>
-#include <math.h>
 #include <mbed.h>
 #include <string>
 
@@ -40,7 +39,7 @@
     WAIT_TSCLKNCS(); ncs_.write(1); wait_us(x); ncs_.write(0); WAIT_TNCSSCLK()
 
 #define DEFAULT_MAX_FPS             1958
-#define DEFAULT_MAX_FRAME_PERIOD    ((int)ceil(1e6 / DEFAULT_MAX_FPS))  // in us
+#define DEFAULT_MAX_FRAME_PERIOD    (1000000 / DEFAULT_MAX_FPS + 1) // in us
 #define DEFAULT_X_CPI               1620
 #define DEFAULT_Y_CPI               1620
 #define CPI_CHANGE_UNIT             90
@@ -208,18 +207,20 @@
         spi_.write(SET_BIT(SROM_LOAD_BURST, SPI_WRITE_MODE));
 
         // we expect a line per byte in hex without 0x prefix
-        char buffer[4];
-        ifstream ifs(filename, ifstream::in);
-        while(ifs.getline(buffer, sizeof(buffer)).good()) {
+        FILE *fp = fopen(filename, "r");
+        if (! fp)
+            error("ADNS9500::sromDownload : file %s could not be opened\n", filename);
+
+        unsigned int byte;
+        while (fscanf(fp, "%2x", &byte)) {
             WAIT_TLOAD();
-            int byte = strtol(buffer, NULL, 16);
             spi_.write(byte);
         }
         WAIT_TSCLKNCS();
         ncs_.write(1);
         WAIT_TBEXIT();
 
-        if (! ifs.eof())
+        if (ferror(fp))
             error("ADNS9500::sromDownload : error reading from file: %s\n", filename);
 
         // test if SROM was downloaded successfully