#include <SPI.h>
#include <SSD_13XX.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <timeObj.h>
// The four shared SPI pins..
// These pins are best choice for Arduino & Teensy.
#define LC_CLK 13 // Clock
#define LC_MISO 12 // Input data
#define LC_MOSI 11 // Output data
#define LC_DC 9 // Data/command
// The four the sound card uses.
#define SOUND_CS 20
#define SOUND_SDCS 21
#define SOUND_DRQ 1
#define SOUND_RST 15
// The OLED is only using these two. screen & reset.
#define OLED_CS 10
#define OLED_RST 6
#define OLED_SDCS -1 // Not wired
timeObj songTimer(40); // 40ms song timer.
SSD_13XX tft(OLED_CS, LC_DC, OLED_RST, LC_MOSI, LC_CLK);
Adafruit_VS1053_FilePlayer musicPlayer =
Adafruit_VS1053_FilePlayer
(SOUND_RST, SOUND_CS, LC_DC, SOUND_DRQ, 0);
void setup() {
Serial.begin(9600); while (!Serial);
Serial.println(F("Serial's up."));
SD.begin(SOUND_SDCS);
if (!musicPlayer.begin()) { // initialise the music player
Serial.println(F("Couldn't find VS1053"));
} else {
Serial.println(F("VS1053 found"));
musicPlayer.startPlayingFile("Corvette.mp3");
songTimer.start();
}
/*
tft.begin(false);
Serial.print("For error we got: ");Serial.println(tft.getErrorCode());
tft.fillScreen(BLUE);
tft.print("Ready, Sir!");
*/
}
void loop() {
if (musicPlayer.playingMusic && songTimer.ding()) {
noInterrupts();
musicPlayer.feedBuffer();
interrupts();
songTimer.start();
}
}
#ifndef timeObj_h
#define timeObj_h
// Insainly handy for doing things in the background.
// Set the timer and wait 'till it goes "ding".
// Great for blinking LEDs, updating readings, etc.
// Not fast & accurate enough for RC Servos.
// *** Takes care of roll over issues ***
enum timeType { zero, crossing, normal };
class timeObj {
public:
timeObj(float inMs=10);
void setTime(float inMs,bool startNow=true); // Change the time duration for next start..
virtual void start(void); // Start the timer "now".
virtual void stepTime(void); // Restart the timer from last end time.
bool ding(void); // Timer has expired.
unsigned long getTime(void); // How long does this go for?
protected:
unsigned long waitTime;
unsigned long startTime;
unsigned long endTime;
timeType config;
};
#endif
#include "timeObj.h"
#include <arduino.h>
timeObj::timeObj(float inMs) {
startTime = 0;
endTime = 0;
setTime(inMs); // This sets startTime and endTime. Best go here?
}
void timeObj::setTime(float inMs,bool startNow) {
if (inMs>0)
waitTime = round(1000*inMs);
else
waitTime = 0;
if (startNow) start();
}
void timeObj::start(void) {
if (waitTime==0) {
config = zero;
} else {
startTime = micros();
endTime = startTime + waitTime;
if (endTime < startTime) {
config = crossing;
} else {
config = normal;
}
}
}
void timeObj::stepTime(void) {
if (config!=zero) {
startTime = endTime;
endTime = startTime + waitTime;
if (endTime < startTime) {
config = crossing;
} else {
config = normal;
}
}
}
bool timeObj::ding(void) {
unsigned long now;
now = micros();
switch (config) {
case zero : return true;
case crossing :
if (now >= startTime || now < endTime)
return false;
else
return true;
break;
case normal :
if (now >= startTime && now < endTime)
return false;
else
return true;
break;
}
return true; // To shut up compiler and shut down an broken timer.
}
unsigned long timeObj::getTime(void) { return waitTime; }