I'm trying to use the snooze library (button wakeup demo) with the teensy audio library. When audio objects are instantiated, they wake the teensy up ( ~ every 1sec) so snooze doesn't work as expected. If I try and nest the audio objects in the main loop hibernate works but audio doesn't. I tried wrapping hibernate with disabling/reenabling interrupts on the audio library but that didn't seem to work. Any ideas?
Thanks
Dave
// using teensy 3.6
// snooze 6.2.8
// teensyduino 1.3.6
Thanks
Dave
// using teensy 3.6
// snooze 6.2.8
// teensyduino 1.3.6
Code:
#include <Snooze.h>
#include <Bounce.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// Audio
AudioPlaySdWav playSdWav1;
AudioOutputI2S i2s1;
AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord2(playSdWav1, 1, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1;
// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
// Buttons
#define BT_PLAY_PAUSE 21
#define BT_NEXT 38
#define BT_PREV 37
#define START_HOLD_TIME 3000
#define STOP_HOLD_TIME 1000
#define STATE_WAIT 0
#define STATE_PLAY 1
#define LED 13
// use bounce for pin 21, debounce of 5ms
Bounce bt_play_pause = Bounce(BT_PLAY_PAUSE, 5);
//Bounce bt_next = Bounce(BT_PREV, 5);
//Bounce bt_prev = Bounce(BT_NEXT, 5);
SnoozeDigital digital;
// install drivers to a SnoozeBlock
SnoozeBlock config(digital);
void setup() {
//Serial.begin(9600);
// Configure pins for bounce library
pinMode(21, INPUT_PULLUP);
pinMode(LED, OUTPUT);
//pinMode(BT_NEXT, INPUT_PULLUP);
//pinMode(BT_PREV, INPUT_PULLUP);
//while (!Serial);
delay(2000);
//pin, mode, type
digital.pinMode(21, INPUT_PULLUP, FALLING);
}
int current_index = 0; // while file to play
const char * filelist[10] = {
"SONG1.WAV", "SONG2.WAV", "SONG3.WAV", "SONG4.WAV", "SONG5.WAV",
"SONG6.WAV", "SONG7.WAV", "SONG8.WAV", "SONG9.WAV", "SONG10.WAV"
};
void loop() {
// if start button not held for 3 sec go back here to sleep.
SLEEP:
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
Serial.println("sleep() again delay 3s");
delay(3000);
// you need to update before sleeping.
bt_play_pause.update();
//AudioNoInterrupts();
int who = Snooze.hibernate(config, SLEEP_MODE::VLLS1);
//AudioInterrupts();
//while (!Serial);
Serial.print("wakeup.. who=");Serial.print(who);
elapsedMillis timeout = 0;
// bounce needs to call update longer than the debounce time = 5ms,
// which is set in constructor.
while (timeout < 6) bt_play_pause.update();
// now check for start button hold
bool wake_state = pollWakeupState();
// if button not held for 3 seconds go back to sleep
if (!wake_state) {
Serial.println("Button not held long enough ...");
digitalWrite(LED, LOW);
delay(100);
digitalWrite(LED, HIGH);
goto SLEEP;
}
// If try and move audio down here it compiles but doesn't work. Snooze works as expected..
//AudioPlaySdWav playSdWav1;
//AudioOutputI2S i2s1;
//AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
//AudioConnection patchCord2(playSdWav1, 1, i2s1, 1);
//AudioControlSGTL5000 sgtl5000_1;
AudioMemory(8);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
// Todo - read file list from SD card?
// sd card, wake state
Serial.println("SD=ok;WS="); Serial.println(wake_state);
delay(5);
elapsedMillis elapsedTime = 0;
Serial.println("START:mainLoop");
bool first = true;
while (1) {
// if play pause button pressed
bt_play_pause.update();
if (bt_play_pause.fallingEdge() || first) {
first = false;
Serial.println("Play update");
const char *filename = filelist[current_index];
if (playSdWav1.isPlaying() == true) {
Serial.print("stop playing: ");Serial.println(filename);
playSdWav1.stop();
// reset elapsed time to 0?
} else {
// - if paused then play
playSdWav1.play(filename);
Serial.print("Start playing: ");Serial.println(filename);
}
} // end if update
// back to sleep after 5 minutes (60000x5)
if (elapsedTime > 60000) {
Serial.println("sleeping after 5 mins idle time");
goto SLEEP;
}
if (elapsedTime % 5 == 0) {
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
}
delay(100);
//Serial.println("Next iteration");
//Serial.printf("Loop Time in ms: %i \n", elapsedTime);
} // end while 1 (main loop)
}
bool pollWakeupState() {
// start hold time button press check
while (bt_play_pause.duration() < 1000) {
// get current pin state
bt_play_pause.update();
// check the pin state, if button not
// pressed before <hold_time> seconds go back to
// sleep. Read 0 since pin is
// configured as INPUT_PULLUP.
if (bt_play_pause.read() != 0) {
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
digitalWrite(LED, HIGH);
return false;
}
}
digitalWrite(LED, LOW);
// button was held for 3 seconds so now we are awake
return true;
}
Last edited: