|
| 1 | +# Copyright (c) 2018 Jouko Strömmer |
| 2 | +# Copyright (c) 2017 Waveshare |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +from abc import ABC, abstractmethod |
| 18 | +from PIL import Image |
| 19 | +import spidev |
| 20 | +import RPi.GPIO as GPIO |
| 21 | +import time |
| 22 | + |
| 23 | + |
| 24 | +class DisplayDriver(ABC): |
| 25 | + """Abstract base class for a display driver - be it Waveshare e-Paper, PaPiRus, OLED...""" |
| 26 | + |
| 27 | + # override these if needed |
| 28 | + white = 255 |
| 29 | + black = 0 |
| 30 | + |
| 31 | + def __init__(self): |
| 32 | + super().__init__() |
| 33 | + self.name = None |
| 34 | + self.width = None |
| 35 | + self.height = None |
| 36 | + self.colors = None |
| 37 | + self.type = None |
| 38 | + self.supports_partial = None |
| 39 | + self.partial_refresh = None |
| 40 | + |
| 41 | + @abstractmethod |
| 42 | + def init(self, partial=None): |
| 43 | + """Initialize the display""" |
| 44 | + pass |
| 45 | + |
| 46 | + @abstractmethod |
| 47 | + def draw(self, x, y, image): |
| 48 | + """Draw an image object on the display at (x,y)""" |
| 49 | + pass |
| 50 | + |
| 51 | + def scrub(self, fillsize=16): |
| 52 | + """Scrub display - only works properly with partial refresh""" |
| 53 | + self.fill(self.black, fillsize=fillsize) |
| 54 | + self.fill(self.white, fillsize=fillsize) |
| 55 | + |
| 56 | + def fill(self, color, fillsize): |
| 57 | + """Slow fill routine""" |
| 58 | + image = Image.new('1', (fillsize, self.height), color) |
| 59 | + for x in range(0, self.width, fillsize): |
| 60 | + self.draw(x, 0, image) |
| 61 | + |
| 62 | + |
| 63 | +class SpecialDriver(DisplayDriver): |
| 64 | + """Drivers that don't control hardware""" |
| 65 | + default_width = 640 |
| 66 | + default_height = 384 |
| 67 | + |
| 68 | + def __init__(self, name, width, height): |
| 69 | + super().__init__() |
| 70 | + self.name = name |
| 71 | + self.width = width |
| 72 | + self.height = height |
| 73 | + self.type = 'Dummy display driver' |
| 74 | + |
| 75 | + @abstractmethod |
| 76 | + def init(self, partial=None): |
| 77 | + pass |
| 78 | + |
| 79 | + @abstractmethod |
| 80 | + def draw(self, x, y, image): |
| 81 | + pass |
| 82 | + |
| 83 | + def scrub(self, fillsize=16): |
| 84 | + pass |
| 85 | + |
| 86 | + |
| 87 | +class Dummy(SpecialDriver): |
| 88 | + """Dummy display driver - does not do anything""" |
| 89 | + |
| 90 | + def __init__(self): |
| 91 | + super().__init__(name='No-op driver', width=self.default_width, height=self.default_height) |
| 92 | + |
| 93 | + def init(self, partial=None): |
| 94 | + pass |
| 95 | + |
| 96 | + def draw(self, x, y, image): |
| 97 | + pass |
| 98 | + |
| 99 | + |
| 100 | +class Bitmap(SpecialDriver): |
| 101 | + """Output a bitmap for each frame - overwrite old ones""" |
| 102 | + |
| 103 | + def __init__(self, maxfiles=5, file_format="png"): |
| 104 | + super().__init__(name="Bitmap output driver", width=self.default_width, height=self.default_height, ) |
| 105 | + self.maxfiles = maxfiles |
| 106 | + self.current_frame = 0 |
| 107 | + self.frame_buffer = None |
| 108 | + self.file_format = file_format |
| 109 | + |
| 110 | + def init(self, partial=None): |
| 111 | + self.frame_buffer = Image.new('1', (self.width, self.height), 255) |
| 112 | + self.current_frame = 0 |
| 113 | + |
| 114 | + def draw(self, x, y, image): |
| 115 | + self.frame_buffer.paste(image, box=(x, y)) |
| 116 | + self.frame_buffer.save("bitmap_frame_{}.{}".format(self.current_frame, self.file_format)) |
| 117 | + self.current_frame = (self.current_frame + 1) % self.maxfiles |
| 118 | + |
| 119 | + |
| 120 | +class WaveshareEPD(DisplayDriver): |
| 121 | + """Base class for Waveshare displays with common code for all - the 'epdif.py' |
| 122 | + - 1.54" , 1.54" B , 1.54" C |
| 123 | + - 2.13" , 2.13" B |
| 124 | + - 2.7" , 2.7" B |
| 125 | + - 2.9" , 2.9" B |
| 126 | + - 4.2" , 4.2" B |
| 127 | + - 7.5" , 7.5" B |
| 128 | + """ |
| 129 | + |
| 130 | + # SPI methods |
| 131 | + |
| 132 | + # These pins are common across all models |
| 133 | + RST_PIN = 17 |
| 134 | + DC_PIN = 25 |
| 135 | + CS_PIN = 8 |
| 136 | + BUSY_PIN = 24 |
| 137 | + |
| 138 | + # SPI device, bus = 0, device = 0 |
| 139 | + SPI = spidev.SpiDev(0, 0) |
| 140 | + |
| 141 | + @staticmethod |
| 142 | + def epd_digital_write(pin, value): |
| 143 | + GPIO.output(pin, value) |
| 144 | + |
| 145 | + @staticmethod |
| 146 | + def epd_digital_read(pin): |
| 147 | + return GPIO.input(pin) |
| 148 | + |
| 149 | + @staticmethod |
| 150 | + def epd_delay_ms(delaytime): |
| 151 | + time.sleep(float(delaytime) / 1000.0) |
| 152 | + |
| 153 | + def spi_transfer(self, data): |
| 154 | + self.SPI.writebytes(data) |
| 155 | + |
| 156 | + def epd_init(self): |
| 157 | + GPIO.setmode(GPIO.BCM) |
| 158 | + GPIO.setwarnings(False) |
| 159 | + GPIO.setup(self.RST_PIN, GPIO.OUT) |
| 160 | + GPIO.setup(self.DC_PIN, GPIO.OUT) |
| 161 | + GPIO.setup(self.CS_PIN, GPIO.OUT) |
| 162 | + GPIO.setup(self.BUSY_PIN, GPIO.IN) |
| 163 | + self.SPI.max_speed_hz = 2000000 |
| 164 | + self.SPI.mode = 0b00 |
| 165 | + return 0 |
| 166 | + |
| 167 | + # Basic functionality |
| 168 | + |
| 169 | + def __init__(self, name, width, height): |
| 170 | + super().__init__() |
| 171 | + self.name = name |
| 172 | + self.width = width |
| 173 | + self.height = height |
| 174 | + self.type = 'Waveshare e-Paper' |
| 175 | + |
| 176 | + def digital_write(self, pin, value): |
| 177 | + self.epd_digital_write(pin, value) |
| 178 | + |
| 179 | + def digital_read(self, pin): |
| 180 | + return self.epd_digital_read(pin) |
| 181 | + |
| 182 | + def delay_ms(self, delaytime): |
| 183 | + self.epd_delay_ms(delaytime) |
| 184 | + |
| 185 | + def send_command(self, command): |
| 186 | + self.digital_write(self.DC_PIN, GPIO.LOW) |
| 187 | + # the parameter type is list but not int |
| 188 | + # so use [command] instead of command |
| 189 | + self.spi_transfer([command]) |
| 190 | + |
| 191 | + def send_data(self, data): |
| 192 | + self.digital_write(self.DC_PIN, GPIO.HIGH) |
| 193 | + # the parameter type is list but not int |
| 194 | + # so use [data] instead of data |
| 195 | + self.spi_transfer([data]) |
| 196 | + |
| 197 | + def reset(self): |
| 198 | + self.digital_write(self.RST_PIN, GPIO.LOW) |
| 199 | + self.delay_ms(200) |
| 200 | + self.digital_write(self.RST_PIN, GPIO.HIGH) |
| 201 | + self.delay_ms(200) |
| 202 | + |
| 203 | + def init(self, **kwargs): |
| 204 | + pass |
| 205 | + |
| 206 | + def draw(self, x, y, image): |
| 207 | + pass |
0 commit comments