@alphanumeric thank you for that tip!.
Here is my script:
#
# SPDX-License-Identifier: Unlicense
#
# 2021-12-19 22h00, by @Paulskpt
# Downloaded from:
# https://learn.adafruit.com/adafruit-monochrome-1-12-in-128x128-oled/circuitpython.
# After modifying pin numbers for the actual hardware, the OLED was displaying text.
# I only had to change the ROTATION TO 270 degrees to have the orientation in the right way.
# This is version 2.0
# In this version are implemented various measures to prevent exaust of memory.
#
"""
Based on example by Mark Roberts (mdroberts1243).
This example writes text to the display, and draws a series of squares and a rectangle.
"""
import sys
import time
import board
import busio
import displayio
import terminalio
import adafruit_tmp117
from adafruit_display_text import bitmap_label as label
from adafruit_displayio_sh1107 import SH1107, DISPLAY_OFFSET_ADAFRUIT_128x128_OLED_5297
displayio.release_displays()
# Global variable definitions
display = None
tmp117 = None
# Width, height and rotation for Monochrome 1.12" 128x128 OLED
WIDTH = 128
HEIGHT = 128
ROTATION = 270
# Border width
BORDER = 2
def setup():
global SH1107, display, WIDTH, HEIGHT, ROTATION, BORDER, tmp117
# For SPI:
dc = board.GP16
cs = board.GP17 # for BG SPI socket A (front). GP22 for socket B
sck = board.GP18
mosi = board.GP19
rst = board.GP20 # for BG SPI socket A (front). GP21 for socket B
spi_bus = busio.SPI(sck, mosi)
display_bus = displayio.FourWire(spi_bus, command=dc, chip_select=cs, reset=rst)
display = SH1107(
display_bus,
width=WIDTH,
height=HEIGHT,
display_offset=DISPLAY_OFFSET_ADAFRUIT_128x128_OLED_5297,
rotation=ROTATION,
)
# i2c for sensors like the TMP117 or the RTC RV3028
i2c = busio.I2C(board.GP5, board.GP4)
# Create tmp117 object
tmp117 = adafruit_tmp117.TMP117(i2c)
def drw():
global WIDTH, HEIGHT, ROTATION, BORDER, tmp117
white = 0xFFFFFF
black = 0x000000
# Make the display context
splash = displayio.Group()
color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1)
color_palette = displayio.Palette(1)
color_palette[0] = white
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)
# Draw a smaller inner rectangle in black
inner_bitmap = displayio.Bitmap(WIDTH - BORDER * 2, HEIGHT - BORDER * 2, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = black
inner_sprite = displayio.TileGrid(
inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER
)
splash.append(inner_sprite)
b_0 = [8, 16, 32, 110]
b_1 = [8, 16, 32, 50]
b_x = [58, 71, 91, 10]
b_y = [17, 15, 28, 69]
ble = len(b_0)
# Draw three white squares and a white rectangle
for _ in range(ble):
bm = displayio.Bitmap(b_0[_], b_1[_], 1)
sq = displayio.TileGrid(bm, pixel_shader=color_palette, x=b_x[_], y=b_y[_])
splash.append(sq)
fnt = terminalio.FONT
# Draw some label text
t_lst = ["Monochrome 1.12in", "128x128", "OLED", "TEMP:", " "]
s_lst = [1, 1, 2, 2, 2]
x_lst = [8, 8, 9, 15, 15]
y_lst = [8, 25, 44, 80, 104]
c_lst = [white, white, white, black, black]
le = len(x_lst)
for _ in range(le):
if _ < le-1:
t = t_lst[_]
else:
t = "{:0.2f} C".format(tmp117.temperature)
t_area = label.Label(fnt, text=t, scale=s_lst[_], color=c_lst[_], x=x_lst[_], y=y_lst[_])
splash.append(t_area)
display.show(splash)
del color_bitmap # to prevent memory exception error
del splash # idem
def main():
setup()
while True:
try:
drw()
time.sleep(5)
except KeyboardInterrupt:
break
sys.exit(0)
if __name__=="__main__":
main()