#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);
}
}
/* Nice code start here :-) */
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};
const int nice_monsters_number = sizeof(nice_monsters) / sizeof(nice_monsters[0]);
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);
}
}
}
}
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);
}
void loop() {
static int seed = 0; // 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++;
}
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;
draw_monster(center_x, center_y, MONSTER_WIDTH, MONSTER_HEIGHT, PIXEL_SIZE, WHITE, 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();
delay(1000);
//Serial.println("coucou");
}