Thank you so much, your code does what I needed. I just had to adapt it to my naming scheme and add a member to my Button struct. I wasn't able to follow its logic at 100%, but at least, it made some sense!
Here is how my function and my new struct turned out to be, in case anyone is following this thread.
struct Button
{
const uint8_t pin:7;
const uint8_t weight:7;
bool pressDetected:1;
bool longPressDetected:1; /*this is new*/
bool state:1;
unsigned long timestamp;
};
int pollButtonNB (struct Button* button)
/* Non-blocking version */
{
unsigned long currentTime = millis();
bool buttonPressed = digitalRead(button->pin) == PRESSED;
if (buttonPressed != button->pressDetected &&
currentTime - button->timestamp > DEBOUNCE)
{
button->timestamp = currentTime;
button->pressDetected = buttonPressed;
if (button->pressDetected)
// Button was just pressed
button->longPressDetected = false;
else if (! button->longPressDetected)
// Button was just released
return button->weight;
}
if (button->pressDetected && ! button->longPressDetected &&
currentTime - button->timestamp > LONG_PRESS)
{
button->longPressDetected = true;
return 0 - button->weight;
}
return 0;
} //end pollButtonNB()