terça-feira, 10 de junho de 2014

Código-fonte: functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

TMRpcm player;

/*
 * AVISO: Código duplicado à vista!
 */

void playNote(MUX mux)
{
  int z_val = mux.readZ() >= 1015;
  int mux_y = mux.getY();
  
  static int active_mux = 0;
  static int active_y = -1;
  
  if (z_val && !player.isPlaying())
  {
    active_y = mux_y;
    active_mux = mux.id();
    
    if (active_mux == 1)
    {
      switch (mux_y)
      {
        case 0:
          player.play("CF3/CF3_C4.wav");
          break;
        case 1:
          player.play("CF3/CF3_Cs4.wav");
          break;
        case 2:
          player.play("CF3/CF3_D4.wav");
          break;
        case 3:
          player.play("CF3/CF3_Ds4.wav");
          break;
        case 4:
          player.play("CF3/CF3_E4.wav");
          break;
        case 5:
          player.play("CF3/CF3_F4.wav");
          break;
        case 6:
          player.play("CF3/CF3_Fs4.wav");
          break;
        case 7:
          player.play("CF3/CF3_G4.wav");
          break;
      }
    }
    else if (active_mux == 2)
    {
      switch (mux_y)
      {
        case 0:
          player.play("CF3/CF3_Gs4.wav");
          break;
        case 1:
          player.play("CF3/CF3_A4.wav");
          break;
        case 2:
          player.play("CF3/CF3_As4.wav");
          break;
        case 3:
          player.play("CF3/CF3_B4.wav");
          break;
        case 4:
          player.play("CF3/CF3_C5.wav");
          break;
        case 5:
          player.play("CF3/CF3_Cs5.wav");
          break;
        case 6:
          player.play("CF3/CF3_D5.wav");
          break;
        case 7:
          player.play("CF3/CF3_Ds5.wav");
          break;
      }
    }
    else if (active_mux == 3)
    {
      switch (mux_y)
      {
        case 0:
          player.play("CF3/CF3_E5.wav");
          break;
        case 1:
          player.play("CF3/CF3_F5.wav");
          break;
        case 2:
          player.play("CF3/CF3_Fs5.wav");
          break;
        case 3:
          player.play("CF3/CF3_G5.wav");
          break;
        case 4:
          player.play("CF3/CF3_Gs5.wav");
          break;
        case 5:
          player.play("CF3/CF3_A5.wav");
          break;
        case 6:
          player.play("CF3/CF3_As5.wav");
          break;
        case 7:
          player.play("CF3/CF3_B5.wav");
          break;
      }
    }
  }
  else if (z_val == 0 && mux_y == active_y && active_mux == mux.id())
  {
    player.stopPlayback();
    active_y = -1;
    active_mux = 0;
  }
}

void toneNote(MUX mux)
{
  int z_val = mux.readZ() >= 1015;
  int mux_y = mux.getY();
  
  static int active_mux = 0;
  static int active_y = -1;
  
  if (z_val)
  {
    active_y = mux_y;
    active_mux = mux.id();
    
    if (active_mux == 1)
    {
      switch (mux_y)
      {
        case 0:
          tone(46, NOTE_C4);
          break;
        case 1:
          tone(46, NOTE_CS4);
          break;
        case 2:
          tone(46, NOTE_D4);
          break;
        case 3:
          tone(46, NOTE_DS4);
          break;
        case 4:
          tone(46, NOTE_E4);
          break;
        case 5:
          tone(46, NOTE_F4);
          break;
        case 6:
          tone(46, NOTE_FS4);
          break;
        case 7:
          tone(46, NOTE_G4);
          break;
      }
    }
    else if (active_mux == 2)
    {
      switch (mux_y)
      {
        case 0:
          tone(46, NOTE_GS4);
          break;
        case 1:
          tone(46, NOTE_A4);
          break;
        case 2:
          tone(46, NOTE_AS4);
          break;
        case 3:
          tone(46, NOTE_B4);
          break;
        case 4:
          tone(46, NOTE_C5);
          break;
        case 5:
          tone(46, NOTE_CS5);
          break;
        case 6:
          tone(46, NOTE_D5);
          break;
        case 7:
          tone(46, NOTE_DS5);
          break;
      }
    }
    else if (active_mux == 3)
    {
      switch (mux_y)
      {
        case 0:
          tone(46, NOTE_E5);
          break;
        case 1:
          tone(46, NOTE_F5);
          break;
        case 2:
          tone(46, NOTE_FS5);
          break;
        case 3:
          tone(46, NOTE_G5);
          break;
        case 4:
          tone(46, NOTE_GS5);
          break;
        case 5:
          tone(46, NOTE_A5);
          break;
        case 6:
          tone(46, NOTE_AS5);
          break;
        case 7:
          tone(46, NOTE_B5);
          break;
      }
    }
  }
  else if (z_val == 0 && mux_y == active_y && active_mux == mux.id())
  {
    noTone(46);
    active_y = -1;
    active_mux = 0;
  }
}

void checkEaster(MUX mux1, MUX mux2)
{
  int z1, z2, y1, y2;
  y1 = mux1.getY();
  z1 = mux1.readZ() >= 1015;
  
  y2 = mux2.getY();
  z2 = mux2.readZ() >= 1015;
  
  if (z1 && z2 && y1 ==0 && y2 == 0)
  {
    noTone(46);
    if (player.isPlaying()) player.stopPlayback();
    digitalWrite(13, !digitalRead(13));
    delay(1000);
  }
}


#endif

Nenhum comentário:

Postar um comentário