Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: ADNS-2051 configuration help

  1. #1

    ADNS-2051 configuration help

    I would have been totally lost without the Arduino IDE software. I do have a question. Here is a page from the ADNS2051 data sheet and my sketch so far. ADNS2051.h is in the Optimouse library. How do I send the configuration to the ADNS2051. Specifically, I want to make sure the RES is set to 800 counts per inch, and would like to set Sleep to Always awake. What is the line of code to put in the sketch. Thank you in advance


    // ADNS2051.h reads out ADNS-2051 the Optical Navigation Sensor
    // It's used in many cheap optical mice
    //
    // Written by Martijn The -> post [at] martijnthe.nl
    // Tutorial: http://www.martijnthe.nl/optimouse/
    // Based on the sketches by Beno”t Rousseau
    // Trackball part of Panther XL clone

    #include "ADNS2051.h"
    #include <Bounce.h> // Create Bounce objects for each button. The Bounce object
    // automatically deals with contact chatter or "bounce", and
    // it makes detecting changes very simple.

    #define SCLK 0 // Serial clock pin on the Arduino
    #define SDIO 1 // Serial data (I/O) pin on the Arduino

    ADNS2051 Optical1 = ADNS2051(SCLK, SDIO); // Create an instance of the ADNS2051 object

    Bounce button2 = Bounce(2, 10); // if a button is too "sensitive"
    Bounce button3 = Bounce(3, 10); // to rapid touch, you can
    Bounce button4 = Bounce(4, 10); // increase this time.
    void setup()
    {
    Serial.begin(38400);
    Optical1.begin(); // Resync (not really necessary?)

    pinMode(2, INPUT_PULLUP); // Left Mouse Click
    pinMode(3, INPUT_PULLUP); // Middle Mouse Click
    pinMode(4, INPUT_PULLUP); // Right Mouse Click
    }

    void loop()
    {
    button2.update();
    button3.update();
    button4.update();

    if (button2.fallingEdge()) {
    Mouse.press(MOUSE_LEFT);
    }
    if (button2.risingEdge()) {
    Mouse.release(MOUSE_LEFT);
    }
    if (button3.fallingEdge()) {
    Mouse.press(MOUSE_MIDDLE);
    }
    if (button3.risingEdge()) {
    Mouse.release(MOUSE_MIDDLE);
    }
    if (button4.fallingEdge()) {
    Mouse.press(MOUSE_RIGHT);
    }
    if (button4.risingEdge()) {
    Mouse.release(MOUSE_RIGHT);
    }

    // The status commands are available only for the PAN3101 and the ADNS2051:

    Optical1.updateStatus(); // Get the latest motion status
    if (Optical1.motion()) // If the 'Motion' status bit is set,
    {

    Mouse.move(-1*Optical1.dx(), -1*Optical1.dy());

    }
    }
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Screenshot at 2018-01-22 12-34-15.jpg 
Views:	85 
Size:	100.8 KB 
ID:	12704  

  2. #2
    Wow, I figured it out. First of all I,m using the OptiMouse-master library from Github, which has support for 5 different mouse sensors. The "extra commands" were written for the ADNS2620 in that library, and I had looked at examples for other mouse sensor libraries that sent configuration data. I added lines to ADNS2051.cpp ADNS.h and Optical1.writeConfig(B000101); in the setup of my sketch which sets 800 counts per inch and always awake. WooHoo. I am a complete total noob to this so I really impressed myself. Now I can get on with setting up a thumbstick for scrolling :-D

  3. #3
    Here is the new ADNS2051.cpp

    Code:
    /*
     ADNS2051.cpp - Part of optical mouse sensor library for Arduino
     Copyright (c) 2008 Martijn The.  All right reserved.
     http://www.martijnthe.nl/
     Conversion to Arduino 1.x by zapmaker (zapmaker.org)
     
     Configuration command added by Jeff Pearson
    
     Based on sketches by Benoît Rousseau.
     
     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
     License as published by the Free Software Foundation; either
     version 2.1 of the License, or (at your option) any later version.
     
     This library is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Lesser General Public License for more details.
     
     You should have received a copy of the GNU Lesser General Public
     License along with this library; if not, write to the Free Software
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     */
    
    /******************************************************************************
     * Includes
     ******************************************************************************/
    
    #include "Arduino.h"
    #include "OptiMouse.h"
    #include "ADNS2051.h"
    
    /******************************************************************************
     * Definitions
     ******************************************************************************/
    
    #define Config              0x0a             // added 
    #define Delta_Y				0x04
    #define Delta_X				0x03
    #define Motion_Status		0x02
    
    #define Mask_Motion			0x80
    #define Mask_DYOVF			0x10
    #define Mask_DXOVF			0x08
    
    /******************************************************************************
     * Constructors
     ******************************************************************************/
    
    
    ADNS2051::ADNS2051(uint8_t sclkPin, uint8_t sdioPin) : OptiMouse::OptiMouse(sclkPin, sdioPin)
    {
    
    }
    
    /******************************************************************************
     * User API
     ******************************************************************************/
    signed char ADNS2051::config(void)                   // added
    {                                                    //
    	return (signed char) readRegister(Config);       // for
    }                                                    // 
                                                         // configuration
    void ADNS2051::writeConfig(signed char c)            //
                                                         // command
    	writeRegister(Config, c);                        // 
    }                                                    // added
     
    void ADNS2051::updateStatus(void)
    {
    	_status = readRegister(Motion_Status);
    }
    
    signed char ADNS2051::dx(void)
    {
    	return (signed char) readRegister(Delta_X);
    }
    
    signed char ADNS2051::dy(void)
    {
    	return (signed char) readRegister(Delta_Y);
    }
    
    uint8_t ADNS2051::motion() const
    {
    	return (uint8_t) (_status & Mask_Motion) == Mask_Motion;
    }
    
    uint8_t ADNS2051::dxOverflow() const
    {
    	return (uint8_t) (_status & Mask_DXOVF) == Mask_DXOVF;
    }
    uint8_t ADNS2051::dyOverflow() const
    {
    	return (uint8_t) (_status & Mask_DYOVF) == Mask_DYOVF;
    }
    
    // Private Methods /////////////////////////////////////////////////////////////
    The new ADNS2051.h

    Code:
    /*
     ADNS2051.h - Part of optical mouse sensor library for Arduino
     Copyright (c) 2008 Martijn The.  All right reserved.
     http://www.martijnthe.nl/
     Conversion to Arduino 1.x by zapmaker (zapmaker.org)
     
     Configure command added by Jeff Pearson
    
     Based on sketches by Benoît Rousseau.
     
     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
     License as published by the Free Software Foundation; either
     version 2.1 of the License, or (at your option) any later version.
     
     This library is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Lesser General Public License for more details.
     
     You should have received a copy of the GNU Lesser General Public
     License along with this library; if not, write to the Free Software
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     */
    
    #ifndef ADNS2051_h
    #define ADNS2051_h
    
    #include <inttypes.h>
    #include "OptiMouse.h"
    
    class ADNS2051 : public OptiMouse
    {
      private:
    	uint8_t _status;
      public:
    	ADNS2051(uint8_t, uint8_t);
    	signed char config(void);              // added
    	void writeConfig(signed char c);       // added
    	void updateStatus(void);
    	signed char dx(void);
    	signed char dy(void);
    	uint8_t motion() const;
       
    	uint8_t dxOverflow() const;
    	uint8_t dyOverflow() const;
    };
    
    #endif
    And my Trackball sketch with Config command

    Code:
    // ADNS2051.h reads out ADNS-2051 the  Optical Navigation Sensor
    // It's used in many cheap optical mice
    //
    // Written by Martijn The -> post [at] martijnthe.nl
    // Tutorial: http://www.martijnthe.nl/optimouse/
    // Based on the sketches by Beno”t Rousseau
    // Trackball part of Panther XL clone
    
    #include "ADNS2051.h"
    #include <Bounce.h>  // Create Bounce objects for each button.  The Bounce object
                         // automatically deals with contact chatter or "bounce", and
                         // it makes detecting changes very simple.
                         
    #define SCLK 0                            // Serial clock pin on the Arduino
    #define SDIO 1                            // Serial data (I/O) pin on the Arduino
       
    ADNS2051 Optical1 = ADNS2051(SCLK, SDIO); // Create an instance of the ADNS2051 object
    
    Bounce button2 = Bounce(2, 10);           // if a button is too "sensitive"
    Bounce button3 = Bounce(3, 10);           // to rapid touch, you can
    Bounce button4 = Bounce(4, 10);           // increase this time.
    void setup()
    {
      Serial.begin(38400);
      Optical1.begin();                       // Resync (not really necessary?)
    
     Optical1.writeConfig(B000101);           // added command. set 800 CPI and always awake 
                                              // (B000100), set 800 CPI and auto sleep
                                              // Default is 400 CPI and auto sleep
    
      pinMode(2, INPUT_PULLUP);               // Left Mouse Click
      pinMode(3, INPUT_PULLUP);               // Middle Mouse Click
      pinMode(4, INPUT_PULLUP);               // Right Mouse Click
    }
    
    void loop()
    {
      button2.update();
      button3.update();
      button4.update();
    
       if (button2.fallingEdge()) {
        Mouse.press(MOUSE_LEFT);
      }
      if (button2.risingEdge()) {
        Mouse.release(MOUSE_LEFT);
      }
      if (button3.fallingEdge()) {
        Mouse.press(MOUSE_MIDDLE);
      }
      if (button3.risingEdge()) {
        Mouse.release(MOUSE_MIDDLE);
      }
      if (button4.fallingEdge()) {
        Mouse.press(MOUSE_RIGHT);
      }
      if (button4.risingEdge()) {
        Mouse.release(MOUSE_RIGHT);
      }
      
    //  The status commands are available only for the PAN3101 and the ADNS2051:
    
      Optical1.updateStatus();                // Get the latest motion status
     if (Optical1.motion())                   // If the 'Motion' status bit is set,
      {
        
     Mouse.move(-1*Optical1.dx(), -1*Optical1.dy());
          
      }
    }

    Am I supposed to send this upstream to Github????
    Last edited by Jeff Pearson; 01-26-2018 at 09:55 PM. Reason: pasted wrong code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •