Welcome, Guest. Please Login or Register
Arduino IDE for Visual Studio
 
 
When Logging a Support Issue in the Forum, please ensure you have also:-
  • Enabled vMicro > Compiler > Show Build Properties and Verbose
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
Support requests without the output above may be impossible to answer, so please help us to help you
 
  HomeSearch Tutorials & Wiki visualmicro.com LoginHelpRegister  
 
Page Index Toggle Pages: 1
Send Topic Print
Ace Button library example won't compile in VM (Read 140 times)
paulsoulsby
Newbies
*
Offline



Posts: 2
Ace Button library example won't compile in VM
Oct 3rd, 2019 at 12:59am
 
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 



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;
  }
}
 


Back to top
« Last Edit: Oct 3rd, 2019 at 10:26am by paulsoulsby »  
 
IP Logged
 
Visual Micro
Administrator
*****
Offline



Posts: 10623
United Kingdom
Re: Ace Button library example won't compile in VM
Reply #1 - Oct 3rd, 2019 at 11: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); 



Does this help?
Back to top
 
WWW  
IP Logged
 
paulsoulsby
Newbies
*
Offline



Posts: 2
Re: Ace Button library example won't compile in VM
Reply #2 - Oct 3rd, 2019 at 12:13pm
 
That's perfect thanks!  All working now.
Back to top
 
 
IP Logged
 
Page Index Toggle Pages: 1
Send Topic Print