Gugen展示版

Fork of MagneWave by kouzi osaki

MagneWave.cpp

Committer:
doctorura
Date:
2013-11-08
Revision:
7:b0bb700d8383
Parent:
6:d3e35dd7bfc5
Child:
8:34dc92a836a3

File content as of revision 7:b0bb700d8383:

#include "MagneWave.h"


#define DATA_OFFSET 44
#define DAC16_CENTER  32768
#define NEW_CODE

MagneWave::MagneWave( SoftMCP4922 *dac )
{
    m_wave_DAC = dac;
    m_wave_DAC->write_u16( DAC16_CENTER );
    
    m_wptr = 0;
    m_rptr = 0;
    m_dacOn = false;
    m_fileEnd = false;
    
    //NVIC_SetPriority(SysTick_IRQn, 1);
    tick.attach_us( this, &MagneWave::dacOut, 250 ); 
}

void MagneWave::play( FILE *wavefile )
{
    printf("seek\n");
    // jump to wave data
    fseek( wavefile, DATA_OFFSET, SEEK_SET );
    
    printf("1\n");
    m_dacOn = true;
    printf("2\n");
    m_fileEnd = false;
    
    printf("loop\n");
    while( m_dacOn ){
        if( m_rptr != ( m_wptr + 1 ) & 0x7f ){
#ifdef NEW_CODE
            int tmpL = 0;
            int tmpH = 0;
            long dacVal16 = 0;
            tmpL = fgetc( wavefile );
            if( tmpL == EOF ){
                printf("eofL\n.");
                m_fileEnd = true;
                break;
            }
            tmpH = fgetc( wavefile );
            if( tmpH == EOF ){
                printf("eofH\n.");
                m_fileEnd = true;
                break;
            }
            dacVal16 = tmpL;
            dacVal16 |= ( tmpH << 8 );
            dacVal16 += DAC16_CENTER;
            m_fifo[m_wptr]= ( unsigned short )dacVal16;
#else
            size_t readSize = fread( (char *)m_fifo[m_wptr], 2, 1, wavefile); 
            if( readSize < 1 ){
                m_fileEnd = true;
                break;
            }
#endif
            m_wptr = ( m_wptr + 1 ) & 0x7f;
        }else{
            printf("full\n");
        }
    }
    printf("off\n");
}

void MagneWave::dacOut()
{
    if ( m_dacOn ) {
        if( m_rptr != m_wptr ){
#ifdef NEW_CODE
            m_wave_DAC->write_u16( m_fifo[ m_rptr ] );
#else
            unsigned short tmpL = ( m_fifo[ m_rptr ] >> 8) & 0x00ff;
            unsigned short tmpH = ( m_fifo[ m_rptr ] << 8) & 0xff00;
            short dacVal16 = tmpH | tmpL;
            
            dacVal16 += DAC16_CENTER;
            m_wave_DAC->write_u16( dacVal16 );
#endif
            m_rptr = ( m_rptr + 1 ) & 0x7f;
        }else{
            printf("nohit r=%d,w=%d\n", m_rptr, m_wptr);
            //printf("nh\n");
            if( m_fileEnd ){
                printf("eof\n");
                m_wptr = 0;
                m_rptr = 0;
                m_dacOn = false;
                m_fileEnd = false;
                m_wave_DAC->write_u16( DAC16_CENTER );
            }
        }
    }
}