pointers, how do i use them?

Hello everybody!

Im trying to do some magic with a bunch of buttons, connected to a handful of i2c pin extenders.

I read all the buttons at once as a readGPIO so in the beginning of the doButtons function, i have 3 buffers,
_bufA, _bufB and _bufC.

i then have a stuct of aButton, with members like device, pin and vector (what function to modify).
This aButton struct gets instanced as a array, to keep track of all the buttons.

So when im reading if my button is pressed, the structure looks something like this:

void doButtons() {

_bufA = mcpA.readGPIO(); // mcpA = Adafruit MCP23008 library
_bufB = mcpB.readGPIO();
_bufC = mcpC.readGPIO();

for (uint8_t i = 0; i < numButtons; i++) {
  switch (Button[i].device)
    case mcpA_addr: {
      if (bitRead(_bufA,Button[i].pin)) {
        Button[i].isPressed = true;
      }
      else {
        Button[i].isPressed = false;
      }
      break;
    }
    case mcpB_addr: {
      if (bitRead(_bufB,Button[i].pin)) {
        Button[i].isPressed = true;
      }
      else {
        Button[i].isPressed = false;
      }
      break;
    }
  }
}

Note that this code snippet is written from the top of my head and would not compile, its just to show how i work.. :slight_smile:

Now, if i could somehow translate the what buffer to bitRead from, it would eliminate a lot of extra code and maintenance..

Ive been trying to learn pointers, trying to implement a tutorial example into my problem, but as you might have guessed, im just failing harder.. :slight_smile:

So what im trying now is add a "uint8_t *buffer;" at the start of my example above, then planning a rewrite of the rest of the function, implementing something the switch below, with a single function reading any buffer and button. :slight_smile:

void doButtons() {
uint8_t *buffer;

_bufA = mcpA.readGPIO(); // mcpA = Adafruit MCP23008 library
_bufB = mcpB.readGPIO();
_bufC = mcpC.readGPIO();

for (uint8_t i = 0; i < numButtons; i++) {
   switch (Button[i].device) {
      case mcpA_addr: {
        _bufA = &buffer;
        break;
      }
      case mcpB_addr: {
        _bufB = &buffer;
        break;
      }
      case mcpC_addr: {
        _bufC = &buffer;
        break;
      }
   }
   if (bitRead(*buffer,Button[i].pin)) {
      Button[i].isPressed = true;
   } // and so on...
}

But when im finally getting my test_project.ino to compile, i get a bunch of warnings about:
"warning: invalid conversion from 'uint8_t** {aka unsigned char**}' to 'uint8_t {aka unsigned char}' [-fpermissive]"

and the refered code line is: "_bufA = &buffer;" and its counterparts in the switch cases.

So, how am i supposed to achieve this?

Any hints, suggestions or ideas? :slight_smile:

But when im finally getting my test_project.ino to compile,

Please post a complete sketch which demonstrates the problem

#include <Adafruit_MCP23008.h>
const uint8_t numButtons = 9; // the total number of buttons
const uint8_t numDevices = 4; // the total number of devices

  // device vectors
const uint8_t devHBULeft = 0;
const uint8_t devHBURight = 1;
const uint8_t devRear   = 2;
const uint8_t devTrailer = 3;
const uint8_t devArduino = 4;

  // button vectors
const uint8_t  btnPos   = 0;
const uint8_t  btnEx    = 1;
const uint8_t  btnLeft  = 2;
const uint8_t  btnRight = 3;
const uint8_t  btnHrnP  = 4;
const uint8_t  btnHrnL  = 5;
const uint8_t  btnBrake = 6;
const uint8_t  btnTrailrDetect = 7;


struct aInputButton {
  uint8_t device;
  uint8_t pin;
  uint8_t vector;
  bool isPressed;
  uint8_t clickCount;
  uint8_t clickType;
};
aInputButton Button[numButtons] = { // for each button, do: {device,pin,vector, isPressed, clickCount, clickType}
  {devHBULeft,  0,  btnPos,   false,0,0},
  {devHBULeft,  1,  btnEx,    false,0,0},
  {devHBULeft,  2,  btnLeft,  false,0,0},
  {devHBULeft,  3,  btnRight, false,0,0},
  
  {devHBURight, 0,  btnHrnP,  false,0,0},
  {devHBURight, 1,  btnHrnL,  false,0,0},
  {devHBURight, 2,  btnLeft,  false,0,0},
  {devHBURight, 3,  btnRight, false,0,0},
  
  {devTrailer,  0,  btnTrailrDetect,  false,0,0}
};
Adafruit_MCP23008 mcpA, mcpB, mcpC;
void setup() {
  mcpA.begin(0);
  mcpB.begin(1);
  mcpC.begin(2);
}
void doButtons() {
uint8_t *buffer;
uint8_t _bufA, _bufB, _bufC = 0;
_bufA = mcpA.readGPIO(); // mcpA = Adafruit MCP23008 library
_bufB = mcpB.readGPIO();
_bufC = mcpC.readGPIO();

  for (uint8_t i = 0; i < numButtons; i++) {
     switch (Button[i].device) {
        case devHBULeft: {
          _bufA = &buffer;
          break;
        }
        case devHBURight: {
          _bufB = &buffer;
          break;
        }
        case devTrailer: {
          _bufC = &buffer;
          break;
        }
     }
     if (bitRead(*buffer,Button[i].pin)) {
        Button[i].isPressed = true;
     } // and so on...
  }
}


void loop() {
  doButtons();
  delay(50);
}

Does compile, but gives warnings; warning: invalid conversion from 'uint8_t** {aka unsigned char**}' to 'uint8_t {aka unsigned char}' [-fpermissive]

This error points out line numbers 60, 64, 68.

Another one:
../test_pointer.ino:72:10: warning: 'buffer' is used uninitialized in this function [-Wuninitialized]
if (bitRead(buffer,Button.pin)) {*

  • ^*
_bufA = &buffer;

'_bufA' is a uint8_t. '&buffer' is a pointer to a pointer to a uint8_t. Seems like an odd assignment to try. There's two more similar assignments in your code.

gfvalvo:

_bufA = &buffer;

'_bufA' is a uint8_t. '&buffer' is a pointer to a pointer to a uint8_t. Seems like an odd assignment to try. There's two more similar assignments in your code.

Im not quite following you there..

I am trying to make "buffer" to read as _bufA, without having to copy _bufA to buffer.

Im trying to bitRead the pointer target (_bufA/_bufB/_bufC) and as i understand it, i need to assign the address of the selected target to my pointer "buffer".

So if im doing things wrong, how should i go about doing this?

So how would i use the "buffer" to select what _buf to read from?

Maybe something like:

buffer = &_bufA;

gfvalvo:
Maybe something like:

buffer = &_bufA;

Hehe yes, indeed!

I did find that part of the tutorial odd but i chalked it up to ignorance on my behalf..

But the code does work fine with the corrected pointers, so thanks alot folks!

Just finished with the outputs rewrite, things are looking a LOT better now! :slight_smile: