//This code combines Duff's code for scheduled teensy sleep with the WAV File player example
#include <Snooze.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
//---------------------------------------------------------------------------
// Load drivers
SnoozeAlarm alarm;
// SnoozeUSBSerial driver does what Serial does like "print", -
// "println", "prinf", etc and also handles the effects of using -
// the Arduino Serial monitor and sleeping. Use it when you -
// want to print to serial monitor for sleeping applications.
SnoozeUSBSerial usb;
//---------------------------------------------------------------------------
// install drivers to a SnoozeBlock
SnoozeBlock config_teensy36(usb, alarm);
//---------------------------------------------------------------------------
// Teensy Audio Setup
//Going sequentially from the top, commenting out all audio stuff, this is where I hit issues
//once these lines are uncommented, the teensy does not wake up as instructed
AudioPlaySdWav playWav1;
AudioOutputI2S audioOutput;
AudioConnection patchCord1(playWav1, 0, audioOutput, 0);
AudioConnection patchCord2(playWav1, 1, audioOutput, 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
//---------------------------------------------------------------------------
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWriteFast(LED_BUILTIN, HIGH); //turn the LED on
// Set RtcTimer for test mode (Teensy should wake up in 10 seconds)
alarm.setRtcTimer(0, 0, 10);// hour, min, sec
// set the Time library to use Teensy 3.6's RTC to keep time
setSyncProvider(getTeensy3Time);
while (!usb); // Wait for Arduino Serial Monitor to open
delay(100);
if (timeStatus() != timeSet) {
usb.println("Unable to sync with the RTC");
} else {
usb.println("RTC has set the system time");
}
// Set timer for test mode
Alarm.timerRepeat(10, GoToSleep); //go to sleep 10 sec after power on
// Set timer For productive mode
//Alarm.alarmRepeat(22,00,00,GoToSleep);
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
/*
AudioMemory(8);
// Comment these out if not using the audio adaptor board.
// This may wait forever if the SDA & SCL pins lack
// pullup resistors
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here, but print a message repetitively
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
*/
}
//---------------------------------------------------------------------------
void loop() {
if (usb.available()) {
time_t t = processSyncMessage();
if (t != 0) {
Teensy3Clock.set(t); // set the RTC
setTime(t);
}
}
digitalClockDisplay();
Alarm.delay(1000);
}
//---------------------------------------------------------------------------
void GoToSleep() {
// Set RtcTimer for productive mode (Teensy should wake up at 7:00am )
//alarm.setRtcTimer(9, 0, 0);// hour, min, sec
usb.println("Going to hibernate Sleep now");
delay(5);
digitalWriteFast(LED_BUILTIN, LOW);
Snooze.hibernate( config_teensy36 );
digitalWriteFast(LED_BUILTIN, HIGH);
setSyncProvider(getTeensy3Time);
//usb.println("Playing Audio");
//delay(5);
//playFile("BACH.WAV"); // filenames are always uppercase 8.3 format
}
//---------------------------------------------------------------------------
time_t getTeensy3Time() {
return Teensy3Clock.get();
}
//---------------------------------------------------------------------------
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(year());
Serial.print("-");
Serial.print(month());
Serial.print("-");
Serial.print(day());
Serial.println();
}
//---------------------------------------------------------------------------
/* code to process time sync messages from the serial port */
#define TIME_HEADER "T" // Header tag for serial time sync message
unsigned long processSyncMessage() {
unsigned long pctime = 0L;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if (Serial.find(TIME_HEADER)) {
pctime = usb.parseInt();
return pctime;
// check the value is a valid time (greater than Jan 1 2013)
if ( pctime < DEFAULT_TIME) {
// return 0 to indicate that the time is not valid
pctime = 0L;
}
}
return pctime;
}
//---------------------------------------------------------------------------
void printDigits(int digits) {
// utility function for digital clock display: prints preceding colon and leading 0
usb.print(":");
if (digits < 10)
usb.print('0');
usb.print(digits);
}
//---------------------------------------------------------------------------
/*
void playFile(const char *filename)
{
Serial.print("Playing file: ");
Serial.println(filename);
// Start playing the file. This sketch continues to
// run while the file plays.
playWav1.play(filename);
// A brief delay for the library read WAV info
delay(25);
// Simply wait for the file to finish playing.
while (playWav1.isPlaying()) {
// uncomment these lines if you audio shield
// has the optional volume pot soldered
//float vol = analogRead(15);
//vol = vol / 1024;
// sgtl5000_1.volume(vol);
}
}
*/
//---------------------------------------------------------------------------