"""
This Simulation displays a text based on user's input on the OLED SSD1306.
On pressing the Red push button, the displayed Text shifts to the left side.
On pressing the Yellow button, the text scrolls Down and Up and comes back
to it's original position.
On pressing the Green button, the text shifts to the right side.
"""
from machine import Pin, I2C
import ssd1306
import time
oled_width = 128
oled_height = 64
def display_text(oled, text, x, y):
oled.fill(0)
oled.text(text, x, y) # Display text on the OLED
oled.show()
if __name__ == '__main__' :
text = input("Please enter the text to be displayed : ")
l = len(text)
i2c = I2C(0, scl = Pin(22), sda = Pin(21))
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
pin_0 = Pin(32, mode = Pin.IN) # Configure pins on the ESP32
pin_1 = Pin(34, mode = Pin.IN) # board as input pins.
pin_2 = Pin(35, mode = Pin.IN)
def handle_interrupt0(pin) : # ISR (Interrupt Service Routine)
oled.fill(0) # for pin_0, i.e pin no. 32.
display_text(oled, text, 0, 27)
time.sleep_ms(500)
pin_0.irq(trigger = Pin.IRQ_RISING, handler = handle_interrupt0)
def handle_interrupt1(pin) : # ISR for pin_1, i.e pin no. 34.
oled.fill(0)
display_text(oled, text, 128 - 8*l, 27)
time.sleep_ms(500)
pin_1.irq(trigger = Pin.IRQ_RISING, handler = handle_interrupt1)
def handle_interrupt2(pin) : # ISR for pin_2, i.e pin no. 35.
oled.fill(0)
for i in range(8) :
oled.fill(0)
display_text(oled, text, 64 - 8*(l // 2), 8*i)
time.sleep_ms(50)
time.sleep_ms(100)
for i in range(8) :
oled.fill(0)
display_text(oled, text, 64 - 8*(l // 2), 56 - 8*i)
time.sleep_ms(50)
pin_2.irq(trigger = Pin.IRQ_RISING, handler = handle_interrupt2)
oled.fill(0)
while True : # While Uninterrupted, do this...
oled.fill(0)
display_text(oled, text, 64 - 8*(l // 2), 27) # Displays text at
# the centre.