|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from luma.core import cmdline, error |
| 5 | +from PIL import Image |
| 6 | +import sys |
| 7 | +import logging |
| 8 | +from fpms.modules.constants import PLATFORM, DISPLAY_TYPE |
| 9 | +from fpms.modules.display import * |
| 10 | +from fpms.modules.platform import * |
| 11 | +from fpms.modules.screen.screen import AbstractScreen |
| 12 | + |
| 13 | +# set possible vars to None |
| 14 | +I2C_PORT = None |
| 15 | +SPI_BUS_SPEED = None |
| 16 | +I2C_ADDRESS = None |
| 17 | +INTERFACE_TYPE = None |
| 18 | +WIDTH = None |
| 19 | +HEIGHT = None |
| 20 | +COLOR_ORDER_BGR = True |
| 21 | +GPIO_DATA_COMMAND = None |
| 22 | +GPIO_RESET = None |
| 23 | +GPIO_BACKLIGHT = None |
| 24 | +GPIO_CS = None |
| 25 | +BACKLIGHT_ACTIVE = None |
| 26 | +H_OFFSET = None |
| 27 | +V_OFFSET = None |
| 28 | + |
| 29 | +if DISPLAY_TYPE == DISPLAY_TYPE_SSD1351: |
| 30 | + # ssd1351 128 x 128 |
| 31 | + INTERFACE_TYPE = "spi" |
| 32 | + WIDTH = "128" |
| 33 | + HEIGHT = "128" |
| 34 | +elif DISPLAY_TYPE == DISPLAY_TYPE_ST7735: |
| 35 | + # 128x128 1.44 in LCD Display HAT |
| 36 | + INTERFACE_TYPE = "gpio_cs_spi" |
| 37 | + SPI_BUS_SPEED = "2000000" |
| 38 | + WIDTH = "128" |
| 39 | + HEIGHT = "128" |
| 40 | + GPIO_DATA_COMMAND = "25" |
| 41 | + GPIO_RESET = "27" |
| 42 | + GPIO_BACKLIGHT = "24" |
| 43 | + GPIO_CS = "8" |
| 44 | + BACKLIGHT_ACTIVE = "high" |
| 45 | + H_OFFSET = "1" |
| 46 | + V_OFFSET = "2" |
| 47 | +elif DISPLAY_TYPE == DISPLAY_TYPE_ST7789: |
| 48 | + # 240x240 1.3 in LCD Display HAT |
| 49 | + INTERFACE_TYPE = "gpio_cs_spi" |
| 50 | + SPI_BUS_SPEED = "52000000" |
| 51 | + WIDTH = "240" |
| 52 | + HEIGHT = "240" |
| 53 | + GPIO_DATA_COMMAND = "25" |
| 54 | + GPIO_RESET = "27" |
| 55 | + GPIO_BACKLIGHT = "24" |
| 56 | + GPIO_CS = "8" |
| 57 | + BACKLIGHT_ACTIVE = "high" |
| 58 | + |
| 59 | +''' |
| 60 | +### This code is borrowed from https://github.com/rm-hull/luma.examples/blob/master/examples/demo_opts.py |
| 61 | +(MIT License) |
| 62 | +''' |
| 63 | +# logging |
| 64 | +logging.basicConfig( |
| 65 | + level=logging.DEBUG, |
| 66 | + format='%(asctime)-15s - %(message)s' |
| 67 | +) |
| 68 | +# ignore PIL debug messages |
| 69 | +logging.getLogger('PIL').setLevel(logging.ERROR) |
| 70 | + |
| 71 | +DISPLAY_WIDTH = int(WIDTH) |
| 72 | +DISPLAY_HEIGHT = int(HEIGHT) |
| 73 | + |
| 74 | +def display_settings(device, args): |
| 75 | + """ |
| 76 | + Display a short summary of the settings. |
| 77 | +
|
| 78 | + :rtype: str |
| 79 | + """ |
| 80 | + iface = '' |
| 81 | + display_types = cmdline.get_display_types() |
| 82 | + if args.display not in display_types['emulator']: |
| 83 | + iface = 'Interface: {}\n'.format(args.interface) |
| 84 | + |
| 85 | + lib_name = cmdline.get_library_for_display_type(args.display) |
| 86 | + if lib_name is not None: |
| 87 | + lib_version = cmdline.get_library_version(lib_name) |
| 88 | + else: |
| 89 | + lib_name = lib_version = 'unknown' |
| 90 | + |
| 91 | + import luma.core |
| 92 | + version = 'luma.{} {} (luma.core {})'.format( |
| 93 | + lib_name, lib_version, luma.core.__version__) |
| 94 | + |
| 95 | + return '{0}\nVersion: {1}\nDisplay: {2}\n{3}Dimensions: {4} x {5}\nMode: {6}\n{7}'.format( |
| 96 | + '-' * 50, version, args.display, iface, device.width, device.height, device.mode, '-' * 50) |
| 97 | + |
| 98 | + |
| 99 | +def get_device(actual_args=None): |
| 100 | + """ |
| 101 | + Create device from command-line arguments and return it. |
| 102 | + """ |
| 103 | + if actual_args is None: |
| 104 | + actual_args = sys.argv[1:] |
| 105 | + parser = cmdline.create_parser(description='luma.examples arguments') |
| 106 | + args = parser.parse_args(actual_args) |
| 107 | + |
| 108 | + if args.config: |
| 109 | + # load config from file |
| 110 | + config = cmdline.load_config(args.config) |
| 111 | + args = parser.parse_args(config + actual_args) |
| 112 | + |
| 113 | + # create device |
| 114 | + try: |
| 115 | + device = cmdline.create_device(args) |
| 116 | + print(display_settings(device, args)) |
| 117 | + return device |
| 118 | + |
| 119 | + except error.Error as e: |
| 120 | + parser.error(e) |
| 121 | + return None |
| 122 | + |
| 123 | +''' |
| 124 | +### End of borrowed code |
| 125 | +''' |
| 126 | + |
| 127 | +actual_args = [] |
| 128 | + |
| 129 | +if DISPLAY_TYPE: |
| 130 | + actual_args.append("-d") |
| 131 | + actual_args.append(DISPLAY_TYPE) |
| 132 | + |
| 133 | +if INTERFACE_TYPE: |
| 134 | + actual_args.append("--interface") |
| 135 | + actual_args.append(INTERFACE_TYPE) |
| 136 | + |
| 137 | +if WIDTH: |
| 138 | + actual_args.append("--width") |
| 139 | + actual_args.append(WIDTH) |
| 140 | + |
| 141 | +if HEIGHT: |
| 142 | + actual_args.append("--height") |
| 143 | + actual_args.append(HEIGHT) |
| 144 | + |
| 145 | +if I2C_PORT: |
| 146 | + actual_args.append("--i2c-port") |
| 147 | + actual_args.append(I2C_PORT) |
| 148 | + |
| 149 | +if SPI_BUS_SPEED: |
| 150 | + actual_args.append("--spi-bus-speed") |
| 151 | + actual_args.append(SPI_BUS_SPEED) |
| 152 | + |
| 153 | +if COLOR_ORDER_BGR: |
| 154 | + actual_args.append("--bgr") |
| 155 | + |
| 156 | +if GPIO_DATA_COMMAND: |
| 157 | + actual_args.append("--gpio-data-command") |
| 158 | + actual_args.append(GPIO_DATA_COMMAND) |
| 159 | + |
| 160 | +if GPIO_RESET: |
| 161 | + actual_args.append("--gpio-reset") |
| 162 | + actual_args.append(GPIO_RESET) |
| 163 | + |
| 164 | +if GPIO_BACKLIGHT: |
| 165 | + actual_args.append("--gpio-backlight") |
| 166 | + actual_args.append(GPIO_BACKLIGHT) |
| 167 | + |
| 168 | +if GPIO_CS: |
| 169 | + actual_args.append("--gpio-chip-select") |
| 170 | + actual_args.append(GPIO_CS) |
| 171 | + |
| 172 | +if BACKLIGHT_ACTIVE: |
| 173 | + actual_args.append("--backlight-active") |
| 174 | + actual_args.append(BACKLIGHT_ACTIVE) |
| 175 | + |
| 176 | +if H_OFFSET: |
| 177 | + actual_args.append("--h-offset") |
| 178 | + actual_args.append(H_OFFSET) |
| 179 | + |
| 180 | +if V_OFFSET: |
| 181 | + actual_args.append("--v-offset") |
| 182 | + actual_args.append(V_OFFSET) |
| 183 | + |
| 184 | +class Luma(AbstractScreen): |
| 185 | + |
| 186 | + def init(self): |
| 187 | + self.device = get_device(actual_args=actual_args) |
| 188 | + if PLATFORM == PLATFORM_PRO: |
| 189 | + # Reduce the contrast to also help reduce the noise |
| 190 | + # that's being produced by the display for some reason |
| 191 | + device.contrast(128) |
| 192 | + return True |
| 193 | + |
| 194 | + def drawImage(self, image): |
| 195 | + img = image.convert(self.device.mode) |
| 196 | + width, height = img.size |
| 197 | + if DISPLAY_WIDTH != width or DISPLAY_HEIGHT != height: |
| 198 | + img = img.resize((DISPLAY_WIDTH, DISPLAY_HEIGHT), Image.LANCZOS) |
| 199 | + |
| 200 | + self.device.display(img) |
| 201 | + |
| 202 | + def clear(self): |
| 203 | + self.device.clear() |
| 204 | + |
| 205 | + def sleep(self): |
| 206 | + self.device.clear() |
| 207 | + if PLATFORM != PLATFORM_PRO: |
| 208 | + self.device.backlight(False) |
| 209 | + |
| 210 | + def wakeup(self): |
| 211 | + if PLATFORM != PLATFORM_PRO: |
| 212 | + self.device.backlight(True) |
0 commit comments