Need Advice from C Gurus
JonnyMac
Posts: 9,104
in Propeller 1
Those who know me know that I live and breath Spin. I write Spin programs most of the day, most days of the week. I like it.
That said... a few customers with C experience are wanting some of my starter programs in C. I'm making progress, but have a couple areas where I take advantage of Spin and am not sure how to crowbar it in to C.
For example. This bit of code appears in my embedded "background" object, hence gets called every millisecond.
I made the musical note and timing constants #defines and put them into their own .h file. That seems to be okay. The trick is the music. In my Spin program you can pass a pointer to an array of musical notes/times (the "Charge!" ditty is built in) and have it play. I'm not sure how to structure this in C.
Sorry for the beginner question. I appreciate the assist.
That said... a few customers with C experience are wanting some of my starter programs in C. I'm making progress, but have a couple areas where I take advantage of Spin and am not sure how to crowbar it in to C.
For example. This bit of code appears in my embedded "background" object, hence gets called every millisecond.
var long p_music long nmillis dat BPM long 60 Charge long N_G3, T_8TH, N_C4, T_8TH, N_E4, T_8TH long N_G4, T_8TH * 3 / 2, N_E4, T_8TH, N_G4, T_DHALF long -1, -1 OneNote long 0, 0, -1, -1 ' for tone_on pri play_music | nfreq if (p_music =< 0) ' manual stop invoked? io.freq_out(0, SPKR, 0) return if (--nmillis > 0) ' is note playing now? return nfreq := long[p_music] ' get note p_music += 4 ' advance to timing if (nfreq < 0) ' if end longfill(@p_music, 0, 2) return nmillis := long[p_music] * 60 / BPM ' adjust timing for bpm p_music += 4 io.freq_out(0, SPKR, nfreq) ' start the note
I made the musical note and timing constants #defines and put them into their own .h file. That seems to be okay. The trick is the music. In my Spin program you can pass a pointer to an array of musical notes/times (the "Charge!" ditty is built in) and have it play. I'm not sure how to structure this in C.
Sorry for the beginner question. I appreciate the assist.
Comments
Though if you compile your program as COG only is it not possible to just index from a pointer passed in PAR?
dgately
What is the link to the spin2cpp programs?
Thanks
Tom
The source code is at https://github.com/totalspectrum/spin2cpp/.
There are binary releases here.
21st Century C
For instance to initialize a structure :
MY_TYPE a = { .flag = true, .value = 123, .stuff = 0.456 }; (from a stack overflow answer but the book describes this more accurately)
Other members are initialized as zero
That's new syntax for me! Thanks for sharing
I really need to see about C on the Propeller, seeing some of the examples there would be very little change between C code for WiringPi on the Raspberry Pi and code for the Propeller in many cases, this could definitely make for some interesting possibilities.
E.g Javascript:
let a = {flag : true, value : 123, stuff : 0.456};
Or Python:
a = {'flag': True, 'value': 123, 'stuff': 0.456}
What language does it more concisely ?