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..
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..
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.
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?