Scroll text using I2C SH1106

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Scroll text using I2C SH1106

Post by soggycashew » Tue Jan 11, 2022 7:14 pm

Hello, I'm using the sh1106 library from github and I needed my text to scroll vertically down and off screen and start over at top again. How is this done?

Thanks!

Code: Select all

import machine
from machine import I2C
import utime
from sh1106 import SH1106_I2C

i2c = I2C(0, sda=machine.Pin(4), scl=machine.Pin(5), freq=400000)

display = SH1106_I2C(128, 64, i2c)
display.sleep(False)
display.fill(0)
display.text('Testing 1', 20, 0, 1)
display.text('Testing 2', 20, 12, 1)
display.text('Testing 3', 20, 24, 1)
display.text('Testing 4', 20, 36, 1)
display.text('Testing 5', 20, 48, 1)
display.show()

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Scroll text using I2C SH1106

Post by scruss » Wed Jan 12, 2022 3:52 am

the SH1106 library inherits scroll() from the framebuf library

soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Re: Scroll text using I2C SH1106

Post by soggycashew » Wed Jan 12, 2022 5:44 pm

If anyone is interested I found the answer...

Code: Select all

'''
Continuous vertical scroll
If you want to scroll the screen in and out vertically continuously, you
can use the scroll_in_out_screen_v(screen) function.
'''

import machine
from machine import I2C
from sh1106 import SH1106_I2C
# ------------------------------------------------------------------------
# Define GPIO pins

i2c = I2C(0, sda=machine.Pin(4), scl=machine.Pin(5), freq=400000)
# ------------------------------------------------------------------------

oled_width = 128
oled_height = 64
oled = SH1106_I2C(oled_width, oled_height, i2c)
# ----------------------
screen1_row1 = "Screen 1, row 1"
screen1_row2 = "Screen 1, row 2"
screen1_row3 = "Screen 1, row 3"
# ----------------------
screen2_row1 = "Screen 2, row 1"
screen2_row2 = "Screen 2, row 2"
# ----------------------
screen1 = [[0, 0 , screen1_row1], [0, 16, screen1_row2], [0, 32, screen1_row3]]
screen2 = [[0, 0 , screen2_row1], [0, 16, screen2_row2]]

# ------------------------------------------------------------------------
#Define Functions

def scroll_screen_in_out_v(screen):
  for i in range (0, (oled_height*2+1), 1):
    for line in screen:
      oled.text(line[2], line[0], -oled_height+i+line[1])
    oled.show()
    if i!= oled_height:
        oled.fill(0)
# ------------------------------------------------------------------------
while True:

# Continuous verticall scroll
    scroll_screen_in_out_v(screen1)
    scroll_screen_in_out_v(screen2)

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Scroll text using I2C SH1106

Post by scruss » Fri Jan 14, 2022 3:29 am

I ran the following on an SSD1306, which should have a very similar API as the SH1106:

Code: Select all

from machine import Pin, I2C
import s2mini  # on Lolin ESP32-S2 Mini
import ssd1306

WIDTH = 64  # screen size
HEIGHT = 48
SIZE = 8  # text size

# set up and clear screen
i2c = I2C(0, scl=Pin(s2mini.I2C_SCL), sda=Pin(s2mini.I2C_SDA))
oled = ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c)
oled.fill(0)

n = 0
while True:
    oled.scroll(0, -SIZE)  # scroll up one text line
    oled.fill_rect(0, HEIGHT-SIZE, WIDTH, SIZE, 0)  # blank last line
    oled.text("%8d" % n, 0, HEIGHT-SIZE)  # write text
    oled.show()
    n = n+1
The procedure seems to be:
  1. scroll the display up one line
  2. blank out the last line
  3. draw your text on the last line

soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Re: Scroll text using I2C SH1106

Post by soggycashew » Fri Jan 14, 2022 5:05 am

@scruss i have everything working except i cant figure out how to update my sensor data. I posted this question in a separate thread HERE

Post Reply