TM1637 Driver (C) with Demo
JonnyMac
Posts: 9,102
As an exercise I decided to convert my TM1637 driver from Spin to C. I still need to document the code, but it does seem to be working properly.
An interesting note... converting the Spin code to C caused me to think about the str() function differently. I went back to the Spin version and was able to improve the speed of str() by 2x, and give it better behavior vis-a-vis strings with decimal points. The two versions are identical now.
Any feedback you care to offer is appreciated.
An interesting note... converting the Spin code to C caused me to think about the str() function differently. I went back to the Spin version and was able to improve the speed of str() by 2x, and give it better behavior vis-a-vis strings with decimal points. The two versions are identical now.
Any feedback you care to offer is appreciated.
Comments
Ray
You're welcome. The process requires more effort than I like, so I'm not sure I'm going to update my entire library of Spin objects. We'll see; I don't know if SimpleIDE is getting in my way, or my own ignorance of how to use it. It seems like the process of creating and editing the .h and .c files for a C library is more cumbersome than it should be.
I am not sure how much more improvement Eric has made with the FlexGUI C, but that just might be a better platform to deal with. At least with FlexGUI C, I think the use of libraries would work out better, less cumbersome for everybody.
The big problem with SimpleIDE is, all the C drivers that are in the Simple Library, is a real pain to extract. And if you add something to the Simple library, on your computer, it is not universally available for everyone. I do not think that anybody will be wasting their time to extract the C drivers from the Simple Libraries.
Ray
There are still a few features missing in FlexGUI's C, but it's mostly complete. The main missing piece is that structure passing and return isn't implemented properly (some cases work but some don't). Most programs in practice pass pointers to structures rather than the whole structure, and that works fine.
There are a few C samples in the flexgui samples folder. I think your demo should be able to compile with FlexGUI, unless it depends on simpletools (which at present hasn't been ported... As far as I can see it's awkward to use simpletools.h outside of the SimpleIDE environment, which does some magic things behind the scenes to hide a lot from the compiler).
(1) I moved all the "static" declarations from tm1637.h to tm1637.c. Stylistically a .h file should not have any "static" definitions; those declare private variables which are not accessible anywhere else. That means that every .c file which #includes the .h will get its own copy of those variables, which causes data bloat. I believe what you really wanted with those variables was to declare them in tm1637.c.
(There was also a bug in fastspin which caused a complaint about a static being initialized twice in different files. That should actually be legal, since those are different variables, but that's what clued me in to the fact that multiple copies of e.g. the hexTable[] array were being created.)
(2) I changed "const static" to "static const" to work around another bug in fastspin (which will be fixed Real Soon Now, thanks for the example which reveals it).
(3) In TM1637_Demo.c I replaced the "#include "simpletools.h" with:
pause() seems to be the only function used from simpletools, and fastspin has a built-in that does the same thing. For porting to Catalina and other C compilers you might want to provide your own pause() function using waitcnt, but that's a minor issue.
At least, I think that is so...
Sure. Or you could use the C driver from Spin code, whatever way you prefer is fine. Some people will prefer to read/write C rather than Spin. I think in this particular case Jon was exploring the use of C on the Propeller, and FlexGUI is a usable option for that. There are other reasons to port the code from Spin to C (e.g. having a portable driver that will work in PropGCC, Catalina, and FlexGUI is sometimes nice).
It's not likely that I will ever mix languages in a client project, but it's interesting that FlexGUI allows it. A couple weeks ago I wrote a C program that used a bunch of my Spin drivers. What I like about FlexGUI is that it doesn't hide anything when it comes to the command line. What I don't like is the super minimalist editor. I understand that this is about a cross-platform test-bed for fastspin, not about creating a new editor. I am asking Jeff Martin of Parallax to look at your approach in FlexGUI: allow an easy selection of target and user customization of command line by advanced users. I know that Jeff is updating Propeller Tool to support the P2, and I hope he takes the approach you used in FlexGUI.