예제 #1
0
        async Task TurnOnLED(int index, int duration = 400)
        {
            leds[index].IsOn = true;
            await speaker.PlayTone(notes[index], duration);

            leds[index].IsOn = false;
        }
예제 #2
0
        static void StartGameLoop()
        {
            while (true)
            {
                graphics.Clear();

                game.Update();
                //draw food
                graphics.DrawPixel(game.FoodPosition.X, game.FoodPosition.Y);

                //draw food
                for (int i = 0; i < game.SnakePosition.Count; i++)
                {
                    var point = (Point)game.SnakePosition[i];

                    graphics.DrawPixel(point.X, point.Y);
                }

                if (game.PlaySound)
                {
                    speaker.PlayTone(440, 25);
                }

                //show
                graphics.Show();

                Thread.Sleep(250 - game.Level * 5);
            }
        }
예제 #3
0
        void CheckInput()
        {
            if (portLeft.State == false)
            {
                if (currentColumn > 0)
                {
                    currentColumn -= 1;
                }
            }
            else if (portRight.State == false)
            {
                if (currentColumn < connectGame.Width - 1)
                {
                    currentColumn += 1;
                }
            }
            else if (portDown.State == false)
            {
                connectGame.AddChip(currentColumn);
                speaker?.PlayTone(440, 200);
            }

            /*   else if (portReset.State == true)
             * {
             *     connectGame.Reset();
             * } */
        }
 void ButtonPressStarted(object sender, EventArgs e)
 {
     piezo.PlayTone(440);
     stopWatch.Reset();
     stopWatch.Start();
     timer.Stop();
 }
예제 #5
0
        public static void Main()
        {
            const int NUMBER_OF_NOTES = 16;

            float[] melody = new float[NUMBER_OF_NOTES]
            {
                NoteFrequencies.NOTE_A3,
                NoteFrequencies.NOTE_B3,
                NoteFrequencies.NOTE_CS4,
                NoteFrequencies.NOTE_D4,
                NoteFrequencies.NOTE_E4,
                NoteFrequencies.NOTE_FS4,
                NoteFrequencies.NOTE_GS4,
                NoteFrequencies.NOTE_A4,
                NoteFrequencies.NOTE_A4,
                NoteFrequencies.NOTE_GS4,
                NoteFrequencies.NOTE_FS4,
                NoteFrequencies.NOTE_E4,
                NoteFrequencies.NOTE_D4,
                NoteFrequencies.NOTE_CS4,
                NoteFrequencies.NOTE_B3,
                NoteFrequencies.NOTE_A3,
            };

            var piezo = new PiezoSpeaker(N.PWMChannels.PWM_PIN_D5);

            while (true)
            {
                for (int i = 0; i < NUMBER_OF_NOTES; i++)
                {
                    //PlayTone with a duration in synchronous
                    piezo.PlayTone(melody[i], 600);
                    Thread.Sleep(50);
                }

                Thread.Sleep(1000);

                //PlayTone without a duration will return immediately and play the tone
                piezo.PlayTone(NoteFrequencies.NOTE_A4);
                Thread.Sleep(2000);

                //call StopTone to end a tone started without a duration
                piezo.StopTone();

                Thread.Sleep(2000);
            }
        }
예제 #6
0
 protected void TestPiezoSpeaker()
 {
     while (true)
     {
         Console.WriteLine("Playing A4 note!");
         piezoSpeaker.PlayTone(440, 1000);
         piezoSpeaker.StopTone();
         Thread.Sleep(500);
     }
 }
예제 #7
0
        private void StartMetronome()
        {
            if (isThreadActive)
            {
                return;
            }

            isPlaying = true;

            interval = 60000f / BPM; //ms
            lastTick = DateTime.Now;

            var thread = new Thread(() =>
            {
                isThreadActive = true;
                while (isPlaying)
                {
                    lastTick = DateTime.Now;

                    leds[step].IsOn = true;
                    if (step == 3)
                    {
                        speaker.PlayTone(440, 50);
                    }
                    else
                    {
                        speaker.PlayTone(220, 50);
                    }
                    Thread.Sleep((int)interval);

                    if (step == 3)
                    {
                        SetAllLEDs(false);
                    }

                    step = (step + 1) % 4;
                }
                isThreadActive = false;
            });

            thread.Start();
        }
예제 #8
0
 void CheckInput()
 {
     if (buttonUp.State == true)
     {
         frogger.OnUp();
         speaker.PlayTone(440, 100);
     }
     else if (buttonLeft.State == true)
     {
         frogger.OnLeft();
         speaker.PlayTone(440, 100);
     }
     else if (buttonRight.State == true)
     {
         frogger.OnRight();
         speaker.PlayTone(440, 100);
     }
     else if (buttonDown.State == true)
     {
         frogger.OnDown();
         speaker.PlayTone(440, 100);
     }
 }
        void PlayNote(char note, int duration)
        {
            char[] names = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
            int[]  tones = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

            // play the tone corresponding to the note name
            for (int i = 0; i < names.Length; i++)
            {
                if (names[i] == note)
                {
                    piezoSpeaker.PlayTone(tones[i], duration);
                }
            }
        }
예제 #10
0
        private void ShowGameOverAnimation()
        {
            if (isAnimating)
            {
                return;
            }

            isAnimating = true;

            if (spdt.IsOn)
            {
                speaker.PlayTone(123.47f, 750);
            }

            for (int i = 0; i < 20; i++)
            {
                SetAllLEDs(false);
                Thread.Sleep(50);
                SetAllLEDs(true);
                Thread.Sleep(50);
            }

            isAnimating = false;
        }
예제 #11
0
 private void ToggleNote(int index, bool play, bool shift)
 {
     if (play)
     {
         var frequency = notes[index + (shift ? 4 : 0)];
         speaker.PlayTone(frequency);
         leds[index].IsOn = true;
         DrawText(frequency + " hz");
     }
     else
     {
         speaker.StopTone();
         leds[index].IsOn = false;
         display.Clear(true);
     }
 }
예제 #12
0
        void StartGameLoop()
        {
            while (spdt.IsOn)
            {
                graphics.Clear(false);
                graphics.DrawFilledCircle(ballX, ballY, ballRadius);
                graphics.DrawFilledRectangle(playerX, playerY, paddleWidth, paddleHeight);
                graphics.DrawFilledRectangle(cpuX, cpuY, paddleWidth, paddleHeight);

                graphics.DrawText(0, 0, 0, playerScore.ToString());
                graphics.DrawText(120, 0, 0, cpuScore.ToString());

                graphics.Show();

                //move player
                if (buttons[0].State == false)
                {
                    if (playerY > 0)
                    {
                        playerY -= 4;
                    }
                }
                else if (buttons[1].State == false)
                {
                    if (playerY - paddleHeight < 64)
                    {
                        playerY += 4;
                    }
                }

                //move cpu
                if (cpuY < ballY)
                {
                    cpuY++;
                }
                else if (cpuY > ballY)
                {
                    cpuY--;
                }

                ballX += ballXVelocity;
                if (ballY >= playerY &&
                    ballY <= playerY + paddleHeight &&
                    ballX - ballRadius <= playerX)
                {
                    ballXVelocity *= -1;
                    ballX         += ballXVelocity;
                    speaker.PlayTone(300, 25);
                }
                else if (ballX - ballRadius < 0)
                {
                    cpuScore++;
                    ballXVelocity *= -1;
                    ballX         += 64;
                    ballY          = 32;
                    speaker.PlayTone(440, 25);
                    ShowGameOverAnimation();
                }
                else if (ballY >= cpuY &&
                         ballY <= cpuY + paddleHeight &&
                         ballX + ballRadius > cpuX)
                {
                    ballXVelocity *= -1;
                    ballX         += ballXVelocity;
                    speaker.PlayTone(300, 25);
                }
                else if (ballX + ballRadius > 128)
                {
                    playerScore++;
                    ballXVelocity *= -1;
                    ballX          = 64;
                    ballY          = 32;
                    speaker.PlayTone(440, 25);
                    ShowGameOverAnimation();
                }

                ballY += ballYVelocity;
                if (ballY - ballRadius < 0)
                {
                    ballYVelocity *= -1;
                    ballY         += ballYVelocity;
                    speaker.PlayTone(220, 25);
                }
                else if (ballY + ballRadius > 64)
                {
                    ballYVelocity *= -1;
                    ballY         += ballYVelocity;
                    speaker.PlayTone(220, 25);
                }

                Thread.Sleep(25);
            }
        }
예제 #13
0
        private void PlaySong(Song song)
        {
            //smallest note length is a 32nd note 
            //change value to adjust tempo 
            var len32Note = 1500 / 32;

            //index of the currently playing note
            int melodyIndex = 0;
            int bassIndex = 0;

            //remaining steps for the currently playing note
            int melodyRemaining = 0;
            int bassRemaining = 0;
           
            //loop until we've played every melody and bass note
            while (melodyIndex < song.Melody.Length && 
                   bassIndex < song.Bass.Length)
            {
                if (melodyRemaining == 0 && melodyIndex < song.Melody.Length)
                {
                    speakerMelody.StopTone();
                    ledMelody.IsOn = false;

                    //get the length of the next note
                    melodyRemaining = song.Melody[melodyIndex].Length;

                    //if the note isn't silence (i.e. don't play rests)
                    if (song.Melody[melodyIndex].Pitch != 0)
                    {
                        speakerMelody.PlayTone(song.Melody[melodyIndex].Pitch);
                        ledMelody.IsOn = true;
                    }
                    melodyIndex++;
                }
                melodyRemaining--;

                if (bassRemaining == 0 && bassIndex < song.Bass.Length)
                {
                    speakerBass.StopTone();
                    ledBass.IsOn = false;

                    bassRemaining = song.Bass[bassIndex].Length;

                    if (song.Bass[bassIndex].Pitch != 0)
                    {
                        speakerBass.PlayTone(song.Bass[bassIndex].Pitch);
                        ledBass.IsOn = true;
                    }
                    bassIndex++;    
                }
                bassRemaining--;

                Thread.Sleep(len32Note);
            }

            Thread.Sleep(len32Note * 32);

            ledMelody.IsOn = false;
            ledBass.IsOn = false;

            speakerMelody.StopTone();
            speakerBass.StopTone();
        }
예제 #14
0
 private void PlayBuzzer()
 {
     speaker.PlayTone(notes[0], 50);
 }
예제 #15
0
 void TurnOnLED(int index, int duration = 400)
 {
     leds[index].IsOn = true;
     speaker.PlayTone(notes[index], duration);
     leds[index].IsOn = false;
 }