OLED 128x64 bonnet code issue (Python)

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Daniac
 
Posts: 16
Joined: Fri Feb 09, 2018 6:45 am

OLED 128x64 bonnet code issue (Python)

Post by Daniac »

I am building a menu system on the OLED and used the buttons demo as a starting point. The menu is working and I can select a menu function.

I am having trouble writing a single line to the OLED within a function, below is a simple function to reboot, the function itself works.

The screen clears, but the writing of "Shutting Down Now" does not arrive onto the screen until after the timer has executed, the function ends and the program writes the whole menu back to the screen (along with the "Shutting Down Now" over top or under it the menu) What am I doing wrong?

I assumed that:
disp.clear() clears the buffer
disp.text() will put location, text, font and color into the buffer
disp.display will show whats in the buffer on the screen



Code: Select all

# Initialize library.
disp.begin()

# Clear display.
disp.clear()
disp.display()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
x = 12

padding = -2
top = padding
bottom = height-padding
font = ImageFont.load_default()

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

#Draw menu items
draw.text((x, top),       "System Status",  font=font, fill=1)
draw.text((x, top+8),     "Beacon Settings", font=font, fill=1)
draw.text((x, top+16),    "Beacon Status",  font=font, fill=1)
draw.text((x, top+24),    "Hide Display",  font=font, fill=1) 
draw.text((x, top+32),    "Reboot",  font=font, fill=1) 
draw.text((x, top+40),    "Shutdown",  font=font, fill=1) 


def shutdown(): #this will not function unless you start the script as sudo  
       disp.clear()
       disp.display()
       draw.text((x, top+16),    "Shutting Down Now",  font=font, fill=1)
       disp.display()
       time.sleep(15)
       #os.system("shutdown now -h")

User avatar
adafruit_support_carter
 
Posts: 29465
Joined: Tue Nov 29, 2016 2:45 pm

Re: OLED 128x64 bonnet code issue (Python)

Post by adafruit_support_carter »

You need to call disp.image(image) before calling disp.display(). See here:
https://github.com/adafruit/Adafruit_Py ... ts.py#L128

The variable draw is a draw buffer for image and is not part of the actual display. So when you do something like draw.text(), nothing is actually being sent to the display. The idea is to do all the drawing into the draw buffer and then send it to the display so it is shown.

User avatar
Daniac
 
Posts: 16
Joined: Fri Feb 09, 2018 6:45 am

Re: OLED 128x64 bonnet code issue (Python)

Post by Daniac »

Perfect! thank you very much!

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”