C with a SPIN object - example - PropWare_Spin2Dat
AS
Posts: 149
in Propeller 1
Hi David Zemon, I hope you can help me.
I was thinking how is the best way to use a SPIN object from a C program. After see some examples of PropWare I understand that I need some help to understand!!
I think that "PropWare_Spin2Dat" is a good example for some questions.
1 - I can use any SPIN object in C?
2 - In the example PropWare_Spin2Dat how you call the "pst.spin"? But if is
3 - You have more examples with other SPIN objects?
Thanks
I was thinking how is the best way to use a SPIN object from a C program. After see some examples of PropWare I understand that I need some help to understand!!
I think that "PropWare_Spin2Dat" is a good example for some questions.
1 - I can use any SPIN object in C?
2 - In the example PropWare_Spin2Dat how you call the "pst.spin"? But if is
#include <fdserial.h>why you include "pst.spin" too? This is probably a no sense question, but I need to ask because I don´t understand how this work.
3 - You have more examples with other SPIN objects?
Thanks
Comments
2 - You won't want to use Spin2Dat most likely. This option only extracts the DAT section of the Spin file and converts it to a binary blob (usually that means PASM instructions). This allows you to extract the driver from a Spin file like pst.spin and then use a C or C++ interface to interact with it. But I believe, from your other posts, that's not what you really want. You want to let Spin2Cpp extract both the driver portion of your Spin file and the high-level wrapper code.
3 - I'm afraid I don't have any more examples. I might have time tonight to put something more together for you. Sorry that it is so sparse right now, I know the one example for Spin2Cpp isn't enough.
You right I looking for something like: "let Spin2Cpp extract both the driver portion of your Spin file and the high-level wrapper code".
There are any example to use Spin2Cpp to a SPIN object and after call it from C?
I think I can understand it from the code generated by Spin2Cpp. I will see it better.
Ha, this is hard for me. I have to find some Spin first. I haven't touched Spin in years :P
There's a GUI for spin2cpp now. See forums.parallax.com/discussion/163635/interactive-spin-to-pasm-or-c-converter.
I´m not sure if my question is to @ersmith or @DavidZemon, but I think it´s for @DavidZemon!
I used Spin2Cpp as a compiler, everything ok!
(https://sites.google.com/site/propellergcc/documentation/tutorials/spin2cpp) The spin file was compiled onto the Propeller.
But I think It´s interesting use the object file from a code made in C++.
Before I try to change the code maded with spin2cpp, I tried use PropWare to concatenate the files. I´m not sure if I can do this :P (I attached the files)
I will wait for PropWare 2.1, I think @DavidZemon have a solution to this
Any sugestion is very welcome.
Thanks!
The spin2cpp() function in PropWare invokes spin2cpp for you, so you don't have to convert the files before creating your PropWare project. I have attached an example that has a simple Spin file to generate a random number and then a C++ file which will invoke the converted code. Note that, when you create the bin directory and compile the code, you never actually see the C++ source code in your project - just the spin file (RealRandom.spin) and your own C++ file (main.cpp).
I'll walk you through the CMakeLists.txt file:
Whole file
And line by line...
Gotta have your header...
And some random project name that doesn't matter
Here's the good stuff. We're going to call a special PropWare function to invoke the spin2cpp executable (aptly named: "spin2cpp()"). The first parameter is the name of our Spin file. The second parameter is actually an output, because CMake does not support return values (I know... it's dumb... one of my few big complaints about CMake). So I repeat, the second parameter is an output value from the spin2cpp() function. By passing "RealRandomCpp" in as the second parameter, we're telling the spin2cpp() function that the names of the generated C++ files should be stored in a variable named "RealRandomCpp". We need to know the names of those C++ files because they need to be added into our executable.
And finally, we create the executable. Notice there are three arguments to the function this time:
1) Executable name
2) Our C++ file
3) Our convert Spin files
If you have any more source files, you just go ahead and add them on as extra arguments. Just keep on adding all of your source files and they'll all get compiled into one executable. This function, "create_simple_executable()", takes an unlimited number of arguments and every argument after the first is interpreted as a source file.
So there you have it! Nice and easy to use spin2cpp with PropWare.
Now, while writing this post I found a small bug in the spin2cpp() function. I've fixed it already and the fix is available from download on my build server, but just be aware that you'll need to download a new version of PropWare again to pick up the fix.
Thanks a lot to both, @DavidZemon and @ersmith!
DavidZemon,
At this moment I really want to install (update) the PropWare 2.1.
As you know I have PropWare 2.0 working great in the Raspberry. I already download from here your last work in PropWare, but I´m not sure, how to do (best solution), to update PropWare.
I need some help to update PropWare, but if you think this is not the best timing to update to 2.1 version, I prefer wait until you tell me.
Thank you!
I can´t belive!! I can use a SPIN object so easy!! this is amazing! :thumb:
I just copy the files from:
- "PropWare-2.1.0.22-Generic-propware" to the PropWare directory.
- "PropWare-2.1.0.22-Generic-standalone-cmake" to the cmake´s directories with the same name, "Modules" and "Platform".
And the Scratch is working!!! :thumb:
When I write I think is everything ok!
But when I write I get a few warnings! It´s normal?
Really thanks!
Yes, that warning is normal, don't worry about it . @ersmith might see it and feel like changing something about the spin2cpp code generator so that the warning goes away, but if I recall correctly, that warning is okay in this case, and it will function correctly still.