#include "U8glib.h"
//#include "SdFat.h"
// U8glib
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0); // Add " | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST" if the screen isn't working
// Classes
#include "bitmaps.h"
// Pins
#define jsXPin 0
#define jsYPin 1
#define jsSWPin 2
//#define mcSDPin 10
// Screen
#define screenWidth 128
#define screenHeight 64
// MicroSD
//#define SPI_SPEED SD_SCK_MHZ(4)
//#define CS_PIN 10
//SdFat SD;
//File SDroot;
// Joystick
byte jsX = 0;
byte jsY = 0;
bool jsXReversed = false;
bool jsYReversed = false;
bool jsSW = false;
bool jsSWD = false;
// Cursor
byte cursorX = round(screenWidth / 2);
byte cursorY = round(screenHeight / 2);
bool hideCursor = false;
#define cursorSpeed 10
// Snake Variables
byte snakeX = 0;
byte snakeY = 0;
byte appleX = 0;
byte appleY = 0;
byte snakeDirection = 0;
byte snakeTimer = 0;
byte snakePoints = 0;
// Application Class
typedef struct
{
unsigned char* bits;
byte width;
byte height;
} application;
// Home
byte selectedApp = 0;
bool appSelected = false;
application apps[] =
{
{notesBMP_bits, notesBMP_width, notesBMP_height},
{snakeBMP_bits, snakeBMP_width, snakeBMP_height},
};
void setup()
{
// Joystick
digitalWrite(jsSWPin, HIGH);
// MicroSD
//SD.begin(mcSDPin, SPI_SPEED);
//SDroot = SD.open("/");
// Screen
u8g.setFont(u8g_font_tpssb);
u8g.setColorIndex(1);
}
void loop()
{
// Joystick
getJoystickInput();
// Cursor
cursorX = constrain(cursorX + ((jsXReversed ? -1 : 1) * round(jsX * 0.1 * cursorSpeed)), 0, screenWidth - 1);
cursorY = constrain(cursorY - ((jsYReversed ? -1 : 1) * round(jsY * 0.1 * cursorSpeed)), 0, screenHeight - 1);
hideCursor = false;
// Screen
u8g.firstPage();
do
{
if (!appSelected)
draw();
else
{
switch (selectedApp)
{
case 0:
notesDraw();
break;
case 1:
snakeDraw();
break;
}
}
// Joystick
jsSWD = false;
// Cursor
if (!hideCursor)
u8g.drawXBM(cursorX, cursorY, cursorBMP_width, cursorBMP_height, cursorBMP_bits);
} while ( u8g.nextPage() );
}
void draw()
{
// Applications
byte x = 2, y = 2;
for (byte i = 0; i < (sizeof apps / sizeof apps[0]); i++)
{
application app = apps[i];
if (bitmapPressed(x, y, app.width, app.height, app.bits))
{
selectedApp = i;
switch (selectedApp)
{
case 1:
snakeX = round(round(screenWidth / 2) / 2) * 2;
snakeY = round(round(screenHeight / 2) / 2) * 2;
appleX = round(randomNumber(0, screenWidth - 1) / 2) * 2;
appleY = round(randomNumber(20, screenHeight - 1) / 2) * 2;
snakeDirection = 0;
snakeTimer = 0;
snakePoints = 0;
break;
}
appSelected = true;
}
x += app.width + 2;
if (x > screenWidth)
{
x = 2;
y += app.height + 2;
}
}
u8g.drawStr(1, screenHeight - 2, "Welcome user!");
}
void getJoystickInput()
{
int oldJsX = (0.0 - (analogRead(jsXPin) / 1023.0 * 2 - 1)) * 10;
int oldJsY = (analogRead(jsYPin) / 1023.0 * 2 - 1) * 10;
jsX = round(abs(oldJsX));
jsY = round(abs(oldJsY));
jsXReversed = oldJsX < 0;
jsYReversed = oldJsY < 0;
bool reading = !digitalRead(jsSWPin);
if (jsSW != reading)
{
jsSW = reading;
if (jsSW == true)
jsSWD = true;
}
else
jsSWD = false;
}
bool bitmapPressed(int x, int y, int w, int h, unsigned char* bits)
{
u8g.drawXBM(x, y, w, h, bits);
return cursorX > (x - 1) && cursorX < (x + w + 1) && cursorY > (y - 1) && cursorY < (y + h + 1) && jsSWD;
}
int randomNumber(int minValue, int maxValue)
{
randomSeed(millis() + maxValue * minValue);
return random(minValue, maxValue);
}
// Applications
void notesDraw()
{
}
void snakeDraw()
{
// Cursor
cursorX = round(screenWidth / 2);
cursorY = round(screenHeight / 2);
hideCursor = true;
// Joysick
if (jsSWD) appSelected = false;
if (round(jsX) > 0 && !jsXReversed) snakeDirection = 0;
if (round(jsY) > 0 && !jsYReversed) snakeDirection = 1;
if (round(jsX) > 0 && jsXReversed) snakeDirection = 2;
if (round(jsY) > 0 && jsYReversed) snakeDirection = 3;
if (snakeTimer > 10)
{
snakeTimer = 0;
switch (snakeDirection)
{
case 0:
snakeX += 2;
break;
case 1:
snakeY -= 2;
break;
case 2:
snakeX -= 2;
break;
case 3:
snakeY += 2;
break;
}
}
else
snakeTimer++;
// Screen
u8g.drawBox(snakeX, snakeY, 2, 2);
u8g.drawBox(appleX, appleY, 2, 2);
// Apple
if (snakeX == appleX && snakeY == appleY)
{
appleX = round(randomNumber(0, screenWidth - 1) / 2) * 2;
appleY = round(randomNumber(20, screenHeight - 1) / 2) * 2;
snakePoints++;
}
if (snakeX < 2) snakeX = round((screenWidth - 2) / 2) * 2;
if (snakeX > screenWidth + 4) snakeX = 2;
if (snakeY < 20) snakeY = round((screenHeight - 2) / 2) * 2;
if (snakeY > screenHeight + 4) snakeY = 20;
u8g.drawStr(5, 15, "Apples:");
u8g.setPrintPos(50, 15);
u8g.print(snakePoints);
}