A BYTE array question
idbruce
Posts: 6,197
Hello All
Here is my question.· Actually 2 questions.
Can the number of elements in a byte array be set dynamically or is the number of elements·always a constant?
If it is possible, what is the proper way to accomplish this, and get the count of elements?
Bruce
Here is my question.· Actually 2 questions.
Can the number of elements in a byte array be set dynamically or is the number of elements·always a constant?
If it is possible, what is the proper way to accomplish this, and get the count of elements?
Bruce
Comments
Assuming·the number of elements can be dynamically changed, let's also assume that the value of this dynamic change is unknown, but must be acquired.
Your code should count the items placed in memory.
Post Edited (Mike G) : 7/14/2010 12:53:06 PM GMT
That said, you don't have to use the whole array and you can have a variable somewhere that contains a count of the number of items you're keeping in the array. Sometimes array[noparse][[/noparse] 0 ] is used to hold that while the data elements themselves start at array[noparse][[/noparse] 1 ].
In my programs, I'll typically have a constant "tableSize" for an array called "table" and declare things like this:
That way, I can use "tableSize" in my REPEAT statements.
Post Edited (Mike Green) : 7/14/2010 2:17:12 PM GMT
Thanks for the reply.· Going back to my C++ programming days with TCHAR[noparse]/noparse, I remember that the number of elements was a constant.· I figured SPIN was the same, but I thought I would ask anyway.··My proposed solution is similar to what you suggest.· In my case, the number of elements will always be less than 8.· So something along the lines of:
Here is the real deal.· I am writing an Eagle (PCB layout software) drill file (*drd) parser for a CNC PCB drilling machine that I am building.· Most of it is complete except for parsing the coordinate lines of the file, eg:
I am thinking that the best alternative is to declare·x and y variables and parse each line from x till y and store the data in between (converted·into a whole number) as a long, and then parse the remainder from y until "Carriage Return" and store it similarly.
Can you offer any suggestions on accomplishing this task?
Thanks once again for your previous reply, and thanks in advance to any further advice or comments you may provide.
Bruce
Note that I'm assuming that X and Y values will fit in a word (0-65535). If not or if you need signed numbers, you'll need to change things.
For your case, the regex would be "X($1\d+)Y($2\d+)" for each line in the file.
-Phil
Thanks once again.· I will have to take a look at that example.· X and Y are both temporary variables.· When I reach a coordinate line, parsing is paused and·the machine moves to the acquired coordinates and drills the hole returning a task completed result, parsing·then continues·until the next set of coordinates is found.· So on and so forth until·fileline = "M30" (End of file, no more coordinates).
Your assumtion is correct, word assignment should be adequate for Eagle *drd files, dependent upon the math used for machine movement.· However, Eagle only allows for a certain size board, and my machine will do much larger boards, so I must provide a solution for·values larger than 65535 for homemade *drd files.
Thanks so much Mike, I will take a good look at that example.
Bruce
Yes, I have had my eye on that object for sometime.· Now you are taking me back to my Perl programming days.· I loved parsing with regular expressions.· A very powerful tool indeed.· I just have not taken the time to examine it closely, but rest assured, I will be using that object in the future.· Perhaps·for this project.·
And you even spelled it out for me "X($1\d+)Y($2\d+)"· [noparse]:)[/noparse]
Now that's·some cool cryptography!· Eliminates all the necessary ciphering and deciphering.
I am tempted.· Thanks Phil
Another example:
X = 1.1000
Parse the left of the decimal: 1
Parse the right of the decimal: 1000
1*40000 = 40000 steps = 1 inch of travel with 20TPI lead screw.
1000*4 = 4000 steps = .1 inch of travel
Total: 44000 steps, or 1.1 inches of travel.
Store 44000 as a long, using a long array.
Post Edited (T Chap) : 7/14/2010 4:43:07 PM GMT
That was very interesting!· I have not reached the part of stepper control conversion yet, but I soon will and your input will prove helpful, especially since my steppers have a 1.8 degree step angle and my leadscrews have 20 TPI.· But I am not going to be microstepping, they will either run full or half step.· With full step, I should be at .00025 resolution and with half I should be at .000125.· I figure that should be close enough, provided I get control of the upcoming·backlash.
However my coordinate lines only contain·alpha-numeric characters with·no decimal point.··There must be some type of software setting within Eagle to provide the decimal point, but I·haven't it yet.· Either way, between Mike's, Phil's, and your input, I am certain this machine will be running soon.
Bruce
RS-274 data does not contain decimal points. The structure is defined in the Format Statement at the beginning of the file. I have not looked at an Eagle Gerber file, but they should conform to the standards. The attached example shows %FSLAX24Y24*%.
FS-Format statement
LA-Leading zeros omitted, Absolute Coordinates
X24-00.0000
Y24-00.0000
You will have to read the format statement and decide how to parse the data, adding the decimal point in the appropriate place.
Jim
A couple of good reads:
http://www.artwork.com/gerber/274x/rs274x.htm
http://en.wikipedia.org/wiki/Gerber_File
Post Edited (hover1) : 7/14/2010 6:46:20 PM GMT
It is always nice to have options.· If that ULP does not work with the latest version of Eagle, then I will have to alter it, for possible future use.· However, I am very close to having my parser work the way I want it.· I was looking at another parser from pcb2gcode, which parsed the drill file and determined the decimal location programmatically.· I might include something similar.· Are you still drilling circuit boards?· Your previous post used past tense.
Thanks for the ULP file.
Bruce
Well thanks for all your input.· I sincerely appreciate it.
Bruce
I know you are busy looking at and answering questions, but if you are still listening, thank you for your guidance.· It appears that two of the functions within ExtendedFullDuplexSerial will prove useful to me.· Thank you so much for your input and guidance.
Bruce
Don't worry, I will put your object to use ·· I know what it can do.· Thankfully someone ("you in this case"), took the time to write such a valuable tool.
Bruce
I did not say it at the time, but during my review of some old stuff, this was a pretty good tip. Thanks.
The SPIN language (and C, for that matter) does not support arrays. In fact, there's no support for any data types at all.
Declarations may specify a size multiplier and references may include an offset computation; both use the [] operator to accomplish this.
Once one gets one's head around that subtle distinction, the rest follows pretty quickly.
You sound like a real smart feller.
Look up "array" in the dictionary.
Look up "array index indicators" in the Propeller manual.
Do a search for C or C++ arrays on google.
Hint: Orderly arrangement.
-Phil
The definition of array (you really mean vector) that you're attempting to use is a layman's definition, not very applicable to computer science. Case in point, your definition applies to any hunk of memory that's sequentially addressable. In other words, consider the following declarations:
dat
A byte 0, 0, 0
B byte 0 [3]
C byte 0
D byte 0
E byte 0
You'd claim that A, B and C are all identical arrays (vectors) of size 3 (or more!) because they are names for the addresses at which three successive addressable locations exist. I suppose you can adopt that position, but it's pretty perverse, and it wouldn't get you far in a class on compiler construction
Languages that supported typed variables are considerably different than those which do not. Typing allows compile and runtime detection of programming errors and allows dramatic improvements in compile-time optimization that are not possible in languages like C and SPIN. They're also far better at capturing the semantics of programs in a manner that's easy to read and understand and maintain.
Clearly, the C language does support types. Type specifications are used at run-time to check for consistency in variable usage. Multi-dimensional arrays are also supported in C, so it is inaccurate to say that C does not support arrays. However, C does not include any type or array-bounds checking at runtime. At run-time you can pretty much do anything you want with a pointer and index without any error checking. So in that sense there is no type or array support at run-time.
And despite the fact that the [] operator can be applied several times and create the illusion of multi-dimension arrays, the languages remain untyped with no support for vectors or arrays at all.
Let's come at this another way - think of an assembly language program for almost any architecture. Sure, you can create abstract data structures and operate on them endlessly - but the assembler and the assembly language and the instruction set have no support for arrays. This is the same situation with C and with SPIN.
Here's another example of how you can conclude there's no support for arrays:
dat
A long 0 [3]
B long 0 [3]
if I have the statement
A := B
That moves a single 32-bit long. Because A and B are declared as scalar variables, not arrays. There is no array type. There's just some syntax for allocating blocks of storage and doing some addressing math with the [] operator.
Similarly, if I have a subprogram declaration:
pub foo (N)
And I call foo with
foo (A)
The array isn't passed - either by value or by reference. Instead, the scalar variable A is passed by value to Foo - again, there's no array object, there are no array types. There's just scalars, block allocations at declaration time and addressing math with [].
Taken another way, think about floats. There's no float type even though there are several abstract class libraries that implement floating point. But within those libraries, there's no exported type definition for double precision float - because there's no types.
Finally, allow me to quote from the Kernighan and Ritchie (p 120 of 2nd edition of The C Programming Language) explanation of the travesty they created:
It must be emphasized that a typedef declaration does not create a new type in any sense; it
merely adds a new name for some existing type. Nor are there any new semantics: variables
declared this way have exactly the same properties as variables whose declarations are spelled
out explicitly. In effect, typedef is like #define, except that since it is interpreted by the
compiler, it can cope with textual substitutions that are beyond the capabilities of the
preprocessor.
C has no types.
edit: p.s. runtime checking has even less to do with it. It's not necessary to specify that in the language. I have used two C compiler/runtime systems with array bounds checking, one was the native C on a minicomputer and the other was a special GCC variant.
-Phil