Return array from macro

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

markavo
Posts: 33
Joined: Wed Apr 11, 2018 11:10 pm
Has thanked: 10 times
Been thanked: 2 times
Contact:

Return array from macro

Postby markavo » Sun Jun 03, 2018 2:05 pm

No sure if this is something I’m missing please advise

In flowcode 8 when setting a return as a byte I then have the options (when clicking edit) to add an array to the .return once I assign for example [2] click ok no errors but when I come to use the return var I get an error. The error is saying I can not assign an array to the calculation due to the return not being an array.

What’s the correct procedure to return an array from a macro please as I can assign one to the .return.

If this is not possible what’s the best way as I was going to convert numbers to a string add them to return and split the string again the other side after the return is returned.

Thanks

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1184
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 376 times
Been thanked: 601 times
Contact:

Re: Return array from macro

Postby QMESAR » Sun Jun 03, 2018 2:50 pm

Hi.

With out seeing your Flowchart.
I would expect that you return a value to an element of an array not the complete array
I would think it would be something like this

.return = Array[1] or Array[n]
place a flowchart for better help from us :D
These users thanked the author QMESAR for the post:
markavo (Sun Jun 03, 2018 3:20 pm)
Rating: 5.26%
 

mnf
Valued Contributor
Valued Contributor
Posts: 621
Joined: Wed May 31, 2017 11:57 am
Has thanked: 56 times
Been thanked: 340 times
Contact:

Re: Return array from macro

Postby mnf » Sun Jun 03, 2018 2:58 pm

Unfortunately you can't return an array from a macro - a result of the underlying C language.. In C - you can allocate the memory for the array (using 'new') and return a pointer to it - but at some point your program needs to take responsibility and delete the memory used....

One way around this is to pass an array to store the results by reference - Flowcode only shows this as an option for strings (untick the 'create local copy' box - however arrays of other types seem to be passed by reference anyway (which is good - you probably want this in most cases - just don't modify values in the array and expect this to be a local (to the macro) copy)

A simple program that demonstrates this:

array.fcfx
(10.14 KiB) Downloaded 62 times


Better make sure that your array is big enough... Flowcode actually also passes the array size (in this case as FCLsz_X) - which you could access using a C icon (and a new local variable I've called len) Fortunately Flowcode ignores the size in the macro definition - so you can pass any size array to your function macro without it 'knowing' the size in advance..

array2.png
array2.png (22.32 KiB) Viewed 1562 times


Martin
Last edited by mnf on Sun Jun 03, 2018 3:22 pm, edited 1 time in total.
These users thanked the author mnf for the post (total 2):
markavo (Sun Jun 03, 2018 3:17 pm) • medelec35 (Sun Jun 03, 2018 3:49 pm)
Rating: 10.53%
 

markavo
Posts: 33
Joined: Wed Apr 11, 2018 11:10 pm
Has thanked: 10 times
Been thanked: 2 times
Contact:

Re: Return array from macro

Postby markavo » Sun Jun 03, 2018 3:20 pm

Perfect thank you!!!

Great example.

mnf
Valued Contributor
Valued Contributor
Posts: 621
Joined: Wed May 31, 2017 11:57 am
Has thanked: 56 times
Been thanked: 340 times
Contact:

Re: Return array from macro

Postby mnf » Sun Jun 03, 2018 4:52 pm

For fun:

I wrote a small program to 'compare' the speed of a Raspberry Pi and an Arduino (in my case a Nano (328p)).

So a simple bubble sort - using array passing as above.

bubble.fcfx
(12.21 KiB) Downloaded 63 times
Sorts 900 numbers - nearly the max the RAM can hold
pibubble.fcfx
(12.83 KiB) Downloaded 64 times
- sorts 20000 numbers (it just displays first 800 sorted numbers at the end) (and the Pi barely raises a sweat!)
(I use UART for output on Nano and the Console on the Pi)

and MCU's are so much quicker than I was first learning to program (on a ZXSpectrum)! So although I still wouldn't use Bubble Sort as my algorithm of choice - if you've got a data set and don't need to sort it often then performance isn't as bad as I remembered....

A couple of gripes:

Still can't allocate an array using a constant as the size in Flowcode v8 :-(

Random on the Arduino doesn't seem to generate negative results (it does on the Pi)

+ a question:

Is it possible to 'compile to chip' programs to the Pi without running them? (just compile and upload)

Left as an exercise - repeat the randomise/sort 100 times to make the timings more meaningful (and add times - to save using a stopwatch :-) )

Martin

User avatar
LeighM
Matrix Staff
Posts: 1962
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 474 times
Been thanked: 674 times
Contact:

Re: Return array from macro

Postby LeighM » Mon Jun 04, 2018 9:01 am

Is it possible to 'compile to chip' programs to the Pi without running them? (just compile and upload)

This is possible by creating a copy of the rprog.bat file found in the \Compilers\RPI\batch directory.
Then edit the copy to remove the "@start ..." line from the "Running" section.
Create a new custom Compiler Options config to invoke your version of rprog.bat

It is worth noting that the downloaded program remains on the target Pi,
so with the standard programmer you can kill the version that auto runs by pressing Ctrl-C in the putty console window.
These users thanked the author LeighM for the post:
mnf (Mon Jun 04, 2018 6:02 pm)
Rating: 5.26%