|
| 1 | +/* Copyright (c) 2018 FIRST. All rights reserved. |
| 2 | + * |
| 3 | + * Redistribution and use in source and binary forms, with or without modification, |
| 4 | + * are permitted (subject to the limitations in the disclaimer below) provided that |
| 5 | + * the following conditions are met: |
| 6 | + * |
| 7 | + * Redistributions of source code must retain the above copyright notice, this list |
| 8 | + * of conditions and the following disclaimer. |
| 9 | + * |
| 10 | + * Redistributions in binary form must reproduce the above copyright notice, this |
| 11 | + * list of conditions and the following disclaimer in the documentation and/or |
| 12 | + * other materials provided with the distribution. |
| 13 | + * |
| 14 | + * Neither the name of FIRST nor the names of its contributors may be used to endorse or |
| 15 | + * promote products derived from this software without specific prior written permission. |
| 16 | + * |
| 17 | + * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS |
| 18 | + * LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 20 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 22 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + */ |
| 29 | + |
| 30 | +package org.firstinspires.ftc.robotcontroller.external.samples; |
| 31 | + |
| 32 | +import android.content.Context; |
| 33 | + |
| 34 | +import com.qualcomm.ftccommon.SoundPlayer; |
| 35 | +import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; |
| 36 | +import com.qualcomm.robotcore.eventloop.opmode.TeleOp; |
| 37 | +import com.qualcomm.robotcore.eventloop.opmode.Disabled; |
| 38 | + |
| 39 | +/** |
| 40 | + * This file demonstrates how to play one of the several SKYSTONE/Star Wars sounds loaded into the SDK. |
| 41 | + * It does this by creating a simple "chooser" controlled by the gamepad Up Down buttons. |
| 42 | + * This code also prevents sounds from stacking up by setting a "playing" flag, which is cleared when the sound finishes playing. |
| 43 | + * |
| 44 | + * Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name. |
| 45 | + * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list |
| 46 | + * |
| 47 | + * Operation: |
| 48 | + * Use the DPAD to change the selected sound, and the Right Bumper to play it. |
| 49 | + */ |
| 50 | + |
| 51 | +@TeleOp(name="SKYSTONE Sounds", group="Concept") |
| 52 | +@Disabled |
| 53 | +public class ConceptSoundsSKYSTONE extends LinearOpMode { |
| 54 | + |
| 55 | + // List of available sound resources |
| 56 | + String sounds[] = {"ss_alarm", "ss_bb8_down", "ss_bb8_up", "ss_darth_vader", "ss_fly_by", |
| 57 | + "ss_mf_fail", "ss_laser", "ss_laser_burst", "ss_light_saber", "ss_light_saber_long", "ss_light_saber_short", |
| 58 | + "ss_light_speed", "ss_mine", "ss_power_up", "ss_r2d2_up", "ss_roger_roger", "ss_siren", "ss_wookie" }; |
| 59 | + boolean soundPlaying = false; |
| 60 | + |
| 61 | + @Override |
| 62 | + public void runOpMode() { |
| 63 | + |
| 64 | + // Variables for choosing from the available sounds |
| 65 | + int soundIndex = 0; |
| 66 | + int soundID = -1; |
| 67 | + boolean was_dpad_up = false; |
| 68 | + boolean was_dpad_down = false; |
| 69 | + |
| 70 | + Context myApp = hardwareMap.appContext; |
| 71 | + |
| 72 | + // create a sound parameter that holds the desired player parameters. |
| 73 | + SoundPlayer.PlaySoundParams params = new SoundPlayer.PlaySoundParams(); |
| 74 | + params.loopControl = 0; |
| 75 | + params.waitForNonLoopingSoundsToFinish = true; |
| 76 | + |
| 77 | + // In this sample, we will skip waiting for the user to press play, and start displaying sound choices right away |
| 78 | + while (!isStopRequested()) { |
| 79 | + |
| 80 | + // Look for DPAD presses to change the selection |
| 81 | + if (gamepad1.dpad_down && !was_dpad_down) { |
| 82 | + // Go to next sound (with list wrap) and display it |
| 83 | + soundIndex = (soundIndex + 1) % sounds.length; |
| 84 | + } |
| 85 | + |
| 86 | + if (gamepad1.dpad_up && !was_dpad_up) { |
| 87 | + // Go to previous sound (with list wrap) and display it |
| 88 | + soundIndex = (soundIndex + sounds.length - 1) % sounds.length; |
| 89 | + } |
| 90 | + |
| 91 | + // Look for trigger to see if we should play sound |
| 92 | + // Only start a new sound if we are currently not playing one. |
| 93 | + if (gamepad1.right_bumper && !soundPlaying) { |
| 94 | + |
| 95 | + // Determine Resource IDs for the sounds you want to play, and make sure it's valid. |
| 96 | + if ((soundID = myApp.getResources().getIdentifier(sounds[soundIndex], "raw", myApp.getPackageName())) != 0){ |
| 97 | + |
| 98 | + // Signal that the sound is now playing. |
| 99 | + soundPlaying = true; |
| 100 | + |
| 101 | + // Start playing, and also Create a callback that will clear the playing flag when the sound is complete. |
| 102 | + SoundPlayer.getInstance().startPlaying(myApp, soundID, params, null, |
| 103 | + new Runnable() { |
| 104 | + public void run() { |
| 105 | + soundPlaying = false; |
| 106 | + }} ); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Remember the last state of the dpad to detect changes. |
| 111 | + was_dpad_up = gamepad1.dpad_up; |
| 112 | + was_dpad_down = gamepad1.dpad_down; |
| 113 | + |
| 114 | + // Display the current sound choice, and the playing status. |
| 115 | + telemetry.addData("", "Use DPAD up/down to choose sound."); |
| 116 | + telemetry.addData("", "Press Right Bumper to play sound."); |
| 117 | + telemetry.addData("", ""); |
| 118 | + telemetry.addData("Sound >", sounds[soundIndex]); |
| 119 | + telemetry.addData("Status >", soundPlaying ? "Playing" : "Stopped"); |
| 120 | + telemetry.update(); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments