// WORK IN PROGRESS
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SELECT_BUTTON 4
#define CONFIRM_BUTTON 3
#define CANCEL_BUTTON 2
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
// temporary sprites
// 'b5', 16x16px
const unsigned char epd_bitmap_b5 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x78, 0x3f, 0xfc, 0x3f, 0xfc, 0x3f, 0xfc, 0x3f, 0xf8,
0x1f, 0xf8, 0x0f, 0xf0, 0x07, 0xc0, 0x07, 0x80, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'b4', 16x16px
const unsigned char epd_bitmap_b4 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x78, 0x3f, 0xfc, 0x3f, 0xfc, 0x3f, 0xfc, 0x3f, 0xf8,
0x1f, 0xf8, 0x1f, 0xf0, 0x3f, 0xfc, 0x37, 0x9c, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 96)
const int epd_bitmap_allArray_LEN = 2;
const unsigned char* epd_bitmap_allArray[2] = {
epd_bitmap_b4,
epd_bitmap_b5
};
uint8_t current_pet_sprite = 0;
const uint8_t icons_xy[16] {
8,2,
8,18,
8,34,
8,50,
104,2,
104,18,
104,34,
104,50
};
uint8_t selected_icon = 0;
uint8_t game_mode = 0;
int pet_pos_x = 64;
int pet_pos_y = 32;
long random_number = 0;
const uint8_t pet_size = 16;
void setup() {
// put your setup code here, to run once:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
unsigned long start = millis();
randomSeed(analogRead(0));
pinMode(SELECT_BUTTON, INPUT_PULLUP);
pinMode(CONFIRM_BUTTON, INPUT_PULLUP);
pinMode(CANCEL_BUTTON, INPUT_PULLUP);
// Display the splash screen (we're legally required to do so)
display.display();
display.clearDisplay();
//while(millis() - start < 2000);
display.drawRect(31, 0, 64, 64, WHITE);
display.setTextColor(WHITE, BLACK);
display.setCursor(12, 5);
display.print("J");
display.setCursor(12, 21);
display.print("S");
display.setCursor(12, 37);
display.print("Z");
display.setCursor(12, 53);
display.print("L");
display.setCursor(108, 5);
display.print("C");
display.setCursor(108, 21);
display.print("I");
display.setCursor(108, 37);
display.print("D");
display.setCursor(108, 53);
display.print("X");
display.display();
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long time = millis();
static bool select_state = false;
static bool confirm_state = false;
static bool cancel_state = false;
select_state = (digitalRead(SELECT_BUTTON) == LOW);
confirm_state = (digitalRead(CONFIRM_BUTTON) == LOW);
cancel_state = (digitalRead(CANCEL_BUTTON) == LOW);
if (game_mode > 0) {
if (cancel_state == true) {
game_mode = 0;
selected_icon = 0;
}
}
if (game_mode == 0) {
if (select_state == true) {
if (selected_icon < 8) {selected_icon++;}
else { selected_icon = 0;}
select_icon(selected_icon);
}
if (cancel_state == true) {
selected_icon = 0;
select_icon(selected_icon);
}
move_pet();
}
display.display();
// game tick
while(millis() - time < 250);
}
void select_icon(uint8_t icon) {
for (int i = 0;i <8; i++) {
display.drawRect(icons_xy[(i*2)], icons_xy[(i*2+1)], 12, 12, BLACK);
}
// selected_icon == 0 means no selection
if (icon > 0) {
icon--;
display.drawRect(icons_xy[(icon*2)], icons_xy[(icon*2+1)], 12, 12, WHITE);
}
}
void move_pet() {
random_number = random(1, 9);
pet_pos_x = pet_pos_x + random_number - 5;
random_number = random(1, 9);
pet_pos_y = pet_pos_y + random_number - 5;
// calculate new random position
if (pet_pos_x < 33) {
pet_pos_x = 33;
}
if ((pet_pos_x + pet_size) > 96) {
pet_pos_x = 96 - pet_size;
}
if (pet_pos_y < 2) {
pet_pos_y = 2;
}
if ((pet_pos_y + pet_size) > 62) {
pet_pos_y = (62 - pet_size);
}
// clear_pet_area
display.fillRect(33,1,61,62,BLACK);
// display.setCursor(pet_pos_x, pet_pos_y);
// display.print("XX");
display.drawBitmap(pet_pos_x, pet_pos_y, epd_bitmap_allArray[current_pet_sprite], 16, 16, WHITE);
if (current_pet_sprite == 0) {
current_pet_sprite = 1;
}
else {
current_pet_sprite = 0;
}
}
void tmp() {
display.drawRect(8, 2, 12, 12, WHITE);
display.drawRect(8, 18, 12, 12, WHITE);
display.drawRect(8, 34, 12, 12, WHITE);
display.drawRect(8, 50, 12, 12, WHITE);
display.drawRect(104, 2, 12, 12, WHITE);
display.drawRect(104, 18, 12, 12, WHITE);
display.drawRect(104, 34, 12, 12, WHITE);
display.drawRect(104, 50, 12, 12, WHITE);
}