// LOLRGB GIF player example
//
// Written by Larry Bank
//
// This Arduino example sketch demonstrates how to play animated GIF images
// on the UnexpectedMaker 5x14 RGB LED shield. This code depends on two libraries
// (both are available from the Arduino library manager), my AnimatedGIF library and
// Adafruit's NeoPixel.
//
#include <AnimatedGIF.h>
#include "rainbow_5x14.h"
#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <Wire.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Bounce2.h>
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 14 // TinyPico + LOLRGB Shield use this GPIO pin
#define disp_w 128
#define disp_h 64
#define CS_PIN 5
#define path "/" /* This will be replaced with the actual path
with the animations - so there are no false
files or directories.*/
// 5x14 = 70
#define NUMPIXELS 70
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
AnimatedGIF gif; // static instance of the class uses 22.5K of RAM
// Normal color values are too bright on the LED array, so reduce the values
#define BRIGHT_SHIFT 1
byte options = 0;
byte cursor = 0;
byte selected = 0;
byte line = 0;
//Buttons
byte button_pins[] = {34, 35, 32};
#define NUMBUTTONS sizeof(button_pins)
Bounce * buttons = new Bounce[NUMBUTTONS];
File open_path;
Adafruit_SSD1306 display(disp_w, disp_h, &Wire, -1);
void setup()
{
//Set up bounce
for (int i=0; i<NUMBUTTONS; i++){
buttons[i].attach(button_pins[i], INPUT_PULLUP);
buttons[i].interval(25);
}
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.display();
Serial.begin(9600);
while (!Serial);
Serial.print("Initializing SD card...");
display.println("Initializing SD card");
display.display();
if (!SD.begin(CS_PIN)) {
Serial.println("initialization failed. Things to check:");
Serial.println("1. is a card inserted?");
Serial.println("2. is your wiring correct?");
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
display.println("Init failed.");
display.display();
while (true);
}
Serial.println("initialization done.");
display.println("Init done.");
display.display();
open_path = SD.open(path);
populateDir();
Serial.print("done! Options: ");
Serial.println(options);
displayMenu();
}
void loop() {
for (int i = 0; i<NUMBUTTONS; i++){
buttons[i].update();
if (buttons[i].fell()){
if (i==2){
selected = cursor;
Serial.print("Select pressed, cursor is now: ");
Serial.println(cursor);
}
else if (i==0){
cursor++;
Serial.println("Down pressed.");
if (cursor>(options-1)) cursor=0;
}
else if (i==1){
cursor--;
Serial.println("Up pressed");
if (cursor==255) cursor=options-1;
}
displayMenu();
}
}
}
void populateDir() {
while (true) {
File entry = open_path.openNextFile();
if (! entry) {
// no more files
break;
}
Serial.println(entry.name());
options ++;
if (entry.isDirectory()) {
Serial.println("Directory found - shits fucky.");
}
entry.close();
}
open_path.rewindDirectory();
}
char* getOptionName(byte option_num){
File entry;
char* optionName = new char[13];
for (int i = 0; i<=option_num; i++) {
entry = open_path.openNextFile();
entry.close();
}
Serial.print("Get option ");
Serial.println(entry.name());
entry.close();
open_path.rewindDirectory();
strcpy(optionName, entry.name());
return optionName;
}
void displayMenu(){
display.clearDisplay();
display.setCursor(0,0);
display.print(": ");
display.println(getOptionName(selected));
display.println("---------------------");
File entry;
for (int i = 0; i<cursor;i++){
entry = open_path.openNextFile();
entry.close();
}
for (int i = 0; i<=6;i++){
entry = open_path.openNextFile();
display.print(" ");
display.println(entry.name());
entry.close();
}
display.setCursor(0,16);
display.print(">");
open_path.rewindDirectory();
display.display();
}