First C: C on the Propeller for Beginners
Tymkrs
Posts: 539
So some of you may be familiar with First Spin, our podcast about learning how to program in Spin on the Parallax Propeller from zero programming experience. If you haven't had an opportunity to check it out, anytime is a good time!
But this post is to introduce folks to First C! It's a podcast we just started about how to program in C with little programming experience - or maybe only Spin experience. We approach it step by step and go through some of the tutorials provided by Parallax while adding in some extra tips.
First Episode: http://www.tymkrs.com/shows/episode/first-c-episode-001/
Second Episode: http://www.tymkrs.com/shows/episode/firstc-episode-002/
Third Episode: http://www.tymkrs.com/shows/episode/firstc-episode-003/
This short series will be posted weekly on Tuesdays on http://tymkrs.com/shows and we welcome any questions!
But this post is to introduce folks to First C! It's a podcast we just started about how to program in C with little programming experience - or maybe only Spin experience. We approach it step by step and go through some of the tutorials provided by Parallax while adding in some extra tips.
First Episode: http://www.tymkrs.com/shows/episode/first-c-episode-001/
Second Episode: http://www.tymkrs.com/shows/episode/firstc-episode-002/
Third Episode: http://www.tymkrs.com/shows/episode/firstc-episode-003/
This short series will be posted weekly on Tuesdays on http://tymkrs.com/shows and we welcome any questions!
Comments
Per Roy: "Reading an IR sensor is just reading a pin and tracking the state over time. You don't need anything special."
If I hear of anyone who has code available to share, I'll let you know
As reference, here's the Spin version - may be easily ported to C? http://learn.parallax.com/ir-remote-propeller-spin-demo
The way remotes work is that, with each button press, they send a set of pulses with varying length in the low state of the pulse. So a long low state = 1, and a short low state = 0.
So first tyou wait for the pin to go HIGH, then you wait for it to go low and count the time that it is low. If the time it is low is longer than a threshold amount (in the spin example above it is 25 iterations of a spin loop reading the pin state), then it is a 1. If its low is shorter than that, then it's a 0.
You track bits until the code is done, shift them into a long, then that number is your code for what button was pressed on the remote. For example, this could be what is seen from a single button press:
You can make a program to just track the pin state and tell you the length of time it stays high and low, and then aim a remote at it and press buttons. And it'll tell you the lengths of the high and low stats. The sequence of highs/lows will be different for each button, but the timing of low and high pulses will be consistent.
Don't know if this helps?
I've queued up some keyboard and VGAtext stuff and will be doing TVtext soon.
I write all programs with SimpleIDE. Gotta eat that dog food as some say ;-)
Episode Link: http://www.tymkrs.com/shows/episode/firstc-episode-004/
Code Links:http://learn.parallax.com/propeller-c-start-simple/make-several-decisions
http://learn.parallax.com/propeller-c-start-simple/make-complicated-decisions
http://learn.parallax.com/propeller-c-start-simple/code-repeats
http://tymkrs.tumblr.com/post/56977491012/array-variable-experimentation-in-c-on-propeller
http://tymkrs.tumblr.com/post/57070755682/more-array-variable-experimentation
Episode Link: http://www.tymkrs.com/shows/episode/firstc-episode-005/
Links:
Episode Link: http://www.tymkrs.com/shows/episode/firstc-episode-006/
Links:
Episode Link: http://www.tymkrs.com/shows/episode/firstc-episode-007/
Temperature + Voltage: http://pastebin.com/5NeN8HTg
LED Brightness + Voltage: http://pastebin.com/a2k5Tcc5
Railroad Sign: http://pastebin.com/BiPj5UsB
Example code for a2k5Tcc5.c has a bug or two...
Example code should probably not depend on "-std=c99" options since most new users will not know about this...
dgately
Just one man's opinion I see nothing wrong with -std=c99 and for(int v = 0; v < 256; v++)
1999 was a long time ago and we do not need to remain in prehistoric C times
SimpleIDE comes set up for c99
New users will not have an issue with it, I am sure they can handle being told
it is c99. Because the greatest lesson for new programmers is that nothing ever
is exactly the standard and to be prepared for that because it is always evolving.
Tom
I think you missed the point... My default install of propgcc and SimpleIDE was not configured for c99 (possibly a difference between Win and Mac OS X installs?). So, if that's true for other new C users, they may not know that "-std-c99" would need to be added to compiler options! Not a matter of right or wrong, more a matter of how to help an unfamiliar user.
dgately
Um, and then this is the first time I've ever heard of -std/c99. I'm sure that's something that we can touch on in next week's chat, but I'm going to assume from context that -std/99 is what allows you to define a variable after {, whereas before, you had to define variables before the functions (which is good form, I agree).
What I think is interesting, but anticipated, was that listeners who knew how to code in C would be OMG-ing over my noob code! I welcome suggestions for the direction we should take to help other newbies.
Addie
Regarding --std=c99
GCC allows specifying the "C standard vintage" to use. --std=c99 is (usually) set on the Learn examples and the Welcome project.
The description of "Language Standards Supported by GCC" for Propeller-GCC can be found here: http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc.pdf
Propeller-GCC is based on the 4.6.1 revision of GCC, so -std=c11 is not supported yet.
Clicking "New Project" should set --std=c99 by default. New Project for Simple View should also set 32-bit doubles, but Project View does not at the moment (can be irritating).
The whole idea of standards can be confusing and irritating. Parallax Education chose to explicitly use GCC --std=c99 most likely to say "hey, we are using this" to the Professor and not necessarily the student.
I thought it would, but for some reason I've had some real issues with saving my files such that pulling them up later = strange state of affairs (half edited code, or original code, or original code wiped by current code). I'll see if I can replicate the issue I've had, and get back to you on it.
I am also in Simple View, no idea if that changes things
#1: Use Simple View.
#2: Other steps to recreate the problem.
Thanks,
--Steve
Sorry, should I not have brought up the issue?
Anyway, I just thought of the "newbie" C users and how they might react to compiling example code. It could be very frustrating if the example code they are learning from does not compile without edit.
dgately
I think it's silly to be debating using standard compliant code for the 14 year old standard. C99 is perfectly fine to assume and teach.
I personally think it's better and more clear to have variable declaration closer to usage instead of only at the top of the function. I think it's silly to teach people the "old way" that is less desirable.
Of course, if it was up to me, it would all be C++ instead of C.
http://pastebin.com/Ng4nNGBM
And have gotten the following error:
In function '_main':
(.text+0x0): multiple definition of '_main'
cmm/Seven Segment Test TWo.o:(.text+0x0): first defined here
collect2: ld returned 1 exit status
Done. Build Failed!
It was suggested that I add -lsimpletools to "Other Linker Options" in the Linker tab, but that didn't change the error. The line at fault that's highlighted when errors come up is whatever line my cursor happens to be on.
I asked the tymkrs IRC and longhornengineer said he was able to compile it with no errors. Any suggestions?
Addie
So first you'll notice I added the -lsimpletools, but then I also deleted Test two - and when I ran it, it compiled! huzzah! lessons learned! And thanks to lostcauz/tautic/longhorn for their genius
You don't need that as long as Simple Libraries are used, and Properties -> General Tab -> Auto Include Libraries is checked.
(The Simple Library alternative is to use the Add Library Button. Other libraries can be added with the Project -> Add Include Path and Add Library Path.)
Hey dgately, no not a problem I learned something from your post about an easily visible item on SimpleIDE so that's a good thing! And you are right, it would be good for code to compile . I'll be more careful in future situations.
Episode Link: http://www.tymkrs.com/shows/episode/firstc-episode-008/
Each segment individually: http://pastebin.com/7ZwrPKG0
a-y long code version: http://pastebin.com/k38skz7Y
0-9 a-y abridged: http://pastebin.com/Ng4nNGBM
Error: http://pastebin.com/ai9PAh8e
Picture of Error: http://i44.tinypic.com/2h58od2.jpg
Congrats on Episode 8.
One clarification. If you don't specify -std=c99, you will get the gnu standard as of GCC 4.6.1 which includes much of C99. To get C89, you must say -std=c89 or -std=ansi.
Another item may be unclear because of the way Qt works on Windows. The project side-bar in project view can be hidden if you grab the line between the project side-bar and the editor by click and drag all the way to the left. It is also possible to hide build status, but that is left as an exercise. On Mac and Linux the grab bars are more obvious.
Also, if you really want access to file button items in simple view, you can right click on the toolbar and check File to show the file buttons. Hiding the file menu in simple view was required by Parallax as a way to focus on just one set of tools so that users don't get confused about what to do - use at your own risk .
--Steve
In answering the big question from the eighth podcast "Does saving the project in Simple View, save the currently edited file?", the answer is Yes! It saves the entire project (yes, you can have many files within a project) and all of its files and settings!
And even though you are in Simple View when editing a file, Control-S (or Command-S on Mac OS X) will save the currently edited file... Even though the File menu is not showing at that time, the command is still available from the keyboard (yay!).
Thanks for all your updates in the podcast about the last episode's comments...
Take care,
dgately
I was pretty sure that what you said was true, but Atdiy kept saying that her files were not saving somehow. I think it may just have been confusion on her part.
jazzed,
Thanks for the clarification on the way GCC works with and without the -std-c99 flag . I mostly work with MSVC++ and g++ (gcc c++ mode), so I'm not as readily familiar with the straight C stuff in gcc.
I personally prefer the Project mode (obviously), and can't stand the Simple view mode :P. I understand why it's there, but I don't have to like it or use it! Although, for the show I do switch back to that mode to check things.
Everyone,
We really like getting the feedback and comments here. We think it makes for a better show to have some of it be talking about and/or answering your comments/questions. There's a lot of things I wouldn't think about talking about because it's so "obvious/trivial" to me, but to new folks it's not. Atdiy hits a lot of those things with her questions, because she's been doing this for a while now with First Spin and now 8 shows of First C, she's becoming less of a "new to it all" person. So please keep those comments and questions coming!
Roy
I've been enjoying your shows. Here are some random thoughts on future content.
My experience with C is that as I build an application at some point it starts doing some very wierd stuff. Errors move and warnings change when you insert debug statements. This usually means that I am stepping on memory somewhere. A discussion on how to debug this situation would be great. This would include how the stack is used and the implications of recursion.
While I can write code C, but I am not a C programmer. I am interested in learning the C way of doing things, particulary in the context of using it to develop propeller apps. Examples of "gold standard" C code would be great, but probably beyond the scope of your shows. I have been using the propgcc libraries as my examples of "good" C/propeller code.
How about spending some time on compiler and linker options ( other than the check boxes)? Also, some time spent on how to use a map file might also be useful.
Thanks for doing the shows.
-markM