#include "U8glib.h"
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
// Screen
#define screenWidth 128
#define screenHeight 64
// Joystick
float jsX = 0.0;
float jsY = 0.0;
bool jsSW = false;
bool jsSWD = false;
// Cursor
int cursorX = 0;
int cursorY = 0;
#define cursorSpeed 10
// Application Class
typedef struct
{
unsigned char* bits;
int width;
int height;
} application;
// Home
application apps[] =
{
{notesBMP_bits, notesBMP_width, notesBMP_height},
{drawBMP_bits, drawBMP_width, drawBMP_height},
{snakeBMP_bits, snakeBMP_width, snakeBMP_height}
};
void setup()
{
// Joystick
digitalWrite(jsSWPin, HIGH);
// Screen
u8g.setFont(u8g_font_tpssb);
u8g.setColorIndex(1);
}
void loop()
{
// Joystick
getJoystickInput();
// Cursor
cursorX = constrain(cursorX + round(jsX * cursorSpeed), 0, screenWidth - 1);
cursorY = constrain(cursorY - round(jsY * cursorSpeed), 0, screenHeight - 1);
// Screen
u8g.firstPage();
do { draw(); jsSWD = false; } while ( u8g.nextPage() );
}
void draw()
{
// Cursor
u8g.drawXBM(cursorX, cursorY, cursorBMP_width, cursorBMP_height, cursorBMP_bits);
// Applications
int x = 1, y = 1;
for (int i = 0; i < (sizeof apps / sizeof apps[0]); i++)
{
application app = apps[i];
u8g.drawXBM(x, y, app.width, app.height, app.bits);
x += app.width + 1;
if (x > screenWidth)
{
x = 1;
y += app.height + 1;
}
}
}
void getJoystickInput()
{
jsX = 0.0 - (analogRead(jsXPin) / 1023.0 * 2 - 1);
jsY = analogRead(jsYPin) / 1023.0 * 2 - 1;
bool reading = !digitalRead(jsSWPin);
if (jsSW != reading)
{
jsSW = reading;
if (jsSW == true)
jsSWD = true;
}
else
jsSWD = false;
}