V5 C++ VEXCode Text - Questions

Hi - a bit of a newb w/some VEXCode Text programming questions:
A - when trying to find example code for non-VEX code/libraries what should we specify as the language? I know it’s C++ but it seems all of the hits are for either for Windows or Linux - which doesn’t seem to work

B - We’d like to move our functions from main.cpp to another file (functions.cpp). I see how to add a .cpp or .h file by right-clicking on ‘src’ but unsure how to make it’s content ‘public’ for main.cpp to access.

Thanks!

1 Like

A:
If you are trying to copy-paste non-vex code into your code then yes, it has to be c++ since that’s the language VEXcode is in. Why don’t you just translate it to C++ instead of trying to copy-paste a specific c++ example? Seems kind of sketch

B:
Two methods:

  1. In main.cpp (before you use the functions) add a line declaring the function like so:

    void function(int input);
    This pretty much tells your code to look in the other files for the function.

  2. at the start of main.cpp add #include 'filename.cpp' (note there are no semicolons).
    This says ‘copy what’s in this file directly into this file’.

When using these make sure you are saving files before trying to call the function in a different file. It’s not fun when you search for an error endlessly and then realized you just had to ctrl+shift+s

EDIT: also if you are using vex stuff in your new file make sure there you include vex.h/robot-config.cpp and use namespace vex so you get your motor instances and vex classes

1 Like

What do you mean by “code for non-VEX code/libraries”. Vex code uses c++ all the fundamentals are the same. If you are trying to find out about vex function here is VEXcode API Reference.

With c++ to access stuff defined in other files you need to include them. However it isn’t that simple, since the compiler looks at everything in src folder. Like @Deicer said you need to add a forward declaration of the function like this…

void function(int input);

or for varaibles…

extern int variable;

Including the file, where the function is actually defined will cause an error multiple definition of 'function(int)'. If the files is in the src folder, a forward declaration is all you need. The convention is to put these forward declarations in a header file, located in the include folder and include that file instead of put all the forward declarations in the main file.

Also you should use include guards and include the header file in the complimentary cpp file. This isn’t necessary for only function, but is sometimes need when creating classes to make things easier and prevent redefinition. Either way, you can just add it to not worry about that later. Include guard…

#pragma once
4 Likes

Awesome info Deicer - THANKS!

re: A - Sorry 4 not being clear. Right now I am trying to keep a history of the motor current/temp . When I look for ways handle files (check for, rename, append) the examples I find seem to use libraries from other systems (VBasic, POSIX) - I ‘assume’ VEX won’t run

B - SO COOL - THANKS!

Good point, to clarify my post, do one of the listed options, not both simultaneously

1 Like

You still cannot include other files in src, even if you don’t have a forward declaration. The reason for the error is that the compiler already looks at all the files in src folder.

2 Likes

Sorry 4 not being clear.

Right now I am trying to keep a history of the motor current/temp . When I look for ways handle files (check for, rename, append) the examples I find seem to use libraries from other systems (VBasic, POSIX) - I ‘assume’ VEX won’t like

I found this which makes sense but doesn’t help w/file handling.

I gather I want to keep file size small. Hoping to either append/clear files or use incremental filename

1 Like

The sd card class seems very limited. I did find some functions in v5_api file to do more.

SD card functions in v5_api
// SD card
 FRESULT               vexFileMountSD( void );
 FRESULT               vexFileDirectoryGet( const char *path, char *buffer, uint32_t len );
 FIL                  *vexFileOpen(const char *filename, const char *mode );
 FIL                  *vexFileOpenWrite(const char *filename);
 FIL                  *vexFileOpenCreate(const char *filename);
 void                  vexFileClose(FIL *fdp);
 int32_t               vexFileRead( char *buf, uint32_t size, uint32_t nItems, FIL *fdp );
 int32_t               vexFileWrite( char *buf, uint32_t size, uint32_t nItems, FIL *fdp );
 int32_t               vexFileSize( FIL *fdp );
 FRESULT               vexFileSeek( FIL *fdp, uint32_t offset, int32_t whence );
 bool                  vexFileDriveStatus( uint32_t drive );
 int32_t               vexFileTell( FIL *fdp );

I don’t know too much about this, so perhaps someone could provide better help.

2 Likes

OMG all - (B) works - I had some issues w/motor_group not being recognized but found I just had to make an ‘extern’ declaration in robot-config.h

One lingering question re:B

why does
void functions(int input)
have a parameter?

THANKS SO MUCH!!!

That’s just an example. If your function is ‘void drive()’ then you’d just put that.

Sometimes functions have params like ‘double drive(int distance, char units)’ and you have to remember that when adding it to main

OK - took some thinking but think I got it

functions.cpp - contains all of the methods()
main.cpp - must have ‘prototypes’ all of the methods() in functions.cpp

One last issue is how do I make a variable global so we can set it on main.cpp but function.cpp knows about it?

AWESOME!!

(I edited my first post with the answer earlier)
Similar to function you, but you have to use the keyword extern for the forward declarations (or prototypes)…

extern int variable;

Also I would recommend putting the forward declaration (or prototypes) in a seperate header file. This doesn’t change anything but makes it easier to read the files.

1 Like

I think you mentioned it earlier and there’s a good example of it in robot-config.h

In an included header file add extern type var;
example: extern int distance;

oop codec beat me to it

1 Like

thanks all - so cool !

…now about C++ & file mgt…

I’m still not sure what your question is. VEXcode uses C++ so therefore if you want to use VEXcode you have to use C++.

^You seem to have the documentation for the SD cards and Codec sent all of the SD card functions that I know of.

https://api.vexcode.cloud/v5/html/classvex_1_1brain_1_1sdcard.html
https://api.vexcode.cloud/v5/html/classvex_1_1brain.html#ab8b81fb00ce566b0837c30f176ffc7ed

2 Likes

yea, if I can’t describe a problem sure can’t expect ppl to help… :slight_smile:

I’m trying to keep a log of motor conditions & am stuck on how to keep the file small

I (kind of) get the ofstream stuff - just trying to figure a way to backup/clear the log file every time the Brains starts…

that’s likely just a matter of formatting it differently and writing to the file less often

when the program starts:

if(file exists){
    delete it
}
1 Like

Yes, thanks - it looks like ofstream has flags that can both append and clear

So I’m thinking

  • history.txt - all historical data (H)
  • log.txt - data since startup (L)

1 - open H in append mode
2 - go to H eof
3 - H << L (read L into H)
3 - close H
4 - del L or open overwrite-mode (?)
5 - close L
loop(10s){
6 - open L (append mode)
5 - write log
6 - close L

I need to check, but I think delete file is deliberately not enabled. I didn’t want bad code accidentally deleting all files on an SD Card. But opening an existing file for write will start over and old data would be removed. Open with append to keep existing data.

5 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.