#include <Arduino.h>
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ A5, /* data=*/ A4, /* reset=*/ U8X8_PIN_NONE);
int16_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 = "Shutter Speed Tester"; // scroll this text from right to left
const uint8_t tile_area_x_pos = 1; // Update area left position (in tiles)
const uint8_t tile_area_y_pos = 1; // Update area upper position (distance from top in tiles)
const uint8_t tile_area_width = 14;
const uint8_t tile_area_height = 2; // this will allow cour18 chars to fit into the area
const u8g2_uint_t pixel_area_x_pos = tile_area_x_pos*8;
const u8g2_uint_t pixel_area_y_pos = tile_area_y_pos*8;
const u8g2_uint_t pixel_area_width = tile_area_width*8;
const u8g2_uint_t pixel_area_height = tile_area_height*8;
void setup(void) {
u8g2.begin();
u8g2.clearBuffer(); // clear the internal memory
//u8g2.setFont(u8g2_font_helvR10_tr); // choose a suitable font
//u8g2.drawStr(0,12,"UpdateDisplayArea"); // write something to the internal memory
// draw a frame, only the content within the frame will be updated
// the frame is never drawn again, but will stay on the display
u8g2.drawBox(pixel_area_x_pos-1, pixel_area_y_pos-1, pixel_area_width+2, pixel_area_height+2);
u8g2.sendBuffer(); // transfer internal memory to the display
u8g2.setFont(u8g2_font_tenthinguys_tr); // set the target font for the text width calculation
width = u8g2.getUTF8Width(text); // calculate the pixel width of the text
offset = width+pixel_area_width;
}
void loop(void) {
u8g2.clearBuffer(); // clear the complete internal memory
// draw the scrolling text at current offset
u8g2.setFont(u8g2_font_tenthinguys_tr); // set the target font
u8g2.drawUTF8(pixel_area_x_pos-width+offset,pixel_area_y_pos+pixel_area_height+u8g2.getDescent()-1, text); // draw the scolling text
// now only update the selected area, the rest of the display content is not changed
u8g2.updateDisplayArea(tile_area_x_pos, tile_area_y_pos, tile_area_width, tile_area_height);
offset--; // scroll by one pixel
if ( offset == 0 )
offset = width+pixel_area_width; // start over again
delay(10); // do some small delay
}