#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
/*
* Connections
* SSD1306 OLED | Arduino Uno
* ---------------------------
* VCC | +5V (Vcc/power/+ve)
* GND | GND (Ground/-ve/0v)
* SCL | A5 (Serial Clock Line)
* SDA | A4 (Serial Data Line)
*/
const int SCREEN_WIDTH = 128; // OLED display width, in pixels
const int SCREEN_HEIGHT = 64; // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
while(true);
}
}
// Draw a string centered to (x, y) thanks to getTextBounds()
// From https://forum.arduino.cc/t/adafruit-oled-how-to-center-text/617181/5
void drawCentreString(const String &buf, int x, int y)
{
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
display.setCursor(x - w / 2, y);
display.print(buf);
}
const int MONSTER_WIDTH = 6; // Better if multiple of 2
const int MONSTER_HEIGHT = 6;
const int PIXEL_SIZE = 9; // Do not use a too big value else out of screen
const int NAME_HEIGHT_PX = 7; // link to the font size setTextSize(1);
const int print_nice_monsters = 1; // Put 1 to enable the nice_monsters list
const int nice_monsters[] =
{2, 5, 8, 9, 11, 20, 21, 23, 24, 25, 26, 32, 38, 46, 48, 49, 59, 60,
63, 71, 75, 78, 81, 87, 92, 100, 101, 105, 106, 107, 109, 110, 113,
118, 119, 136, 143, 150, 152, 155, 159, 160, 162, 164, 167, 175, 177,
182, 185, 197, 199, 201, 206, 208, 213, 217, 218, 221, 227, 233, 240,
246, 247, 250, 252, 255, 260, 266, 271, 275, 280, 282, 285, 288, 289,
293, 295, 297, 301, 307, 309, 317, 321, 325, 328, 333, 335, 337, 344,
349, 358, 360, 363, 365, 366, 374, 375, 388, 392, 404, 406, 412, 417,
424, 428, 433, 436, 437, 447, 456, 457, 464, 471, 481, 482, 483, 486,
499, 500, 508, 510, 514, 525, 531, 546, 554, 556, 556, 571, 576, 583,
592, 593, 599};
const int nice_monsters_number = sizeof(nice_monsters) / sizeof(nice_monsters[0]);
// The most important function: Draw a monster :-)
// The monster is randomly generated with an axial symmetry, see below 6x6 example
// ** **
// **
// * *
// ****
void draw_monster(int x0, int y0, int width, int height, int size_px, int color, int seed)
{
int x, y;
randomSeed(seed);
for (y = 0; y < height; y++) {
for (x = 0; x < width / 2; x++) {
if (random(2) == 1) {
display.writeFillRect(x0 + x * size_px, y0 + y * size_px, size_px, size_px, color);
display.writeFillRect(x0 + (width - x - 1) * size_px, y0 + y * size_px, size_px, size_px, color);
}
}
}
}
int get_seed(void)
{
static int seed = 255; // Note randomSeed(0) means no seed!
static int counter = 0;
if (print_nice_monsters == 1) {
seed = nice_monsters[counter];
counter++;
if (counter == nice_monsters_number)
counter = 0;
} else {
seed++;
}
return seed;
}
void draw_monster_portrait_with_name(int color)
{
int seed;
display.clearDisplay();
int center_x = (SCREEN_WIDTH - MONSTER_WIDTH * PIXEL_SIZE) / 2;
int center_y = (SCREEN_HEIGHT - NAME_HEIGHT_PX - MONSTER_HEIGHT * PIXEL_SIZE) / 2;
seed = get_seed();
draw_monster(center_x, center_y, MONSTER_WIDTH, MONSTER_HEIGHT, PIXEL_SIZE, color, seed);
// Print the name of the monster (its seed)
display.setTextSize(1);
display.setTextColor(WHITE);
drawCentreString(String(seed), SCREEN_WIDTH / 2, SCREEN_HEIGHT - NAME_HEIGHT_PX);
display.display();
}
void draw_monster_gallery(int size_px, int color, int margin_left, int margin_right,
int margin_top, int margin_bottom)
{
int seed;
int x, y, w, h;
display.clearDisplay();
x = margin_left;
y = margin_top;
w = MONSTER_WIDTH * size_px + margin_left + margin_right;
h = MONSTER_HEIGHT * size_px + margin_top + margin_bottom;
while (1) {
seed = get_seed();
draw_monster(x, y, MONSTER_WIDTH, MONSTER_HEIGHT, size_px, color, seed);
x += w;
if (x >= SCREEN_WIDTH) {
x = margin_left;
y += h;
if (y >= SCREEN_HEIGHT) {
break;
}
}
}
display.display();
}
void loop() {
draw_monster_portrait_with_name(WHITE);
delay(1000);
draw_monster_gallery(1, WHITE, 1, 1, 1, 1);
delay(1000);
//Serial.println("coucou");
}
Loading
ssd1306
ssd1306