VCCv7 code samples & how to
Javalin
Posts: 892
All,
I'm learning C on the propeller!· So I thought i'd share my wisdom as I adapt from SPIN & ASM!· If I like it (more than SPIN) I plan to purchase and use for a data-logging project re-write!
Part (0) - Structure!
1)· Names!·· C is case sensitive so "James", "JAMES" and "james" are all different and the compiler will throw an error about an unrecognised variable.· Best to be consistant then.
2)· All C programs start with a "main" function - much like the "start" in SPIN
3)· "#include" - include a header or definition file·- in this case "propeller.h"
4)· C structures (if, while, etc) all use { } to mark blocks and don't (though its very good practice) require indenting.· There are exceptions to the rule as always
5)· statements end with ";"
6)· Comments!· see Below
Thats it!
James
Post Edited (Javalin) : 8/26/2009 9:10:26 PM GMT
I'm learning C on the propeller!· So I thought i'd share my wisdom as I adapt from SPIN & ASM!· If I like it (more than SPIN) I plan to purchase and use for a data-logging project re-write!
Part (0) - Structure!
1)· Names!·· C is case sensitive so "James", "JAMES" and "james" are all different and the compiler will throw an error about an unrecognised variable.· Best to be consistant then.
2)· All C programs start with a "main" function - much like the "start" in SPIN
3)· "#include" - include a header or definition file·- in this case "propeller.h"
4)· C structures (if, while, etc) all use { } to mark blocks and don't (though its very good practice) require indenting.· There are exceptions to the rule as always
5)· statements end with ";"
6)· Comments!· see Below
#include <propeller.h> short myVariable1=0; [color=#008000][color=#008000]// a "Short" variable declaration and set to 0[/color][/color] void main() { [color=#008000][color=#008000] // this is a single line comment[/color][/color] [color=#008000][color=#008000] /* this is a multi line (block) comment */[/color][/color] myVariable1=1; [color=#008000][color=#008000]// set to 1[/color][/color] myVariable1=myVariable1+1; [color=#008000][color=#008000]// add by 1[/color][/color] myVariable1++; [color=#008000][color=#008000]// add by 1[/color][/color] myVariable1=myVariable1-1; [color=#008000][color=#008000]// subtract by 1 (good spot Beau)[/color][/color] myVariable1--; [color=#008000][color=#008000]// subtract by 1[/color][/color] }
Thats it!
James
Post Edited (Javalin) : 8/26/2009 9:10:26 PM GMT
Comments
Part 1 - pins!
First step - set the direction of the pin:
Second step - switch it on and off slowly:
Second and a half step - toggle it:
Third step - test a pin and act on the finding:
Fourth step·- putting it all together, PWM'ing all pins - example assumes pins 0-5 are connected to an LED:
Finally - this is a bit cleverer/nicer way of doing it (updated!):
The example above will PWM as many (upto 32 of course) pins as you set "LedIDXcnt" to.· You can change the PWM values in arrays like:
(Note :- the above examples have been updated following the comments/improvements in posts below!!)
Trying to keep these examples fairly basic, I am sure it could be done "better" at the expense of read-ability!
I think thats it for pins!
James
PS - is there a power-of operator in VCCv7?
Post Edited (Javalin) : 8/27/2009 6:51:05 PM GMT
So we can now (correctly - thanks all) do pins and we know how to layout a C program!· Phew.· How about if we want to control program flow?· If you know SPIN, VB, etc this is easy - but with several gotcha's!!
Part 2 - Controlling program flow:
Firstly - conditional statements - IF!
Secondly - FOR Loops:
Basic structure of a for loop is:
so an example or two;
Thirdly - the WHILE loop!
The format of the loop is easy peesy!
and some examples!
Thats all campers.· I am not going into Continue and Goto elements for those that know these things.· I am also not going into complicated loops, nesting etc here.
James
Post Edited (Javalin) : 9/3/2009 8:11:50 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propeller Tools
Of course, there's only one 1TBS.
Albeit it is for C++, the book "C++ Programming Guidlines" by Dan Saks is quite good (as far as I remember).
There are also styles for naming variables (I developed my own), but ICCV7Prop always ends in ints, as they are the most efficent ones.
Oh, and forward declarations of functions are ... um ... rare. I avoid them.
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
Always prefered what I now know is the "Allman" style - but as long as the programmer has one!!!
>Oh, and forward declarations of functions are ... um ... rare. I avoid them.
fair comment - but you get to keep the "main" at the top of the file!
James
propeller.h also defines constant TRUE that you can use instead of 1 (makes the code a bit more readable):
The "Allman style" (as described in the wiki) is as dominant for C/C++ as the K&R or 1TBS.
Strangely after going on about style, you then suggest to avoid forward declarations of functions (a bad style practice).
Function/Variable naming:
I tend to prefer Java/C# "camelCaseStyle" or "PascalCaseStyle".·
Post Edited (NetHog) : 8/26/2009 9:22:43 PM GMT
This is so cool.
I've gotten moderately comfortably with propellers, spin and asm. I'm on the verge of learning C, and your simple introduction to C on the propeller is a strong nudge in that direction.
You got any more simple teaching examples?
Jim C.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propeller Tools
> unless someone pays you a lot to do it
just in case... I charge $250 per bag to sack raw syntax.
@Javalin: nice explainer --- this is good stuff for folks just starting out. When I learned C (too long ago :-|) there were few 'baby step' examples. So all of the 'header' stuff and code block layout was a bit daunting. Keep at it !
- H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Post Edited (CounterRotatingProps) : 8/26/2009 11:07:52 PM GMT
Regardless, it's nice to see people get excited about C [noparse]:)[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propeller Tools
yup - more comming. Watch this space!
>Regardless, it's nice to see people get excited about C [noparse]:)[/noparse]
indeed - lets leave the style questions for another thread on another day. As long as there is some!
Cheers all - glad its of help
James
That's the style for people who get paid by LOC.
> Talking C style is like sacking cow manure. It's best to avoid it unless someone pays you a lot to do it.
Or you are working in a team and have to read / modify files of others.
It won't take long until you ...
* Get kicked because your addition looks completely different then the rest of the file
* Get kicked because you made a put which resulted in 800 lines changed (of 900 lines code) with the comment "reformatted".
> fair comment - but you get to keep the "main" at the top of the file!
What!? Writing top down, you get the maximum of forwards.
Talking 'bout style. This is hard to read:
This is easy to read:
Funny to squeeze out every blank possible and then waste lines by putting the lCurly at the next line.
and a
actually meant to mean something different:
You were thinking in bit-patterns, not decimals!
And about idioms in programming languages:
while (TRUE)
is mostly written as
for (;[noparse];)[/noparse]
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
Post Edited (Nick Mueller) : 8/27/2009 7:38:08 AM GMT
meant to mean this:
Don't use globals (like for pwm) if you don't need them. And don't use pwm as a counter, it only confuses things. Especially if you initialize pwm with 0 firsthand (it's zero anyhow in your example) and then set it to 2.
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
Post Edited (Nick Mueller) : 8/27/2009 8:19:47 AM GMT
will fail.
Index is already to big in the comparison and gets too big by two in the LedIDX++!
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
Post Edited (Nick Mueller) : 8/27/2009 8:22:58 AM GMT
Oh - the forum software has eaten some of the formatting - i.e. the for {} you mentioned - i'll correct that too - it should be spaced as you suggest:
Oh and, whats the equality operator in C?· I tried "=" and "==" in the for loop as;
and it didn't appear to work.
>was that meant to mean this:
Yes - thats much nicer.
James
Post Edited (Javalin) : 8/27/2009 2:39:45 PM GMT
> and it didn't appear to work.
No, this will always fail.
The for loop is maybe a bit funny in C. Left side initializes, middle checks for am-I-still-within-range and right side modifies the index.
When you write ...; i == 5; ... comparison fails and then the for loop does nothing.
You can do multiple things in the 3 fields in a for-statement. Like:
for (i = 0, p = 10; i <= whatEver; i++, p--)
Or you can ommit one to all of them like:
for ( ; i < 10; )
And yes, equality is '=='
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
Post Edited (Nick Mueller) : 8/27/2009 9:03:13 AM GMT
My take is that Spin is very much C like, so I would think that anybody who grasps Spin should be able to easily migrate to C.· Actually, if Spin were compiled like C is implemented it wouldn't be so bad (much faster), but it may be that the memory limitations of the Prop were the primary factor in making it interpreted.· I think that to some degree those same limitations work against any high level language implemented on the Propeller.·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
thats the basis of this post - it assumes the reader is familar with programming generally, SPIN and of course the Propeller.
James
Be warned! I'll shoot the next one who says that!
I don't say that because I'm a C-fan (I'm not) or a SPIN-fan (not at all). I say this, because SPIN lacks a lot of key-elements of C.
Maybe because it's a higher-level language that makes you think they are alike. But then, you could have chosen any language.
And C isn't complex per se, it can be complex. But that only depends on the coder.
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
Post Edited (Nick Mueller) : 8/27/2009 3:08:07 PM GMT
True, but the reason it will fail is that sizeof() is misunderstood and not very well explained. The function gives the number of bytes used by the variable or type being tested, not the number of items the array. You should define the size of the array in a variable where the array is defined as: "LedPWMlen = sizeof(LedPWM)/2;" because LedPWM is a short array. If you want the index to remain in the valid range, you should use "if(LedIDX < LedPWMlen) LedIDX++;" using "<=" will allow the index to go 1 too far. C arrays use "0 offset" indices, not "1 offset".
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propeller Tools
Obviously there are differences, but things like assignment operators, bitwise operations, address pointers, pre/post operations such as inc/dec set/clr· make Spin more similar to C than other hl languages such as Basic, which is probably the biggest alternative hl language for the Propeller audience.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I missed that bug. So it will fail for THREE reasons.
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propeller Tools
For the time being though, I think that the lack Spin / C compatibility plus the smaller number of C objects available might be something to consider.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Post Edited (Agent420) : 8/27/2009 3:09:46 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propeller Tools
You're dead by now!
structures?
unions?
Includes? (together with #defines #ifdefs etc)
Type-defs?
Type-checking?
Type-aware pointers?
function-pointers?
casting? (not that this is elegant, but it helps at low-level)
sizeof?
Macros?
Alloc / free? (OK, not a language-element, but a must for even the most simple set of libraries).
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔