#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_I2C_ADDR 0x3C // or 0x3C
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RST_PIN -1 // Reset pin (-1 if not available)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST_PIN);
// 0 = menu;; 1 = space shooter;; 2 = OLED test;;
int display_action = 0;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_I2C_ADDR);
// pinMode(7, INPUT);
}
void loop() {
display.clearDisplay();
if(display_action == 0){
drawMenuItem("TEST", SCREEN_WIDTH, SCREEN_HEIGHT);
if(digitalRead(7) == HIGH) {
display_action = 2;
}
} else if(display_action == 1){
// Placeholder for other actions
} else if(display_action == 2) {
generate_test();
delay(5000); // Delay to see the test content, adjust as needed
} else {
display.setCursor(0, 0);
display.setTextColor(WHITE);
display.setTextSize(1);
display.print("Undefined or null menu item. Current menu item number: " + String(display_action));
}
display.display();
}
void drawMenuItem(String text, int dw, int dh) {
int box_width = 60;
int box_height = 20;
int pos_x = (dw - box_width) / 2; // Centering horizontally
int pos_y = (dh - box_height) / 2; // Centering vertically
display.drawRect(pos_x, pos_y, box_width, box_height, WHITE);
// Calculate the position to draw the text
int text_x = pos_x + (box_width - text.length() * 6) / 2; // Adjusting the multiplier (6) for font size
int text_y = pos_y + (box_height - 8) / 2; // Assuming font size 1 is used, 8 is the height of one character
display.setCursor(text_x, text_y);
display.setTextColor(WHITE);
display.setTextSize(1);
display.print(text);
}
void generate_test() {
display.clearDisplay();
// Test drawing pixels
for (int x = 0; x < SCREEN_WIDTH; x += 2) {
for (int y = 0; y < SCREEN_HEIGHT; y += 2) {
display.drawPixel(x, y, WHITE);
}
}
// Test drawing lines
for (int i = 0; i < SCREEN_WIDTH; i += 4) {
display.drawLine(0, 0, i, SCREEN_HEIGHT - 1, WHITE);
display.display();
delay(1);
}
display.clearDisplay();
for (int i = 0; i < SCREEN_HEIGHT; i += 4) {
display.drawLine(0, 0, SCREEN_WIDTH - 1, i, WHITE);
display.display();
delay(1);
}
display.clearDisplay();
// Test drawing rectangles
for (int i = 0; i < SCREEN_WIDTH; i += 4) {
display.drawRect(i, i, SCREEN_WIDTH - i * 2, SCREEN_HEIGHT - i * 2, WHITE);
display.display();
delay(1);
}
display.clearDisplay();
// Test drawing circles
int radius = max(SCREEN_WIDTH, SCREEN_HEIGHT) / 2;
for (int i = radius; i > 0; i -= 6) {
display.drawCircle(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, i, WHITE);
display.display();
delay(1);
}
display.clearDisplay();
// Test drawing text
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, SCREEN_HEIGHT / 2 - 4);
display.println("Hello, World!");
display.display();
delay(100);
display.clearDisplay();
}