Created
February 14, 2016 04:09
-
-
Save Technicus/d7a4068fdee34b3e9a87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1351.h> | |
#include <Adafruit_VS1053.h> | |
#include <SPI.h> | |
#define VERSION "Development - 2" | |
// Color definitions | |
#define BLACK 0x0000 | |
#define BLUE 0x001F | |
#define RED 0xF800 | |
#define GREEN 0x07E0 | |
#define CYAN 0x07FF | |
#define MAGENTA 0xF81F | |
#define YELLOW 0xFFE0 | |
#define WHITE 0xFFFF | |
// Text Boxes | |
#define TOP 0 | |
#define BOTTOM 1 | |
#define LEFT 2 | |
#define RIGHT 3 | |
#define CENTER 4 | |
#define CLEAR 0 | |
#define PRINT 1 | |
// Media Symbols | |
#define PLAY 0 | |
#define RECORD 1 | |
#define STOP 2 | |
#define PAUSE 3 | |
#define PREVIOUS 4 | |
#define NEXT 5 | |
#define UP 6 | |
#define DOWN 7 | |
#define VS1053_RESET A2 // VS1053 reset pin (output) | |
#define VS1053_CS A1 // VS1053 chip select pin (output) | |
#define VS1053_DCS A3 // VS1053 Data/command select pin (output) | |
#define VS1053_CARDCS A4 // Card chip select pin | |
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt | |
#define VS1053_DREQ 2 // VS1053 Data request, ideally an Interrupt pin | |
#define TFT_DC 8 | |
#define TFT_CS 10 | |
#define TFT_RESET 9 | |
// Create object for VS1053 Codec | |
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, VS1053_CARDCS); | |
// Create object for TFT SSD1351 | |
Adafruit_SSD1351 tft = Adafruit_SSD1351(TFT_CS, TFT_DC, TFT_RESET); | |
int volume[] = {20, 20}; | |
#define HEART_PIN A0 | |
#define NUMBUTTONS sizeof(button) | |
// Variables will change: | |
int button[] = { 0, 1, 2, 3, 4, 5, 6, 7}; | |
int buttonState_Reading[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}; | |
int buttonState_Current[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}; | |
byte buttonState_Previous[8]; | |
unsigned long firstPress[8]; // how long since the button was first pressed | |
long millis_held[8]; // How long the button was held (milliseconds) | |
long secs_held[8]; // How long the button was held (seconds) | |
long prev_secs_held[8]; // How long the button was held in the previous check | |
// the following variables are long's because the time, measured in miliseconds, | |
// will quickly become a bigger number than can be stored in an int. | |
long debounceDelay = 500; // the debounce time; increase if the output flicke | |
long lastDebounceTime[] = {0, 0, 0, 0, 0, 0, 0, 0}; // the last time the output pin was toggled | |
int count[] = {0, 0, 0, 0, 0, 0, 0, 0}; // How long the button was held (secs) | |
int beat = 0; | |
// Function prototypes | |
void heartBeat(int beat); | |
void ledblink(int times, int lengthms, int pinnum); | |
boolean array_cmp(int *a, int *b, int len_a, int len_b); | |
void textBox(int boxRegion, int textAction, char *text, uint16_t color); | |
void systemControl(int action); | |
const unsigned char mugg_1[] PROGMEM = { | |
0x00, 0x00, 0x04, 0x92, 0x04, 0x81, 0x00, 0x00, 0x00, 0x4a, 0x98, 0x20, 0x86, 0xf8, 0x10, 0x00, | |
0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x92, 0x00, 0x00, 0x00, 0x10, 0x00, | |
0x00, 0x30, 0x24, 0x00, 0x00, 0x80, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x92, 0x00, | |
0x00, 0x22, 0x20, 0x00, 0x00, 0x12, 0x09, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x08, 0x12, 0x00, | |
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, | |
0x00, 0x12, 0x00, 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x81, 0xf8, 0x01, 0x30, 0x00, | |
0x00, 0x00, 0x00, 0x43, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0xff, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x11, 0x07, 0xff, 0x80, 0x40, 0x00, 0x00, 0x00, 0x01, 0x0f, 0xff, 0xc0, 0x04, 0x00, | |
0x00, 0x00, 0x23, 0x0f, 0xff, 0xe0, 0x50, 0x00, 0x00, 0x00, 0x22, 0x1f, 0xff, 0xa0, 0x24, 0x00, | |
0x00, 0x80, 0x02, 0x1f, 0xff, 0xa0, 0x08, 0x00, 0x00, 0x00, 0x06, 0x3f, 0xff, 0xa0, 0x04, 0x00, | |
0x00, 0x80, 0x04, 0x3f, 0xff, 0xa0, 0x08, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfe, 0xa0, 0x10, 0x00, | |
0x00, 0x80, 0x00, 0x1f, 0x83, 0x20, 0x09, 0x00, 0x00, 0x00, 0x08, 0x8f, 0xff, 0xf0, 0x06, 0x00, | |
0x00, 0x00, 0x01, 0x0f, 0xe1, 0xf0, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x4f, 0xc1, 0x30, 0x04, 0x00, | |
0x00, 0x00, 0x00, 0x3f, 0xd0, 0x30, 0x09, 0x00, 0x00, 0x00, 0x03, 0xff, 0xfb, 0x70, 0x22, 0x00, | |
0x00, 0x00, 0x7f, 0xff, 0xff, 0xf8, 0x01, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xf8, 0x00, 0x00, | |
0x00, 0x00, 0x7f, 0xff, 0xff, 0xf8, 0x01, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xf0, 0x02, 0x00, | |
0x00, 0x00, 0x7f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x80, 0x3f, 0xff, 0xff, 0xf0, 0x00, 0x00, | |
0x00, 0x00, 0x3f, 0xff, 0xff, 0xe0, 0x18, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x3f, 0xe4, 0x30, 0x00, | |
0x00, 0x00, 0x1f, 0xc0, 0x3f, 0xe0, 0x18, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x7f, 0xe0, 0x20, 0x00, | |
0x00, 0x04, 0x1f, 0xe0, 0x7f, 0xe0, 0x08, 0x00, 0x00, 0x00, 0x0f, 0xfd, 0xff, 0xc0, 0x14, 0x00, | |
0x00, 0x00, 0x0f, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x3f, 0x80, 0x00, 0x00, | |
0x00, 0x00, 0x07, 0x0d, 0x8f, 0x80, 0x09, 0x00, 0x00, 0x00, 0x02, 0x0f, 0x6f, 0x00, 0x04, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, | |
0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf6, 0x80, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x3f, 0xe0, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x03, 0x88, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x4f, 0x24, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x0f, 0xcf, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xdf, 0xfc, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x1f, 0x8e, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x9f, 0xfc, 0x00, | |
}; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println(""); | |
Serial.println(VERSION); | |
tft.begin(); | |
tft.fillScreen(BLACK); | |
textBox(TOP, 1, VERSION, YELLOW); | |
Serial.println("TFT Initalized"); | |
tft.drawBitmap(32, 32, mugg_1, 64, 64, RED); | |
// initialize the LED pin as an output: | |
pinMode(HEART_PIN, OUTPUT); | |
//textBox(0, 1, "Heartbeat initalized.", YELLOW); | |
heartBeat(1); | |
delay(200); | |
heartBeat(2); | |
Serial.println("Heartbeat initalized."); | |
if (! musicPlayer.begin()) { // initialise the music player | |
//textBox(0, 1, "Couldn't find VS1053, HALT!", RED); | |
Serial.println("Couldn't find VS1053, HALT!"); | |
while (1); // don't do anything more | |
} | |
//textBox(0, 1, "VS1053 found.", YELLOW); | |
Serial.println("VS1053 found."); | |
musicPlayer.sineTest(0x44, 500); // Make a tone to indicate VS1053 is working | |
delay(500); | |
if (!SD.begin(VS1053_CARDCS)) { | |
//textBox(0, 1, "SD failed, or not present, HALT!", RED); | |
Serial.println("SD failed, or not present, HALT!"); | |
while (1); // don't do anything more | |
} | |
//textBox(0, 1, "SD OK.", YELLOW); | |
delay(500); | |
// Set volume for left, right channels. lower numbers == louder volume! | |
musicPlayer.setVolume(volume[0], volume[1]); | |
if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT)){ | |
//textBox(0, 1, "DREQ pin is not an interrupt pin", YELLOW); | |
Serial.println("DREQ pin is not an interrupt pin"); | |
} else { | |
//textBox(0, 1, "DREQ pin is an interrupt pin", YELLOW); | |
Serial.println("DREQ pin is an interrupt pin"); | |
} | |
delay(500); | |
//Setup the buttons with as inputs : | |
for (int pinSelect = 0; pinSelect < NUMBUTTONS; pinSelect++) { | |
musicPlayer.GPIO_pinMode(button[pinSelect], INPUT); | |
delay(100); | |
} | |
Serial.println("musicPlayer.GPIO initalized."); | |
Serial.println(". . . Ready . . ."); | |
} | |
void loop() { | |
heartBeat(0); | |
// switch (press_0) | |
// { | |
// case WAITING: | |
// break; | |
// case PRESSED: | |
// Serial.println("pressed 1 time"); | |
// break; | |
// case DOUBLE_PRESSED: | |
// Serial.println("pressed 2 times"); | |
// break; | |
// case MULTI_PRESSED: | |
// Serial.println("pressed 3 times"); | |
// break; | |
// case HELD: | |
// Serial.println("Button HELD"); | |
// break; | |
// } | |
// read the state of the switch into a local variable: | |
for (int pinSelect = 0; pinSelect < NUMBUTTONS; pinSelect++) { | |
buttonState_Current[pinSelect] = musicPlayer.GPIO_digitalRead(pinSelect); | |
// if the button changes to pressed, remember the time | |
if (buttonState_Current[pinSelect] == LOW && buttonState_Previous[pinSelect] == HIGH && millis() - firstPress[pinSelect] > 200) { | |
firstPress[pinSelect] = millis(); | |
} | |
millis_held[pinSelect] = (millis() - firstPress[pinSelect]); | |
secs_held[pinSelect] = millis_held[pinSelect] / 1000; | |
// This if statement is a basic debouncing tool, the button must be pushed for at least | |
// 100 milliseconds in a row for it to be considered as a push. | |
if (millis_held[pinSelect] > 100) { | |
if (buttonState_Current[pinSelect] == LOW && secs_held[pinSelect] > prev_secs_held[pinSelect]) { | |
//ledblink(1, 50, ledPin); // Each second the button is held blink the indicator led | |
} | |
// When the button is released, if the counter is between the | |
// two numbers (3-6 blinks or secs) run the program (here just another blink) | |
if (buttonState_Current[pinSelect] == HIGH && buttonState_Previous[pinSelect] == LOW && count[pinSelect] >=3 && count[pinSelect] < 6){ | |
//ledblink(10,200,ledPin); | |
} | |
if (buttonState_Current[pinSelect] == HIGH && buttonState_Previous[pinSelect] == LOW && count[pinSelect] >=0 && count[pinSelect] < 3) { | |
Serial.println("It Works!!!"); // This is where you would put your own functions | |
systemControl(pinSelect); | |
// with different time parameters | |
} | |
} | |
buttonState_Previous[pinSelect] = buttonState_Current[pinSelect]; | |
prev_secs_held[pinSelect] = secs_held[pinSelect]; | |
} | |
} | |
void systemControl(int action){ | |
//for (int pinSelect = 0; pinSelect < NUMBUTTONS; pinSelect++) { | |
switch(action) { | |
case 0: | |
musicPlayer.sineTest(0x44, 500); | |
Serial.println("0"); | |
break; | |
case 1: | |
musicPlayer.sineTest(0x44, 500); | |
Serial.println("1"); | |
break; | |
case 2: | |
musicPlayer.sineTest(0x44, 500); | |
Serial.println("2"); | |
break; | |
case 3: | |
musicPlayer.sineTest(0x44, 500); | |
Serial.println("3"); | |
break; | |
case 4: | |
musicPlayer.sineTest(0x44, 500); | |
Serial.println("4"); | |
break; | |
case 5: | |
musicPlayer.sineTest(0x44, 500); | |
Serial.println("5"); | |
break; | |
case 6: | |
musicPlayer.sineTest(0x44, 500); | |
Serial.println("6"); | |
break; | |
case 7: | |
musicPlayer.sineTest(0x44, 500); | |
Serial.println("7"); | |
break; | |
default: | |
break; | |
} | |
} | |
void heartBeat(int beat) { | |
switch(beat){ | |
case 0: { | |
digitalWrite(HEART_PIN, LOW); | |
break; | |
} | |
case 1: { | |
digitalWrite(HEART_PIN, HIGH); | |
} | |
case 2: { | |
if (digitalRead(HEART_PIN) == LOW){ | |
digitalWrite(HEART_PIN, HIGH); | |
} | |
else { | |
digitalWrite(HEART_PIN, LOW); | |
} | |
} | |
case 3: { | |
if (digitalRead(HEART_PIN) == LOW){ | |
digitalWrite(HEART_PIN, HIGH); | |
delay(200); | |
digitalWrite(HEART_PIN, LOW); | |
delay(200); | |
} | |
else { | |
digitalWrite(HEART_PIN, LOW); | |
delay(200); | |
digitalWrite(HEART_PIN, HIGH); | |
delay(200); | |
} | |
} | |
} | |
} | |
// Just a simple helper function to blink an led in various patterns | |
void ledblink(int times, int lengthms, int pinnum){ | |
for (int x=0; x<times;x++) { | |
digitalWrite(pinnum, HIGH); | |
delay (lengthms); | |
digitalWrite(pinnum, LOW); | |
delay(lengthms); | |
} | |
} | |
boolean array_cmp(int *a, int *b, int len_a, int len_b){ | |
// where: | |
// a = first array | |
// b = second array | |
// len_a = number of elements (length) of array a | |
// len_b = number of elements (length) of array b | |
int n; | |
// if their lengths are different, return false | |
if (len_a != len_b) return false; | |
// test each element to be the same. if not, return false | |
for (n=0;n<len_a;n++) if (a[n]!=b[n]) return false; | |
//ok, if we have not returned yet, they are equal :) | |
return true; | |
} | |
void textBox(int boxRegion, int textAction, char *text, uint16_t color){ | |
switch (boxRegion) { | |
case TOP: | |
tft.fillRect(0, 0, 128, 32, BLACK); | |
tft.setCursor(0,0); | |
tft.setTextColor(color); | |
tft.print(text); | |
break; | |
case BOTTOM: | |
tft.fillRect(0, 96, 128, 128, BLACK); | |
tft.setCursor(0,96); | |
tft.setTextColor(color); | |
tft.print(text); | |
break; | |
case LEFT: | |
tft.fillRect(0, 32, 32, 96, BLACK); | |
tft.setCursor(0,32); | |
tft.setTextColor(color); | |
tft.print(text); | |
break; | |
case RIGHT: | |
tft.fillRect(96, 32, 128, 96, BLACK); | |
tft.setCursor(96, 32); | |
tft.setTextColor(color); | |
tft.print(text); | |
break; | |
case CENTER: | |
tft.fillRect(32, 32, 96, 96, BLACK); | |
tft.setCursor(32, 32); | |
tft.setTextColor(color); | |
tft.print(text); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment