Merge of the 2 sound_5ms() and audio.heartbeat() functions. Perhaps will be reverted, depending on what Mike thinks of it...

This commit is contained in:
bsongis
2012-05-03 17:28:22 +00:00
parent 6e201bc4b6
commit 523f988b16
5 changed files with 108 additions and 153 deletions
+1 -1
View File
@@ -419,7 +419,7 @@ extern "C" void TC2_IRQHandler()
dummy = TC0->TC_CHANNEL[2].TC_SR; dummy = TC0->TC_CHANNEL[2].TC_SR;
(void) dummy ; // Discard value - prevents compiler warning (void) dummy ; // Discard value - prevents compiler warning
sound_5ms() ; AUDIO_HEARTBEAT();
if ( ++pre_scale >= 2 ) { if ( ++pre_scale >= 2 ) {
Tenms |= 1 ; // 10 mS has passed Tenms |= 1 ; // 10 mS has passed
+33 -39
View File
@@ -42,10 +42,6 @@ audioQueue::audioQueue()
t_queueWidx = 0; t_queueWidx = 0;
} }
#define QUEUE_TONE(tf,ttl,tfi) queueTone(tf * 61 / 2, ttl * 10, tfi * 61 / 2)
// heartbeat is responsibile for issueing the audio tones and general square waves // heartbeat is responsibile for issueing the audio tones and general square waves
// it is essentially the life of the class. // it is essentially the life of the class.
// it is called every 10ms // it is called every 10ms
@@ -55,58 +51,51 @@ void audioQueue::heartbeat()
return; return;
#endif #endif
if (toneTimeLeft == 0) { if (toneTimeLeft > 0) {
if (tonePause == 0) { if (toneChanged) {
if (t_queueRidx != t_queueWidx) { toneChanged = 0;
set_frequency(toneFreq);
tone_start(0);
}
else if (toneFreqIncr && (toneTimeLeft&1) == 0) {
toneFreq += toneFreqIncr;
set_frequency(toneFreq);
}
toneTimeLeft--; //time gets counted down
}
else {
if (tonePause > 0) {
DACC->DACC_IDR = DACC_IDR_ENDTX ; // Disable interrupt
// SPEAKER_OFF;
tonePause--; //time gets counted down
}
else if (t_queueRidx != t_queueWidx) {
toneChanged = 1;
toneFreq = queueToneFreq[t_queueRidx]; toneFreq = queueToneFreq[t_queueRidx];
toneTimeLeft = queueToneLength[t_queueRidx]; toneTimeLeft = queueToneLength[t_queueRidx];
toneFreqIncr = queueToneFreqIncr[t_queueRidx]; toneFreqIncr = queueToneFreqIncr[t_queueRidx];
tonePause = queueTonePause[t_queueRidx]; tonePause = queueTonePause[t_queueRidx];
if ((toneFreq == 0) || (toneTimeLeft == 0)) {
//SPEAKER_OFF;
}
if (!queueToneRepeat[t_queueRidx]--) { if (!queueToneRepeat[t_queueRidx]--) {
t_queueRidx = (t_queueRidx + 1) % AUDIO_QUEUE_LENGTH; t_queueRidx = (t_queueRidx + 1) % AUDIO_QUEUE_LENGTH;
} }
} }
else { else {
if ((tone2Freq == 0) & (tone2TimeLeft == 0)) {
//SPEAKER_OFF;
}
}
}
}
if ((toneFreq > 0) & (toneTimeLeft > 0)) {
QUEUE_TONE(toneFreq, toneTimeLeft, toneFreqIncr);
}
else if (((tone2Freq > 0) & (tone2TimeLeft > 0)) & (tonePause == 0)) {
//second flow tone here, priority on 1st, pause of 1st not allow to start second
QUEUE_TONE(tone2Freq, tone2TimeLeft, 0);
}
if (toneTimeLeft > 0) {
toneTimeLeft--; //time gets counted down
toneFreq += toneFreqIncr;
}
else {
if (tonePause > 0) {
// SPEAKER_OFF;
tonePause--; //time gets counted down
}
}
if (tone2TimeLeft > 0) { if (tone2TimeLeft > 0) {
if (tone2Changed) {
tone2Changed = 0;
set_frequency(toneFreq);
tone_start(0);
}
tone2TimeLeft--; //time gets counted down tone2TimeLeft--; //time gets counted down
} }
else { else {
if (toneTimeLeft == 0) { DACC->DACC_IDR = DACC_IDR_ENDTX ; // Disable interrupt
//SPEAKER_OFF;
}
if (tone2Pause > 0) { if (tone2Pause > 0) {
tone2Pause--; //time gets counted down tone2Pause--; //time gets counted down
} }
} }
}
}
} }
inline uint8_t audioQueue::getToneLength(uint8_t tLen) inline uint8_t audioQueue::getToneLength(uint8_t tLen)
@@ -124,7 +113,11 @@ inline uint8_t audioQueue::getToneLength(uint8_t tLen)
void audioQueue::play(uint8_t tFreq, uint8_t tLen, uint8_t tPause, void audioQueue::play(uint8_t tFreq, uint8_t tLen, uint8_t tPause,
uint8_t tFlags, int8_t tFreqIncr) uint8_t tFlags, int8_t tFreqIncr)
{ {
// tLen *= 10;
// tPause *= 10;
if (tFlags & PLAY_SOUND_VARIO) { if (tFlags & PLAY_SOUND_VARIO) {
tone2Changed = 1;
tone2Freq = tFreq; tone2Freq = tFreq;
tone2TimeLeft = tLen; tone2TimeLeft = tLen;
tone2Pause = tPause; tone2Pause = tPause;
@@ -133,6 +126,7 @@ void audioQueue::play(uint8_t tFreq, uint8_t tLen, uint8_t tPause,
tFreq += g_eeGeneral.speakerPitch + BEEP_OFFSET; // add pitch compensator tFreq += g_eeGeneral.speakerPitch + BEEP_OFFSET; // add pitch compensator
tLen = getToneLength(tLen); tLen = getToneLength(tLen);
if (tFlags & PLAY_NOW || (!busy() && empty())) { if (tFlags & PLAY_NOW || (!busy() && empty())) {
toneChanged = 1;
toneFreq = tFreq; toneFreq = tFreq;
toneTimeLeft = tLen; toneTimeLeft = tLen;
tonePause = tPause; tonePause = tPause;
+2
View File
@@ -73,12 +73,14 @@ class audioQueue
uint8_t t_queueRidx; uint8_t t_queueRidx;
uint8_t t_queueWidx; uint8_t t_queueWidx;
uint8_t toneChanged;
uint8_t toneFreq; uint8_t toneFreq;
int8_t toneFreqIncr; int8_t toneFreqIncr;
uint8_t toneTimeLeft; uint8_t toneTimeLeft;
uint8_t tonePause; uint8_t tonePause;
// vario // vario
uint8_t tone2Changed;
uint8_t tone2Freq; uint8_t tone2Freq;
uint8_t tone2TimeLeft; uint8_t tone2TimeLeft;
uint8_t tone2Pause; uint8_t tone2Pause;
+13 -54
View File
@@ -52,7 +52,7 @@ void audioDefevent( uint8_t e ) ;
extern uint32_t Master_frequency ; // TODO in a .h? extern uint32_t Master_frequency ; // TODO in a .h?
volatile uint8_t Buzzer_count ; volatile uint8_t Buzzer_count ;
/*
struct t_sound_globals struct t_sound_globals
{ {
uint32_t Next_freq ; uint32_t Next_freq ;
@@ -63,7 +63,7 @@ struct t_sound_globals
uint32_t Frequency_increment ; uint32_t Frequency_increment ;
uint32_t Next_frequency_increment ; uint32_t Next_frequency_increment ;
} Sound_g ; } Sound_g ;
*/
// Must NOT be in flash, PDC needs a RAM source. // Must NOT be in flash, PDC needs a RAM source.
uint16_t Sine_values[] = uint16_t Sine_values[] =
@@ -164,6 +164,8 @@ void set_frequency( uint32_t frequency )
register Tc *ptc ; register Tc *ptc ;
register uint32_t timer ; register uint32_t timer ;
frequency = frequency * 61 / 2;
timer = Master_frequency / (800 * frequency) ; // MCK/8 and 100 000 Hz timer = Master_frequency / (800 * frequency) ; // MCK/8 and 100 000 Hz
if ( timer > 65535 ) if ( timer > 65535 )
{ {
@@ -200,7 +202,7 @@ void start_timer1()
ptc->TC_CHANNEL[1].TC_CMR = 0x0009C001 ; // 0000 0000 0000 1001 1100 0000 0000 0001 ptc->TC_CHANNEL[1].TC_CMR = 0x0009C001 ; // 0000 0000 0000 1001 1100 0000 0000 0001
// MCK/8, set @ RA, Clear @ RC waveform // MCK/8, set @ RA, Clear @ RC waveform
ptc->TC_CHANNEL[1].TC_CCR = 5 ; // Enable clock and trigger it (may only need trigger) ptc->TC_CHANNEL[1].TC_CCR = 5 ; // Enable clock and trigger it (may only need trigger)
Sound_g.Frequency = 1000 ; // Sound_g.Frequency = 1000 ;
} }
@@ -243,13 +245,13 @@ extern "C" void DAC_IRQHandler()
DACC->DACC_TNPR = (uint32_t) Sine_values ; DACC->DACC_TNPR = (uint32_t) Sine_values ;
#endif #endif
DACC->DACC_TNCR = 50 ; // words, 100 16 bit values DACC->DACC_TNCR = 50 ; // words, 100 16 bit values
if ( Sound_g.Tone_timer ) /* if ( Sound_g.Tone_timer )
{ {
if ( --Sound_g.Tone_timer == 0 ) if ( --Sound_g.Tone_timer == 0 )
{ {
DACC->DACC_IDR = DACC_IDR_ENDTX ; DACC->DACC_IDR = DACC_IDR_ENDTX ;
} }
} } */
// if ( Tone_ms_timer == 0 ) // if ( Tone_ms_timer == 0 )
// { // {
// Tone_ms_timer = -1 ; // Tone_ms_timer = -1 ;
@@ -267,51 +269,7 @@ void end_sound()
PMC->PMC_PCER0 &= ~0x40000000L ; // Disable peripheral clock to DAC PMC->PMC_PCER0 &= ~0x40000000L ; // Disable peripheral clock to DAC
} }
// Called every 5mS from interrupt routine #if 0
void sound_5ms()
{
if ( Sound_g.Tone_ms_timer > 0 )
{
Sound_g.Tone_ms_timer -= 1 ;
}
if ( Sound_g.Tone_ms_timer == 0 )
{
if ( Sound_g.Sound_time )
{
Sound_g.Tone_ms_timer = ( Sound_g.Sound_time + 4 ) / 5 ;
if ( Sound_g.Next_freq ) // 0 => silence for time
{
Sound_g.Frequency = Sound_g.Next_freq ;
Sound_g.Frequency_increment = Sound_g.Next_frequency_increment ;
set_frequency( Sound_g.Frequency ) ;
tone_start( 0 ) ;
}
else
{
DACC->DACC_IDR = DACC_IDR_ENDTX ; // Silence
}
Sound_g.Sound_time = 0 ;
}
else
{
DACC->DACC_IDR = DACC_IDR_ENDTX ; // Disable interrupt
Sound_g.Tone_timer = 0 ;
}
}
else if ( ( Sound_g.Tone_ms_timer & 1 ) == 0 ) // Every 10 mS
{
if ( Sound_g.Frequency )
{
if ( Sound_g.Frequency_increment )
{
Sound_g.Frequency += Sound_g.Frequency_increment ;
set_frequency( Sound_g.Frequency ) ;
}
}
}
}
// frequency in Hz, time in mS // frequency in Hz, time in mS
void playTone( uint32_t frequency, uint32_t time ) void playTone( uint32_t frequency, uint32_t time )
{ {
@@ -322,7 +280,8 @@ void playTone( uint32_t frequency, uint32_t time )
// Tone_ms_timer = ( time + 4 ) / 5 ; // Tone_ms_timer = ( time + 4 ) / 5 ;
// tone_start( 0 ) ; // tone_start( 0 ) ;
} }
#endif
/*
uint32_t queueTone( uint32_t frequency, uint32_t time, uint32_t frequency_increment ) uint32_t queueTone( uint32_t frequency, uint32_t time, uint32_t frequency_increment )
{ {
if ( Sound_g.Sound_time == 0 ) if ( Sound_g.Sound_time == 0 )
@@ -334,19 +293,19 @@ uint32_t queueTone( uint32_t frequency, uint32_t time, uint32_t frequency_increm
} }
return 0 ; return 0 ;
} }
*/
// Time is in milliseconds // Time is in milliseconds
void tone_start( register uint32_t time ) void tone_start( register uint32_t time )
{ {
PMC->PMC_PCER0 |= 0x40000000L ; // Enable peripheral clock to DAC PMC->PMC_PCER0 |= 0x40000000L ; // Enable peripheral clock to DAC
Sound_g.Tone_timer = Sound_g.Frequency * time / 1000 ; // Sound_g.Tone_timer = Sound_g.Frequency * time / 1000 ;
DACC->DACC_IER = DACC_IER_ENDTX ; DACC->DACC_IER = DACC_IER_ENDTX ;
} }
void tone_stop() void tone_stop()
{ {
DACC->DACC_IDR = DACC_IDR_ENDTX ; // Disable interrupt DACC->DACC_IDR = DACC_IDR_ENDTX ; // Disable interrupt
Sound_g.Tone_timer = 0 ; // Sound_g.Tone_timer = 0 ;
} }
+1 -1
View File
@@ -2093,7 +2093,7 @@ void perMain()
#if defined(PCBARM) #if defined(PCBARM)
HAPTIC_HEARTBEAT(); HAPTIC_HEARTBEAT();
AUDIO_HEARTBEAT(); // the queue processing // AUDIO_HEARTBEAT(); // the queue processing
#endif #endif
} }