adafruit / seesaw

I2C friend to expand capabilities of other chips.
Other
76 stars 34 forks source link

getEncoderDelta returns positve value in each direction #66

Closed matthewepler closed 1 year ago

matthewepler commented 1 year ago

Steps taken:

  1. turn knob in one direction, and then the other.

Expected result: serial monitor prints "2" when knob turned left and "-2" in the opposite direction. Actual result: serial monitor prints "2" in both directions

Description: The function def for getEncoderDelta should returned a signed integer. I am only seeing positive values. Either I'm using the function wrong or this is a bug.

Sketch:

#include "Adafruit_seesaw.h"
#define SEESAW_ADDR      0x36
#define SS_SWITCH        24
Adafruit_seesaw ss;

int32_t encoderId;
int32_t encoder_position;

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(10);

  initEncoder();
  encoderId = ss.getOptions();

  Serial.println(encoderId);   // prints 149507
  Serial.println(SEESAW_ADDR); // prints 54
}

void loop() {
  int32_t delta = ss.getEncoderDelta(encoderId);
  if(delta != 0) {
    Serial.println(delta);
  }
}

void initEncoder() {
  if (! ss.begin(SEESAW_ADDR)) {
    Serial.println("Couldn't find seesaw on default address. You may need to add 0x64 as a parameter to the begin function.");
    while(1) delay(10);
  }

  uint32_t version = ((ss.getVersion() >> 16) & 0xFFFF);
  if (version  != 4991){
    Serial.print("Wrong firmware loaded? ");
    Serial.println(version);
    while(1) delay(10);
  }

  // use a pin for the built in encoder switch
  ss.pinMode(SS_SWITCH, INPUT_PULLUP);

  // get starting position
  encoder_position = ss.getEncoderPosition();

  delay(10);
  ss.setGPIOInterrupts((uint32_t)1 << SS_SWITCH, 1);
  ss.enableEncoderInterrupt();
}
matthewepler commented 1 year ago

The error was a fluke. I power-cycled the board and it worked fine...with one exception in the above code. The argument encoderId in the call to getEncoderDelta should not be there. The function should be called with no argument: int32_t delta = ss.getEncoderDelta();.

Along the way to this conclusion, I learned a few things by trial/error that are not clearly stated in the docs and could be of use to someone else trying to figure out how to use this function...

The function definition for getEncoderDelta includes a parameter encoder (Adafruit_seesaw.cpp, line 784). It's unclear from the docstring what this refers to and how it should be used. This argument is meant target a single encoder when more than one encoder is attached to your I2C. A value of 1 would refer to the encoder whose address is 1 value more than the default encoder address (aka SEESAW_BASE_ADDR in the library code).

If you only have one encoder, you don't need any argument when calling getEncoderDelta. If you have two, then calling it with no argument or 0 will address the first encoder, and 1 address the second. And so on.

In short, it's just a number added to the base address and has no meaning by itself.