SH1107 OLED Featherwing Scrolling Text

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Post Reply
User avatar
andrewdsd
 
Posts: 12
Joined: Mon Jun 13, 2022 1:31 pm

SH1107 OLED Featherwing Scrolling Text

Post by andrewdsd »

Hello,

My setup:
--> Board - Feather M4 Express
--> OLED - OLED FeatherWing SH1107 (128x64)
--> Coding - Arduino IDE

Project:
--> Is there a way to scroll a line of text off of the OLED screen? Meaning all the way off the screen and not to reapear from the other side.

Appreciate any guidance!

Code: Select all

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);

// OLED FeatherWing buttons map to different pins depending on board:
#if defined(ESP8266)
  #define BUTTON_A  0
  #define BUTTON_B 16
  #define BUTTON_C  2
#elif defined(ESP32) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2)
  #define BUTTON_A 15
  #define BUTTON_B 32
  #define BUTTON_C 14
#elif defined(ARDUINO_STM32_FEATHER)
  #define BUTTON_A PA15
  #define BUTTON_B PC7
  #define BUTTON_C PC5
#elif defined(TEENSYDUINO)
  #define BUTTON_A  4
  #define BUTTON_B  3
  #define BUTTON_C  8
#elif defined(ARDUINO_NRF52832_FEATHER)
  #define BUTTON_A 31
  #define BUTTON_B 30
  #define BUTTON_C 27
#else // 32u4, M0, M4, nrf52840, esp32-s2 and 328p
  #define BUTTON_A  9
  #define BUTTON_B  6
  #define BUTTON_C  5
#endif

void setup() {
  delay(250); // wait for the OLED to power up
  display.begin(0x3C, true); // Address 0x3C default
  display.display();
  delay(1000);

  // Clear the buffer.
  display.clearDisplay();
  display.display();

  display.setRotation(1);

  pinMode(BUTTON_A, INPUT_PULLUP);
  pinMode(BUTTON_B, INPUT_PULLUP);
  pinMode(BUTTON_C, INPUT_PULLUP);

  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
}

//*************************************** Begin Main Loop *******************************************
void loop() {
  display.setCursor(0,0);
  display.println("Ham is Good, Ham is Life, All Hail Ham!");
  //Would like to scroll the above text to the left and off the screen.
  delay(10);
  yield();
  display.display();  
}
//*************************************** End Main Loop *******************************************

User avatar
mikeysklar
 
Posts: 8427
Joined: Mon Aug 01, 2016 8:10 pm

Re: SH1107 OLED Featherwing Scrolling Text

Post by mikeysklar »

You can redraw the screen and shift the x-position as this code does:

Code: Select all

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_DC    11
#define OLED_CS    12
#define OLED_CLK   10
#define OLED_MOSI   9
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

char message[] = "Hello WOrrrrrllllllllllllllllllllllllllllldddd";
int  x, minX;

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setTextWrap(false);
  x    = display.width();
  minX = -12 * strlen(message); // 12 = 6 pixels/character * text size 2
}

void loop() {
  display.clearDisplay();
  display.setCursor(x, 20);
  display.print(message);
  display.display();

  if(--x < minX) x = display.width();
}
viewtopic.php?t=48762

User avatar
andrewdsd
 
Posts: 12
Joined: Mon Jun 13, 2022 1:31 pm

Re: SH1107 OLED Featherwing Scrolling Text

Post by andrewdsd »

Ok. Thank you for the starting point mikeysklar!

I cannibalized, experimented and got a working code.

My code below displays the message for 2 seconds. Then begins scrolling left from Cursor position "0" instead of display.width(), which in my case would be cursor position 128 (the right side of the screen).

Question...Is there a way to speed the scrolling up, make it move off the screen faster?

Code: Select all

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);

// OLED FeatherWing buttons map to different pins depending on board:
#if defined(ESP8266)
  #define BUTTON_A  0
  #define BUTTON_B 16
  #define BUTTON_C  2
#elif defined(ESP32) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2)
  #define BUTTON_A 15
  #define BUTTON_B 32
  #define BUTTON_C 14
#elif defined(ARDUINO_STM32_FEATHER)
  #define BUTTON_A PA15
  #define BUTTON_B PC7
  #define BUTTON_C PC5
#elif defined(TEENSYDUINO)
  #define BUTTON_A  4
  #define BUTTON_B  3
  #define BUTTON_C  8
#elif defined(ARDUINO_NRF52832_FEATHER)
  #define BUTTON_A 31
  #define BUTTON_B 30
  #define BUTTON_C 27
#else // 32u4, M0, M4, nrf52840, esp32-s2 and 328p
  #define BUTTON_A  9
  #define BUTTON_B  6
  #define BUTTON_C  5
#endif

char message[] = "Ham is Good, Ham is Life, All Hail Ham!";
int x, minX;

void setup() {
  delay(250); // wait for the OLED to power up
  display.begin(0x3C, true); // Address 0x3C default
  display.display();
  delay(1000);

  // Clear the buffer.
  display.clearDisplay();
  display.display();

  display.setRotation(1);

  pinMode(BUTTON_A, INPUT_PULLUP);
  pinMode(BUTTON_B, INPUT_PULLUP);
  pinMode(BUTTON_C, INPUT_PULLUP);

  display.setTextSize(2);
  display.setTextColor(SH110X_WHITE);
  display.setTextWrap(false);
  //x = display.width(); Don't need to use this for my code
  minX = -12 * strlen(message); // 12 = 6 pixels/character * text size 2  
}

//*************************************** Begin Main Loop *******************************************
void loop() {
  display.clearDisplay();
  display.setCursor(0, 30);
  display.print(message);
  display.display();
  delay(2000);  
  for(x = 0; x > minX; x--){  
    display.clearDisplay();
    display.setCursor(x, 30);
    display.print(message);
    display.display();    
  }
}
//*************************************** End Main Loop *******************************************

User avatar
mikeysklar
 
Posts: 8427
Joined: Mon Aug 01, 2016 8:10 pm

Re: SH1107 OLED Featherwing Scrolling Text

Post by mikeysklar »

You have a fast M4 board driving the OLED FeatherWing so I think you might be able to bump the i2c clock speed up from the default of 400000 (400 KHz) to 1000000 (1 MHz).

https://adafruit.github.io/Adafruit_SH1 ... h1107.html

Code: Select all

Adafruit_SH1107::Adafruit_SH1107	(	uint16_t 	w,
uint16_t 	h,
TwoWire * 	twi = &Wire,
int8_t 	rst_pin = -1,
uint32_t 	clkDuring = 400000,
uint32_t 	clkAfter = 100000 
)		
Constructor for I2C-interfaced SH1107 displays.

Parameters
w Display width in pixels
h Display height in pixels
twi Pointer to an existing TwoWire instance (e.g. &Wire, the microcontroller's primary I2C bus).
rst_pin Reset pin (using Arduino pin numbering), or -1 if not used (some displays might be wired to share the microcontroller's reset pin).
clkDuring Speed (in Hz) for Wire transmissions in SH110X library calls. Defaults to 400000 (400 KHz), a known 'safe' value for most microcontrollers, and meets the SH110X datasheet spec. Some systems can operate I2C faster (800 KHz for ESP32, 1 MHz for many other 32-bit MCUs), and some (perhaps not all) SH110X's can work with this – so it's optionally be specified here and is not a default behavior. (Ignored if using pre-1.5.7 Arduino software, which operates I2C at a fixed 100 KHz.)
clkAfter Speed (in Hz) for Wire transmissions following SH110X library calls. Defaults to 100000 (100 KHz), the default Arduino Wire speed. This is done rather than leaving it at the 'during' speed because other devices on the I2C bus might not be compatible with the faster rate. (Ignored if using pre-1.5.7 Arduino software, which operates I2C at a fixed 100 KHz.)
Note
Call the object's begin() function before use – buffer allocation is performed there!

User avatar
andrewdsd
 
Posts: 12
Joined: Mon Jun 13, 2022 1:31 pm

Re: SH1107 OLED Featherwing Scrolling Text

Post by andrewdsd »

Thank you for the suggestion but, I don't have a clue on how and where to place this within my code.

Is there an example or suggestion on how to use this?

User avatar
mikeysklar
 
Posts: 8427
Joined: Mon Aug 01, 2016 8:10 pm

Re: SH1107 OLED Featherwing Scrolling Text

Post by mikeysklar »

Try overriding the i2c bus speed from default 400kHz to 1 MHz with this line at the beginning of your setup() function.

You can also experiment with the high speed and 3.4 MHz as mentioned below.

Code: Select all

Wire.setClock(1000000);
clockFrequency: the value (in Hertz) of the desired communication clock. Accepted values are 100000 (standard mode) and 400000 (fast mode). Some processors also support 10000 (low speed mode), 1000000 (fast mode plus) and 3400000 (high speed mode). Please refer to the specific processor documentation to make sure the desired mode is supported.
https://www.arduino.cc/reference/en/lan ... /setclock/

User avatar
andrewdsd
 
Posts: 12
Joined: Mon Jun 13, 2022 1:31 pm

Re: SH1107 OLED Featherwing Scrolling Text

Post by andrewdsd »

Ok, figured it out. I could have sworn I tried this already but, this is what works..."x-=n", where "n" is any value you want. Again, I swear I tried this before. :(

In any case, mikeysklar thank you so much for taking the time to assist me. I appreciate it. As for the adjusting the clock frequency, I did not see any change.

Below is my working code.

Code: Select all

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);

// OLED FeatherWing buttons map to different pins depending on board:
#if defined(ESP8266)
  #define BUTTON_A  0
  #define BUTTON_B 16
  #define BUTTON_C  2
#elif defined(ESP32) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2)
  #define BUTTON_A 15
  #define BUTTON_B 32
  #define BUTTON_C 14
#elif defined(ARDUINO_STM32_FEATHER)
  #define BUTTON_A PA15
  #define BUTTON_B PC7
  #define BUTTON_C PC5
#elif defined(TEENSYDUINO)
  #define BUTTON_A  4
  #define BUTTON_B  3
  #define BUTTON_C  8
#elif defined(ARDUINO_NRF52832_FEATHER)
  #define BUTTON_A 31
  #define BUTTON_B 30
  #define BUTTON_C 27
#else // 32u4, M0, M4, nrf52840, esp32-s2 and 328p
  #define BUTTON_A  9
  #define BUTTON_B  6
  #define BUTTON_C  5
#endif

char message[] = "Ham is Good, Ham is Life, All Hail Ham!";
int x, minX;

void setup() {
  delay(250); // wait for the OLED to power up
  display.begin(0x3C, true); // Address 0x3C default
  
  display.display();
  delay(1000);

  // Clear the buffer.
  display.clearDisplay();
  display.display();

  display.setRotation(1);

  pinMode(BUTTON_A, INPUT_PULLUP);
  pinMode(BUTTON_B, INPUT_PULLUP);
  pinMode(BUTTON_C, INPUT_PULLUP);

  display.setTextSize(2);
  display.setTextColor(SH110X_WHITE);
  display.setTextWrap(false);
  //x = display.width(); Don't need to use this for my code
  minX = -12 * strlen(message); // 12 = 6 pixels/character * text size 2  
}

//*************************************** Begin Main Loop *******************************************
void loop() {
  display.clearDisplay();
  display.setCursor(0, 30); //Start my message at around half way down my screen height..."(0,30)"
  display.print(message);
  display.display();
  delay(2000);  //Staticly display my message for 2 seconds 
  for(x = 0; x > minX; x-=4){ //Adjust the "x-=4" (change the numeric value "4"), to make the message scroll faster or slower to the left. Slowest would be "x--"  
    display.clearDisplay(); 
    display.setCursor(x, 30);
    display.print(message);
    display.display();    
  }
}
//*************************************** End Main Loop *******************************************

User avatar
mikeysklar
 
Posts: 8427
Joined: Mon Aug 01, 2016 8:10 pm

Re: SH1107 OLED Featherwing Scrolling Text

Post by mikeysklar »

Changing the i2c lock speed probably need to be done with the Adafruit_SH1107() constructor call.

If you are already seeing the desired performance you can ignore this, but I think it worth a try:

change this:

Code: Select all

Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);
to this:

Code: Select all

Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire, -1, 1000000);

Post Reply
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”