VEXcode for experts

That is the reason, but in the case of the v5 it usually doesn’t apply since you really aren’t running much, and the important task like motors and communication are handled on other cpu’s. It is probably the reason and good practice but not too significant.

They are required for a multi line macro (#define). Since it isn’t terminated with a semicolon it ends when there is a new line, unless there is a back slash.

3 Likes

The back slash is line continuation, it lets you define a macro over several lines as opposed to having to write it like this.

#define waitUntil(condition) do { wait(5, msec); } while (!(condition))

Two files ? If you mean two projects then use “new window” and then in the new window open the second project. If you meant two files, you should just be able to click on any file in the directory tree and it will open in a new tab.

6 Likes

Thanks :-). Owe you a chocolate macnut.

2 Likes

Example code for the motor group and drivetrain classes is here.

2 Likes

I saw this comment in our feedback system

task::sleep was eliminated, causing a lot of pain

I want to reaffirm that nothing has been removed from the SDK with the VEXcode text 1.0 release.

5 Likes

I understand having multiple source files open at the same time, but It was also stated that “You can have multiple projects open at the same time”. When I try to open a new project, it closes the first project. How do you have multiple projects open at the same time? Do you just do multiple windows or can you do them all in the same window?

2 Likes

One project per window. So yes, open up a new window and then open the project.

2 Likes

can we use vex::event or mevent ?

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);
    }
}
8 Likes

Are you able to pass a port as a parameter in a function? If so, how and would it need to be passed by reference? With the season coming to an end soon, we want to spend some more time on programming and help the kids build function libraries. We would like to help them build a function, such as:

void turn(int degree, port p){
pid controls to make a turn based on the degrees passed in and the port specified.
}

This would allow reusability for any port if we could simply call up port p in the function and have it call the port passed into it.

Why not just pass a reference to the motor instead?

This seems like the thing to do to me.


But, if you really want to pass port numbers to functions, there is no special type for ports - the constants PORT1 through PORT22 (yes, port 22, that’s where all the three-wire ports live) are just int32_ts.

So, you could write a function like the following:

void doSomething(int32_t port){
    motor Motor(port);
    //some code to do stuff with the motor you just instantiated goes here
}

and then call it on whatever port you want:

doSomething(PORT3);

But it seems kind of wasteful to have to initialize a new motor object every time you want to move a motor - so I agree it would be a better idea to pass a reference to the motor instead like so:

void doSomething(motor& Motor){
    //some code to do stuff with Motor goes here
}

int main(){
    motor Motor1(PORT1);
    doSomething(Motor1);
}
7 Likes

Perfect. I actually wanted to use a reference, but I was unsure if you could pass a motor type. Thanks so much.

1 Like

Brain.Screen.yPosition() . Brain.Screen.xPosition(); update rate 200ms??? can it be faster?

Only if we changed vexos, and we have no plans for that.

5 Likes

thx, can i code to clear vexcode terminal contents printed by cout?

can i code to clear vexcode terminal contents printed by cout??

you cannot cause the VEXcode terminal to clear from a user program, the only way to clear the terminal is to click the “trashcan” icon in the top right corner of the terminal panel.

2 Likes

what is the file order on the left side of vexcode?? disorder will occur when i add new files or directory…
thx!

We give priority to one or two of the vex build files, otherwise I think it’s just the order they are added to the project.

3 Likes