#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h> // library requires for IIC communication
//#define OLED_SDA 14
//#define OLED_SCL 12
#define THREAD_FREQ 20 //milisec
//Define the constructor for NODEMCU ESP8266 Board with built-in 128x64 OLED based on SSD1306 driver
//U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0,U8X8_PIN_NONE,OLED_SCL,OLED_SDA); // [full framebuffer, size = 1024 bytes]
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); // [full framebuffer, size = 1024 bytes]
// images from https://lopaka.app/
//static const unsigned char image_Lock_7x8_bits[] U8X8_PROGMEM = {0x1c,0x22,0x22,0x7f,0x7f,0x77,0x7f,0x3e};
//static const unsigned char image_Bluetooth_Idle_5x8_bits[] U8X8_PROGMEM = {0x04,0x0d,0x16,0x0c,0x0c,0x16,0x0d,0x04};
//static const unsigned char image_Volup_8x6_bits[] U8X8_PROGMEM = {0x48,0x8c,0xaf,0xaf,0x8c,0x48};
static const unsigned char image_Alert_9x8_bits[] U8X8_PROGMEM = {0x10,0x00,0x38,0x00,0x28,0x00,0x6c,0x00,0x6c,0x00,0xfe,0x00,0xee,0x00,0xff,0x01};
int progress = 0; // progress of the progressbar
char buffer[32]; // helper buffer to construct a string to be displayed
char int_time[4];
//timer global variables
unsigned long current_time;
unsigned long previous_time;
const char to_print[] = "Test1 \n Test2 \n Test3 \n Test4 \n Test5 \n Test6";
char to_print_2[] = "-> Weather forecast for today is:";
char to_print_3[] = "-> Quote of the day is:";
int print_pointer=127;
int to_print_len;
void printwords(const char *msg, int xloc, int yloc /*bottom*/ );
void setup(void) {
u8g2.begin(); // start the u8g2 library
previous_time=0;
//to_print_len = sizeof(to_print);
}
//********* MAIN LOOP
void loop(void) {
current_time = millis();
if ((current_time - previous_time)>=THREAD_FREQ) {
//refresh_OLED_now();
//refresh_scroll (print_pointer);
draw_text(to_print);
}else{
}
}
/*........... Print text on display..........
// take input stream and display it on SSD1306 OLED
*/
void draw_text (const char *input_text) {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_haxrcorp4089_tr);
u8g2.drawStr(0, 7, "> Display Screen");
sprintf(int_time, "-> T:%u", (current_time-previous_time));
u8g2.drawStr(90, 7, int_time); // refresh rate
u8g2.drawLine(0, 9, 127, 9);
printwords(input_text, 0, 16);
u8g2.sendBuffer();
previous_time=current_time;
}
//Text scrolling function
void refresh_scroll (int &brojac_) {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_haxrcorp4089_tr);
u8g2.drawStr(brojac_, 7, to_print);
u8g2.drawStr(brojac_, 20, to_print_2);
u8g2.drawStr(brojac_, 33, to_print_3);
u8g2.sendBuffer();
previous_time=current_time;
if (brojac_==-127) {
brojac_=127;
} else {
brojac_-=1;
}
}
//******************************************* display refresh function
void refresh_OLED_now(void) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setBitmapMode(1);
u8g2.drawFrame(12, 21, 104, 20);
u8g2.drawBox(14, 23, progress, 16); // draw the progressbar fill
u8g2.setFont(u8g2_font_helvB08_tr);
sprintf(buffer, "Progress: %d%%", progress); // construct a string with the progress variable
u8g2.drawStr(33, 53, buffer); // display the string
u8g2.setFont(u8g2_font_haxrcorp4089_tr);
u8g2.drawStr(0, 7, "Progress Bar Screen");
sprintf(int_time, "-> T:%u", (current_time-previous_time));
u8g2.drawStr(90, 7, int_time); // refresh rate
u8g2.drawLine(0, 9, 127, 9);
//u8g2.drawXBMP( 100, 0, 7, 8, image_Lock_7x8_bits);
//u8g2.drawXBMP( 111, 0, 5, 8, image_Bluetooth_Idle_5x8_bits);
//u8g2.drawXBMP( 120, 1, 8, 6, image_Volup_8x6_bits);
u8g2.drawXBMP( 22, 45, 9, 8, image_Alert_9x8_bits);
u8g2.sendBuffer();
// increase the progress value to go over 0-100
progress = progress + 1;
if (progress > 100) {
progress = 0;
} // transfer internal memory to the display
}
// function for text print
void printwords(const char *msg, int xloc, int yloc /*bottom*/ ) {
int dspwidth = u8g2.getDisplayWidth(); // display width in pixels
int strwidth = 0; // string width in pixels
char glyph[2]; glyph[1] = 0;
for (const char *ptr = msg, *lastblank = NULL; *ptr; ++ptr) {
while (xloc == 0 && (*msg == ' ' || *msg == '\n'))
if (ptr == msg++) ++ptr; // skip blanks and newlines at the left edge
glyph[0] = *ptr;
strwidth += u8g2.getStrWidth(glyph); // accumulate the pixel width
if (*ptr == ' ') lastblank = ptr; // remember where the last blank was
else ++strwidth; // non-blanks will be separated by one additional pixel
if (*ptr == '\n' || // if we found a newline character,
xloc + strwidth > dspwidth) { // or if we ran past the right edge of the display
int starting_xloc = xloc;
// print to just before the last blank, or to just before where we got to
while (msg < (lastblank ? lastblank : ptr)) {
glyph[0] = *msg++;
xloc += u8g2.drawStr(xloc, yloc, glyph); }
strwidth -= xloc - starting_xloc; // account for what we printed
yloc += u8g2.getMaxCharHeight(); // advance to the next line
xloc = 0; lastblank = NULL; } }
while (*msg) { // print any characters left over
glyph[0] = *msg++;
xloc += u8g2.drawStr(xloc, yloc, glyph); } }