NMEA Sentences & wishing to learn repeat and combining steps

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
AZSun
Posts: 30
Joined: Tue Mar 20, 2012 10:44 pm

NMEA Sentences & wishing to learn repeat and combining steps

Post by AZSun » Mon Jun 03, 2013 1:37 am

Hi, somehow the format of NMEA 0183 sentences caught my interest. (I saw a SailTimer Wind Vane online that uses them, and I just thought they were cool.) Anyway, I thought I could use the format of the sentences to work my way through some features of LiveCode. I've been doing lessons and tutorials and wanted to try something that sparked an interest from me and use what I've learned. (I used replace, itemdelimiter, get char, it, functions like charToNum, baseConvert and an operator like bitXor)

I was able to take a sentence $PMTK604*30 (The info to be evaluated is between the $ and *) and do the checksum at the end that shows why it should be 30 as shown.

I was successful because I broke up the work into baby steps where I could see each part work. Also, I was just trying to make it work with this sentence not for every NMEA sentence. I did this just as a learning device.

Now, I'd like help learning how to use repeat and other ways of combining my steps.

Button called strip_characters

Code: Select all

on mouseUp
   put the text of field "nmea_sentence" into thisVariable
   replace "$" with "*" in thisVariable
   set the itemdelimiter to "*"
   get item 2 of thisVariable
   put it into field "nmea_core"
end mouseUp
Button called separate

Code: Select all

on mouseUp
   get char 1 of field "nmea_core"
   put it into field "first"
   get char 2 of field "nmea_core"
   put it into field "2nd"
   get char 3 of field "nmea_core"
   put it into field "3rd"
   get char 4 of field "nmea_core"
   put it into field "4th"
   get char 5 of field "nmea_core"
   put it into field "5th"
   get char 6 of field "nmea_core"
   put it into field "6th"
   get char 7 of field "nmea_core"
   put it into field "7th"
end mouseUp
Button called convert to dec

Code: Select all

on mouseUp
   put charToNum (field "first") into field "dec1" 
   put charToNum (field "2nd") into field "dec2"
   put charToNum (field "3rd") into field "dec3"
   put charToNum (field "4th") into field "dec4"
   put charToNum (field "5th") into field "dec5"
   put charToNum (field "6th") into field "dec6"
   put charToNum (field "7th") into field "dec7"
end mouseUp
Button called XorIt

Code: Select all

on mouseUp
   put (field "dec1" bitXor field "dec2") into v1Dec
   put v1Dec into field "total1"
   put (field "total1" bitXor field "dec3") into v2Dec
   put v2Dec into field "total2"
   put (field "total2" bitXor field "dec4") into v3Dec
   put v3Dec into field "total3"
   put (field "total3" bitXor field "dec5") into v4Dec
   put v4Dec into field "total4"
   put (field "total4" bitXor field "dec6") into v5Dec
   put v5Dec into field "total5"
   put (field "total5" bitXor field "dec7") into v6Dec
   put v6Dec into field "total6"
end mouseUp
Button called HexIt

Code: Select all

on mouseUp
   put baseConvert(field "total6", 10,16) into field "Hex_Total"
end mouseUp
Thanks for any suggestions,
Eric

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: NMEA Sentences & wishing to learn repeat and combining s

Post by dunbarx » Mon Jun 03, 2013 5:13 am

Hi.

Welcome. Baby steps indeed.

This is not tested at all, but I hope you get the idea. In a single button script:

on mouseUp
put text of field "nmea_sentence" into thisVariable --your first few lines of code. These can be condensed.
replace "$" with "*" in thisVariable
set the itemdelimiter to "*"
get item 2 of thisVariable
put it into field "nmea_core"
put fld "nmea_core" into tString --work with variables whenever possible

put charToNum(char 1 of tString) bitXOr charToNum(char 2 of tString) into temp --initialize the upcoming loop
repeat with y = 3 to the number of chars in tString
put temp bitXOR charToNum(char y of tString) into temp
end repeat
put baseConvert(temp, 10,16) into field "Hex_Total" --OK, put the result into something you can view
end mouseUp
---------------
Now this is so compact (providing it actually works) that you may bury the underlying process. There is always a tradeoff between raw compactness, readability and the ability to modify code later on. You have to just learn what is best in each situation from experience.

To answer your main question. you could certainly do a whole lot of staged condensation. You do not need to put everything into a field. So:

get char 1 of field "nmea_core"
put it into field "first"

can be shortened to:

put char 1 of field "nmea_core" into char_1 --lose the "get" line, go directly into variable instead of a field as a matter of practice

and you could further combine that with its follow up associated line into:

put charToNum(char 1 of field "nmea_core") into field "dec1" --what? Another field???

And so forth. You should try this kind of thing out, and see where superfluous code is simply not needed, and in fact can be considered clutter.

Stop using so many fields.

At root, though you are faced with creating a compact structure that will reduce the several lines of code in that last handler into something readable and manageable. Heaven help us if the string of chars to be validated was considerably longer. But this is actually a fairly sophisticated loop, and not normally one that a new user uses to learn repeat (or switch) structures with. You are starting out with intermediate stuff. All you did is valid, but verbose. You must see how the proper selection of a good process obviates almost all the code you wrote.

So what? Go for it. Play around with all this. Let me know if my repeat gizmo actually works. The concept should be sound, in that a temporary result is updated and reused in a quasi-recursive way. In fact, a recursive function is called for here, but that is another story.

Craig Newman

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: NMEA Sentences & wishing to learn repeat and combining s

Post by dunbarx » Mon Jun 03, 2013 5:44 am

Hi again.

I see my script works.

I also see that the reason you used fields so much is that you needed a place to store data as you invoked the several mouseUp handlers, likely located in several buttons. Again, sound and valid, as far as that goes, but you need to learn about what LC can offer to make all that go away.

Write back, please. You have started with a sophisticated project. You will get all the help you need. Practice...

Craig Newman

AZSun
Posts: 30
Joined: Tue Mar 20, 2012 10:44 pm

Re: NMEA Sentences & wishing to learn repeat and combining s

Post by AZSun » Mon Jun 03, 2013 5:47 pm

Hi, Thanks for the good info. I will play with the directions you've given to see how it works. I had an ah-ha moment when you wrote
There is always a tradeoff between raw compactness, readability and the ability to modify code later on. You have to just learn what is best in each situation from experience.
I had not even thought about finding this balance. Oh, yes, I'm field happy--I'll work on that. I think it is a combination of two things. I wanted to see things work along the way, and I'm coming from a FileMaker6 background.

Thanks again, I'll report back after digging-in.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: NMEA Sentences & wishing to learn repeat and combining s

Post by dunbarx » Mon Jun 03, 2013 7:04 pm

Good luck.

Do you see where the repeat loop I wrote is a little clunky? That initializing line really does not have to be so dramatic, and the loop counter can start at 2. Can you fix it?

Craig Newman

AZSun
Posts: 30
Joined: Tue Mar 20, 2012 10:44 pm

Re: NMEA Sentences & wishing to learn repeat and combining s

Post by AZSun » Wed Jun 05, 2013 1:11 am

Ok, I dug into repeat did more tutorials and got side-tracked on Functions. Now, I'm back. I made the change you hinted at. Also, due to your use of the code "to the number of chars in tString" this works for all the standard NMEA sentences I've tried--not just the short one I used as an example.

Code: Select all

on mouseUp
put text of field "nmea_sentence" into thisVariable
replace "$" with "*" in thisVariable
set the itemdelimiter to "*"
get item 2 of thisVariable
put it into field "nmea_core"
put fld "nmea_core" into tString 

put charToNum(char 1 of tString) into temp --Change made here to prime the repeat pump because the bitXor gets done below
repeat with y = 2 to the number of chars in tString --Change made here to alter the start point of the repeat with
put temp bitXOR charToNum(char y of tString) into temp --I know what this is doing but don't know how. I think I'm unclear on the variable y
end repeat
put baseConvert(temp, 10,16) into field "Hex_Total" 
end mouseUp 
I pointed out where I made changes and where I'm unclear in the code above.
Again, thanks for the help.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: NMEA Sentences & wishing to learn repeat and combining s

Post by dunbarx » Wed Jun 05, 2013 3:17 am

Bravo with the correct change to the initialization. Really.

The variable "y" in the loop is arbitrarily named. It could just as well have been "theFirstChar". In fact, it ought to have been, so that the repeat loop had a more readable starting variable. We like readability.

The repeat loop needs (in this form) an ending value as well. It could have been "17", if our argument string had such an explicit length. The cool thing about "the number of chars of tString" is that this ending value is derived from the length of the argument itself, on the fly, and need not be managed by anyone. I am gratified you appreciated this. Really.

If you put a value into the variable "temp" and then bitXOR it with another value, and put that right back into the variable "temp", and then bitXOR that with yet another value, you accumulate a series of ever increasing results. This is a pseudo recursion of sorts.

It is time you try some experiments. Can you write a short handler that adds the first nine integers using this schema? Please do. Step through the code line by line, and you will see what happens. The bitXOR thing is identical.

There are better ways to do this, by the way, but this is an experiment with this particular process. If you are not writing test handlers to get yourself out of intractable dilemmas, you haven't lived. Is there a way to run this sort of thing without an explicit first pass outside the loop? In other words, can everything be inside, so that the starting value of the loop is "1'?

Write back...

Craig Newman

Post Reply