you can.
I’ve never really talked or explained events much. We use them internally for things like button presses using function on other classes, but you can create you own events and choose when to use them. For programmers coming from blocks, this should be quite familiar, although many of the places they are used in blocks should really be using function calls instead.
if you have a function like this.
void
eventHandlerA() {
static int event_count = 0;
Brain.Screen.printAt( 10, 90, "Event A %d", event_count++ );
}
you can register that as an event in this way
vex::event myUserEvent( eventHandlerA );
and then cause the callback to run using the broadcast function.
myUserEvent.broadcast();
more than one event handler can be assigned to a single event, see the demo at the end of the post for that.
An “mevent” is a little different, it was designed for use in a polled environment where using the pressing() method may not work well, either because you missed the button being pushed or because you used it in a loop and were only interested in the fact that the button was pushed and didn’t want to add the additional logic to filter out the fact it may stay in that state for a while. They were intended for use by the blocks part of VCS, but in fact were never used in that way.
It makes a good replacement if you find yourself doing this a lot.
if( Controller1.ButtonA.pressing() ) {
while( Controller1.ButtonA.pressing() )
this_thread::sleep_for(10);
// do something
}
or this
static int isPressed = 0;
if( Controller1.ButtonA.pressing() && !isPressed ) {
isPressed = true;
// do something
}
else
if( !Controller1.ButtonA.pressing() ){
isPressed = false;
}
Both of those can be replaced by
if( Controller1.ButtonA.PRESSED ) {
// do something
}
Here’s a small demo using both ideas, they happen to be combined in this demo but don’t have to be.
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: james */
/* Created: Mon Feb 03 2020 */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/
#include "vex.h"
using namespace vex;
// A global instance of vex::brain used for printing to the V5 brain screen
vex::brain Brain;
// define your global instances of motors and other devices here
vex::controller Controller1;
void
eventHandlerA() {
static int event_count = 0;
Brain.Screen.printAt( 10, 90, "Event A %d", event_count++ );
}
void
eventHandlerB() {
static int event_count = 0;
Brain.Screen.printAt( 10,110, "Event B %d", event_count++ );
}
int main() {
int count = 0;
// create a user event
vex::event myUserEvent( eventHandlerA );
// add second callback to the same event
myUserEvent( eventHandlerB );
while(1) {
Brain.Screen.printAt( 10, 50, "Hello V5 %d", count++ );
// this uses an mevent, it will act like a one shot
// that is, it will only happen once when the button is pressed
if( Controller1.ButtonA.PRESSED ) {
// trigger the other event handlers, both callbacks will run
myUserEvent.broadcast();
}
// Allow other tasks to run
this_thread::sleep_for(10);
}
}