#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
int progress, total;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* rest= */ U8X8_PIN_NONE, /* clock= */ 22, /* data= */ 21);
/*void setup(void) {
Serial.begin(115200);
u8g2.begin();
progress = 78, total = 100;
}
*/
// This example shows a scrolling text.
// If U8G2_16BIT is not set (default), then the pixel width of the text must be lesser than 128
// If U8G2_16BIT is set, then the pixel width an be up to 32000
u8g2_uint_t offset; // current offset for the scrolling text
u8g2_uint_t width; // pixel width of the scrolling text (must be lesser than 128 unless U8G2_16BIT is defined
const char *text = "U8g2 "; // scroll this text from right to left
void setup(void) {
/* U8g2 Project: SSD1306 Test Board */
//pinMode(10, OUTPUT);
//pinMode(9, OUTPUT);
//digitalWrite(10, 0);
//digitalWrite(9, 0);
/* U8g2 Project: T6963 Test Board */
//pinMode(18, OUTPUT);
//digitalWrite(18, 1);
/* U8g2 Project: KS0108 Test Board */
//pinMode(16, OUTPUT);
//digitalWrite(16, 0);
u8g2.begin();
u8g2.setFont(u8g2_font_helvB18_tr); // set the target font to calculate the pixel width
width = u8g2.getUTF8Width(text); // calculate the pixel width of the text
u8g2.setFontMode(0); // enable transparent mode, which is faster
// draw the constant part of the screen
// this will not be overwritten later
u8g2.firstPage();
do {
u8g2.drawUTF8(18, 20, "[ U8g2 ]"); // This part will stay constantly on the screen
} while ( u8g2.nextPage() );
}
void drawText(void) {
u8g2_uint_t x = offset;
do { // repeated drawing of the scrolling text...
u8g2.drawUTF8(x, 19, text); // draw the scolling text
x += width; // add the pixel width of the scrolling text
} while ( x < u8g2.getDisplayWidth() ); // draw again until the complete display is filled
}
void loop(void) {
int i;
// write the scrolling text to the lower part of the display
for ( i = 0; i < 3; i++ )
{
// draw to lines 0...23 (3*8-1)
// draw to the upper part of the screen
u8g2.setBufferCurrTileRow(i);
u8g2.clearBuffer();
drawText();
// but write the buffer to the lower part (offset 4*8 = 32)
u8g2.setBufferCurrTileRow(4 + i);
u8g2.sendBuffer();
}
// calculate the new offset for the scrolling
offset -= 1; // scroll by one pixel
if ( (u8g2_uint_t)offset < (u8g2_uint_t) - width )
offset = 0; // start over again
delay(10); // do some small delay
}
/*
void loop(void) {
//u8g2.clearBuffer(); // clear the internal memory
//u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
//u8g2.drawStr(0, 10, "Hello World!"); // write something to the internal memory
// u8g2.sendBuffer(); // transfer internal memory to the display
//delay(1000);
if (progress) progress--;
else progress = 100;
Serial.printf("Progress: %u%%\r", progress / (total / 100));
u8g2.firstPage();
do {
u8g2.drawRFrame(0, 0, 88, 48, 3);
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.setFontPosTop();
u8g2.drawStr(0, 0, "SW Updating!");
} while (u8g2.nextPage());
delay(2000);
u8g2.firstPage();
do {
u8g2.drawRFrame(0, 0, 88, 48, 3);
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.setFontPosTop();
//u8g2.drawStr(0, 0, "SW Updating!");
u8g2.drawStr(0, 0, "Connection");
u8g2.drawStr(0, 12, "Failed!");
u8g2.drawFrame(4, 20, 80, 8);
u8g2.drawBox(5, 21, map(progress / (total / 100), 0, 100, 0, 78), 6);
u8g2.setCursor(0, 36);
u8g2.printf("%d%%", progress / (total / 100));
//u8g2.print(F("%"));
} while (u8g2.nextPage());
delay(1000);
//ESP.restart();
u8g2.firstPage();
do {
u8g2.drawRFrame(0, 0, 88, 48, 3);
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.setFontPosTop();
u8g2.drawStr(0, 36, "Restarting!");
} while (u8g2.nextPage());
delay(2000);
}
*/