diff --git a/include/sound4calc.h b/include/sound4calc.h index 73ee0a1..5e6e000 100644 --- a/include/sound4calc.h +++ b/include/sound4calc.h @@ -1,11 +1,15 @@ #ifndef _SOUND4CALC_H #define _SOUND4CALC_H -extern char wave_signal[25]; -extern unsigned short duration; -extern unsigned int freq; +extern struct Note +{ + char* wave_signal; + unsigned short duration; + unsigned int freq; +} note; void CallSequence(); +void CallNote(); void PutPinState(char level); void PlayNote(); void InitPorts(); diff --git a/src/main.c b/src/main.c index 7b5b4fc..1ffb0e0 100644 --- a/src/main.c +++ b/src/main.c @@ -1,15 +1,15 @@ #include "main.h" #include "sound4calc.h" -//#include // add timer fonction -#include // add display fonction +#include +#include #include - +#include "string.h" //#define PI 3.141592653584 int main(void) { unsigned int key = 0; - unsigned short place = 2; + unsigned short place = 0; //unsigned int i=0; InitPorts(); @@ -18,31 +18,34 @@ int main(void) { dclear(); - dprint(1, 1, "%d", freq); - dprint(1, 10, "%s", wave_signal); - dprint(1, 20, "F1:note/F2:sequence", place); + dprint(1, 1, "%d", note.freq); + dprint(1, 10, "%s", note.wave_signal); + dprint(1, 20, "%d, %d", strlen(note.wave_signal), place); + dprint(1, 30, "F1:note/F2:sequence"); dtext(1, 50, "F5 _ F6 -"); - - dupdate(); key = getkey(); switch(key) { - case KEY_RIGHT : freq+=1; break; - case KEY_LEFT : freq-=1; break; - case KEY_UP : freq+=10; break; - case KEY_DOWN : freq-=10; break; + case KEY_RIGHT : note.freq+=1; break; + case KEY_LEFT : note.freq-=1; break; + case KEY_UP : note.freq+=10; break; + case KEY_DOWN : note.freq-=10; break; - case KEY_F5 : *(wave_signal+place++) = '_'; break; - case KEY_F6 : *(wave_signal+place++) = '-'; break; - case KEY_DEL : place=(place>0 ? place - 1 : 0); *(wave_signal+place) = 0; break; + case KEY_F5 : *(note.wave_signal+place++) = '_'; break; + case KEY_F6 : *(note.wave_signal+place++) = '-'; break; + case KEY_DEL : place=(place>0 ? place - 1 : 0); *(note.wave_signal+place) = 0; break; case KEY_F1 : CallNote(); break; case KEY_F2 : CallSequence(); break; + case KEY_EXE : + timer_start(TIMER_USER, note.freq * place, Clock_Hz, PlayNote, place * note.freq * note.duration / 1000); + break; + case KEY_EXIT : StopTimer(); return 1; diff --git a/src/sound4calc.c b/src/sound4calc.c index 6a4b3fe..741618b 100644 --- a/src/sound4calc.c +++ b/src/sound4calc.c @@ -2,6 +2,8 @@ #include // from Gint #include //from Gint +static int strlen(char* str); + static struct { int freq; int length_ms; @@ -12,30 +14,33 @@ static struct { { -1, -1 } }; -char wave_signal[25] = {0}; // simple waveform -unsigned short duration = 2000; // equivalent as 2000 ms duration sound -unsigned int freq = 440; +struct Note note; +/* + CallSequence() + make a little sound sequence +*/ void CallSequence() { static int length_end = 0; - static int note = -1 ; + static int notes = -1 ; if(length_end<=0) { - note++; + notes++; //Stop the timer - if(seq[note].freq < 0) + if(seq[notes].freq < 0) { + length_end = 0; timer_stop(TIMER_USER); return; } // update of the number of turn - length_end = seq[note].freq * seq[note].length_ms / 1000; - - timer_start(TIMER_USER, 2 * seq[note].freq, Clock_Hz, CallSequence, 0); + length_end = strlen(note.wave_signal) * seq[notes].freq * seq[notes].length_ms / 1000; + // start or reload the timer + timer_start(TIMER_USER, 2 * seq[notes].freq, Clock_Hz, CallSequence, 0); } PlayNote(); @@ -44,38 +49,40 @@ void CallSequence() void CallNote() { - unsigned int wavelength = strlen(wave_signal); - - timer_start(TIMER_USER, freq * wavelength, Clock_Hz, PlayNote, freq * duration / 1000); + note.duration = 1000; + timer_start(TIMER_USER, note.freq * strlen(note.wave_signal), Clock_Hz, PlayNote, strlen(note.wave_signal) * note.freq * note.duration / 1000); } +/* + +*/ void StopTimer() { timer_stop(TIMER_USER); } /* - PlayNote() - allow to make different waveforms with '-' and '_' + PlayNote() + allow to make different waveforms with '-' and '_' */ void PlayNote() { static int x = 0; - PutPinState(wave_signal[x]=='-'); + PutPinState(note.wave_signal[x]=='-'); x = x + 1; - if(!(wave_signal[x])) x = 0; + if(!(note.wave_signal[x])) x = 0; } /* - PutPinState() - put the pin at the level wanted + PutPinState() + put the pin at the level wanted - SH4 : - state 0 : xxxx.01xx - state 1 : xxxx.10xx - SH3 : - state 0 : xxxx.xxx0 - state 1 : xxxx.xxx1 + SH4 : + state 0 : xxxx.01xx + state 1 : xxxx.10xx + SH3 : + state 0 : xxxx.xxx0 + state 1 : xxxx.xxx1 */ void PutPinState(char level) { @@ -93,9 +100,9 @@ void PutPinState(char level) /* - InitPorts(); - Ports initialisation - Open all need ports to use SwitchPinState() + InitPorts(); + Ports initialisation + Open all need ports to use SwitchPinState() */ void InitPorts() { @@ -145,3 +152,14 @@ void InitPorts() */ } } + +/* + Strings functions +*/ + +static int strlen(char *str) +{ + int i = 0; + while(*str++) i++; + return i; +}