Hi.. I'm trying to understand C code a lot better. I get most constructs. What I'm trying to understand is the line below (from tx blinky)
The line... RFM69 radio;
...What does this line actually do.. and what would you call that kinda of construct C wise
#define LED_GREEN 4 //GREEN LED on the SENDER
#define LED_RED 5 //RED LED on the SENDER
#define RX_TOGGLE_PIN 7 //GPIO to toggle on the RECEIVER
#ifdef ENABLE_ATC
RFM69_ATC radio;
#else
RFM69 radio;
#endif
void setup() {
Serial.begin(SERIAL_BAUD);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef
The line RFM69 radio; defines a variable called radio and allocates storage for it, RFM69 will be a typedef declaration somewhere in the code, or a #define of it (probably a structure type).
Edit: If you're trying to understand C there's some things to realise that will make your understanding of what is really going on so much easier. To create a working executable the compiler does 4 main things:
1) Pre-process. This is where all the #defines replace text in the code and conditional directives evaluated.
2) Each C file is compiled separately and independently from each other. At this point all the compiler needs is information about variables and functions, such as their size and type and what types need to be passed to and returned from functions, so it can generate intermediate 'object' code to reference them properly. At this point it doesn't know anything about specific addresses at all, these specific addresses are put in as dummy 'place holders' in the object files for the linker to replace with real values later on.
3) Linking. It's at this point that the actual addresses of the variables and function calls are calculated, and the place holders are replaced in the object files with those real values.
4) Assembly. The final machine code is generated.
So, a header file that is included in a C file contains only information needed for the C file to be compiled into the intermediate object code, it does not allocate storage. In C terms this header file consists mainly of 'declarations'. The C files then use these declarations and allocate storage, they then become 'definitions'.
So the line RFM69 radio; uses a declaration called RFM69 of a structure which is (probably) in a header file that is included in that C file, and 'defines' that variable by allocating storage for it. The linker will then resolve that storage address to a real address during the linking process.
Mark.
Thanks perky, nice overview!
That is a super overview.! I've been doing a bit of playing around with variables and types of variables.. that line doesn't see to specify the type of variable tho.? And I guess I'm wondering why does it need to be defined with the RFM69 piece first..Just define it globally and then it would be usable all over the place.? Ie something like 'int radio;" etc.
Thanks for your patience! I've come from a PICAXE background, Ibwas quite competent there. With PICAXE I felt like I could understand every part of the chip, every code function and at any time I knew everything the picxe was doing. Granted there is no where near the power available in terms of options when you're coding in basic. I love the Arduino stuff and the power of C but it feels like a massive casm which I can only ever Understand a tiny part of.. Especially with all these libraries, linked files, .C files etc etc I guess in my head, when I press compile I want to appreciate all that goes on - is that a foolish goal maybe.?
Quote from: vk3jap on March 31, 2017, 04:30:06 PM
That is a super overview.! I've been doing a bit of playing around with variables and types of variables.. that line doesn't see to specify the type of variable tho.? And I guess I'm wondering why does it need to be defined with the RFM69 piece first..Just define it globally and then it would be usable all over the place.? Ie something like 'int radio;" etc.
C is much more of a structured and typed language than BASIC, it readily lends itself to modular code with separate C files and associated headers and it allows you to define the scope of access so that some variables or functions may be private to a function, private to a file, accessible only by other specific modules, global etc.
My explanation was for C and it's significantly more complicated in this case because it's actually C++ you're dealing with, but in essence it boils down to something very similar in concept. RFM69 class is declared in RFM69.h and holds information about what your defining. You'll need to do some reading on C++ to know what it's really doing with classes and constructors etc., personally I use C only in my projects so I'm likely to get explanations completely wrong with C++ ;)
Mark.
Ahh, cool. So is it fair to say C++ is C with extra's? I mean..How can I tell, by looking at the code..if it is C, C++ or C# etc.?
Arduino is based on and supports C++, not really obvious from the simple sketch structure of setup() and loop() where you could simply just use a C-only program. But there are some dead give-aways, for instance lots of libs (like RFM69) use C++ which uses classes whereas C allows only structs and does not support classes or object oriented concepts or other more advanced programming topics like generics. Lots to touch on here, if you just google around you will find lots of discussions and articles on the differences.
And not foolish to try to dive deeply. My first language was VB 6.0 and I've slowly become decent at C/C++. My projects always use the existing massive C++ libraries at first but I'm never happy until I've boiled things down to the bare minimum which is almost always a few scant lines of C. As Felix said, C++ is OOP while C isn't.
I love the Arduino stuff but I see the need to use C++ as an architectural flaw. You get this situation where people build Arduino things, where they just follow the dots from some maker website, with sample code and they "make something". Then, they get inspired, and they might make something else, in the same fashion. Then they think about using Arduino to make a 'system' or something more complicated but really, at that point you can't just borrow snippets of code as sometimes they clash (libraries/pinouts etc). So this is kinda where I'm at. I reckon in many ways it would be easier to use BASIC. God knows what I'm saying here but I do feel that many people who get into Arduino are BASIC level coders..But they're forced to go C++ ..And that is an issue, it's very daunting to have to fully appreciate C++, OOP, modules .H file...cpp files etc etc.
Commentary on my rant welcome :) .
You don't actually need to use C++ to Arduino it up but of the projects I stumble across, 99% of them do use big libraries as opposed to simple code snippets. For instance, one of the first things I did with my Arduino was use a DS18B20 to measure the temperature of fermenting beer (another nerd hobby) and the code online uses the OneWire and Dallas libraries which made a sketch size of like 10k. Once I worked out how to actually interact with the sensor (which was a major and time-consuming undertaking to be sure but very instructive) I have code that only takes up 480 bytes on the chip. My code tends to execute appreciably faster than bigger code which also gives a modest power savings as well. Most folks, don't like to spend 40+ hours working with one sensor which I get but to me there is no wrong way to pursue a hobby as long as you are mostly having fun.
Yes, very good points and I agree. I do recall when I started with PICAXE and got into RTCs with DS3232 etc I had to read the data sheet of the RTC and appreciate different memory locations etc to get the time where as on Arduino there is probably some LIbrary and you just say DS3232.GetTime or something like that.
I suppose one of the reasons I like Arduino is because there is such a big community and those libraries exist. Because those libraries exist and because it used C++ it's amazing what can be done. Take for example, coding PICAXE in BASIC..You pretty quickly hit limits of what you can do and think about blowing away the PICAXE bootloader and programming the underlying PIC in a C++ IDE or even assembler. Try for example getting wifi/Ethernet/IP and HTML pages happening, or tweeting for example also, in BASIC on PICAXE and you very quickly hit frustration town.!
Sounds like I've just Answered my own initial post. I guess if I look hard enough there is probably someone who has made up a BASICduino Framework of sorts.
Thinking I best just take a cup of concrete and headen up.!