Page 1 of 2

string manipulation

PostPosted: Tue May 21, 2013 11:50 pm
by JB_AU
In my attachment i am trying to manipulate user input as string variables which will be used to manipulate io control.
My arduino sketch for this system is 2kb. I am trying to build a universal io interface.

From left to right, the first selector allows the user to choose the type of control needed, the next selector depending on the value of the first, makes available the required input/outputs available.
This information is sent to an edit box, which ideally sets the led red until all command requirements are met, and turns green to signify argument is ready to be sent from the button, where required feedback is written to the final edit box.

How do i keep the led red until needed?
All read requirements are thus far met, How would i set the led green?
Were a write argument requires a numerical value input, how can i accomplish this?

Thankyou

Re: string manipulation

PostPosted: Wed May 22, 2013 1:31 am
by MyCo
Hope this is what you mean... I commented in the schematic.

Re: string manipulation

PostPosted: Wed May 22, 2013 1:48 am
by RJHollins
Nice touch with the arrow pointers, MyCo !
:)

Re: string manipulation

PostPosted: Wed May 22, 2013 8:09 am
by JB_AU
As nice as that reply was, & thankful that i am.
The led state changes to green even when the statement is wrong, this would be my mistake, as i was expecting a verbal answer & not a full blown demonstration :ugeek:

For all statements of ar & dr are true, as no further input is needed, whereas aw & dw statements are false until the user has inputted the correct parameters.

"dw7" by itself would be false, led would equal red, until "dw7+" is supplied a user value, "dw7+1" , writing pin7 HIGH.

My apologies and thanks for not going into further detail from the first question.

Re: string manipulation

PostPosted: Wed May 22, 2013 5:41 pm
by MyCo
For me, it's often easier to build the schematic than explaining it in english (which is obviously not my native language). So here is a version, that has the Data added to the write commands and at the end it gets cheaply verified. When this does what you want, you can ask questions about the parts of the schematic that you don't understand. That's some kind of the other way around: First find a solution to your problem, and then understand the way this solution works :twisted: Sorry!

Re: string manipulation

PostPosted: Wed May 22, 2013 10:51 pm
by trogluddite
Another alternative to this problem would be to use 'regular expressions' inside a Ruby primitive.
Regular expressions are common to many programming languages, and are an extremely versatile way to parse any kind of text.
The downside is that they are very cryptic and take some learning - though there are many excellent resources out there on the web that can help with this.

First you need a Ruby editor primitive, and right click the output connector to choose the 'boolean' data type for the output. Then, the Ruby code for your example would look something like this...

Code: Select all
if @in =~ /[ad]r\d+|[ad]w\d+=\d+/
  output true
else
  output false
end


Guess you see now what I mean about cryptic"! ;) But the regular expression can be broken down into smaller chunks that are easier to undertand (I'll assume that the if...else part is fairly intuitive).

Firstly @in will be the string coming in at the input, and '=~' means "does this string match the character pattern'.
The regular expression is contained between the forward slashes (all the other kind of brackets are already used for other things!).
It then divides into two parts, with the 'pipe' character '|' in the middle. The '|' just means OR - the text must either match the bit before the '|' or the bit after it.

Part 1...
[ad]r\d+
[ad] means 'find any of the characters inside the brackets' - in this case either 'a' or 'd', though you can include as many as you need.
Then 'r', which simply means, 'the next character must be 'r'' (so this parts checks only for the read codes)
The backward slash means that the next item is a special character code - in this case '\d' means "any number character" (i.e. digit).
That's followed by '+', which means "find one or more of the previous character" - so that '\d' can find numbers with more than one digit.
So that takes care of the 'read' part - we find an 'a' or a 'd', followed by 'r', followed by an integer number (we didn't say to look for decimal points!).

The second half of the 'OR' looks pretty similar, the beginning...
[ad]w\d+
...is just like the 'pattern' for the read codes, but with 'w' substituted for 'r'.
Then there is '=' to check for an equals character separating the channel from the value that you want to assign, and finally '\d+' again to find the second number sequence.

The trickiest bit to get used to is knowing when a character in the pattern will simply match a character of the input string, and when it has a special meaning, like the {, }, \, + characters - but you get used to that after a while. (NB putting a '\' before a special character cancels its effect so that you can check for the literal character; e.g. '\+' means 'look for a plus sign'.

That's just scratching the surface of regular expressions - it could be taken much further. For example, it is possible to extract the '\d+' sections as so-called "captures", enabling you to treat them as numbers and check that the values are in the correct range - or to parse a whole string with multiple commands to send them as a sequence.

Re: string manipulation

PostPosted: Thu May 23, 2013 3:51 am
by JB_AU
:lol: Gloriously Stupendous Chaps!

aka Impressive work.

Might take a while to get my head around those ruby arguments, but they do make sense, after reading your unabridged break down trogluddite :roll:
Your examples MyCo are just as impressive, breaking down a module and rebuilding it in a seperate sheet , is helping to understand what each component does.

My hat goes off to both of you! :ugeek:

Why neither has 5 dsp logo's below their avatar is a mystery to me, your examples and explanations prove & show your greater understanding of the flowstone system.

Thank you for you time & diligence.

Re: string manipulation

PostPosted: Thu May 23, 2013 6:25 am
by RJHollins
Agreed ... on BOTH !

Wish there was a way to assemble a practical 'USER's MANUAL' from the explanations and examples that
GURU members so graciously share !

Granted, I certainly need to continue re-reading the manual, but so often I stumble into a thread ... on a topic I might have no interest or understanding ... only to learn something that may very well be a 'key' to my project, or a revelation to a contemplated idea for one.

Thanks for sharing your time, knowledge, and experience ! And to be able to ask follows is priceless.

I know I have a ton to learn ... but I'm also realizing that a 'elegant' programming concept becomes a cornerstone and building block that can ascend a initial idea [program] into a powerful application. Its' like playing with musicians that are better than me ... I might really stink it up ... but the things I learn from that .... !
:D

Re: string manipulation

PostPosted: Thu May 23, 2013 4:44 pm
by JB_AU
I am missing just a little something on what is expected as output.
From a terminal dw12+1 , dr12 returns 1.
But there is something i am not doing, hex-->string? , ascii-->string , binary?
Or something i can't find in the forum.

Re: string manipulation

PostPosted: Thu May 23, 2013 5:15 pm
by MyCo
There are primitives that handle the conversions. See Attachment. It can also be done in Ruby using "String unpack" method.