Masahiko Hasebe / microbit_ymf825

Dependencies:   microbit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fmasgn.c Source File

fmasgn.c

00001 //  fmasgn.c
00002 #include "fmtype.h"
00003 #include "fmasgn.h"
00004 #include "fmvoice.h"
00005 
00006 //  Variable
00007 static Fmvoice  _fmvc[MAX_FM_VOICE];
00008 static Fmvoice* _firstEmptyVc;      //  old
00009 static Fmvoice* _lastEmptyVc;       //  new
00010 static Fmvoice* _firstOccupiedVc;   //  old
00011 static Fmvoice* _lastOccupiedVc;    //  new
00012 
00013 //  Prototype
00014 static void setToEmptyList( Fmvoice* prevVc, Fmvoice* rlsVc );
00015 
00016 //  setter
00017 void Asgn_setFirstEmptyVc( Fmvoice* vc ){ _firstEmptyVc = vc; }
00018 void Asgn_setLastEmptyVc( Fmvoice* vc ){ _lastEmptyVc = vc; }
00019 
00020 //  getter
00021 Fmvoice* Asgn_voice( int num ){ return &(_fmvc[num]); }
00022 Fmvoice* Asgn_firstEmptyVc( void ){ return _firstEmptyVc; }
00023 Fmvoice* Asgn_lastEmptyVc( void ){ return _lastEmptyVc; }
00024 
00025 void Asgn_init( void )
00026 {
00027     int     i;
00028 
00029     for ( i=0; i<MAX_FM_VOICE; i++) {
00030         Fmvoice_init(&_fmvc[i]);
00031     }
00032 
00033     _firstOccupiedVc = 0;
00034     _lastOccupiedVc = 0;
00035 
00036     for ( i=0; i<MAX_FM_VOICE-1; i++ ){
00037         Fmvoice_setVoiceNum(&_fmvc[i], i);
00038         Fmvoice_setNextVc(&_fmvc[i], &_fmvc[i + 1]);
00039     }
00040 
00041     //  for No.MAX_FM_VOICE-1
00042     Fmvoice_setVoiceNum(&_fmvc[MAX_FM_VOICE - 1], MAX_FM_VOICE - 1);
00043     Fmvoice_setNextVc(&_fmvc[MAX_FM_VOICE - 1], FMNULL);
00044 
00045     _firstEmptyVc = &_fmvc[0];
00046     _lastEmptyVc = &_fmvc[MAX_FM_VOICE-1];
00047 }
00048 bool Asgn_chkEmpty( void )
00049 {
00050     if ( _firstEmptyVc == FMNULL ){ return false; }
00051     else { return true; }
00052 }
00053 Fmvoice* Asgn_getEmptyVc( void )
00054 {
00055     if ( _firstEmptyVc != FMNULL ){
00056         Fmvoice* ret = _firstEmptyVc;
00057         _firstEmptyVc = Fmvoice_nextVc(_firstEmptyVc);
00058         if ( _lastOccupiedVc != FMNULL ){
00059             Fmvoice_setNextVc(_lastOccupiedVc, ret);
00060         }
00061         _lastOccupiedVc = ret;
00062         if ( _firstOccupiedVc == FMNULL ){
00063             _firstOccupiedVc = ret;
00064         }
00065         return ret;
00066     }
00067     else {
00068         FMASSERT(0);
00069         return FMNULL;
00070     }
00071 }
00072 void Asgn_releaseOneVc( void )
00073 {
00074     if ( _firstOccupiedVc == FMNULL ){
00075         FMASSERT(0);
00076         return;
00077     }
00078 
00079     //  Search keyoffed Voice
00080     Fmvoice* rlsVc = _firstOccupiedVc;
00081     Fmvoice* prevVc = FMNULL;
00082     while (rlsVc != FMNULL ){
00083         if ( Fmvoice_isKeyon(rlsVc) == false ){
00084             break;
00085         }
00086         prevVc = rlsVc;
00087         rlsVc = Fmvoice_nextVc(rlsVc);
00088     }
00089 
00090     //  if no keyoffed vc, select first one.
00091     if ( rlsVc == FMNULL ){
00092         rlsVc = _firstOccupiedVc;
00093     }
00094 
00095     setToEmptyList(prevVc,rlsVc);
00096 }
00097 void Asgn_releaseParticularVc( Fmvoice* pVc )
00098 {
00099     //  Search pVc & its prevVc
00100     Fmvoice* rlsVc = _firstOccupiedVc;
00101     Fmvoice* prevVc = FMNULL;
00102     while ( rlsVc != FMNULL ){
00103         if ( pVc == rlsVc ){
00104             break;
00105         }
00106         prevVc = rlsVc;
00107         rlsVc = Fmvoice_nextVc(rlsVc);
00108     }
00109     setToEmptyList(prevVc,rlsVc);
00110 }
00111 static void setToEmptyList( Fmvoice* prevVc, Fmvoice* rlsVc )
00112 {
00113     //  Release from Occupied list
00114     if ( rlsVc == _firstOccupiedVc ){
00115         _firstOccupiedVc = Fmvoice_nextVc(rlsVc);
00116     }
00117     if ( rlsVc == _lastOccupiedVc ){
00118         _lastOccupiedVc = prevVc;
00119     }
00120     if ( prevVc != FMNULL ){
00121         Fmvoice_setNextVc(prevVc, Fmvoice_nextVc(rlsVc));
00122     }
00123     Fmvoice_release(rlsVc);
00124 
00125     //  Set Empty list
00126     Fmvoice_setNextVc(_lastEmptyVc, rlsVc);
00127     _lastEmptyVc = rlsVc;
00128     if ( _firstEmptyVc == FMNULL ){
00129         _firstEmptyVc = rlsVc;
00130     }
00131 }