abusaeedmirza
Posts: 66
Joined: Tue Mar 06, 2018 3:02 pm

How to highlight text on OLED 2.42 SSD1306

Wed May 23, 2018 1:58 pm

hello!
i am working on OLED 2.42" SSD1306 (Adafruit) display. i have successfully made connection according to this link https://satoshinm.github.io/blog/171110 ... _zero.html
i have also been able to write text on OLED successfully with this code

Code: Select all

import time
import os.path
from demo_opts import get_device
from luma.core.virtual import viewport
from luma.core.render import canvas
from PIL import Image, ImageDraw, ImageFont
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
button=17
GPIO.setup(button,GPIO.IN,pull_up_down=GPIO.PUD_UP)

def main():

    virtual = viewport(device, width=device.width, height=768)
    background = Image.new("RGB", device.size, "white")
    draw = ImageDraw.Draw(background)

    for _ in range(2):
        with canvas(virtual) as draw:

            font=ImageFont.truetype('/home/pi/luma.examples/examples/fonts/ApplyBeefShadow.ttf', 10)
            draw.text((40, 0), "WELCOME", font=font, fill="white")
            draw.text((20, 12), 'Its Wine Mill', fill="white")
            draw.text((20, 24), "have a nice wine", fill="white")

    time.sleep(60)


if __name__ == "__main__":
    try:
        device = get_device()
        main()
    except KeyboardInterrupt:
        pass

now i want to highlight this text i mean background should be white and text should be black.
looking for answers :)

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: How to highlight text on OLED 2.42 SSD1306

Wed May 23, 2018 2:36 pm

if you want to turn the whole display to negative, the SSD1306 chip has a specific command for that (google it)
If you want to "highlight" only a portion, well as you can see from the code what is displayed is an image. so i think you have to create an image :) for example, a rectangle "white" and a text "black"

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: How to highlight text on OLED 2.42 SSD1306

Wed May 23, 2018 9:24 pm

Ok I did have a very quick look at the Luma module and it seems like it is an extension to the Pil module. I didn't find a way of setting the background color of the text and I think the Pil text method always draw text with a transparent background. So what you might be able to do is draw a white rectangle where you want the text then write the text onto the white rectangle in a black colour. :)

abusaeedmirza
Posts: 66
Joined: Tue Mar 06, 2018 3:02 pm

Re: How to highlight text on OLED 2.42 SSD1306

Thu May 24, 2018 11:15 am

OutoftheBOTS wrote:
Wed May 23, 2018 9:24 pm
Ok I did have a very quick look at the Luma module and it seems like it is an extension to the Pil module. I didn't find a way of setting the background color of the text and I think the Pil text method always draw text with a transparent background. So what you might be able to do is draw a white rectangle where you want the text then write the text onto the white rectangle in a black colour. :)
thank you so much. it worked. now i want to shift the position of rectangle with rotary encoder. i used this tutorial https://guy.carpenter.id.au/gaugette/20 ... pberry-pi/ it worked and i have edited the code to make shift for rectangle but it shifts only one step down. i want to shift the rectangle down when rotary rotates.

Code: Select all

import time
import os.path
from demo_opts import get_device
from luma.core.virtual import viewport
from luma.core.render import canvas
from PIL import Image, ImageDraw, ImageFont
import RPi.GPIO as GPIO
import gaugette.rotary_encoder
import gaugette.switch
import gaugette.gpio

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
button=17
GPIO.setup(button,GPIO.IN,pull_up_down=GPIO.PUD_UP)
A_PIN  = 7
B_PIN  = 8
SW_PIN = 9
gpio = gaugette.gpio.GPIO()
encoder = gaugette.rotary_encoder.RotaryEncoder.Worker(gpio, A_PIN, B_PIN)
encoder.start()
switch = gaugette.switch.Switch(gpio, SW_PIN)
last_state = None
x1=0
y1=0
x2=128
y2=12
def main():

    while True:
        delta = encoder.get_steps()
        with canvas(device) as draw:

            draw.rectangle((x1,y1,x2,y2), outline="white", fill="white")
            font=ImageFont.truetype('/home/pi/luma.examples/examples/fonts/ApplyBeefShadow.ttf', 10)
            draw.text((40, 0), "WELCOME", font=font, fill="black")
            draw.text((20, 12), 'Its Wine Mill', fill="white")
            draw.text((20, 24), "have a nice wine", fill="white")
         
        
            if delta!=0:
                draw.rectangle((x1,y1+10,x2,y2+10), outline="white", fill="white")
                time.sleep(1)          
if __name__ == "__main__":
    try:
        device = get_device()
        main()
    except KeyboardInterrupt:
        pass
any suggestion to move the rectangle?

Return to “Automation, sensing and robotics”