# Import the required modules
import time
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
# Set up the OLED display
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=200000)
oled = SSD1306_I2C(128, 32, i2c)
# Load the text from the file
with open('sample.txt') as file:
text = file.read().replace("'", "")
# Wrap each line of text to fit on the screen
def wrap_text_lines(lines, width):
wrapped_lines = []
for line in lines:
wrapped_lines += wrap_text(line, width)
return wrapped_lines
# Wrap the text to fit on the screen
def wrap_text(text, width):
words = text.split()
lines = []
current_line = ""
for word in words:
if "\n\n" in word:
# Add the current line to the list of lines
if current_line:
lines.append(current_line.strip())
current_line = ""
# Add an empty line to the list of lines
lines.append(" " * width)
# Add the word after the double new line to a new line
lines.append(word.replace("\n\n", "").strip())
# Add another empty line after the new line
lines.append(" " * width)
elif "\n" in word:
# Add the current line to the list of lines
if current_line:
lines.append(current_line.strip())
current_line = ""
# Add the word after the new line to a new line
lines.append(word.strip())
elif len(current_line + word) <= width:
current_line += word + " "
else:
lines.append(current_line.strip())
current_line = word + " "
if current_line:
lines.append(current_line.strip())
return lines
lines = wrap_text_lines(text.splitlines(), width=16)
# Set up the buttons
# Up button
button1 = Pin(2, Pin.IN, Pin.PULL_UP)
# Down button
button2 = Pin(3, Pin.IN, Pin.PULL_UP)
# Clear button
button3 = Pin(4, Pin.IN, Pin.PULL_UP)
# Initialize scroll mode and scroll index
scroll_mode = True
scroll_index = 0
# Display current lines
display_lines = lines[scroll_index:scroll_index+4]
oled.fill(0)
for i, line in enumerate(display_lines):
oled.text(line, 0, i*8)
oled.show()
# Loop indefinitely
while True:
# Check if button 1 was pressed
if not button1.value():
if not scroll_mode:
# Set to scroll mode and display current lines
scroll_mode = True
display_lines = lines[scroll_index:scroll_index+4]
oled.fill(0)
for i, line in enumerate(display_lines):
oled.text(line, 0, i*8)
oled.show()
time.sleep(0.1) # Add a 100ms delay
else:
# Scroll up if possible
if scroll_index > 0:
scroll_index -= 1
display_lines = lines[scroll_index:scroll_index+4]
oled.fill(0)
for i, line in enumerate(display_lines):
oled.text(line, 0, i*8)
oled.show()
time.sleep(0.1) # Add a 100ms delay
# Check if button 2 was pressed
if not button2.value():
if not scroll_mode:
# Set to scroll mode and display current lines
scroll_mode = True
display_lines = lines[scroll_index:scroll_index+4]
oled.fill(0)
for i, line in enumerate(display_lines):
oled.text(line, 0, i*8)
oled.show()
time.sleep(0.1) # Add a 100ms delay
else:
# Scroll down if possible
if scroll_index < len(lines) - 4:
scroll_index += 1
display_lines = lines[scroll_index:scroll_index+4]
oled.fill(0) # Clear the OLED display
for i, line in enumerate(display_lines):
oled.text(line, 0, i*8)
oled.show()
time.sleep(0.1) # Add a 100ms delay
# Check if the clear button was pressed
if not button3.value():
# Turn off the OLED display and set scroll mode to off
oled.fill(0)
oled.show()
scroll_mode = False
time.sleep(0.5) # Add a 500ms delay