VS Arduino
>> >> Ace Button library example won't compile in VM
https://www.visualmicro.com/forums/YaBB.pl?num=1570060795

Message started by paulsoulsby on Oct 2nd, 2019 at 11:59pm

Title: Ace Button library example won't compile in VM
Post by paulsoulsby on Oct 2nd, 2019 at 11:59pm
Hi I'm using AS7 with VM 1909.27.0 and Arduino 1.8.5
I want to use the Ace Button library (https://github.com/bxparks/AceButton) and am currently playing with the examples.

I have opened the ArrayButtons.ino example. It compiles and works fine in Arduino, but throws following errors in VM:

Code (]Error compiling project sources
ArrayButtons.ino: 17:18: error: variable or field 'handleEvent' declared void
Debug build failed for project 'ArrayButtons'

ArrayButtons.ino: 17:18: error: 'AceButton' was not declared in this scope
ArrayButtons.ino:17: note  suggested alternative

AceButton.h:38: In file included from
ArrayButtons.ino:11: from
AceButton.h:50: note    ace_button  AceButton
   class AceButton {

ArrayButtons.ino: 17:29: error: 'button' was not declared in this scope

ArrayButtons.ino: 17:45: error: expected primary-expression before 'eventType

ArrayButtons.ino: 17:64: error: expected primary-expression before 'buttonState[/code):



Error is thrown by the  event handle.  If I remove the event handle and its prototype (and lines referencing it) the source code compiles.

I have seen similar posts that required the prototype adding at the top of the code.  However the prototype is clearly present in this case. 

Is it a parsing order difference between Arduino and VM?  Any way to fix?

Here's the code:

[code c++]/*
* A demo of an array of 6 AceButton objects controlling an array of 6 LEDs. If
* you have more than about 8 buttons or so, it might be worth using a
* column/row button scanner instead. (Unfortunately AceButton does not support
* that right now, maybe in the future).
*
* WARNING: This has been verified to compile, but I have not tested it with
* actual hardware.
*/

#include <AceButton.h>
using namespace ace_button;

// LED states. Some microcontrollers wire their built-in LED the reverse.
const int LED_ON = HIGH;
const int LED_OFF = LOW;

// Number of buttons and LEDs.
const uint8_t NUM_LEDS = 6;

// Helper struct that keeps track of the buttons and leds. An alternative is to
// use multiple arrays (e.g. BUTTON_PINS, LED_PINS, LED_STATES) which has the
// potential of reduing memory using PROGMEM, but for small to medium number of
// buttons, using a struct makes the program easier to read.
struct Info {
  const uint8_t buttonPin;
  const uint8_t ledPin;
  bool ledState;
};

// An array of button pins, led pins, and the led states. Cannot be const
// because ledState is mutable.
Info INFOS[NUM_LEDS] = {
  {2, 8, LED_OFF},
  {3, 9, LED_OFF},
  {4, 10, LED_OFF},
  {5, 11, LED_OFF},
  {6, 12, LED_OFF},
  {7, 13, LED_OFF},
};

// Define the buttons in an array using the default constructor. A static
// initializer array using { {int, int, int}, ...} does not seem to work,
// probably because of the 'explicit' keyword. So use the init() method in
// setup() instead. This has the disadvantage of invoking the default
// constructor NUM_LEDS times, but this is no worse than explicitly declaring
// NUM_LEDS instances of AceButton.
AceButton buttons[NUM_LEDS];

void handleEvent(AceButton*, uint8_t, uint8_t);

void setup() {
  delay(1000); // some microcontrollers reboot twice
  Serial.begin(115200);
  while (! Serial); // Wait until Serial is ready - Leonardo/Micro
  Serial.println(F("setup(): begin"));

  for (uint8_t i = 0; i < NUM_LEDS; i++) {
    // initialize built-in LED as an output
    pinMode(INFOS[i].ledPin, OUTPUT);

    // Button uses the built-in pull up register.
    pinMode(INFOS[i].buttonPin, INPUT_PULLUP);

    // initialize the corresponding AceButton
    buttons[i].init(INFOS[i].buttonPin, HIGH, i);
  }

  // Configure the ButtonConfig with the event handler, and enable all higher
  // level events.
  ButtonConfig* buttonConfig = ButtonConfig::getSystemButtonConfig();
  buttonConfig->setEventHandler(handleEvent);
  buttonConfig->setFeature(ButtonConfig::kFeatureClick);
  buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
  buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
  buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);

  Serial.println(F("setup(): ready"));
}

void loop() {
  // Should be called every 4-5ms or faster, for the default debouncing time
  // of ~20ms.
  for (uint8_t i = 0; i < NUM_LEDS; i++) {
    buttons[i].check();
  }
}

// The event handler for the button.
void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) {

  // Print out a message for all events.
  Serial.print(F("handleEvent(): eventType: "));
  Serial.print(eventType);
  Serial.print(F("; buttonState: "));
  Serial.println(buttonState);

  // Get the LED pin
  uint8_t id = button->getId();
  uint8_t ledPin = INFOS[id].ledPin;

  // Control the LED only for the Pressed and Released events.
  // Notice that if the MCU is rebooted while the button is pressed down, no
  // event is triggered and the LED remains off.
  switch (eventType) {
    case AceButton::kEventPressed:
      digitalWrite(ledPin, LED_ON);
      INFOS[id].ledState = LED_ON;
      break;
    case AceButton::kEventReleased:
      digitalWrite(ledPin, LED_OFF);
      INFOS[id].ledState = LED_OFF;
      break;
  }
}


Title: Re: Ace Button library example won't compile in VM
Post by Visual Micro on Oct 3rd, 2019 at 10:58am
Thanks for the clear example. The prototypes need to be n exact atch for the signature of the corresponding function.

[code]void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState);[/code]

Does this help?

Title: Re: Ace Button library example won't compile in VM
Post by paulsoulsby on Oct 3rd, 2019 at 11:13am
That's perfect thanks!  All working now.

Title: Re: Ace Button library example won't compile in VM
Post by David Newberry on Dec 29th, 2020 at 4:08am
**Update: Fix mentioned doesn't work

This issue still exists with AceButton but only in debug mode.  If you load up any AceButton example and compile in debug mode you will get:

Error compiling project sources
AceButton.h:45: In file included from
Debug build failed for project 'ClickVersusDoubleClickUsingSuppression'
ClickVersusDoubleClickUsingSuppression.ino:22: from
AceButton.h:51: note    ace_button  AceButton
   class AceButton {

ClickVersusDoubleClickUsingSuppression.ino: 26:29: error: expected primary-expression before ',' token

ClickVersusDoubleClickUsingSuppression.ino: 26:39: error: expected primary-expression before 'eventType

ClickVersusDoubleClickUsingSuppression.ino: 26:58: error: expected primary-expression before ')' token


The issue seems to be with adding the ace_button namespace.  If you replace


Code (c++):
using namespace ace_button;


with


Code (c++):
using ButtonConfig = ace_button::ButtonConfig;
using AceButton = ace_button::AceButton;


then the example will compile just fine.  I can't even comprehend where the error comes from because it references lines in the .ino file that don't match the error provided.

Title: Re: Ace Button library example won't compile in VM
Post by David Newberry on Dec 29th, 2020 at 4:46am
Turns out that I jumped ahead and the using statements I mentioned don't work.  I had to edit the example and explicitly put the namespace on each ace_button Class as follows to make it work:


Code (c++):
/*
* A demo that uses kFeatureSuppressClickBeforeDoubleClick to distinguish a
* Clicked event from a DoubleClicked event. Click turns on the LED. A
* DoubleClick turns off the LED.
*
* The only way to suppress the Clicked "after" a DoubleClicked is to postpone
* the sending of the Clicked event until getDoubleClickDelay() time after the
* Clicked. At that time, we can tell if a DoubleClicked has occurred or not.
* But this means that every Clicked event is delayed by (kClickDelay +
* kDoubleClickDelay + 2 * kDebounceDelay) which is 700 ms using the default
* values, and you'll notice this delay in the LED turning on.
*
* The other side-effect is that if a user doesn't input a clean Click (which
* results in a normal Press/Release sequence), then nothing happens to the LED.
* Depending on the application, this may or may not be the desirable result.

* See Also:
*    examples/ClickVersusDoubleClickUsingReleased/
*      - uses the Released event instead of the Clicked event
*/

#include <AceButton.h>
//using namespace ace_button;

// The pin number attached to the button.
const int BUTTON_PIN = 2;

#ifdef ESP32
  // Different ESP32 boards use different pins
  const int LED_PIN = 2;
#else
  const int LED_PIN = LED_BUILTIN;
#endif

// LED states. Some microcontrollers wire their built-in LED the reverse.
const int LED_ON = HIGH;
const int LED_OFF = LOW;

// One button wired to the pin at BUTTON_PIN. Automatically uses the default
// ButtonConfig. The alternative is to call the AceButton::init() method in
// setup() below.
ace_button::AceButton button(BUTTON_PIN);

void handleEvent(ace_button::AceButton*, uint8_t eventType, uint8_t);

void setup() {
  // initialize built-in LED as an output
  pinMode(LED_PIN, OUTPUT);

  // Button uses the built-in pull up register.
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  ace_button::ButtonConfig* buttonConfig = button.getButtonConfig();
  buttonConfig->setEventHandler(handleEvent);
  buttonConfig->setFeature(ace_button::ButtonConfig::kFeatureDoubleClick);
  buttonConfig->setFeature(
      ace_button::ButtonConfig::kFeatureSuppressClickBeforeDoubleClick);
}

void loop() {
  // Should be called every 20ms or faster for the default debouncing time
  // of ~50ms.
  button.check();
}

// The event handler for the button.
void handleEvent(ace_button::AceButton* /* button */, uint8_t eventType,
    uint8_t /* buttonState */) {
  switch (eventType) {
    case ace_button::AceButton::kEventClicked:
      digitalWrite(LED_PIN, LED_ON);
      break;
    case ace_button::AceButton::kEventDoubleClicked:
      digitalWrite(LED_PIN, LED_OFF);
      break;
  }
}

VS Arduino » Powered by YaBB 2.6.12!
YaBB Forum Software © 2000-2024. All Rights Reserved.