// simple project using Arduino UNO and 128x64 SSD1306 IIC OLED Display to show battery charging indicator
// created by upir, 2023
// youtube channel: https://www.youtube.com/upir_upir
// YOUTUBE VIDEO: https://youtu.be/caHcaUoQ2kg
// SOURCE files: https://github.com/upiir/arduino_oled_battery_indicator
// Links from the video:
// 72x40 SSD1306 OLED 0.42" Display: https://s.click.aliexpress.com/e/_Ddq0EwJ
// 128x64 SSD1306 OLED Display 1.54": https://s.click.aliexpress.com/e/_DCYdWXb
// 128x64 SSD1306 OLED Display 0.96": https://s.click.aliexpress.com/e/_DCKdvnh
// 128x64 SSD1306 OLED Display 2.42": https://s.click.aliexpress.com/e/_DFdMoTh
// Arduino UNO: https://s.click.aliexpress.com/e/_AXDw1h
// Arduino breadboard prototyping shield: https://s.click.aliexpress.com/e/_ApbCwx
// Image2cpp (convert array to image): https://javl.github.io/image2cpp/
// Photopea (online graphics editor like Photoshop): https://www.photopea.com/
// Related videos with Arduino UNO and 128x64 OLED screen:
// Arduino + OLED displays playlist: https://www.youtube.com/playlist?list=PLjQRaMdk7pBZ1UV3IL5ol8Qc7R9k-kwXA
#include <Arduino.h>
#include <U8g2lib.h> // u8g2 library used for drawing on the OLED display
#include <Wire.h> // library requires for IIC communication
// I´m using two different displays, 128x64px in the WOKWI emulation and 72x40px for real Arduino
// Please uncomment the correct initialization line and comment out the other one
//U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // initialization for the 128x64px display
U8G2_SSD1306_72X40_ER_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);// 72x40px display [full framebuffer, size = 360 bytes]
int battery_fill_perc = 0; // battery fill percentage 0-100%
int anim_frame = 0; // animation frame for the blob / metaballs animation
int xoff = 29;//0;//29; // x offset for the battery
int yoff = 12;//0;//24; // y offset for the battery
void setup(void) {
u8g2.begin();
}
void loop(void) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setBitmapMode(1); // draw transparent images
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(0,15,"Hello World!");
u8g2.sendBuffer();
delay(1000);
}
/*
void loop(void) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setBitmapMode(1); // draw transparent images
u8g2.drawXBMP(xoff, yoff, 70, 40, epd_bitmap_battery_outline); // draw the image of the outline of the battery icon
int fill_width = map(battery_fill_perc, 0, 100, 4, 61); // width of the fill rectangle
u8g2.drawRBox(xoff+3, yoff+3, fill_width, 34, 1); // battery fill, rounded filled rectangle
u8g2.setClipWindow(xoff+3, yoff+3, xoff + 3 + 61, yoff+3 + 34); // restrict all graphics output to the specified range
u8g2.drawXBMP(xoff + 3 + fill_width - 1, yoff+3, 32, 34, epd_bitmap_allArray[anim_frame]); // draw blob animation
u8g2.setMaxClipWindow(); // restore writing to the complete window
//u8g2.drawFrame(29, 24, 70, 40); // draw fullscreen rectangle - this was only used for testing
u8g2.sendBuffer(); // transfer internal memory to the display
// increase battery fill percentage 0-100
battery_fill_perc++;
if (battery_fill_perc > 100) {battery_fill_perc = 0;}
// current frame for the blob animation, 0-29
anim_frame = (anim_frame + 1) % 30;
}
*/