/*
  ScrollingText.ino
  This will scroll text on the display.
  Enable U8g2 16 bit mode (see FAQ) for larger text!
  Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  Copyright (c) 2016, [email protected]
  All rights reserved.
  Redistribution and use in source and binary forms, with or without modification,
  are permitted provided that the following conditions are met:
    Redistributions of source code must retain the above copyright notice, this list
    of conditions and the following disclaimer.

    Redistributions in binary form must reproduce the above copyright notice, this
    list of conditions and the following disclaimer in the documentation and/or other
    materials provided with the distribution.
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define U8G2_16BIT

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif


/*
  U82glib Example Overview:
    Frame Buffer Examples: clearBuffer/sendBuffer. Fast, but may not work with all Arduino boards because of RAM consumption
    Page Buffer Examples: firstPage/nextPage. Less RAM usage, should work with all Arduino boards.
    U8x8 Text Only Example: No RAM usage, direct communication with display controller. No graphics, 8x8 Text only.

  This is a page buffer example.

*/

// Please UNCOMMENT one of the contructor lines below
// U8g2 Contructor List (Picture Loop Page Buffer)
// The complete list is available here: https://github.com/olikraus/u8g2/wiki/u8g2setupcpp
// Please update the pin numbers according to your setup. Use U8X8_PIN_NONE if the reset pin is not connected
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//U8G2_SH1106_128X64_NONAME_1_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
//U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//U8G2_SH1106_128X64_VCOMH0_1_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);    // same as the NONAME variant, but maximizes setContrast() range
//U8G2_SH1106_128X64_WINSTAR_1_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);   // same as the NONAME variant, but uses updated SH1106 init sequence
// End of constructor list


// 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 offset_t;
u8g2_uint_t indent;
u8g2_uint_t width;      // pixel width of the scrolling text (must be lesser than 128 unless U8G2_16BIT is defined
const char *text = "Beaverphone Rotary Dial Phonebook by Rüll "; // scroll this text from right to left


void setup(void) {

  Serial.begin(115200);
  u8g2.begin();

  u8g2.setFont(u8g2_font_amstrad_cpc_extended_8f); // set the target font to calculate the pixel width
  width = u8g2.getUTF8Width(text);    // calculate the pixel width of the text
  indent = u8g2.getUTF8Width("00 ");
  //u8g2.setFontMode(0);    // enable transparent mode, which is faster
}


void loop(void) {
  u8g2_uint_t x;
  u8g2_uint_t y;

  u8g2.firstPage();
  do {

    u8g2.setFont(u8g2_font_6x12_tf);
    u8g2.setFontMode(1);

    // draw the scrolling text at current offset
    x = offset;
    y = offset_t;

    do {                // repeated drawing of the scrolling text...
      //Title
      u8g2.drawUTF8(y, 8, "Hauptmenü");
      y += 120;
    } while ( y < u8g2.getDisplayWidth() );    // draw again until the complete display is filled

    do {                // repeated drawing of the scrolling text...
      u8g2.drawUTF8(x + indent, 16, text);   // draw the scolling text
      u8g2.drawUTF8(x + indent, 32, text);
      x += width;           // add the pixel width of the scrolling text

    } while ( x < u8g2.getDisplayWidth() );    // draw again until the complete display is filled

    //2nd line no-scroll
    u8g2.drawUTF8(indent, 24, "üäöÜÖÄßéèâ@€µ°");
    u8g2.drawUTF8(indent, 40, "Track 4");
    u8g2.drawUTF8(indent, 48, "~~fette Mukke**");
    u8g2.drawUTF8(indent, 56, "ALLES HAT EIN ENDE");
    u8g2.drawUTF8(indent, 64, "alle meine entchen");


    u8g2.setFont(u8g2_font_amstrad_cpc_extended_8n);
    u8g2.setFontMode(0);
    u8g2.drawUTF8(0, 16, "01 ");
    u8g2.drawUTF8(0, 24, "02 ");
    u8g2.drawUTF8(0, 32, "03 ");
    u8g2.drawUTF8(0, 40, "04 ");
    u8g2.drawUTF8(0, 48, "05 ");
    u8g2.drawUTF8(0, 56, "06 ");
    u8g2.drawUTF8(0, 64, "07 ");

  } while ( u8g2.nextPage() );

  offset_t -= 1;            // scroll by one pixel
  if ( (u8g2_uint_t)offset_t < (u8g2_uint_t) - 120)
    offset_t = 0;             // start over again

  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
}