#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
/*
* Pin 2 - OLED Data (SDA)
* Pin 3 - OLED Clock (SCK)
* Pin 18 - SD Chip Select (CS)
* Pin 15 - SD (CLK)
* Pin 14 - SD (MISO)
* Pin 16 - SD (MOSI)
*/
const int sdChipSelectPin = 10;
const int upButtonPin = 9;
const int downButtonPin = 6;
const int testButtonPin = 5;
const int linePerPage = 4;
#define OLED_WIDTH 128 // OLED display width, in pixels
#define OLED_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 oled_1(OLED_WIDTH, OLED_HEIGHT, &Wire, OLED_RESET);
File root;
int fileCount, firstOption, selectedLine;
String fileNames[16] = {};
int x=0;
int zOff = 150;
int xOff = 0;
int yOff = 0;
int cSize = 40;
int view_plane = 64;
float angle = PI/60;
float cube3d[8][3] = {
{xOff - cSize,yOff + cSize,zOff - cSize},
{xOff + cSize,yOff + cSize,zOff - cSize},
{xOff - cSize,yOff - cSize,zOff - cSize},
{xOff + cSize,yOff - cSize,zOff - cSize},
{xOff - cSize,yOff + cSize,zOff + cSize},
{xOff + cSize,yOff + cSize,zOff + cSize},
{xOff - cSize,yOff - cSize,zOff + cSize},
{xOff + cSize,yOff - cSize,zOff + cSize}
};
unsigned char cube2d[8][2];
void bitmap(char* fle,int groundUp)
{
char bootfile[32];
sprintf(bootfile, "%s", fle);
SD.begin();
root = SD.open(bootfile);
if (root) {
x=0;
int y=0;
if(groundUp == 0)
{
y=0;
}
else {
y=64;
}
int addr=0x00;
oled_1.clearDisplay();
// read from the file until there's nothing else in it:
while (root.available()) {
unsigned char readB = root.read();
if(readB > 0)
{
oled_1.drawPixel(x,y,1);
}
else {
oled_1.drawPixel(x,y,0);
}
if(y <= 0 && groundUp == 1)
{
oled_1.display();
//delay(10);
y=64;
x=0;
}
else if(y >= 63 && groundUp != 1){
oled_1.display();
//delay(10);
y=-1;
x=0;
}
x++;
if(x >= 128)
{
if(groundUp == 0)
{
y++;
}
else {
y--;
}
x=0;
}
}
}
}
void setup() {
Serial.begin(9600);
pinMode(upButtonPin, INPUT_PULLUP);
pinMode(downButtonPin, INPUT_PULLUP);
pinMode(testButtonPin, INPUT_PULLUP);
oled_1.begin(SSD1306_SWITCHCAPVCC, 60);
for(int y=0; y<=64; y++)
{
for(int x=0; x<=128; x++)
{
oled_1.drawPixel(x, y, random(2));
}
}
oled_1.display();
}
void loop() {
int rsteps = random(10,60);
switch(random(6)) {
case 0:
for (int i = 0; i < rsteps; i++) {
zrotate(angle);
printcube();
}
break;
case 1:
for (int i = 0; i < rsteps; i++) {
zrotate(2*PI - angle);
printcube();
}
break;
case 2:
for (int i = 0; i < rsteps; i++) {
xrotate(angle);
printcube();
}
break;
case 3:
for (int i = 0; i < rsteps; i++) {
xrotate(2*PI - angle);
printcube();
}
break;
case 4:
for (int i = 0; i < rsteps; i++) {
yrotate(angle);
printcube();
}
break;
case 5:
for (int i = 0; i < rsteps; i++) {
yrotate(2*PI - angle);
printcube();
}
break;
}
}
void printcube() {
//calculate 2d points
for(byte i = 0; i < 8; i++) {
cube2d[i][0] = (unsigned char)((cube3d[i][0] * view_plane / cube3d[i][2]) + (128/2));
cube2d[i][1] = (unsigned char)((cube3d[i][1] * view_plane / cube3d[i][2]) + (64/2));
}
//delay(1);
oled_1.clearDisplay();
draw_cube();
}
void zrotate(float q) {
float tx,ty,temp;
for(byte i = 0; i < 8; i++) {
tx = cube3d[i][0] - xOff;
ty = cube3d[i][1] - yOff;
temp = tx * cos(q) - ty * sin(q);
ty = tx * sin(q) + ty * cos(q);
tx = temp;
cube3d[i][0] = tx + xOff;
cube3d[i][1] = ty + yOff;
}
}
void yrotate(float q) {
float tx,tz,temp;
for(byte i = 0; i < 8; i++) {
tx = cube3d[i][0] - xOff;
tz = cube3d[i][2] - zOff;
temp = tz * cos(q) - tx * sin(q);
tx = tz * sin(q) + tx * cos(q);
tz = temp;
cube3d[i][0] = tx + xOff;
cube3d[i][2] = tz + zOff;
}
}
void xrotate(float q) {
float ty,tz,temp;
for(byte i = 0; i < 8; i++) {
ty = cube3d[i][1] - yOff;
tz = cube3d[i][2] - zOff;
temp = ty * cos(q) - tz * sin(q);
tz = ty * sin(q) + tz * cos(q);
ty = temp;
cube3d[i][1] = ty + yOff;
cube3d[i][2] = tz + zOff;
}
}
void draw_cube() {
oled_1.drawLine(cube2d[0][0],cube2d[0][1],cube2d[1][0],cube2d[1][1],WHITE);
oled_1.drawLine(cube2d[0][0],cube2d[0][1],cube2d[2][0],cube2d[2][1],WHITE);
oled_1.drawLine(cube2d[0][0],cube2d[0][1],cube2d[4][0],cube2d[4][1],WHITE);
oled_1.drawLine(cube2d[1][0],cube2d[1][1],cube2d[5][0],cube2d[5][1],WHITE);
oled_1.drawLine(cube2d[1][0],cube2d[1][1],cube2d[3][0],cube2d[3][1],WHITE);
oled_1.drawLine(cube2d[2][0],cube2d[2][1],cube2d[6][0],cube2d[6][1],WHITE);
oled_1.drawLine(cube2d[2][0],cube2d[2][1],cube2d[3][0],cube2d[3][1],WHITE);
oled_1.drawLine(cube2d[4][0],cube2d[4][1],cube2d[6][0],cube2d[6][1],WHITE);
oled_1.drawLine(cube2d[4][0],cube2d[4][1],cube2d[5][0],cube2d[5][1],WHITE);
oled_1.drawLine(cube2d[7][0],cube2d[7][1],cube2d[6][0],cube2d[6][1],WHITE);
oled_1.drawLine(cube2d[7][0],cube2d[7][1],cube2d[3][0],cube2d[3][1],WHITE);
oled_1.drawLine(cube2d[7][0],cube2d[7][1],cube2d[5][0],cube2d[5][1],WHITE);
oled_1.display();
}