Need Advice from C Gurus
JonnyMac
Posts: 9,519
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?
void test_play_music() { int32_t nfreq; if (p_music <= 0) { // manual stop invoked? test_freq_out(0, TEST_SPKR, 0); return; } if ((--nmillis) > 0) { // is note playing now? return; } nfreq = ((int32_t *)p_music)[0]; // get note p_music = p_music + 4; // advance to timing if (nfreq < 0) { // if end memset( (void *)(&p_music), 0, sizeof(int32_t)*2); return; } nmillis = (((int32_t *)p_music)[0] * 60) / ((int32_t *)&dat[0])[0]; // adjust timing for bpm p_music = p_music + 4; // start the note test_freq_out(0, TEST_SPKR, nfreq); }dgately
int Charge[] = { N_G3, T_8TH, N_C4, /* and so on... */ }; int *p_music; void play_music() { n_freq = *p_music++; nmillisec = (*p_music++) * 60 / BPM; freq_out(0, SPKR, n_freq); } /* somewhere in initialization */ p_music = Charge;C's handling of pointer accesses is very much like Spin's, except that C pointers are typed so "++" advances to the next element rather than the next byte (so you just do "p_music++" rather than "p_music += 4").void PrintArray( long * ptrToNumbers ) // long * means "pointer to a long" { int index = 0; while( ptrToNumbers[index] != -1 ) { // printf is "print formatted", and %d means "decimal number" - it will be // replaced by one of the arguments after the quoted string printf( "Number at index %d is : %d\n", index, ptrToNumbers[index] ); index += 1; } } void main(void) { // declare the array with contents, -1 is the terminating element so you don't have to pass the size long MyArray[] = {5, 6, 7, 8, 9, 10, -1}; // pass the pointer to the array to the printing function PrintArray( MyArray ); }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 ?