Skip to content

Our Eigth Lab: Using an SSD1306 Digital Display

Solution: Moving Your Name Around the Display

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Author: J.E. Tannenbaum
# Initial Release: 01/29/2022
# Writing text to a I2C SSD1306 display
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import time

# Instantiate the I2C & SSD1306 classes
i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
display = SSD1306_I2C(128, 64, i2c)

# Setup the initial position & direction
xPos = 0
yPos = 0
xDir = 1
yDir = 1

# Guess at the edge cases
xEdge = 57
yEdge = 56

# Loop forever
while True:

   # Clear the old framebuffer, write the new text, then push it to the display
   display.fill(0)
   display.text("Hello Jet", xPos, yPos)
   display.show()

   # Update the x position, then check for edge cases
   xPos += xDir
   if xPos < 0:
       xPos = 0
       xDir = 1
   elif xPos > xEdge:
       xPos = xEdge
       xDir = -1

   # Update the y position, then check for edge cases
   yPos += yDir
   if yPos < 0:
       yPos = 0
       yDir = 1
   elif yPos > yEdge:
       yPos = yEdge
       yDir = -1

   # delay
   time.sleep(.25)