// https://forum.arduino.cc/t/need-demo-codes-for-0-42-oled/1407271
// https://forum.arduino.cc/t/oledgps-atgm336h-5n-project/1403413
// https://www.youtube.com/watch?v=caHcaUoQ2kg // arduino with oled 0.42
// https://www.youtube.com/watch?v=icD-hER8YQ8 // esp32 with oled 0.42
#define CASES 9
#define SCREEN_WIDTH 72
#define SCREEN_HEIGHT 40
#define CHAR_HEIGHT 11
#define CHAR_WIDTH 6
#define bmap_width 13
#define bmap_height 7
const unsigned char bmap[] PROGMEM = { // Arduino logo, 13x7, "swap bits in byte" XBM for u8g2
0xbe, 0x0f, 0xe3, 0x18, 0x41, 0x12, 0x5d, 0x17, 0x41, 0x12, 0xe3, 0x18, 0xbe, 0x0f
};
#include <U8g2lib.h> // https://github.com/olikraus/u8g2/wiki
U8G2_SSD1306_72X40_ER_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE); // https://github.com/olikraus/u8g2/wiki/u8g2setupcpp
unsigned long timer, timeout = 5000;
int count, x = 2, y = 2, xMov = 1, yMov = 1, dly = 100;
bool titleflag = 0, colorflag;
void setup(void) {
randomSeed(analogRead(A0));
u8g2.setDrawColor(1); // white
u8g2.setFontPosTop(); // choices: top, bottom, center, baseline
u8g2.setFont(u8g2_font_luRS08_tr); // https://github.com/olikraus/u8g2/wiki/fntlistallplain
// u8g2.setFont(u8g2_font_luRS24_tr); // https://docs.rs/u8g2-fonts/latest/u8g2_fonts/fonts/struct.u8g2_font_luRS24_tr.html
u8g2.setFontMode(1); // transparent
u8g2.setFontDirection(0); // horizontal
u8g2.clear(); // clear buffer and update display
u8g2.begin();
}
void loop(void) {
switch (count) { // https://github.com/olikraus/u8g2/wiki/u8g2reference
case 0: corners(); break;
case 1: pixel(); break;
case 2: line(); break;
case 3: box(); break;
case 4: frame(); break;
case 5: circle(); break;
case 6: disc(); break;
case 7: triangle(); break;
case 8: text(); break;
case 9: bitmap(); break;
default: break;
}
if (millis() - timer > timeout) { // wait for timeout
timer = millis();
if (count++ == CASES) { // next function
count = 1; // reset to first function
}
u8g2.clearBuffer();
u8g2.updateDisplay();
newFrane72x40(); // draw new frame
}
u8g2.updateDisplay(); // used for every function, sends data to buffer
delay(dly);
}
void newFrane72x40() {
titleflag = 0; // reset title flag
// u8g2.clearBuffer(); // clear buffer
u8g2.setDrawColor(1); // set color to white
u8g2.drawFrame(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); // x, y, w, h - u8g2 puts 72x40 origin at oled (28, 0) on 128x64, 128x32
}
void showTitle(const char title[]) {
if (titleflag == 0) { // reset flag
u8g2.setCursor(2, 1); // x, y position
u8g2.print(title); // display
titleflag = 1; // one time only
}
}
void corners() {
showTitle("72x40");
u8g2.drawPixel(0, 0); // corner pixels
u8g2.drawPixel(71, 0);
u8g2.drawPixel(71, 39);
u8g2.drawPixel(0, 39);
u8g2.drawPixel((SCREEN_WIDTH / 2) - 1, (SCREEN_HEIGHT / 2) - 1); // center pixel
}
void pixel() {
showTitle("drawPixel");
byte x = random(2, SCREEN_WIDTH - 2), y = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2);
u8g2.drawPixel(x, y);
}
void line() {
showTitle("drawLine");
byte x0 = random(2, SCREEN_WIDTH - 2), y0 = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2); // first point
byte x1 = random(2, SCREEN_WIDTH - 2), y1 = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2); // second point
u8g2.drawLine(x0, y0, x1, y1);
}
void box() {
showTitle("drawBox");
colorflag = !colorflag; // alternate black and white
u8g2.setDrawColor(colorflag);
byte w = random(1, SCREEN_WIDTH - 4), h = random(CHAR_HEIGHT, SCREEN_HEIGHT - CHAR_HEIGHT - 4); // width, height
byte x = random(2, SCREEN_WIDTH - w), y = random(CHAR_HEIGHT, SCREEN_HEIGHT - h); // origin
u8g2.drawBox(x, y, w, h);
}
void frame() {
showTitle("drawFrame");
colorflag = !colorflag; // alternate black and white
u8g2.setDrawColor(colorflag);
int w = random(1, SCREEN_WIDTH - 4), h = random(CHAR_HEIGHT, SCREEN_HEIGHT - CHAR_HEIGHT - 4); // width, height
int x = random(2, SCREEN_WIDTH - w), y = random(CHAR_HEIGHT, SCREEN_HEIGHT - h); // origin
u8g2.drawFrame(x, y, w, h);
}
void circle() {
showTitle("drawCircle");
colorflag = !colorflag; // alternate black and white
u8g2.setDrawColor(colorflag);
int r = random(2, (SCREEN_HEIGHT - CHAR_HEIGHT) / 2); // radius
int x = random(r, SCREEN_WIDTH - r), y = random(CHAR_HEIGHT + r, SCREEN_HEIGHT - r); // origin
u8g2.drawCircle(x, y, r); // 4:5 chance to draw circle
}
void disc() {
showTitle("drawDisc");
colorflag = !colorflag; // alternate black and white
u8g2.setDrawColor(colorflag);
int r = random(2, (SCREEN_HEIGHT - CHAR_HEIGHT) / 2); // radius
int x = random(r, SCREEN_WIDTH - r), y = random(CHAR_HEIGHT + r, SCREEN_HEIGHT - r); // origin
u8g2.drawDisc(x, y, r); // filled circle
}
void triangle() {
showTitle("drawTriangle");
colorflag = !colorflag; // alternate black and white
u8g2.setDrawColor(colorflag);
int x0 = random(2, SCREEN_WIDTH - 2), y0 = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2); // point 1
int x1 = random(2, SCREEN_WIDTH - 2), y1 = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2); // point 2
int x2 = random(2, SCREEN_WIDTH - 2), y2 = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2); // point 3
u8g2.drawTriangle(x0, y0, x1, y1, x2, y2);
}
void text() {
// int width - u8g2, getMaxCharWidth*(); // text horizontal placement
// int height = u8g2.getMaxCharHeight(); // text vertical placement
u8g2.setFontDirection(0); // horizontal
u8g2.setCursor(18, 2);
u8g2.print("NORTH");
u8g2.setFontDirection(1); // top-down
u8g2.setCursor(69, 8);
u8g2.print("EAST");
u8g2.setFontDirection(2); // upside-down
u8g2.setCursor(55, 37);
u8g2.print("SOUTH");
u8g2.setFontDirection(3); // button-up
u8g2.setCursor(2, 33);
u8g2.print("WEST");
u8g2.setFontDirection(0); // reset to horizontal
}
void bitmap() {
if (x < 2 || x >= SCREEN_WIDTH - bmap_width - 1) { // x boundries
xMov = -xMov;
}
if (y < 2 || y >= SCREEN_HEIGHT - bmap_height - 1) // y boundries
yMov = -yMov;
x += xMov; // update x
y += yMov; // update y
u8g2.clearBuffer();
u8g2.setDrawColor(0);
u8g2.drawBox(x, y, bmap_width, bmap_height);
u8g2.setDrawColor(1);
u8g2.drawFrame(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
u8g2.drawXBMP(x, y, bmap_width, bmap_height, bmap);
}Loading
ssd1306
ssd1306