This program plays a sound effect when a button is pressed. If you have an Interface Board plugged into the Fox Board then button P1.0 and button P1.1 can be used to trigger the sound effect. If not, then you will need to connect buttons or switches to P1.0 and P1.1.
Download the program files: simple_SFX_trigger.zip
HOW THE PROGRAM WORKS
In the [device] section, we declare the SNC21030 device. There is one short sound effect in the program which doesn't take up much memory, so we can use the smallest device.
In the [key] section, we set the number of direct inputs to 2. One button is connected to pin P1.0 and one button is connected to pin P1.1.
In the [input_states] section, we define two input states. One is "locked_out" and we set the P1.0 and P1.1 inputs to "x". This means that when the "locked_out" input state is set, pins P1.0 and P1.1 will be ignored. The other state is called "enabled" where a trigger on P1.0 will jump the program to the button1 label and a trigger on P1.1 will jump the program to the button2 label.
The [paths] section contains the various routines used in the program.
init: The "init" label always gets called first on power up and on reset. The first line in the init routine sets the input state to "locked_out". This disables the input pins. The freq=12k command sets the sample playback rate of the audio to 12 kHz. We then set the "enabled" input state which enables the inputs so that the buttons are active. The end command stops the program and puts the chip into low power mode. Even though the chip is asleep, the inputs are active and can be used to wake up the chip and trigger the program.
button1: This routine is called any time the button connected to P1.0 is pressed. When this routine is called the lasershot1_12k.wav file will play. After the sound effect plays the program stops and the chip goes into low power mode (end command). Note that while the sound effect is playing, the inputs are still active so the P1.0 button can be pressed again, which will interrupt the sound effect and restart it from the beginning.
button2: This routine is called any time the button connected to P1.1 is pressed. When this routine is called the "locked_out" input state is set before the lasershot1_12k.wav file plays. Because of this, the buttons are ignored while the sound effect is playing, so the sound effect cannot be interrupted. After the sound effect plays the "enabled" input state is set so that the buttons become active again. After the sound effect plays the program stops and the chip goes into low power mode (end command).