示例#1
0
def create_character(fontpath, size, c, bypp, crop, bpp):
    try:
        from PIL import Image
        from PIL import ImageDraw
        from PIL import ImageFont
        from PIL import ImageChops

    except Exception as e:
        # we will get respawn hopefully after python-PIL is loaded
        print('failed to load PIL to create fonts, aborting...', e)
        return False
        #import time
        #time.sleep(3) # wait 3 seconds to avoid respawning too quickly

        #return ugfx.surface(size, size, bypp, bytes([0]*(size*size*bypp)))
        #exit(1)

    ifont = ImageFont.truetype(fontpath, size)
    size = ifont.getsize(c)
    image = Image.new('RGBA', size)
    draw = ImageDraw.Draw(image)
    draw.text((0, 0), c, font=ifont)

    if crop:
        bg = Image.new(image.mode, image.size, image.getpixel((0, 0)))
        diff = ImageChops.difference(image, bg)
        bbox = diff.getbbox()
        if bbox:
            image = image.crop(bbox)

    if bpp:
        data = list(image.getdata())
        for i in range(len(data)):
            d = 255 / (1 << bpp)
            v = int(
                round(data[i][0] / (255 / (1 << bpp))) * (255 /
                                                          ((1 << bpp) - 1)))
            data[i] = (v, v, v, v)
        image.putdata(data)

    return ugfx.surface(image.size[0], image.size[1], bypp, image.tobytes())
示例#2
0
    def __init__(self, hat):
        self.hat = hat

        if hat:
            self.config = hat.config['lcd']
        elif micropython:
            from config_esp32 import read_config
            self.config = read_config()
        else:
            self.config = {}

        default = {
            'contrast': 60,
            'invert': False,
            'backlight': 20,
            'flip': False,
            'language': 'en',
            'bigstep': 10,
            'smallstep': 1
        }

        for name in default:
            if not name in self.config:
                self.config[name] = default[name]

        # set the driver to the one from hat eeprom
        driver = 'default'
        if self.hat and 'hat' in self.hat.config:
            driver = self.hat.config['hat']['lcd']['driver']
            self.host = self.hat.client.config['host']
        else:
            self.host = False

        for pdriver in [
                'nokia5110', 'jlx12864', 'glut', 'framebuffer', 'tft', 'none'
        ]:
            if pdriver in sys.argv:
                print('overriding driver', driver, ' to command line', pdriver)
                sys.argv.remove(pdriver)
                driver = pdriver
                break

        self.battery_voltage = 0
        use_tft = True if micropython else False

        if not use_tft:
            use_glut = 'DISPLAY' in os.environ
        self.use_glut = False
        self.surface = None

        if driver == 'none':
            page = None
        elif driver == 'tft' or (driver == 'default' and use_tft):
            screen = ugfx.surface(136, 240, 1)
            self.surface = screen
        elif driver == 'nokia5110' or (driver == 'default' and not use_glut):
            screen = ugfx.spiscreen(0)
        elif driver == 'jlx12864':
            screen = ugfx.spiscreen(1)
        elif driver == 'glut' or (driver == 'default' and use_glut):
            self.use_glut = True
            print('using glut')
            import glut
            # emulate which screen resolution?
            #screen = glut.screen((240, 320))
            screen = glut.screen((136, 240))
            #screen = glut.screen((48, 84))
            #screen = glut.screen((96, 168))

            from OpenGL.GLUT import glutKeyboardFunc, glutKeyboardUpFunc
            from OpenGL.GLUT import glutSpecialFunc, glutSpecialUpFunc

            glutKeyboardFunc(self.glutkeydown)
            glutKeyboardUpFunc(self.glutkeyup)
            glutSpecialFunc(self.glutspecialdown)
            glutSpecialUpFunc(self.glutspecialup)
            self.glutkeytime = False
            #        glutIgnoreKeyRepeat(True)
        elif driver == 'framebuffer':
            print('using framebuffer')
            screen = ugfx.screen("/dev/fb0")
            if screen.width > 480:
                print('warning huge width')
                #screen.width = 480
                #screen.height= min(screen.height, 640)

        if screen:
            self.bw = 1 if screen.width < 120 else False
            self.mag = 1

            if not self.surface:
                w, h = screen.width, screen.height
                self.surface = ugfx.surface(w, h, screen.bypp, None)

                # magnify to fill screen
                self.mag = min(screen.width / self.surface.width,
                               screen.height / self.surface.height)
                if self.mag != 1:
                    print('magnifying lcd surface to fit screen')
                    self.magsurface = ugfx.surface(screen)

                self.invsurface = ugfx.surface(self.surface)
        else:
            self.surface = None

        self.lastframetime = 0
        self.screen = screen

        set_language(self.config['language'])
        self.client = False
        self.connect()

        self.menu = False
        self.page = connecting(self)
        self.need_refresh = True

        self.keypad = []
        for i in range(NUM_KEYS):
            self.keypad.append(Key())

        self.blink = black, white  # two cursor states
        self.data_update = False
示例#3
0
def draw(surface, pos, text, size, bw, crop=False):
    if not size in fonts:
        fonts[size] = {}

    font = fonts[size]
    if pos:
        x, y = pos
    else:
        x, y = 0, 0

    origx = x
    width = 0
    lineheight = 0
    height = 0
    for c in text:
        if c == '\n':
            x = origx
            y += lineheight
            height += lineheight
            lineheight = 0
            continue

        if c in font:
            src = font[c]
        else:
            src = None
            while size > 1:
                filename = fontpath + '%03d%03d' % (size, ord(c))
                if bw:
                    filename += 'b'
                if crop:
                    filename += 'c'

                if micropython:
                    #print('try load', filename)
                    character.load(filename.encode('utf-8'), surface.bypp)
                    src = character
                else:
                    src = ugfx.surface(filename.encode('utf-8'), surface.bypp)

                if src.bypp == surface.bypp:
                    break  # loaded

                if not micropython:
                    try:
                        print('create font charater', c, size, src.bypp,
                              surface.bypp)
                    except:
                        print('create font charater', size, src.bypp,
                              surface.bypp)
                        print('unable to print unicode character to console')
                    src = create_character(
                        os.path.abspath(os.path.dirname(__file__)) +
                        "/font.ttf", size, c, surface.bypp, crop, bw)
                    if src:
                        print('store grey', filename)
                        src.store_grey(filename.encode('utf-8'))
                    break
                size -= 1  # try smaller size

        if not src or src.bypp != surface.bypp:
            print('font dont have character', ord(c), size)
            continue

        if pos:
            surface.blit(src, x, y)

        x += src.width
        width = max(width, x - origx)
        lineheight = max(lineheight, src.height)

        if not micropython:
            font[c] = src

    return width, height + lineheight
示例#4
0
#!/usr/bin/env python
#
#   Copyright (C) 2016 Sean D'Epagnier
#
# This Program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
import time
try:
    import micropython
    import ugfx
    #fontpath = '/_#!#_spiffs/ugfxfonts/'
    fontpath = ''
    character = ugfx.surface(64, 84, 1)

except:
    micropython = False
    import os
    from pypilot.hat.ugfx import ugfx
    fontpath = os.path.abspath(os.getenv('HOME') +
                               '/.pypilot/ugfxfonts/') + '/'

    if not os.path.exists(fontpath):
        os.makedirs(fontpath)
    if not os.path.isdir(fontpath):
        raise 'ugfxfonts should be a directory'

global fonts
fonts = {}
示例#5
0
文件: lcd.py 项目: stelian42/pypilot
    def __init__(self, config):
        if config:
            self.config = config['lcd']
        else:
            self.config = {}
        self.pipe = False
        self.poller = False
        self.voltage = False

        default = {
            'contrast': 60,
            'invert': False,
            'backlight': 20,
            'hue': 214,
            'flip': False,
            'language': 'en',
            'bigstep': 10,
            'smallstep': 1,
            'buzzer': 2
        }

        for name in default:
            if not name in self.config:
                self.config[name] = default[name]

        global driver
        # set the driver to the one read from hat eeprom, or specified in hat.conf
        if config and 'hat' in config:
            if driver == 'default':
                driver = config['hat']['lcd']['driver']
            self.host = config['host']
        else:
            self.host = False

        self.battery_voltage = 0
        use_tft = True if micropython else False
        self.keypress = False

        use_glut = not use_tft and 'DISPLAY' in os.environ
        self.surface = None

        self.use_glut = False
        print('lcd driver', driver, use_tft, use_glut)
        if driver == 'none':
            page = None
            screen = None
            self.bw = None
        elif driver == 'tft' or (driver == 'default' and use_tft):
            import gc
            if gc.mem_free() > 1e6:  # larger ttgo display
                screen = ugfx.surface(240, 320, 1)
            else:
                screen = ugfx.surface(136, 240, 1)
            self.surface = screen
        elif driver == 'nokia5110' or (driver == 'default' and not use_glut):
            screen = ugfx.spiscreen(0)
        elif driver == 'jlx12864':
            screen = ugfx.spiscreen(1)
        elif driver == 'glut' or (driver == 'default' and use_glut):
            self.use_glut = True
            print('using glut')
            import glut
            # emulate which screen resolution?
            #screen = glut.screen((240, 320))
            screen = glut.screen((136, 240))
            #screen = glut.screen((48, 84))
            #screen = glut.screen((96, 168))

            from OpenGL.GLUT import glutKeyboardFunc, glutKeyboardUpFunc
            from OpenGL.GLUT import glutSpecialFunc, glutSpecialUpFunc

            glutKeyboardFunc(self.glutkeydown)
            glutKeyboardUpFunc(self.glutkeyup)
            glutSpecialFunc(self.glutspecialdown)
            glutSpecialUpFunc(self.glutspecialup)
            self.glutkeytime = False
            #        glutIgnoreKeyRepeat(True)
        elif driver == 'framebuffer':
            print('using framebuffer')
            screen = ugfx.screen("/dev/fb0")
            if screen.width > 480:
                print('warning huge width')
                #screen.width = 480
                #screen.height= min(screen.height, 640)

        if screen:
            self.bw = 1 if screen.width < 120 else False
            self.mag = 1

            if not self.surface:
                w, h = screen.width, screen.height
                self.surface = ugfx.surface(w, h, screen.bypp, None)

                # magnify to fill screen
                self.mag = min(screen.width / self.surface.width,
                               screen.height / self.surface.height)
                if self.mag != 1:
                    print('magnifying lcd surface to fit screen')
                    self.magsurface = ugfx.surface(screen)

                self.invsurface = ugfx.surface(self.surface)
        else:
            self.surface = None

        self.screen = screen

        set_language(self.config['language'])
        self.client = False
        self.connect()

        self.menu = False
        self.page = connecting(self)
        self.need_refresh = True

        self.keypad = []
        for i in range(NUM_KEYS):
            self.keypad.append(Key())

        self.blink = black, white  # two cursor states
        self.data_update = False
示例#6
0
文件: font.py 项目: stelian42/pypilot
#!/usr/bin/env python
#
#   Copyright (C) 2016 Sean D'Epagnier
#
# This Program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
import time
try:
    import micropython
    import ugfx
    #fontpath = '/_#!#_spiffs/ugfxfonts/'
    fontpath = ''
    #character = ugfx.surface(64, 84, 1)
    character = ugfx.surface(76, 149, 1)

except:
    micropython = False
    import os
    from pypilot.hat.ugfx import ugfx
    fontpath = os.path.abspath(os.getenv('HOME') +
                               '/.pypilot/ugfxfonts/') + '/'

    if not os.path.exists(fontpath):
        os.makedirs(fontpath)
    if not os.path.isdir(fontpath):
        raise 'ugfxfonts should be a directory'

global fonts
fonts = {}