domoticity
Posts: 11
Joined: Fri Sep 01, 2017 8:47 pm

need help for script python

Sun Jun 30, 2019 7:37 pm

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 :

Code: Select all

#!/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()

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: need help for script python

Mon Jul 01, 2019 12:37 am

Does this help?

Code: Select all

#!/usr/bin/env python

#The standard Python libraries "time" and "signal" are required for this program
import time
import signal

#From the libraries supplied with the GFX HAT, we are going to use the touch, lcd, backlight and fonts libraries
from gfxhat import touch, lcd, backlight, fonts

# PIL is the Python Image Library. We will use this to load an image that we will then display on the lcd
from PIL import Image, ImageFont, ImageDraw


################################################################################
#
#  We start off just displaying a useful bit of text on the python terminal. This doesn't go to the LCD.
#
################################################################################

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.

""")


################################################################################
#
# Now we set up various variables to help use use the LCD
#
################################################################################

# led_states[n] is the state of LED n: True=On, False = Off
# This line creates a list with six "False" values
led_states = [False for _ in range(6)]

# We call the function dimensions() in the lcd library to find out the size of the display
width, height = lcd.dimensions()

# We create a PIL image that is the size of the LCD screen
image = Image.new('P', (width, height))

# Create a draw object associated with the image object.
# This is still purely PIL. The image and the draw object are just memory buffers,
# nothing to do with the LCD screen.
draw = ImageDraw.Draw(image)

# This loads the named font. You'll need to check the GFX documentation to find out what the names mean and what values you can use. 
font = ImageFont.truetype(fonts.AmaticSCBold, 38)

# Just some text to put on the LCD screen
text = "Hola gringo"

# Find the size of the rectangle taken up by writing the text using the font.
w, h = font.getsize(text)


# We know the size of the screen and the size of the text. This bit of maths works out
# where on the screen we have to display the text so that it is centred.
#
#   <---------------------width--------------------->
#  +-------------------------------------------------+
#  |                (height - h) / 2                 | ^
#  | (width-w)/2)  +-----------------+ (width-w)/2)  | |
#  |               |<--------w------>|  ^            | |
#  |               |   Hola gringo   |  h            | height
#  |               |                 |  v            | |
#  |               +-----------------+               | |
#  |                (height - h) / 2                 | v
#  +-------------------------------------------------+
#                                    
x = (width - w) // 2
y = (height - h) // 2

# Now we know where to draw it, we draw it there.
draw.text((x, y), text, 1, font)


################################################################################
#
# This function is called whenever someone touches the touch-screen.
# We configure that below, so you haven't seen it yet.
#
################################################################################

def handler(ch, event):
    if event == 'press':
    	# This is a "press" event. Check the GFX documentation for other values you could use.
    	# There appear to be six touch regions called 0-5
    	# The parameter "ch" is the region number. Hmmm. Maybe the GFX docs call it "channel"?
    	
    	# Invert the LED associated with this region
        led_states[ch] = not led_states[ch]
        
        # Set the LED for this region according to what our variable says it should be.
        touch.set_led(ch, led_states[ch])
        
        # If we just set the LED on then we switch the backlight for this region on.
        # (0,255,255) suggests this is an RGB colour, and if it is,
        # and it is in that order, this would be bright cyan.
        # otherwise, if the LED is now off, then switch the backlight off
        if led_states[ch]:
            backlight.set_pixel(ch, 0, 255, 255)
        else:
            backlight.set_pixel(ch, 0, 255, 0)
            
        # set the backlight according to how we have just said.
        # doing it in two steps like this is useful if you want to make a lots of
        # changes and then have them happen at the same time.
        backlight.show()

################################################################################
#
# That's the end of the handler function. Now we are back to setting stuff up.
# Now we are doing stuff with the GFX libraries.
#
################################################################################

        
# One, by one, set each LED on for a tenth of a second and then switch them off again.
# This will sweep a light across the LEDs, over a period of half a second.
for x in range(6):
    touch.set_led(x, 1)
    time.sleep(0.1)
    touch.set_led(x, 0)

# For each region, set the backlight to bright green and configure the touchscreen to
# call the function "handler" when the region is touched. (I said we would do that)
for x in range(6):
    backlight.set_pixel(x, 0, 255, 0)
    touch.on(x, handler)

# We have set the backlight colour for each region, now action those instructions.
backlight.show()

################################################################################
#
# Now we display the image (the one with the text drawn in it) on the screen.
# We just go through all the X and Y coordinates, reading the value of each pixel from the draw object
# And setting that pixel on the LCD display the same.
# Hmmm. It seems to me that we could use "width" and "height" here instead of using 128 and 64.
#
################################################################################

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

# Like the backlight, when we have made changes to the LCD display pixels, we
# have to call a function to make those changes happen all at the same time.
lcd.show()

################################################################################
#
# We have set up the display with the text on it, and we have arranged a handler
# function that will change stuff when we touch the screen. We need to keep running the
# program because otherwise it will stop and the handler function won't get
# called. Buit there is nothing else we need to do.
# So we use a Python library function that simply waits until we get a
# signal -- in this case a keyboard interrupt signal (ctrl-C)
# When  we get that, we switch off everything on the LCD display and exit.
#
################################################################################

try:
    # wait until the user presses Ctrl-C
    signal.pause()
except KeyboardInterrupt:
	# for every region of the LCD display...
    for x in range(6):
    	# switch off the backlight
        backlight.set_pixel(x, 0, 0, 0)
        # switch off the LED
        touch.set_led(x, 0)
    # action the changes to the backlights
    backlight.show()
    # clear the LCD display
    lcd.clear()
    # action the change to the LCD display
    lcd.show()

Return to “Python”