Skip to content

Commit 467bb4c

Browse files
authored
Merge pull request #2 from makermelissa/master
Updated example to work better for I2C
2 parents 393f22e + c9b4d00 commit 467bb4c

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

README.rst

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,42 @@ Usage Example
6161
.. code-block:: python
6262
6363
import board
64+
import digitalio
6465
import displayio
6566
import terminalio
6667
from adafruit_display_text import label
6768
import adafruit_displayio_ssd1305
6869
6970
displayio.release_displays()
7071
71-
oled_reset = board.D9
72-
73-
# Use for I2C
74-
#i2c = board.I2C()
75-
#display_bus = displayio.I2CDisplay(i2c, device_address=0x3c, reset=oled_reset)
72+
# Manually Toggle Reset
73+
reset = digitalio.DigitalInOut(board.D9)
74+
reset.switch_to_output(value=False)
75+
reset.value = True
7676
7777
# Use for SPI
7878
spi = board.SPI()
7979
oled_cs = board.D5
8080
oled_dc = board.D6
8181
display_bus = displayio.FourWire(spi, command=oled_dc, chip_select=oled_cs,
82-
reset=oled_reset, baudrate=1000000)
82+
baudrate=1000000)
83+
84+
# Use for I2C
85+
# i2c = board.I2C()
86+
# display_bus = displayio.I2CDisplay(i2c, device_address=0x3c)
87+
8388
WIDTH = 128
84-
HEIGHT = 64 # Change to 64 if needed
85-
BORDER = 5
89+
HEIGHT = 64 # Change to 32 if needed
90+
BORDER = 8
91+
FONTSCALE = 1
8692
8793
display = adafruit_displayio_ssd1305.SSD1305(display_bus, width=WIDTH, height=HEIGHT)
8894
8995
# Make the display context
9096
splash = displayio.Group(max_size=10)
9197
display.show(splash)
9298
93-
color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1)
99+
color_bitmap = displayio.Bitmap(display.width, display.height, 1)
94100
color_palette = displayio.Palette(1)
95101
color_palette[0] = 0xFFFFFF # White
96102
@@ -100,7 +106,7 @@ Usage Example
100106
splash.append(bg_sprite)
101107
102108
# Draw a smaller inner rectangle
103-
inner_bitmap = displayio.Bitmap(WIDTH-BORDER*2, HEIGHT-BORDER*2, 1)
109+
inner_bitmap = displayio.Bitmap(display.width - BORDER * 2, display.height - BORDER * 2, 1)
104110
inner_palette = displayio.Palette(1)
105111
inner_palette[0] = 0x000000 # Black
106112
inner_sprite = displayio.TileGrid(inner_bitmap,
@@ -110,8 +116,12 @@ Usage Example
110116
111117
# Draw a label
112118
text = "Hello World!"
113-
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF, x=28, y=HEIGHT//2-1)
114-
splash.append(text_area)
119+
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF)
120+
text_width = text_area.bounding_box[2] * FONTSCALE
121+
text_group = displayio.Group(max_size=10, scale=FONTSCALE, x=display.width // 2 - text_width // 2,
122+
y=display.height // 2)
123+
text_group.append(text_area) # Subgroup for text scaling
124+
splash.append(text_group)
115125
116126
while True:
117127
pass

examples/displayio_ssd1305_simpletest.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,42 @@
44
"""
55

66
import board
7+
import digitalio
78
import displayio
89
import terminalio
910
from adafruit_display_text import label
1011
import adafruit_displayio_ssd1305
1112

1213
displayio.release_displays()
1314

14-
oled_reset = board.D9
15+
# Manually Toggle Reset
16+
reset = digitalio.DigitalInOut(board.D9)
17+
reset.switch_to_output(value=False)
18+
reset.value = True
1519

1620
# Use for SPI
1721
spi = board.SPI()
1822
oled_cs = board.D5
1923
oled_dc = board.D6
2024
display_bus = displayio.FourWire(spi, command=oled_dc, chip_select=oled_cs,
21-
reset=oled_reset, baudrate=1000000)
25+
baudrate=1000000)
2226

2327
# Use for I2C
24-
#i2c = board.I2C()
25-
#display_bus = displayio.I2CDisplay(i2c, device_address=0x3c, reset=oled_reset)
28+
# i2c = board.I2C()
29+
# display_bus = displayio.I2CDisplay(i2c, device_address=0x3c)
2630

2731
WIDTH = 128
28-
HEIGHT = 64 # Change to 64 if needed
29-
BORDER = 5
32+
HEIGHT = 64 # Change to 32 if needed
33+
BORDER = 8
34+
FONTSCALE = 1
3035

3136
display = adafruit_displayio_ssd1305.SSD1305(display_bus, width=WIDTH, height=HEIGHT)
3237

3338
# Make the display context
3439
splash = displayio.Group(max_size=10)
3540
display.show(splash)
3641

37-
color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1)
42+
color_bitmap = displayio.Bitmap(display.width, display.height, 1)
3843
color_palette = displayio.Palette(1)
3944
color_palette[0] = 0xFFFFFF # White
4045

@@ -44,7 +49,7 @@
4449
splash.append(bg_sprite)
4550

4651
# Draw a smaller inner rectangle
47-
inner_bitmap = displayio.Bitmap(WIDTH-BORDER*2, HEIGHT-BORDER*2, 1)
52+
inner_bitmap = displayio.Bitmap(display.width - BORDER * 2, display.height - BORDER * 2, 1)
4853
inner_palette = displayio.Palette(1)
4954
inner_palette[0] = 0x000000 # Black
5055
inner_sprite = displayio.TileGrid(inner_bitmap,
@@ -54,8 +59,12 @@
5459

5560
# Draw a label
5661
text = "Hello World!"
57-
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF, x=28, y=HEIGHT//2-1)
58-
splash.append(text_area)
62+
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF)
63+
text_width = text_area.bounding_box[2] * FONTSCALE
64+
text_group = displayio.Group(max_size=10, scale=FONTSCALE, x=display.width // 2 - text_width // 2,
65+
y=display.height // 2)
66+
text_group.append(text_area) # Subgroup for text scaling
67+
splash.append(text_group)
5968

6069
while True:
6170
pass

0 commit comments

Comments
 (0)