rdagger / Pi-ST7565

ST7565 Graphics LCD Display Python Library for Raspberry Pi
MIT License
17 stars 13 forks source link

Centre text in the middle of the display #7

Closed OliverMardle closed 7 years ago

OliverMardle commented 7 years ago

Hi, I would like to centre a variable in the middle of the LCD. the issue is that the variable can be anything between 5 and 30 so I cant just set a position for it and leave it. Is there a way of getting the length of the piece of text to be displayed?

Thanks.

rdagger commented 7 years ago

The XglcdFont class has a measure_text method that will return the text length in pixels.

OliverMardle commented 7 years ago

Aha, Perfect. Thank you for your help. I didn't see that.

OliverMardle commented 7 years ago

Sorry. I understand that I need to enter the text to measure and the spacing but how does it know what font I'm using? are you able to show me how to use the function.

Attached is my code. I'm having issues with the command stating that self is missing

import st7565
import xglcd_font as font
from time import sleep
from time import gmtime, strftime
import RPi.GPIO as GPIO
import threading

GPIO.setmode(GPIO.BCM)

tmpDn = 26
tmpUp = 19
enter = 13
menUp = 6
menDn = 5

GPIO.setup(tmpDn, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(tmpUp, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(enter, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(menUp, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(menDn, GPIO.IN, pull_up_down = GPIO.PUD_UP)

glcd = st7565.Glcd(rgb = [21, 20, 16])
glcd.init()
glcd.set_backlight_color(100, 100, 100)

Icon0 = glcd.load_bitmap('/home/pi/Pi-ST7565/Deg4x4.raw', width = 4, height = 4)
Icon1 = glcd.load_bitmap('/home/pi/Pi-ST7565/Rad12x12.raw', width = 12, height = 12)
Icon2 = glcd.load_bitmap('/home/pi/Pi-ST7565/Tap12x12.raw', width = 12, height = 12)
MSSansSerifSmall = font.XglcdFont('/home/pi/Pi-ST7565/fonts/MSSansSerif11x12.c', 11, 12)
MSSansSerifLarge = font.XglcdFont('/home/pi/Pi-ST7565/fonts/MSSansSerif15x17.c', 15, 17)
MSSansSerif = font.XglcdFont('/home/pi/Pi-ST7565/fonts/MSSansSerif23x24.c', 23, 24)

#glcd.fill_rectangle(0, 0, 128, 12)
#glcd.draw_string('Fri 11 Aug 17', MSSansSerifSmall, 0, 0, spacing = 0, invert = True)
#glcd.draw_string(strftime("%H:%M"), MSSansSerifSmall, 97, 0, spacing = 0, invert = True)

#glcd.draw_string('12', MSSansSerif, 40, 20, spacing = 0)

#glcd.draw_string('Auto', MSSansSerifSmall, 15, 51, spacing = 0)
#glcd.draw_string('All Day', MSSansSerifSmall, 78, 51, spacing = 0)

#glcd.draw_bitmap(Icon0, 66, 21)
#glcd.draw_string('C', MSSansSerif, 70, 20, spacing = 0)
#glcd.draw_bitmap(Icon1, 0, 51)
#glcd.draw_bitmap(Icon2, 63, 51)

#glcd.draw_line(0, 23, 38, 23)
#glcd.draw_line(89, 23, 127, 23)

#glcd.flip()

temp = 9
modes = ['On','Off','All Day','Auto']
Heating = 0
HotWater = 0

while True:
    if GPIO.input(tmpUp) == 0:
        temp += 1
        print('Temp +')
    if GPIO.input(tmpDn) == 0:
        temp -= 1
        print('Temp -')
    if GPIO.input(enter) == 0:
        print('Enter')
    if GPIO.input(menUp) == 0:
        print('Menu Up')
        Heating += 1
        if Heating > 3:
            Heating = 0
    if GPIO.input(menDn) == 0:
        print('Menu Down')
        HotWater += 1
        if HotWater > 3:
            HotWater = 0
    if temp > 30:
        temp = 5
    if temp < 5:
        temp = 30

    length = font.XglcdFont.measure_text(text = str(temp),  spacing = 1)
    print(length)
    glcd.fill_rectangle(0, 0, 128, 12)
    glcd.fill_rectangle(40, 21, 26, 17, color = 0)
    glcd.draw_bitmap(Icon0, 66, 21)
    glcd.draw_string('C', MSSansSerif, 70, 20, spacing = 0)
    glcd.draw_string(str(temp), MSSansSerif, 40, 20, spacing = 0)
    glcd.draw_string(strftime("%a %-d %b %y"), MSSansSerifSmall, 0, 0, spacing = 0, invert = True)
    glcd.draw_string(strftime("%H:%M"), MSSansSerifSmall, 97, 0, spacing = 0, invert = True)
    glcd.fill_rectangle(0, 51, 128, 12, color = 0)
    glcd.draw_string(modes[Heating], MSSansSerifSmall, 15, 51, spacing = 0)
    glcd.draw_string(modes[HotWater], MSSansSerifSmall, 78, 51, spacing = 0)
    glcd.draw_bitmap(Icon1, 0, 51)
    glcd.draw_bitmap(Icon2, 63, 51)
    glcd.flip()
    sleep(0.05)
OliverMardle commented 7 years ago

Ok, I worked it out. Thank You.