Constructing the name of variable, to use in Calculation

For general Flowcode discussion that does not belong in the other sections.
Post Reply
jay_dee
Posts: 198
http://meble-kuchenne.info.pl
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 75 times
Been thanked: 54 times

Constructing the name of variable, to use in Calculation

Post by jay_dee »

Hi,
If I have a series of defined variables.
Str1
Str2
Str3
Str4
Is there a way I can progrmatically call these sequentially within a loop ( similar to how I might work though an array)

for example,
Str+1 = "MyText" + index
and loop through my series of variable?

I did look at trying a multi dimentional byte array Byte[20][4] but it got pretty messy quickly!

Edit: I'm assuming you cant create a String Array?
J.

mnfisher
Valued Contributor
Posts: 1460
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 712 times

Re: Constructing the name of variable, to use in Calculation

Post by mnfisher »

Hi Jay,

There are ways to do this - multi-dimensional array probably being the easiest..

byte x[10][20] - so x[1] is 'equivalent to str1 x[2] is str2 etc
I think you can use strings - but the size would be fixed (you can't then say str[1] = str[1] + "extra bit") - note this is really a multi dimensional array (char x[10][20])
(You can then pass x[1] as an argument to a macro)

Flowcode doesn't really support pointers but another route would be have a array of indexes (or even just a fixed multiplier) into a large array - so byte x[500] and then x[0] is str1 x[50] is str2 etc however - now you might be wasting some space and again you can't use string manipulations or even pass 'strings' as arguments without extra work..

If speed is less important - use a single 'large string' with some form of end-of string marker and iterate over the array to find required string...

The 'pointer' route - would involve a bit of C and uint str[10] - then need to assign each pointer using str[n] = malloc(sizeof string) - which will need to be done using C.

What are you trying to do? A 'table' of user prompts - or other?

Martin

jay_dee
Posts: 198
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 75 times
Been thanked: 54 times

Re: Constructing the name of variable, to use in Calculation

Post by jay_dee »

Hi, I've have a look through that info, thanks.

backgorund on project..
I have a PIC sending a UART stream of values,
As a starting point, I have a header Byte : seperators , and Footer byte ;
with the actual data values slotted in and sent as text strings.

I'm trying to use FAD to reliably recieve and break out each message into its constituant parts. I want the reciever to work with a wide range of streams, so I dont have fixed number of values or fixed string lengths.

Currently I'm breaking the stream into holding strings... str1, str2, str3 etc...
J.

mnfisher
Valued Contributor
Posts: 1460
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 712 times

Re: Constructing the name of variable, to use in Calculation

Post by mnfisher »

Sounds okay - how are you marking the end of strings sent (sending a length before or a 'termination character' after such as 0x0D or 0x0A)

Also how do you want to process the strings - will they be accessed sequentially (process and delete first string .. repeat) or in a 'random' manner (process string '1', process string '2' process string '4' - delete strings 1,2 & ?... repeat)

So - thinking out loud -

It's a almost a circular buffer of strings ? - unless strings are 'used up' at some point you will have to start overwriting earlier data (as RAM will be limited).
Speed shouldn't be too much of an issue as UART can only work at a fixed speed (115200 or 230400?)
Possibly an array of indexes to start of strings in a 'large' buffer - and it might be necessary to 'copy' the string out to process it -
str = GetAndDelete(strIndex) or str = GetDelete() (just returns first string from buffer)

Martin

jay_dee
Posts: 198
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 75 times
Been thanked: 54 times

Re: Constructing the name of variable, to use in Calculation

Post by jay_dee »

Yep using a ";" to mark the end of the message string.

Possibly bit of a side track to the original question but once you see how its used... or maybe I'm coming from the wrong direction!
Value_Monitor_V2-1.fcsx
(90.82 KiB) Downloaded 409 times
very much a work in progress, I've got a fairly reliable retrieval of the message string from the cirular buffer. Using ":" and ";" to denote the start and end of the message.

I then have been playing with splitting the string. from example "12,34,56,100" into seperate strings in thier own right.
the method used works...well, kind of!

The method sequentially works through the received string.
RxChar = RxString[index] and looks for the seperator character ","

If found I use a 'switch' component to move to the next 'results string' str1... str2... str3... etc...
If any other character is is added to the exisintg results string. Str1 = Str +RxChar.

problem is I'm getting the ASCII values, not the ASCII charater? Hmm.. so characters '12' from the source string are turning to values '49 50'

Maybe i'm wrongly converting them somewhere, mis-using a function, a component displays the raw values by default. this has stumped me for a few hours. J.

mnfisher
Valued Contributor
Posts: 1460
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 712 times

Re: Constructing the name of variable, to use in Calculation

Post by mnfisher »

I think the way to go is an array of strings..

I had a little play - and here is a simple example - the 'receiver' receives strings (',' between strings up to max of 10 and ':' as end marker) - and displays them on static texts (staticText[0..9]) I don't have a start marker (and I make no allowance for errors....) Changing the loop counter in Data Send from 9 to random() % 10 will show receiving variable number of data items...
Note that these are 'strings' so can be numeric (send as '1234') or text data...

I not sure I like the idea of using a 'timer' interrupt to receive data on the UART - I feel there should be a Rx interrupt even in app developer - but it does work :-) Is there a correct way - or just try to receive and ignore 255??

The sender - here sending 'Hello world + random numbers' for 9 and and 1234 for last string - i played with various strings and seems okay.. I'm just using an Arduino that was to hand - just something to put data to UART so anything should do...

If you need a numeric version of a string you'll need to do something like .x = StringToInt$(data[n]) (and have valid numeric value...)
Data Send.fcfx
(10.13 KiB) Downloaded 426 times
Data disp.fcsx
(30.81 KiB) Downloaded 458 times
Martin

jay_dee
Posts: 198
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 75 times
Been thanked: 54 times

Re: Constructing the name of variable, to use in Calculation

Post by jay_dee »

Thanks Martin, Let me work though your example.
I know I start with on big load of FC and chop it into Macros during my clean up phase but your approach does look cleaner.
J.

mnfisher
Valued Contributor
Posts: 1460
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 712 times

Re: Constructing the name of variable, to use in Calculation

Post by mnfisher »

Hope it helps - any questions please feel free to ask..

one 'bug' - in ProcessChar

need to replace
cur_str = cur_str + 1 % 10
with
cur_str = (cur_str + 1) % 10

To avoid 'overflow errors' if it gets send more than 10 strings...

Martin

Post Reply