|
| 1 | +// SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +// Random HAL demo; adapted from PiSpeak sketch. When button |
| 6 | +// on A0 is pressed, plays a random WAV file from a list. |
| 7 | + |
| 8 | +#include <WaveHC.h> |
| 9 | +#include <WaveUtil.h> |
| 10 | + |
| 11 | +// REPLACE THESE WITH YOUR ACTUAL WAVE FILE NAMES: |
| 12 | +// These should be at the root level, not in a folder. |
| 13 | +static const char PROGMEM |
| 14 | + file00[] = "0.wav", |
| 15 | + file01[] = "1.wav", |
| 16 | + file02[] = "2.wav", |
| 17 | + file03[] = "3.wav", |
| 18 | + file04[] = "4.wav", |
| 19 | + file05[] = "5.wav", |
| 20 | + file06[] = "6.wav", |
| 21 | + file07[] = "7.wav", |
| 22 | + file08[] = "8.wav", |
| 23 | + file09[] = "9.wav"; |
| 24 | +// If adding files above, include corresponding items here: |
| 25 | +static const char * const filename[] PROGMEM = { |
| 26 | + file00, file01, file02, file03, file04, |
| 27 | + file05, file06, file07, file08, file09 }; |
| 28 | +// Sorry for the sillyness, but this is how PROGMEM string |
| 29 | +// arrays are handled. |
| 30 | + |
| 31 | +#define error(msg) error_P(PSTR(msg)) |
| 32 | +SdReader card; |
| 33 | +FatVolume vol; |
| 34 | +FatReader root; |
| 35 | +FatReader file; |
| 36 | +WaveHC wave; |
| 37 | +uint8_t debounce = 0, // Button debounce counter |
| 38 | + prev = 255; // Index of last sound played |
| 39 | + |
| 40 | +void setup() { |
| 41 | + Serial.begin(9600); |
| 42 | + if(!card.init()) error("Card init. failed!"); |
| 43 | + if(!vol.init(card)) error("No partition!"); |
| 44 | + if(!root.openRoot(vol)) error("Couldn't open dir"); |
| 45 | + // PgmPrintln("Files found:"); |
| 46 | + // root.ls(); |
| 47 | + |
| 48 | + digitalWrite(A0, HIGH); // Enable pullup on button |
| 49 | + randomSeed(analogRead(A1)); // Randomize first sound |
| 50 | +} |
| 51 | + |
| 52 | +void loop() { |
| 53 | + if(digitalRead(A0) == HIGH) { // Button not pressed |
| 54 | + debounce = 0; // Reset debounce counter |
| 55 | + return; // and nothing else |
| 56 | + } |
| 57 | + |
| 58 | + if(++debounce >= 20) { // Debounced button press |
| 59 | + uint8_t n; |
| 60 | + char name[20]; |
| 61 | + |
| 62 | + do { // Choose a random file... |
| 63 | + n = random(sizeof(filename) / sizeof(filename[0])); |
| 64 | + } while(n == prev); // ...but don't repeat last one |
| 65 | + |
| 66 | + prev = n; // Save file # |
| 67 | + debounce = 0; // Reset debounce counter |
| 68 | + strcpy_P(name, (char *)pgm_read_word(&filename[n])); // PROGMEM->RAM |
| 69 | + if(wave.isplaying) wave.stop(); // Stop WAV if playing |
| 70 | + |
| 71 | + if(!file.open(root, name)) { |
| 72 | + PgmPrint("Couldn't open file "); |
| 73 | + Serial.print(name); |
| 74 | + return; |
| 75 | + } |
| 76 | + if(!wave.create(file)) { |
| 77 | + PgmPrintln("Not a valid WAV"); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + wave.play(); // Start playing |
| 82 | + while(wave.isplaying); // Wait for completion |
| 83 | + sdErrorCheck(); // Check for error during play |
| 84 | + while(digitalRead(A0) == LOW); // Wait for button release |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void error_P(const char *str) { |
| 89 | + PgmPrint("Error: "); |
| 90 | + SerialPrint_P(str); |
| 91 | + sdErrorCheck(); |
| 92 | + for(;;); |
| 93 | +} |
| 94 | + |
| 95 | +void sdErrorCheck(void) { |
| 96 | + if(!card.errorCode()) return; |
| 97 | + PgmPrint("\r\nSD I/O error: "); |
| 98 | + Serial.print(card.errorCode(), HEX); |
| 99 | + PgmPrint(", "); |
| 100 | + Serial.println(card.errorData(), HEX); |
| 101 | + for(;;); |
| 102 | +} |
0 commit comments