// 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 8
#define SCREEN_WIDTH 72
#define SCREEN_HEIGHT 40
#define CHAR_HEIGHT 11
#define CHAR_WIDTH 6
#define I2C_ADDRESS 0x3C
#define bmap_width 45
#define bmap_height 20
const unsigned char bmap[] PROGMEM = { // GIMP export to XBM
0xf0, 0xff, 0x1f, 0xf0, 0xff, 0x01, 0xf0, 0xff, 0x1f, 0xf8, 0xff, 0x07,
0x00, 0xfe, 0x1f, 0x7c, 0xe0, 0x0f, 0x78, 0xf0, 0x3f, 0xbc, 0x07, 0x1f,
0x78, 0xe0, 0x3d, 0xde, 0x07, 0x1f, 0x78, 0xe0, 0x3d, 0xcf, 0x07, 0x1f,
0x7c, 0xf0, 0xf9, 0xcf, 0x03, 0x1f, 0x3c, 0xf8, 0xf8, 0xc7, 0x83, 0x0f,
0x3c, 0x7e, 0xf8, 0xc3, 0xe3, 0x07, 0xfc, 0x3f, 0xf0, 0xe1, 0xff, 0x03,
0xfe, 0x0f, 0xf0, 0xe0, 0xff, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xfe, 0xff, 0xff, 0x03, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0x00,
0xff, 0xff, 0x00, 0xfe, 0xff, 0x03, 0xff, 0xff, 0x00, 0xfe, 0xff, 0x03,
0xf8, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00
};
#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, xMov = 1, y, yMov = 1, dly = 100;
bool titleflag = 0, colorflag;
void setup(void) {
randomSeed(analogRead(A0));
u8g2.begin();
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.setFontMode(1); // transparent
u8g2.setFontDirection(0); // horizontal
u8g2.clear(); // clear buffer and update display
}
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: triangle(); break;
case 7: text(); break;
case 8: bitmap(); break;
default: break;
}
if (millis() - timer > timeout) { // wait for timeout
timer = millis();
if (count++ == CASES) { // next function
count = 1; // reset to first function
}
newFrane72x40(); // draw new frame
}
}
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
u8g2.updateDisplay();
}
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
u8g2.updateDisplay();
}
void pixel() {
showTitle("drawPixel");
byte x = random(2, SCREEN_WIDTH - 2), y = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2);
u8g2.drawPixel(x, y);
u8g2.updateDisplay();
delay(dly);
}
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);
u8g2.updateDisplay();
delay(dly);
}
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);
u8g2.updateDisplay();
delay(dly);
}
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);
u8g2.updateDisplay();
delay(dly);
}
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);
u8g2.updateDisplay();
delay(dly);
}
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);
u8g2.updateDisplay();
delay(dly);
}
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
u8g2.updateDisplay();
}
void bitmap() {
if (x < 0 || x >= SCREEN_WIDTH - bmap_width) // x boundries
xMov = -xMov;
if (y < 0 || y >= SCREEN_HEIGHT - bmap_height) // y boundries
yMov = -yMov;
x += xMov; // update x
y += yMov; // update y
u8g2.clearBuffer();
u8g2.drawXBMP(x, y, bmap_width, bmap_height, bmap);
u8g2.sendBuffer();
}