array parsing help
perry.gnoid
Posts: 10
hi folks -
i have a code issue that i need assistance with. any help or suggestions are greatly appreciated as i'm somewhat stumped.
what i need -
I want to parse a long array into individual variables for each index
what i'm doing -
im sending a serial message of an array/table that is indexed from 0-63. each index will have a value of 0-2. i need each index value saved temporarily, until the next message is received. depending upon the index value, another serial message is sent to the motor control board to operate a servo. (ie - if the value = 0, servo rotates to a position, if v=1, rotate to another, generating 3 positions)
i recently bought the BS2px24, hoping this will help w/ memory and speed issues.
simply, i'm hoping the program will listen to the serial, receive the message, break it apart, send the values to the appropriate subroutines to operate motors and then relisten for the next message.
thanks in advance
p
i have a code issue that i need assistance with. any help or suggestions are greatly appreciated as i'm somewhat stumped.
what i need -
I want to parse a long array into individual variables for each index
what i'm doing -
im sending a serial message of an array/table that is indexed from 0-63. each index will have a value of 0-2. i need each index value saved temporarily, until the next message is received. depending upon the index value, another serial message is sent to the motor control board to operate a servo. (ie - if the value = 0, servo rotates to a position, if v=1, rotate to another, generating 3 positions)
i recently bought the BS2px24, hoping this will help w/ memory and speed issues.
simply, i'm hoping the program will listen to the serial, receive the message, break it apart, send the values to the appropriate subroutines to operate motors and then relisten for the next message.
thanks in advance
p
Comments
I’m not clear on the data…It sounds like you will have 64 bytes and each one will either be a 0, 1 or 2? In any event you won’t be able to pull them all into variables, but for example you can pull one at a time into a variable by reading the SPRAM (Assuming that’s where you’re storing it, which is a good idea) into a variable. Now if these are ASCII values you can just subtract $30 from the value you’re storing into the variable to get the actual value. So, if your table looked something like this in SPRAM…
0102121102012102010200102012012…
And you wanted to read the 5th value you would do something like this:
GET 4, temp
Temp = temp - $30
Now, temp = 1
Remember you can always replace the 4 with an index variable so you can step through them.· This would make it easy to compare two tables, especially on the BS2px.
I hope this helps, Take care!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
Thank you for your reply. Your assumption is correct in that I was hoping to generate a 64-byte array where the values in each index would be either (and only) 0, 1, or 2. Your suggestion is very helpful for stepping through the indexes individually. For speed, I was hoping to avoid this, but it seems that i could cycle through with a loop structure and then move onto bigger and better operations. In attempting this, I have generated several more questions from further difficulties.
with the GET command, would I need to use its corollary in the PUT command? How do i gather the info (the large table) in from the SERIN command and write to the SPRAM? As the BS2px has 126 byte locations to use, is there an overspill function - meaning if i begin with segment 0, once its memory is full will it proceed to the next segment, #1 and continue writing and so forth?
Once again, i feel I'm up against the wall with needing to take a large amount of data and segment it so its resulting sections are operable.
Thanks Again
Unfortunately you’re a couple of bytes short of the 128 you would need to compare the two tables. In theory if you had 128 bytes you could copy the previous table from locations 0-63 to 64-127 and then SERIN using SPSTR the next 64 bytes and compare. This would be done byte-by-byte. Currently the only way to do this would involve truncating the last byte from the table, or at least moving it into a temp variable. I am assuming the current data always needs to be compared to previous data? I think it could be done. It will be just a little tricky because of the 2 byte shortage, but that could be worked around…remember, you have multiple slots, so you could designate a slot to handle the details. Take care.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
basically cutting your current 64 byte array in half, it'll take a little longer to process (uSeconds) to handle whole 64 bytes, but you could use a size in between 32 - 64.
Still you'll want to leave a few bytes available for some internal flags and index tracking and such.
z=z & 3 << (x//4*2)
GET (x/4), y
y=~(3 << (x//4*2)) & y
PUT (x/4),(y|z)
using x as the storage address that would store it all in 32 bytes.
zero, one and two can be stored in 2 bits (a crumb). using the algorithm above you can easily extract the crumbs from the bytes and insert the crumbs in the byte.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 9/14/2007 10:33:44 AM GMT
p
Perry if your only dealing with 0, 1, 2 values then (in case you haven't put it together yet)
0 = binary 00
1 = binary 01
2 = binary 10
so you can fit four values into a byte (00001010) = (00)(00)(10)(10) = 0,0,2,2
you'll just need to learn how to parse the bits (crumb) from the byte.
Lets say you've done a "SERIN 1, 16780, [noparse][[/noparse]z]" or a "SERIN 1, 16780, [noparse][[/noparse]DEC z]" input, you now have your byte in z and you want to store it in the 2nd 128· crumb.
First set up your address in the x byte:
··············· x=1
then you need to shift the input into the proper potion in the byte:· z=z & 3 << (x//4*2)
··············· the expression gets evaluated in this· order
··············· x//4 ····· or·· “x modulo 4”, the remainder of x divided by 4 gives a repeating pattern of
······························· 0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,…etc
··············· (x//4*2) multiply the result by 2 and you get ·0,2,4,6, 0,2,4,6,…etc
··············· z & 3····· ·all the bits in z except for ·the 2 least significant get masked ·········
··············· z=z & 3 << (x//4*2)·············· the bits get shifted in to their final position
GET (x/4), y ······ get the final destination byte , divide by four because there are 4 crumbs in a byte
·
(3 << (x//4*2))· create a complimentary mask by starting with a 3 or binary %11
(3 << (x//4*2)) ·shift it into the same position as z········
~(3 << (x//4*2)) invert the bits and you now have your complimentary mask
y=~(3 << (x//4*2)) & y· do a bitwise “and” to mask out the destination for the bits to zero
PUT (x/4),(y|z)· do a bitwise “or” to merge the input with the destination
PUT (x/4),(y|z)·· store the byte with the inserted crumb back in it’s address
And it all happens in an instance. A
if you don't need to extract crumbs, just save them, individually you can simplify it by doing this
for y = 0 to 3
·SERIN 1, 16780, [noparse][[/noparse]z]
··tmp=tmp<<2|(3 & z)
next
PUT x,tmp
next
do the second set like this:
for y = 0 to 3
·SERIN 1, 16780, [noparse][[/noparse]z]
··tmp=tmp<<2|(3 & z)
next
PUT x,tmp
next·
then you can compare. (tmp is a VAR BYTE)
I hope this is clear enough, study the "operators" in the help file of the editor for a more in depth explanation.
·I reposted I forgot to set "tmp" to zero
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 9/18/2007 1:45:21 AM GMT