// Converted from the WavFilePlay from the Teensy release:
// hardware/teensy/avr/libraries/Audio/examples/WavFilePlayer/WavFilePlayer.ino
//
// Simple RAW file player example for the prop shield to use the Analog DAC
// and prop shield amplifier to play mono sounds.
// http://www.pjrc.com/teensy/gui/?info=AudioOutputAnalog
//
// On the prop shield, pin 6 selects the serial flash memory controller,
// and pin 5 enables the amplifier.
//
// This example code is in the public domain.
// After the sound plays do a round of neopixels
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Adafruit_NeoPixel.h>
// GUItool: begin automatically generated code
AudioPlaySerialflashRaw playFlashRaw1; //xy=149,388
AudioMixer4 mixer1; //xy=445,386
AudioOutputAnalog dac1; //xy=591,379
AudioConnection patchCord1(playFlashRaw1, 0, mixer1, 0);
AudioConnection patchCord2(mixer1, dac1);
// GUItool: end automatically generated code
#define PROP_AMP_ENABLE 5
#define FLASH_CHIP_SELECT 6
#define PIN_PROP_LED_ENABLE 7 // Pin to enable 11/13 as outputs
#define PIN_PROP_LED_DATA 11 // Pin #1 for 5v LEDs (data for APA102/dotstar leds)
#define PIN_PROP_LED_CLK 13 // Pin #2 for 5v LEDs (clock for APA102/dotstar leds)
#define PIN_NEOPIXEL PIN_PROP_LED_DATA // PIN_PROP_LED_DATA to use prop shield level shifter
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN_PROP_LED_DATA, NEO_GRB + NEO_KHZ800);
int cur_color;
SPISettings neopixel_spi (20000000, MSBFIRST, SPI_MODE0);
void do_ring ()
{
int red, green, blue;
red = (cur_color & 0x4) ? 5 : 0;
green = (cur_color & 0x2) ? 5 : 0;
blue = (cur_color & 0x1) ? 5 : 0;
Serial.printf ("Neopixel, red = %d, green = %d, blue = %d\n", red, green, blue);
// Enable LEDs and put the pin back into digital mode
if (PIN_NEOPIXEL == PIN_PROP_LED_DATA || PIN_NEOPIXEL == PIN_PROP_LED_CLK)
{
SPI.beginTransaction (neopixel_spi);
digitalWrite (PIN_PROP_LED_ENABLE, HIGH);
pinMode (PIN_NEOPIXEL, OUTPUT);
}
// Set the ring to a given color
for (int i = 0; i < 16; i++)
strip.setPixelColor (i, red, green, blue);
strip.show ();
Serial.println ("Sleep 5 seconds");
delay (5000);
Serial.println ("Done sleep");
cur_color = (cur_color == 0x4) ? 0x1 : cur_color<<1;
// Return pins 11/13 to be in SPI mode and disable the LEDs
if (PIN_NEOPIXEL == PIN_PROP_LED_DATA || PIN_NEOPIXEL == PIN_PROP_LED_CLK)
{
volatile uint32_t *reg;
SPI.endTransaction ();
digitalWrite (PIN_PROP_LED_ENABLE, LOW);
reg = portConfigRegister (PIN_NEOPIXEL);
*reg = PORT_PCR_MUX (2);
}
}
void setup()
{
Serial.begin (9600);
// wait up to 3 seconds for the Serial device to become available
long unsigned debug_start = millis ();
while (!Serial && ((millis () - debug_start) <= 3000))
;
Serial.println ("Start");
// Enable the neopixels
if (PIN_NEOPIXEL == PIN_PROP_LED_DATA || PIN_NEOPIXEL == PIN_PROP_LED_CLK)
{
SPI.begin ();
pinMode (PIN_PROP_LED_ENABLE, OUTPUT);
digitalWrite (PIN_PROP_LED_ENABLE, LOW);
}
strip.begin ();
cur_color = 0x1;
// Enable the amplifier on the prop shield
pinMode (PROP_AMP_ENABLE, OUTPUT);
digitalWrite (PROP_AMP_ENABLE, HIGH);
// Do all color variations of the ring before enabling the sound
for (int i = 0; i < 3; i++)
do_ring ();
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory (8);
// Set initial volume
mixer1.gain (0, 0.5f);
// Start SerialFlash
if (!SerialFlash.begin(FLASH_CHIP_SELECT)) {
while (1)
{
Serial.println ("Cannot access SPI Flash chip");
delay (1000);
}
}
}
void playFile(const char *filename)
{
// Start playing the file. This sketch continues to
// run while the file plays.
playFlashRaw1.play(filename);
// A brief delay for the library read RAW info
delay(5);
// Simply wait for the file to finish playing.
while (playFlashRaw1.isPlaying()) {
;
}
Serial.println ("Done playing");
}
void loop()
{
char filename[12];
uint32_t filesize;
SerialFlash.opendir ();
while (SerialFlash.readdir (filename, sizeof (filename), filesize))
{
const char *dot = strchr (filename, '.');
bool raw_p = (dot && strcmp (dot, ".RAW") == 0);
Serial.printf ("%s %-12s%7lu bytes\n",
(raw_p ? "Playing " : "Skipping"),
filename,
(unsigned long) filesize);
if (raw_p)
{
playFile (filename);
do_ring ();
}
}
Serial.println ("Sleep for 30 seconds");
delay (30000);
}