Python Forum
python script for gfx hat (LCD for raspberry)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python script for gfx hat (LCD for raspberry)
#1
Hy,
i have bought a LCD for my beautifull Pi :)

My LCD is the GFX hat : https://shop.pimoroni.com/products/gfx-hat

There is an Example for script python but i don't understand, i am a very very very rookie in Python Scripts.

what i am looking for with my lcd :

Below the screen : display text like "Hello", "name", etc...

Top the screen : display Pictogram wich are in this folder "/diagbox/pictures"

for example :
When the Pi starts, switch on the LCD and display "Hello, boot in progress"
After, when the wlan0 and eth0 are up, display a pictogramm for the eth0 and for the wlan0

And leave the screen on, as long as the pi is on.


Thank by Advance

Thereafter the script give with the Equipment :

#!/usr/bin/env python

import time
import signal

from gfxhat import touch, lcd, backlight, fonts
from PIL import Image, ImageFont, ImageDraw

print("""hello-world.py

This basic example prints the text "Hello World" in the middle of the LCD

Press any button to see its corresponding LED toggle on/off.

Press Ctrl+C to exit.

""")

led_states = [False for _ in range(6)]

width, height = lcd.dimensions()

image = Image.new('P', (width, height))

draw = ImageDraw.Draw(image)

font = ImageFont.truetype(fonts.AmaticSCBold, 38)

text = "Hola gringo"

w, h = font.getsize(text)

x = (width - w) // 2
y = (height - h) // 2

draw.text((x, y), text, 1, font)

def handler(ch, event):
    if event == 'press':
        led_states[ch] = not led_states[ch]
        touch.set_led(ch, led_states[ch])
        if led_states[ch]:
            backlight.set_pixel(ch, 0, 255, 255)
        else:
            backlight.set_pixel(ch, 0, 255, 0)
        backlight.show()

for x in range(6):
    touch.set_led(x, 1)
    time.sleep(0.1)
    touch.set_led(x, 0)

for x in range(6):
    backlight.set_pixel(x, 0, 255, 0)
    touch.on(x, handler)

backlight.show()

for x in range(128):
    for y in range(64):
        pixel = image.getpixel((x, y))
        lcd.set_pixel(x, y, pixel)


lcd.show()

try:
    signal.pause()
except KeyboardInterrupt:
    for x in range(6):
        backlight.set_pixel(x, 0, 0, 0)
        touch.set_led(x, 0)
    backlight.show()
    lcd.clear()
    lcd.show()
Reply
#2
thats a very specific module. I would contact the authors of that module
https://github.com/pimoroni/gfx-hat/issues
Recommended Tutorials:
Reply
#3
Hi,

@domoticity : and the question is...? You describe what you intend to do - and that's it. I mean it's fine, but if you expect an answer, you may want to raise a question to answer.

General comments:
In case you do not understand the code -> learn Python and read the docs for the Python module driving the display. It won't help you at all if I would try to explain the code to you if you have no basic understanding of Python, its object orientation, classes and methods.

Displaying stuff is independent of that. To run scripts at boot time, you need to write systemd Service Units to start you scripts. Do you ever have heard of that? Otherwise, you need to dive into this topic as well.

Depending on when the hardware drive is loaded by the Linux kernel, your plan may work or not, as it may be that the network is up and running before the driver for the display is even loaded. Or the driver is loaded first but the network is up 0.5 seconds or so later, so you hardly will see the text displayed before.
Anyway, systemd has tools to analyze what is run at which point in the booting process.

Regards, noisefloor
Reply
#4
(Jun-30-2019, 08:53 PM)metulburr Wrote: thats a very specific module. I would contact the authors of that module https://github.com/pimoroni/gfx-hat/issues
Hy
Thank you to answer me .
I have sent email.i'm waiting an answer from Author.

(Jun-30-2019, 10:36 PM)noisefloor Wrote: Hi, @domoticity : and the question is...? You describe what you intend to do - and that's it. I mean it's fine, but if you expect an answer, you may want to raise a question to answer. General comments: In case you do not understand the code -> learn Python and read the docs for the Python module driving the display. It won't help you at all if I would try to explain the code to you if you have no basic understanding of Python, its object orientation, classes and methods. Displaying stuff is independent of that. To run scripts at boot time, you need to write systemd Service Units to start you scripts. Do you ever have heard of that? Otherwise, you need to dive into this topic as well. Depending on when the hardware drive is loaded by the Linux kernel, your plan may work or not, as it may be that the network is up and running before the driver for the display is even loaded. Or the driver is loaded first but the network is up 0.5 seconds or so later, so you hardly will see the text displayed before. Anyway, systemd has tools to analyze what is run at which point in the booting process. Regards, noisefloor


Hy,
Thank you to answer me :)

I am learning, trying, reading a lot about Python,
and i have succeeded to display text on my LCD.

And sorry, i have forgotten to post my questions :)

But now, i try to display little picture.

I documented myself here :

http://docs.pimoroni.com/gfxhat/
pil

And here my python script :

#!/usr/bin/env python
import time
from gfxhat import lcd, backlight, fonts
from PIL import Image, ImageFont, ImageDraw

print("""Image boot""")

backlight.set_all(240,240,240)
backlight.show()

lcd.clear()

image = Image.open("/home/test.jpg")
Image.show()

x = 00 
y = 00

lcd.set_pixel(x, y, 1)

lcd.show()
My LCD lights but no picture.

My OS is Debian with no desktop, more precise Raspbian lite.

My question are :
-Can we display pictures in a LCD on gpio without graphical environment ?
I probably did a big mistake in my script bu don't see where :)
Reply
#5
Hi,

Quote: -Can we display pictures in a LCD on gpio without graphical environment ?
Yes, you can. Basically what the code of your 1st post is doing is to iterate over an object of an PIL image and set the pixels of the display accordingly. So everything which can be read into a PIL image should be possible to display on your LCD.

And this will work on systems with no graphics environment running as well, as long as you try not to display something on the system itself.

Regards, noisefloor
Reply
#6
Hy.
Si that's why my script doesn't work,
image.show() tries to open thé picture in thé system itself,isn't it?
Thank you for your answer
Reply
#7
Hi,

that's correct. You don't need to call image.show(), as there is no need to display on the graphical output of your system. To display the graphics on your LCD, you need to call the methods to do that. That's completely independent of the graphical output of your Raspi.

Regards, noisefloor
Reply
#8
(Jul-03-2019, 12:48 PM)noisefloor Wrote: Hi, that's correct. You don't need to call image.show(), as there is no need to display on the graphical output of your system. To display the graphics on your LCD, you need to call the methods to do that. That's completely independent of the graphical output of your Raspi. Regards, noisefloor


So now i try it :

#!/usr/bin/env python
import time
from gfxhat import lcd, backlight, fonts
from PIL import Image, ImageFont, ImageDraw

print("""Afficher une image""")

backlight.set_all(240,240,240)
backlight.show()

lcd.clear()

image	=Image.open("/tmp/aa.jpg")

x = 00 
y = 40

pixel = image.getpixel((x, y))
lcd.set_pixel(120, 30, image)

lcd.show()
But it doesn't work .

:(

I turn around like Bonnie tyler's song :)
Reply
#9
Hi,

it doesn't work because you ignored what I wrote about and / or don't understand the code you posted in the initial post.

Again: you have to iterate over the image and need to set _each_ pixel of your LCD, one-by-one. That's what the lines 59-62 do in the code of your 1st post.
The Python module for display seems to have no high-level interface, you have to deal low level which each pixel.

Regards, noisefloor
Reply
#10
(Jul-04-2019, 02:09 AM)noisefloor Wrote: Hi, it doesn't work because you ignored what I wrote about and / or don't understand the code you posted in the initial post. Again: you have to iterate over the image and need to set _each_ pixel of your LCD, one-by-one. That's what the lines 59-62 do in the code of your 1st post. The Python module for display seems to have no high-level interface, you have to deal low level which each pixel. Regards, noisefloor

Hy
thank you to answer.

I don't ignore what you wrote.
Ii don't understand the whole code in the initial post.

I try to find informations about to better understand.

I try to modify considering what you have wrote

#!/usr/bin/env python
import time
from gfxhat import lcd, backlight, fonts
from PIL import Image, ImageFont, ImageDraw

print("""Message de demarrage""")

backlight.set_all(240,240,240)
backlight.show()

lcd.clear()

width, height = lcd.dimensions()

image = Image.open("/tmp/aa.jpg")
draw = ImageDraw.Draw(image)

for x in range(50):
    for y in range(50):
        pixel = image.getpixel((x, y))
        lcd.set_pixel(x, y, pixel)

lcd.show()
The error whicvh i have when i execut the scripts :


root@diagbox:/tmp# python test.py
Message de demarrage
Traceback (most recent call last):
File "test.py", line 21, in <module>
lcd.set_pixel(x, y, pixel)
File "/usr/local/lib/python2.7/dist-packages/gfxhat/st7567.py", line 137, in set_pixel
self.buf[offset] |= (value & 1) << bit
TypeError: unsupported operand type(s) for &: 'tuple' and 'int'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Re-write BASH script to Python script popi75 5 2,441 Apr-30-2021, 03:52 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020