From b03f6f747be03a1b66ea53b4e7aaa40cf401fb28 Mon Sep 17 00:00:00 2001 From: RasTacsko Date: Sun, 31 Mar 2024 23:55:15 +0200 Subject: [PATCH 1/4] Generic I2C Oled support , waveshare oled/lcd vertical layout work in progress for the triple screen project --- pwnagotchi/ui/display.py | 6 + pwnagotchi/ui/hw/__init__.py | 8 + pwnagotchi/ui/hw/i2coled.py | 58 +++ .../hw/libs/waveshare/oled/oledlcd/SSD1306.py | 294 ++++++++++++++ .../libs/waveshare/oled/oledlcd/ST7789vert.py | 360 ++++++++++++++++++ .../hw/libs/waveshare/oled/oledlcd/config.py | 37 ++ .../ui/hw/libs/waveshare/oled/oledlcd/epd.py | 23 ++ pwnagotchi/ui/hw/waveshareoledlcdvert.py | 59 +++ pwnagotchi/utils.py | 6 + 9 files changed, 851 insertions(+) create mode 100644 pwnagotchi/ui/hw/i2coled.py create mode 100644 pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/SSD1306.py create mode 100644 pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/ST7789vert.py create mode 100644 pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/config.py create mode 100644 pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/epd.py create mode 100644 pwnagotchi/ui/hw/waveshareoledlcdvert.py diff --git a/pwnagotchi/ui/display.py b/pwnagotchi/ui/display.py index 5e5645b0..28364f30 100644 --- a/pwnagotchi/ui/display.py +++ b/pwnagotchi/ui/display.py @@ -264,6 +264,12 @@ class Display(View): def is_waveshareoledlcd(self): return self._implementation.name == 'waveshareoledlcd' + + def is_waveshareoledlcdvert(self): + return self._implementation.name == 'waveshareoledlcdvert' + + def is_i2coled(self): + return self._implementation.name == 'i2coled' def is_waveshare35lcd(self): return self._implementation.name == 'waveshare35lcd' diff --git a/pwnagotchi/ui/hw/__init__.py b/pwnagotchi/ui/hw/__init__.py index 827c849c..f966af6e 100644 --- a/pwnagotchi/ui/hw/__init__.py +++ b/pwnagotchi/ui/hw/__init__.py @@ -104,6 +104,14 @@ def display_for(config): from pwnagotchi.ui.hw.waveshareoledlcd import Waveshareoledlcd return Waveshareoledlcd(config) + elif config['ui']['display']['type'] == 'waveshareoledlcdvert': + from pwnagotchi.ui.hw.waveshareoledlcdvert import Waveshareoledlcdvert + return Waveshareoledlcdvert(config) + + elif config['ui']['display']['type'] == 'i2coled': + from pwnagotchi.ui.hw.i2coled import I2COled + return I2COled(config) + elif config['ui']['display']['type'] == 'waveshare1in02': from pwnagotchi.ui.hw.waveshare1in02 import Waveshare1in02 return Waveshare1in02(config) diff --git a/pwnagotchi/ui/hw/i2coled.py b/pwnagotchi/ui/hw/i2coled.py new file mode 100644 index 00000000..1392dc04 --- /dev/null +++ b/pwnagotchi/ui/hw/i2coled.py @@ -0,0 +1,58 @@ +# workinprogress based on the displayhatmini driver +# LCD support OK +# OLED support ongoing +# board GPIO: +# Key1: GPIO4 / pin7 +# Key2: GPIO17 / pin11 +# Key3: GPIO23 / pin16 +# Key4: GPIO24 / pin18 +# OLED SDA: GPIO2 / pin3 +# OLED SCL: GPIO3 / pin5 +# OLED info: +# driver: SSD1315 (I2C) +# resolution: 128x64 +# I2C address: 0x3C 0x3D +# HW datasheet: https://www.waveshare.com/wiki/OLED/LCD_HAT_(A) +import logging + +import pwnagotchi.ui.fonts as fonts +from pwnagotchi.ui.hw.base import DisplayImpl + +class I2COled(DisplayImpl): + def __init__(self, config): + super(I2COled, self).__init__(config, 'i2coled') + + def layout(self): + fonts.setup(8, 8, 8, 10, 10, 8) + self._layout['width'] = 128 + self._layout['height'] = 64 + self._layout['face'] = (0, 30) + self._layout['name'] = (0, 10) + self._layout['channel'] = (72, 10) + self._layout['aps'] = (0, 0) + self._layout['uptime'] = (87, 0) + self._layout['line1'] = [0, 9, 128, 9] + self._layout['line2'] = [0, 54, 128, 54] + self._layout['friend_face'] = (0, 41) + self._layout['friend_name'] = (40, 43) + self._layout['shakes'] = (0, 55) + self._layout['mode'] = (107, 10) + self._layout['status'] = { + 'pos': (37, 19), + 'font': fonts.status_font(fonts.Small), + 'max': 18 + } + return self._layout + + def initialize(self): + logging.info("initializing I2C Oled Display on address 0x3C") + from pwnagotchi.ui.hw.libs.waveshare.oled.oledlcd.epd import EPD + self._display = EPD() + self._display.Init() + self._display.Clear() + + def render(self, canvas): + self._display.display(canvas) + + def clear(self): + self._display.clear() \ No newline at end of file diff --git a/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/SSD1306.py b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/SSD1306.py new file mode 100644 index 00000000..4ca97dad --- /dev/null +++ b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/SSD1306.py @@ -0,0 +1,294 @@ +# Copyright (c) 2014 Adafruit Industries +# Author: Tony DiCola +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +from __future__ import division +import logging +import time + +import RPi.GPIO as GPIO +import spidev +from smbus import SMBus +i2cbus = SMBus(1) + + +# Constants +SSD1306_I2C_ADDRESS = 0x3C # 011110+SA0+RW - 0x3C or 0x3D +SSD1306_SETCONTRAST = 0x81 +SSD1306_DISPLAYALLON_RESUME = 0xA4 +SSD1306_DISPLAYALLON = 0xA5 +SSD1306_NORMALDISPLAY = 0xA6 +SSD1306_INVERTDISPLAY = 0xA7 +SSD1306_DISPLAYOFF = 0xAE +SSD1306_DISPLAYON = 0xAF +SSD1306_SETDISPLAYOFFSET = 0xD3 +SSD1306_SETCOMPINS = 0xDA +SSD1306_SETVCOMDETECT = 0xDB +SSD1306_SETDISPLAYCLOCKDIV = 0xD5 +SSD1306_SETPRECHARGE = 0xD9 +SSD1306_SETMULTIPLEX = 0xA8 +SSD1306_SETLOWCOLUMN = 0x00 +SSD1306_SETHIGHCOLUMN = 0x10 +SSD1306_SETSTARTLINE = 0x40 +SSD1306_MEMORYMODE = 0x20 +SSD1306_COLUMNADDR = 0x21 +SSD1306_PAGEADDR = 0x22 +SSD1306_COMSCANINC = 0xC0 +SSD1306_COMSCANDEC = 0xC8 +SSD1306_SEGREMAP = 0xA0 +SSD1306_CHARGEPUMP = 0x8D +SSD1306_EXTERNALVCC = 0x1 +SSD1306_SWITCHCAPVCC = 0x2 + +# Scrolling constants +SSD1306_ACTIVATE_SCROLL = 0x2F +SSD1306_DEACTIVATE_SCROLL = 0x2E +SSD1306_SET_VERTICAL_SCROLL_AREA = 0xA3 +SSD1306_RIGHT_HORIZONTAL_SCROLL = 0x26 +SSD1306_LEFT_HORIZONTAL_SCROLL = 0x27 +SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = 0x29 +SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = 0x2A + + +class SSD1306Base(object): + """Base class for SSD1306-based OLED displays. Implementors should subclass + and provide an implementation for the _initialize function. + """ + + def __init__(self, width, height, address=SSD1306_I2C_ADDRESS, bus=None): + self._log = logging.getLogger('Adafruit_SSD1306.SSD1306Base') + # self._rst = None + self.cmd_mode = 0x00 + self.data_mode = 0x40 + self.bus = i2cbus + self.addr = address + self.width = width + self.height = height + self._pages = height//8 + self._buffer = [0]*(width*self._pages) + + def _initialize(self): + raise NotImplementedError + + def command(self, *cmd): + """Send command byte to display.""" + # I2C write. + assert(len(cmd) <= 31) + self.bus.write_i2c_block_data(self.addr, self.cmd_mode, list(cmd)) + + def data(self, data): + """Send byte of data to display.""" + # I2C write. + for i in range(0, len(data), 31): + self.bus.write_i2c_block_data(self.addr, self.data_mode, list(data[i:i+31])) + + def begin(self, vccstate=SSD1306_SWITCHCAPVCC): + """Initialize display.""" + # Save vcc state. + self._vccstate = vccstate + # Reset and initialize display. + self._initialize() + # Turn on the display. + self.command(SSD1306_DISPLAYON) + + def ShowImage(self): + """ + The image on the "canvas" is flushed through to the hardware display. + Takes the 1-bit image and dumps it to the SSD1306 OLED display. + """ + self.command(SSD1306_COLUMNADDR) + self.command(0) # Column start address. (0 = reset) + self.command(self.width-1) # Column end address. + self.command(SSD1306_PAGEADDR) + self.command(0) # Page start address. (0 = reset) + self.command(self._pages-1) # Page end address. + + for i in range(0, len(self._buffer), 16): + self.bus.write_i2c_block_data(self.addr, self.data_mode, self._buffer[i:i+16]) + + def getbuffer(self, image): + """Set buffer to value of Python Imaging Library image. The image should + be in 1 bit mode and a size equal to the display size. + """ + if image.mode != '1': + raise ValueError('Image must be in mode 1.') + imwidth, imheight = image.size + if imwidth != self.width or imheight != self.height: + raise ValueError('Image must be same dimensions as display ({0}x{1}).' \ + .format(self.width, self.height)) + # Grab all the pixels from the image, faster than getpixel. + pix = image.load() + # Iterate through the memory pages + index = 0 + for page in range(self._pages): + # Iterate through all x axis columns. + for x in range(self.width): + # Set the bits for the column of pixels at the current position. + bits = 0 + # Don't use range here as it's a bit slow + for bit in [0, 1, 2, 3, 4, 5, 6, 7]: + bits = bits << 1 + bits |= 0 if pix[(x, page*8+7-bit)] == 0 else 1 + # Update buffer byte and increment to next byte. + self._buffer[index] = bits + index += 1 + + def clear(self): + """Clear contents of image buffer.""" + self._buffer = [0]*(self.width*self._pages) + + def set_contrast(self, contrast): + """Sets the contrast of the display. Contrast should be a value between + 0 and 255.""" + if contrast < 0 or contrast > 255: + raise ValueError('Contrast must be a value from 0 to 255 (inclusive).') + self.command(SSD1306_SETCONTRAST) + self.command(contrast) + + def dim(self, dim): + """Adjusts contrast to dim the display if dim is True, otherwise sets the + contrast to normal brightness if dim is False. + """ + # Assume dim display. + contrast = 0 + # Adjust contrast based on VCC if not dimming. + if not dim: + if self._vccstate == SSD1306_EXTERNALVCC: + contrast = 0x9F + else: + contrast = 0xCF + self.set_contrast(contrast) + +class SSD1306_128_64(SSD1306Base): + def __init__(self, width, height, address=None, bus=None): + # Call base class constructor. + super(SSD1306_128_64, self).__init__(128, 64, address, bus) + + def _initialize(self): + # 128x64 pixel specific initialization. + self.command(SSD1306_DISPLAYOFF) # 0xAE + self.command(SSD1306_SETDISPLAYCLOCKDIV) # 0xD5 + self.command(0x80) # the suggested ratio 0x80 + self.command(SSD1306_SETMULTIPLEX) # 0xA8 + self.command(0x3F) + self.command(SSD1306_SETDISPLAYOFFSET) # 0xD3 + self.command(0x0) # no offset + self.command(SSD1306_SETSTARTLINE | 0x0) # line #0 + self.command(SSD1306_CHARGEPUMP) # 0x8D + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x10) + else: + self.command(0x14) + self.command(SSD1306_MEMORYMODE) # 0x20 + self.command(0x00) # 0x0 act like ks0108 + self.command(SSD1306_SEGREMAP | 0x1) + self.command(SSD1306_COMSCANDEC) + self.command(SSD1306_SETCOMPINS) # 0xDA + self.command(0x12) + self.command(SSD1306_SETCONTRAST) # 0x81 + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x9F) + else: + self.command(0xCF) + self.command(SSD1306_SETPRECHARGE) # 0xd9 + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x22) + else: + self.command(0xF1) + self.command(SSD1306_SETVCOMDETECT) # 0xDB + self.command(0x40) + self.command(SSD1306_DISPLAYALLON_RESUME) # 0xA4 + self.command(SSD1306_NORMALDISPLAY) # 0xA6 + +class SSD1306_128_32(SSD1306Base): + def __init__(self, width, height, address=None, bus=None): + # Call base class constructor. + super(SSD1306_128_64, self).__init__(128, 64, address, bus) + + def _initialize(self): + # 128x32 pixel specific initialization. + self.command(SSD1306_DISPLAYOFF) # 0xAE + self.command(SSD1306_SETDISPLAYCLOCKDIV) # 0xD5 + self.command(0x80) # the suggested ratio 0x80 + self.command(SSD1306_SETMULTIPLEX) # 0xA8 + self.command(0x1F) + self.command(SSD1306_SETDISPLAYOFFSET) # 0xD3 + self.command(0x0) # no offset + self.command(SSD1306_SETSTARTLINE | 0x0) # line #0 + self.command(SSD1306_CHARGEPUMP) # 0x8D + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x10) + else: + self.command(0x14) + self.command(SSD1306_MEMORYMODE) # 0x20 + self.command(0x00) # 0x0 act like ks0108 + self.command(SSD1306_SEGREMAP | 0x1) + self.command(SSD1306_COMSCANDEC) + self.command(SSD1306_SETCOMPINS) # 0xDA + self.command(0x02) + self.command(SSD1306_SETCONTRAST) # 0x81 + self.command(0x8F) + self.command(SSD1306_SETPRECHARGE) # 0xd9 + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x22) + else: + self.command(0xF1) + self.command(SSD1306_SETVCOMDETECT) # 0xDB + self.command(0x40) + self.command(SSD1306_DISPLAYALLON_RESUME) # 0xA4 + self.command(SSD1306_NORMALDISPLAY) # 0xA6 + + +class SSD1306_96_16(SSD1306Base): + def __init__(self, width, height, address=None, bus=None): + # Call base class constructor. + super(SSD1306_96_16, self).__init__(96, 16, address, bus) + + def _initialize(self): + # 128x32 pixel specific initialization. + self.command(SSD1306_DISPLAYOFF) # 0xAE + self.command(SSD1306_SETDISPLAYCLOCKDIV) # 0xD5 + self.command(0x60) # the suggested ratio 0x60 + self.command(SSD1306_SETMULTIPLEX) # 0xA8 + self.command(0x0F) + self.command(SSD1306_SETDISPLAYOFFSET) # 0xD3 + self.command(0x0) # no offset + self.command(SSD1306_SETSTARTLINE | 0x0) # line #0 + self.command(SSD1306_CHARGEPUMP) # 0x8D + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x10) + else: + self.command(0x14) + self.command(SSD1306_MEMORYMODE) # 0x20 + self.command(0x00) # 0x0 act like ks0108 + self.command(SSD1306_SEGREMAP | 0x1) + self.command(SSD1306_COMSCANDEC) + self.command(SSD1306_SETCOMPINS) # 0xDA + self.command(0x02) + self.command(SSD1306_SETCONTRAST) # 0x81 + self.command(0x8F) + self.command(SSD1306_SETPRECHARGE) # 0xd9 + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x22) + else: + self.command(0xF1) + self.command(SSD1306_SETVCOMDETECT) # 0xDB + self.command(0x40) + self.command(SSD1306_DISPLAYALLON_RESUME) # 0xA4 + self.command(SSD1306_NORMALDISPLAY) # 0xA6 \ No newline at end of file diff --git a/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/ST7789vert.py b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/ST7789vert.py new file mode 100644 index 00000000..f8894b98 --- /dev/null +++ b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/ST7789vert.py @@ -0,0 +1,360 @@ +# Copyright (c) 2014 Adafruit Industries +# Author: Tony DiCola +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +import numbers +import time +import numpy as np + +import spidev +import RPi.GPIO as GPIO + + +__version__ = '0.0.4' + +BG_SPI_CS_BACK = 0 +BG_SPI_CS_FRONT = 1 + +SPI_CLOCK_HZ = 16000000 + +ST7789_NOP = 0x00 +ST7789_SWRESET = 0x01 +ST7789_RDDID = 0x04 +ST7789_RDDST = 0x09 + +ST7789_SLPIN = 0x10 +ST7789_SLPOUT = 0x11 +ST7789_PTLON = 0x12 +ST7789_NORON = 0x13 + +ST7789_INVOFF = 0x20 +ST7789_INVON = 0x21 +ST7789_DISPOFF = 0x28 +ST7789_DISPON = 0x29 + +ST7789_CASET = 0x2A +ST7789_RASET = 0x2B +ST7789_RAMWR = 0x2C +ST7789_RAMRD = 0x2E + +ST7789_PTLAR = 0x30 +ST7789_MADCTL = 0x36 +ST7789_COLMOD = 0x3A + +ST7789_FRMCTR1 = 0xB1 +ST7789_FRMCTR2 = 0xB2 +ST7789_FRMCTR3 = 0xB3 +ST7789_INVCTR = 0xB4 +ST7789_DISSET5 = 0xB6 + +ST7789_GCTRL = 0xB7 +ST7789_GTADJ = 0xB8 +ST7789_VCOMS = 0xBB + +ST7789_LCMCTRL = 0xC0 +ST7789_IDSET = 0xC1 +ST7789_VDVVRHEN = 0xC2 +ST7789_VRHS = 0xC3 +ST7789_VDVS = 0xC4 +ST7789_VMCTR1 = 0xC5 +ST7789_FRCTRL2 = 0xC6 +ST7789_CABCCTRL = 0xC7 + +ST7789_RDID1 = 0xDA +ST7789_RDID2 = 0xDB +ST7789_RDID3 = 0xDC +ST7789_RDID4 = 0xDD + +ST7789_GMCTRP1 = 0xE0 +ST7789_GMCTRN1 = 0xE1 + +ST7789_PWCTR6 = 0xFC + + +class ST7789(object): + """Representation of an ST7789 TFT LCD.""" + + def __init__(self, port, cs, dc, backlight, rst=None, width=320, + height=240, rotation=270, invert=True, spi_speed_hz=60 * 1000 * 1000, + offset_left=0, + offset_top=0): + """Create an instance of the display using SPI communication. + + Must provide the GPIO pin number for the D/C pin and the SPI driver. + + Can optionally provide the GPIO pin number for the reset pin as the rst parameter. + + :param port: SPI port number + :param cs: SPI chip-select number (0 or 1 for BCM + :param backlight: Pin for controlling backlight + :param rst: Reset pin for ST7789 + :param width: Width of display connected to ST7789 + :param height: Height of display connected to ST7789 + :param rotation: Rotation of display connected to ST7789 + :param invert: Invert display + :param spi_speed_hz: SPI speed (in Hz) + + """ + if rotation not in [0, 90, 180, 270]: + raise ValueError("Invalid rotation {}".format(rotation)) + +# if width != height and rotation in [90, 270]: +# raise ValueError("Invalid rotation {} for {}x{} resolution".format(rotation, width, height)) + + GPIO.setwarnings(False) + GPIO.setmode(GPIO.BCM) + + self._spi = spidev.SpiDev(port, cs) + self._spi.mode = 0 + self._spi.lsbfirst = False + self._spi.max_speed_hz = spi_speed_hz + + self._dc = dc + self._rst = rst + self._width = width + self._height = height + self._rotation = rotation + self._invert = invert + + self._offset_left = offset_left + self._offset_top = offset_top + + # Set DC as output. + GPIO.setup(dc, GPIO.OUT) + + # Setup backlight as output (if provided). + self._backlight = backlight + if backlight is not None: + GPIO.setup(backlight, GPIO.OUT) + GPIO.output(backlight, GPIO.LOW) + time.sleep(0.1) + GPIO.output(backlight, GPIO.HIGH) + + # Setup reset as output (if provided). + if rst is not None: + GPIO.setup(self._rst, GPIO.OUT) + self.reset() + self._init() + + def send(self, data, is_data=True, chunk_size=4096): + """Write a byte or array of bytes to the display. Is_data parameter + controls if byte should be interpreted as display data (True) or command + data (False). Chunk_size is an optional size of bytes to write in a + single SPI transaction, with a default of 4096. + """ + # Set DC low for command, high for data. + GPIO.output(self._dc, is_data) + # Convert scalar argument to list so either can be passed as parameter. + if isinstance(data, numbers.Number): + data = [data & 0xFF] + # Write data a chunk at a time. + for start in range(0, len(data), chunk_size): + end = min(start + chunk_size, len(data)) + self._spi.xfer(data[start:end]) + + def set_backlight(self, value): + """Set the backlight on/off.""" + if self._backlight is not None: + GPIO.output(self._backlight, value) + + @property + def width(self): + return self._width if self._rotation == 0 or self._rotation == 180 else self._height + + @property + def height(self): + return self._height if self._rotation == 0 or self._rotation == 180 else self._width + + def command(self, data): + """Write a byte or array of bytes to the display as command data.""" + self.send(data, False) + + def data(self, data): + """Write a byte or array of bytes to the display as display data.""" + self.send(data, True) + + def reset(self): + """Reset the display, if reset pin is connected.""" + if self._rst is not None: + GPIO.output(self._rst, 1) + time.sleep(0.500) + GPIO.output(self._rst, 0) + time.sleep(0.500) + GPIO.output(self._rst, 1) + time.sleep(0.500) + + def _init(self): + # Initialize the display. + + self.command(ST7789_SWRESET) # Software reset + time.sleep(0.150) # delay 150 ms + + self.command(ST7789_MADCTL) + self.data(0x70) + + self.command(ST7789_FRMCTR2) # Frame rate ctrl - idle mode + self.data(0x0C) + self.data(0x0C) + self.data(0x00) + self.data(0x33) + self.data(0x33) + + self.command(ST7789_COLMOD) + self.data(0x05) + + self.command(ST7789_GCTRL) + self.data(0x14) + + self.command(ST7789_VCOMS) + self.data(0x37) + + self.command(ST7789_LCMCTRL) # Power control + self.data(0x2C) + + self.command(ST7789_VDVVRHEN) # Power control + self.data(0x01) + + self.command(ST7789_VRHS) # Power control + self.data(0x12) + + self.command(ST7789_VDVS) # Power control + self.data(0x20) + + self.command(0xD0) + self.data(0xA4) + self.data(0xA1) + + self.command(ST7789_FRCTRL2) + self.data(0x0F) + + self.command(ST7789_GMCTRP1) # Set Gamma + self.data(0xD0) + self.data(0x04) + self.data(0x0D) + self.data(0x11) + self.data(0x13) + self.data(0x2B) + self.data(0x3F) + self.data(0x54) + self.data(0x4C) + self.data(0x18) + self.data(0x0D) + self.data(0x0B) + self.data(0x1F) + self.data(0x23) + + self.command(ST7789_GMCTRN1) # Set Gamma + self.data(0xD0) + self.data(0x04) + self.data(0x0C) + self.data(0x11) + self.data(0x13) + self.data(0x2C) + self.data(0x3F) + self.data(0x44) + self.data(0x51) + self.data(0x2F) + self.data(0x1F) + self.data(0x1F) + self.data(0x20) + self.data(0x23) + + if self._invert: + self.command(ST7789_INVON) # Invert display + else: + self.command(ST7789_INVOFF) # Don't invert display + + self.command(ST7789_SLPOUT) + + self.command(ST7789_DISPON) # Display on + time.sleep(0.100) # 100 ms + + def begin(self): + """Set up the display + + Deprecated. Included in __init__. + + """ + pass + + def set_window(self, x0=0, y0=0, x1=None, y1=None): + """Set the pixel address window for proceeding drawing commands. x0 and + x1 should define the minimum and maximum x pixel bounds. y0 and y1 + should define the minimum and maximum y pixel bound. If no parameters + are specified the default will be to update the entire display from 0,0 + to width-1,height-1. + """ + if x1 is None: + x1 = self._width - 1 + + if y1 is None: + y1 = self._height - 1 + + y0 += self._offset_top + y1 += self._offset_top + + x0 += self._offset_left + x1 += self._offset_left + + self.command(ST7789_CASET) # Column addr set + self.data(x0 >> 8) + self.data(x0 & 0xFF) # XSTART + self.data(x1 >> 8) + self.data(x1 & 0xFF) # XEND + self.command(ST7789_RASET) # Row addr set + self.data(y0 >> 8) + self.data(y0 & 0xFF) # YSTART + self.data(y1 >> 8) + self.data(y1 & 0xFF) # YEND + self.command(ST7789_RAMWR) # write to RAM + + def display(self, image): + """Write the provided image to the hardware. + + :param image: Should be RGB format and the same dimensions as the display hardware. + + """ + # Set address bounds to entire display. + self.set_window() + + # Convert image to 16bit RGB565 format and + # flatten into bytes. + pixelbytes = self.image_to_data(image, self._rotation) + + # Write data to hardware. + for i in range(0, len(pixelbytes), 4096): + self.data(pixelbytes[i:i + 4096]) + + def image_to_data(self, image, rotation=0): + if not isinstance(image, np.ndarray): + image = np.array(image.convert('RGB')) + + # Rotate the image + pb = np.rot90(image, rotation // 90).astype('uint16') + + # Mask and shift the 888 RGB into 565 RGB + red = (pb[..., [0]] & 0xf8) << 8 + green = (pb[..., [1]] & 0xfc) << 3 + blue = (pb[..., [2]] & 0xf8) >> 3 + + # Stick 'em together + result = red | green | blue + + # Output the raw bytes + return result.byteswap().tobytes() \ No newline at end of file diff --git a/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/config.py b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/config.py new file mode 100644 index 00000000..a57a3515 --- /dev/null +++ b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/config.py @@ -0,0 +1,37 @@ +# /***************************************************************************** +# * | File : config.py +# * | Author : Waveshare team +# * | Function : Hardware underlying interface,for Jetson nano +# * | Info : +# *---------------- +# * | This version: V1.0 +# * | Date : 2019-06-06 +# * | Info : +# ******************************************************************************/ +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +import time +from smbus import SMBus + +Device_SPI = 0 +Device_I2C = 1 +Device = Device_I2C +address = 0x3C +bus = SMBus(1) +### END OF FILE ### diff --git a/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/epd.py b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/epd.py new file mode 100644 index 00000000..fed5a7f9 --- /dev/null +++ b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/epd.py @@ -0,0 +1,23 @@ +from . import SSD1306 + +# Display resolution +EPD_WIDTH = 128 +EPD_HEIGHT = 64 + +disp = SSD1306.SSD1306_128_64(128, 64, address=0x3C) + +class EPD(object): + + def __init__(self): + self.width = EPD_WIDTH + self.height = EPD_HEIGHT + + def Init(self): + disp.begin() + + def Clear(self): + disp.clear() + + def display(self, image): + disp.getbuffer(image) + disp.ShowImage() \ No newline at end of file diff --git a/pwnagotchi/ui/hw/waveshareoledlcdvert.py b/pwnagotchi/ui/hw/waveshareoledlcdvert.py new file mode 100644 index 00000000..ee817a26 --- /dev/null +++ b/pwnagotchi/ui/hw/waveshareoledlcdvert.py @@ -0,0 +1,59 @@ +# workinprogress based on the displayhatmini driver +# LCD support OK +# OLED support ongoing +# board GPIO: +# Key1: GPIO4 / pin7 +# Key2: GPIO17 / pin11 +# Key3: GPIO23 / pin16 +# Key4: GPIO24 / pin18 +# OLED SDA: GPIO2 / pin3 +# OLED SCL: GPIO3 / pin5 +# OLED info: +# driver: SSD1315 (I2C) +# resolution: 128x64 +# I2C address: 0x3C 0x3D +# HW datasheet: https://www.waveshare.com/wiki/OLED/LCD_HAT_(A) + +import logging + +import pwnagotchi.ui.fonts as fonts +from pwnagotchi.ui.hw.base import DisplayImpl + + +class Waveshareoledlcdvert(DisplayImpl): + def __init__(self, config): + super(Waveshareoledlcdvert, self).__init__(config, 'waveshareoledlcdvert') + + def layout(self): + fonts.setup(10, 9, 10, 35, 25, 9) + self._layout['width'] = 240 + self._layout['height'] = 320 + self._layout['face'] = (0, 40) + self._layout['name'] = (5, 20) + self._layout['channel'] = (0, 0) + self._layout['aps'] = (28, 0) + self._layout['uptime'] = (175, 0) + self._layout['line1'] = [0, 14, 240, 14] + self._layout['line2'] = [0, 108, 240, 108] + self._layout['friend_face'] = (0, 92) + self._layout['friend_name'] = (40, 94) + self._layout['shakes'] = (0, 109) + self._layout['mode'] = (215, 109) + self._layout['status'] = { + 'pos': (125, 20), + 'font': fonts.status_font(fonts.Medium), + 'max': 20 + } + + return self._layout + + def initialize(self): + logging.info("initializing Waveshare OLED/LCD hat vertical mode") + from pwnagotchi.ui.hw.libs.waveshare.oled.oledlcd.ST7789vert import ST7789 + self._display = ST7789(0,0,22,18) + + def render(self, canvas): + self._display.display(canvas) + + def clear(self): + self._display.clear() \ No newline at end of file diff --git a/pwnagotchi/utils.py b/pwnagotchi/utils.py index 547a1200..e17309b9 100644 --- a/pwnagotchi/utils.py +++ b/pwnagotchi/utils.py @@ -315,9 +315,15 @@ def load_config(args): elif config['ui']['display']['type'] in ('waveshareoledlcd'): config['ui']['display']['type'] = 'waveshareoledlcd' + elif config['ui']['display']['type'] in ('i2coled'): + config['ui']['display']['type'] = 'i2coled' + elif config['ui']['display']['type'] in ('waveshare35lcd'): config['ui']['display']['type'] = 'waveshare35lcd' + elif config['ui']['display']['type'] in ('waveshareoledlcdvert'): + config['ui']['display']['type'] = 'waveshareoledlcdvert' + # E-INK DISPLAYS ------------------------------------------------------------------------ # Adafruit From a71a90ba3db39364178c01f22a9fbdb093914b3d Mon Sep 17 00:00:00 2001 From: RasTacsko Date: Mon, 1 Apr 2024 01:37:47 +0200 Subject: [PATCH 2/4] Delete config.py --- .../hw/libs/waveshare/oled/oledlcd/config.py | 37 ------------------- 1 file changed, 37 deletions(-) delete mode 100644 pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/config.py diff --git a/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/config.py b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/config.py deleted file mode 100644 index a57a3515..00000000 --- a/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/config.py +++ /dev/null @@ -1,37 +0,0 @@ -# /***************************************************************************** -# * | File : config.py -# * | Author : Waveshare team -# * | Function : Hardware underlying interface,for Jetson nano -# * | Info : -# *---------------- -# * | This version: V1.0 -# * | Date : 2019-06-06 -# * | Info : -# ******************************************************************************/ -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -# -import time -from smbus import SMBus - -Device_SPI = 0 -Device_I2C = 1 -Device = Device_I2C -address = 0x3C -bus = SMBus(1) -### END OF FILE ### From ab83de490533f7ad739bb0c305effa77ec55350b Mon Sep 17 00:00:00 2001 From: RasTacsko Date: Tue, 2 Apr 2024 17:46:11 +0200 Subject: [PATCH 3/4] separate I2C Oled display Separate files for ggeneric I2C Oled display with SSD1306 driver 128x64 resolution is tested, 128x32 and 96x16 resolutions is not tested yet --- pwnagotchi/ui/hw/i2coled.py | 26 +- pwnagotchi/ui/hw/libs/i2coled/SSD1306.py | 304 +++++++++++++++++++++++ pwnagotchi/ui/hw/libs/i2coled/epd.py | 27 ++ 3 files changed, 340 insertions(+), 17 deletions(-) create mode 100644 pwnagotchi/ui/hw/libs/i2coled/SSD1306.py create mode 100644 pwnagotchi/ui/hw/libs/i2coled/epd.py diff --git a/pwnagotchi/ui/hw/i2coled.py b/pwnagotchi/ui/hw/i2coled.py index 1392dc04..61a675dd 100644 --- a/pwnagotchi/ui/hw/i2coled.py +++ b/pwnagotchi/ui/hw/i2coled.py @@ -1,18 +1,9 @@ -# workinprogress based on the displayhatmini driver -# LCD support OK -# OLED support ongoing -# board GPIO: -# Key1: GPIO4 / pin7 -# Key2: GPIO17 / pin11 -# Key3: GPIO23 / pin16 -# Key4: GPIO24 / pin18 -# OLED SDA: GPIO2 / pin3 -# OLED SCL: GPIO3 / pin5 -# OLED info: -# driver: SSD1315 (I2C) -# resolution: 128x64 -# I2C address: 0x3C 0x3D -# HW datasheet: https://www.waveshare.com/wiki/OLED/LCD_HAT_(A) +# Created for the Pwnagotchi project by RasTacsko +# HW libraries are based on the adafruit python SSD1306 repo: +# https://github.com/adafruit/Adafruit_Python_SSD1306 +# SMBus parts coming from BLavery's lib_oled96 repo: +# https://github.com/BLavery/lib_oled96 + import logging import pwnagotchi.ui.fonts as fonts @@ -45,8 +36,9 @@ class I2COled(DisplayImpl): return self._layout def initialize(self): - logging.info("initializing I2C Oled Display on address 0x3C") - from pwnagotchi.ui.hw.libs.waveshare.oled.oledlcd.epd import EPD + logging.info("initializing 128x64 I2C Oled Display on address 0x3C") + logging.info("To change resolution or address check pwnagotchi/ui/hw/libs/i2coled/epd.py") + from pwnagotchi.ui.hw.libs.i2coled.epd import EPD self._display = EPD() self._display.Init() self._display.Clear() diff --git a/pwnagotchi/ui/hw/libs/i2coled/SSD1306.py b/pwnagotchi/ui/hw/libs/i2coled/SSD1306.py new file mode 100644 index 00000000..e5c02ecd --- /dev/null +++ b/pwnagotchi/ui/hw/libs/i2coled/SSD1306.py @@ -0,0 +1,304 @@ +# Copyright (c) 2014 Adafruit Industries +# Author: Tony DiCola +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# SMBus parts coming from BLavery's lib_oled96 repo: +# https://github.com/BLavery/lib_oled96 +# +# Modified for the Pwnagotchi project by RasTacsko +# Using SMBus, spidev RPi.GPIO for I2C communication instead of Adafruit libraries +# spidev maybe not necessary... needs some checking!!! +# ToDo: +# rotation support for vertical layouts +# checking luma oled library for support other chipsets/resolutions + +from __future__ import division +import logging +import time + +import RPi.GPIO as GPIO +# import spidev +from smbus import SMBus +i2cbus = SMBus(1) + + +# Constants +SSD1306_I2C_ADDRESS = 0x3C # 011110+SA0+RW - 0x3C or 0x3D +SSD1306_SETCONTRAST = 0x81 +SSD1306_DISPLAYALLON_RESUME = 0xA4 +SSD1306_DISPLAYALLON = 0xA5 +SSD1306_NORMALDISPLAY = 0xA6 +SSD1306_INVERTDISPLAY = 0xA7 +SSD1306_DISPLAYOFF = 0xAE +SSD1306_DISPLAYON = 0xAF +SSD1306_SETDISPLAYOFFSET = 0xD3 +SSD1306_SETCOMPINS = 0xDA +SSD1306_SETVCOMDETECT = 0xDB +SSD1306_SETDISPLAYCLOCKDIV = 0xD5 +SSD1306_SETPRECHARGE = 0xD9 +SSD1306_SETMULTIPLEX = 0xA8 +SSD1306_SETLOWCOLUMN = 0x00 +SSD1306_SETHIGHCOLUMN = 0x10 +SSD1306_SETSTARTLINE = 0x40 +SSD1306_MEMORYMODE = 0x20 +SSD1306_COLUMNADDR = 0x21 +SSD1306_PAGEADDR = 0x22 +SSD1306_COMSCANINC = 0xC0 +SSD1306_COMSCANDEC = 0xC8 +SSD1306_SEGREMAP = 0xA0 +SSD1306_CHARGEPUMP = 0x8D +SSD1306_EXTERNALVCC = 0x1 +SSD1306_SWITCHCAPVCC = 0x2 + +# Scrolling constants +SSD1306_ACTIVATE_SCROLL = 0x2F +SSD1306_DEACTIVATE_SCROLL = 0x2E +SSD1306_SET_VERTICAL_SCROLL_AREA = 0xA3 +SSD1306_RIGHT_HORIZONTAL_SCROLL = 0x26 +SSD1306_LEFT_HORIZONTAL_SCROLL = 0x27 +SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = 0x29 +SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = 0x2A + + +class SSD1306Base(object): + """Base class for SSD1306-based OLED displays. Implementors should subclass + and provide an implementation for the _initialize function. + """ + + def __init__(self, width, height, address=SSD1306_I2C_ADDRESS, bus=None): + self._log = logging.getLogger('Adafruit_SSD1306.SSD1306Base') + self.cmd_mode = 0x00 + self.data_mode = 0x40 + self.bus = i2cbus + self.addr = address + self.width = width + self.height = height + self._pages = height//8 + self._buffer = [0]*(width*self._pages) + + def _initialize(self): + raise NotImplementedError + + def command(self, *cmd): + """Send command byte to display.""" + # I2C write. + assert(len(cmd) <= 31) + self.bus.write_i2c_block_data(self.addr, self.cmd_mode, list(cmd)) + + def data(self, data): + """Send byte of data to display.""" + # I2C write. + for i in range(0, len(data), 31): + self.bus.write_i2c_block_data(self.addr, self.data_mode, list(data[i:i+31])) + + def begin(self, vccstate=SSD1306_SWITCHCAPVCC): + """Initialize display.""" + # Save vcc state. + self._vccstate = vccstate + # Reset and initialize display. + self._initialize() + # Turn on the display. + self.command(SSD1306_DISPLAYON) + + def ShowImage(self): + """ + The image on the "canvas" is flushed through to the hardware display. + Takes the 1-bit image and dumps it to the SSD1306 OLED display. + """ + self.command(SSD1306_COLUMNADDR) + self.command(0) # Column start address. (0 = reset) + self.command(self.width-1) # Column end address. + self.command(SSD1306_PAGEADDR) + self.command(0) # Page start address. (0 = reset) + self.command(self._pages-1) # Page end address. + + for i in range(0, len(self._buffer), 16): + self.bus.write_i2c_block_data(self.addr, self.data_mode, self._buffer[i:i+16]) + + def getbuffer(self, image): + """Set buffer to value of Python Imaging Library image. The image should + be in 1 bit mode and a size equal to the display size. + """ + if image.mode != '1': + raise ValueError('Image must be in mode 1.') + imwidth, imheight = image.size + if imwidth != self.width or imheight != self.height: + raise ValueError('Image must be same dimensions as display ({0}x{1}).' \ + .format(self.width, self.height)) + # Grab all the pixels from the image, faster than getpixel. + pix = image.load() + # Iterate through the memory pages + index = 0 + for page in range(self._pages): + # Iterate through all x axis columns. + for x in range(self.width): + # Set the bits for the column of pixels at the current position. + bits = 0 + # Don't use range here as it's a bit slow + for bit in [0, 1, 2, 3, 4, 5, 6, 7]: + bits = bits << 1 + bits |= 0 if pix[(x, page*8+7-bit)] == 0 else 1 + # Update buffer byte and increment to next byte. + self._buffer[index] = bits + index += 1 + + def clear(self): + """Clear contents of image buffer.""" + self._buffer = [0]*(self.width*self._pages) + + def set_contrast(self, contrast): + """Sets the contrast of the display. Contrast should be a value between + 0 and 255.""" + if contrast < 0 or contrast > 255: + raise ValueError('Contrast must be a value from 0 to 255 (inclusive).') + self.command(SSD1306_SETCONTRAST) + self.command(contrast) + + def dim(self, dim): + """Adjusts contrast to dim the display if dim is True, otherwise sets the + contrast to normal brightness if dim is False. + """ + # Assume dim display. + contrast = 0 + # Adjust contrast based on VCC if not dimming. + if not dim: + if self._vccstate == SSD1306_EXTERNALVCC: + contrast = 0x9F + else: + contrast = 0xCF + self.set_contrast(contrast) + +class SSD1306_128_64(SSD1306Base): + def __init__(self, width, height, address=None, bus=None): + # Call base class constructor. + super(SSD1306_128_64, self).__init__(128, 64, address, bus) + + def _initialize(self): + # 128x64 pixel specific initialization. + self.command(SSD1306_DISPLAYOFF) # 0xAE + self.command(SSD1306_SETDISPLAYCLOCKDIV) # 0xD5 + self.command(0x80) # the suggested ratio 0x80 + self.command(SSD1306_SETMULTIPLEX) # 0xA8 + self.command(0x3F) + self.command(SSD1306_SETDISPLAYOFFSET) # 0xD3 + self.command(0x0) # no offset + self.command(SSD1306_SETSTARTLINE | 0x0) # line #0 + self.command(SSD1306_CHARGEPUMP) # 0x8D + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x10) + else: + self.command(0x14) + self.command(SSD1306_MEMORYMODE) # 0x20 + self.command(0x00) # 0x0 act like ks0108 + self.command(SSD1306_SEGREMAP | 0x1) + self.command(SSD1306_COMSCANDEC) + self.command(SSD1306_SETCOMPINS) # 0xDA + self.command(0x12) + self.command(SSD1306_SETCONTRAST) # 0x81 + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x9F) + else: + self.command(0xCF) + self.command(SSD1306_SETPRECHARGE) # 0xd9 + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x22) + else: + self.command(0xF1) + self.command(SSD1306_SETVCOMDETECT) # 0xDB + self.command(0x40) + self.command(SSD1306_DISPLAYALLON_RESUME) # 0xA4 + self.command(SSD1306_NORMALDISPLAY) # 0xA6 + +class SSD1306_128_32(SSD1306Base): + def __init__(self, width, height, address=None, bus=None): + # Call base class constructor. + super(SSD1306_128_64, self).__init__(128, 64, address, bus) + + def _initialize(self): + # 128x32 pixel specific initialization. + self.command(SSD1306_DISPLAYOFF) # 0xAE + self.command(SSD1306_SETDISPLAYCLOCKDIV) # 0xD5 + self.command(0x80) # the suggested ratio 0x80 + self.command(SSD1306_SETMULTIPLEX) # 0xA8 + self.command(0x1F) + self.command(SSD1306_SETDISPLAYOFFSET) # 0xD3 + self.command(0x0) # no offset + self.command(SSD1306_SETSTARTLINE | 0x0) # line #0 + self.command(SSD1306_CHARGEPUMP) # 0x8D + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x10) + else: + self.command(0x14) + self.command(SSD1306_MEMORYMODE) # 0x20 + self.command(0x00) # 0x0 act like ks0108 + self.command(SSD1306_SEGREMAP | 0x1) + self.command(SSD1306_COMSCANDEC) + self.command(SSD1306_SETCOMPINS) # 0xDA + self.command(0x02) + self.command(SSD1306_SETCONTRAST) # 0x81 + self.command(0x8F) + self.command(SSD1306_SETPRECHARGE) # 0xd9 + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x22) + else: + self.command(0xF1) + self.command(SSD1306_SETVCOMDETECT) # 0xDB + self.command(0x40) + self.command(SSD1306_DISPLAYALLON_RESUME) # 0xA4 + self.command(SSD1306_NORMALDISPLAY) # 0xA6 + + +class SSD1306_96_16(SSD1306Base): + def __init__(self, width, height, address=None, bus=None): + # Call base class constructor. + super(SSD1306_96_16, self).__init__(96, 16, address, bus) + + def _initialize(self): + # 128x32 pixel specific initialization. + self.command(SSD1306_DISPLAYOFF) # 0xAE + self.command(SSD1306_SETDISPLAYCLOCKDIV) # 0xD5 + self.command(0x60) # the suggested ratio 0x60 + self.command(SSD1306_SETMULTIPLEX) # 0xA8 + self.command(0x0F) + self.command(SSD1306_SETDISPLAYOFFSET) # 0xD3 + self.command(0x0) # no offset + self.command(SSD1306_SETSTARTLINE | 0x0) # line #0 + self.command(SSD1306_CHARGEPUMP) # 0x8D + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x10) + else: + self.command(0x14) + self.command(SSD1306_MEMORYMODE) # 0x20 + self.command(0x00) # 0x0 act like ks0108 + self.command(SSD1306_SEGREMAP | 0x1) + self.command(SSD1306_COMSCANDEC) + self.command(SSD1306_SETCOMPINS) # 0xDA + self.command(0x02) + self.command(SSD1306_SETCONTRAST) # 0x81 + self.command(0x8F) + self.command(SSD1306_SETPRECHARGE) # 0xd9 + if self._vccstate == SSD1306_EXTERNALVCC: + self.command(0x22) + else: + self.command(0xF1) + self.command(SSD1306_SETVCOMDETECT) # 0xDB + self.command(0x40) + self.command(SSD1306_DISPLAYALLON_RESUME) # 0xA4 + self.command(SSD1306_NORMALDISPLAY) # 0xA6 \ No newline at end of file diff --git a/pwnagotchi/ui/hw/libs/i2coled/epd.py b/pwnagotchi/ui/hw/libs/i2coled/epd.py new file mode 100644 index 00000000..898aee36 --- /dev/null +++ b/pwnagotchi/ui/hw/libs/i2coled/epd.py @@ -0,0 +1,27 @@ +from . import SSD1306 + +# Display resolution, change if the screen resolution is changed! +EPD_WIDTH = 128 +EPD_HEIGHT = 64 + +# Available screen resolutions: +# disp = SSD1306.SSD1306_128_32(128, 32, address=0x3C) +# disp = SSD1306.SSD1306_96_16(96, 16, address=0x3C) +# If you change for different resolution, you have to modify the layout in pwnagotchi/ui/hw/i2coled.py +disp = SSD1306.SSD1306_128_64(128, 64, address=0x3C) + +class EPD(object): + + def __init__(self): + self.width = EPD_WIDTH + self.height = EPD_HEIGHT + + def Init(self): + disp.begin() + + def Clear(self): + disp.clear() + + def display(self, image): + disp.getbuffer(image) + disp.ShowImage() \ No newline at end of file From fef442edbb61e4e4182d64b30cf3c2b1bab6370b Mon Sep 17 00:00:00 2001 From: RasTacsko Date: Thu, 18 Apr 2024 17:39:53 +0200 Subject: [PATCH 4/4] I2C oled driver mods by NurseJackass I2C oled config can be added in the config.toml Default is 128x64 display on i2c address 0x3C ui.display.type = "i2coled" ui.display.i2c_addr = 0x3C ui.display.width = 128 ui.display.height = 64 --- pwnagotchi/ui/hw/i2coled.py | 27 +++++++++++++++---- pwnagotchi/ui/hw/libs/i2coled/SSD1306.py | 26 +++++++++++------- pwnagotchi/ui/hw/libs/i2coled/epd.py | 23 ++++++++++------ .../hw/libs/waveshare/oled/oledlcd/SSD1306.py | 6 ++--- .../libs/waveshare/oled/oledlcd/ST7789vert.py | 8 ------ 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/pwnagotchi/ui/hw/i2coled.py b/pwnagotchi/ui/hw/i2coled.py index 61a675dd..1c55035e 100644 --- a/pwnagotchi/ui/hw/i2coled.py +++ b/pwnagotchi/ui/hw/i2coled.py @@ -3,20 +3,33 @@ # https://github.com/adafruit/Adafruit_Python_SSD1306 # SMBus parts coming from BLavery's lib_oled96 repo: # https://github.com/BLavery/lib_oled96 +# I2C address, width and height import from config.toml made by NurseJackass import logging import pwnagotchi.ui.fonts as fonts from pwnagotchi.ui.hw.base import DisplayImpl +# +# Default is 128x64 display on i2c address 0x3C +# +# Configure i2c address and dimensions in config.toml: +# +# ui.display.type = "i2coled" +# ui.display.i2c_addr = 0x3C +# ui.display.width = 128 +# ui.display.height = 64 +# + class I2COled(DisplayImpl): def __init__(self, config): + self._config = config['ui']['display'] super(I2COled, self).__init__(config, 'i2coled') def layout(self): fonts.setup(8, 8, 8, 10, 10, 8) - self._layout['width'] = 128 - self._layout['height'] = 64 + self._layout['width'] = self._config['width'] if 'width' in self._config else 128 + self._layout['height'] = self._config['height'] if 'height' in self._config else 64 self._layout['face'] = (0, 30) self._layout['name'] = (0, 10) self._layout['channel'] = (72, 10) @@ -36,10 +49,14 @@ class I2COled(DisplayImpl): return self._layout def initialize(self): - logging.info("initializing 128x64 I2C Oled Display on address 0x3C") - logging.info("To change resolution or address check pwnagotchi/ui/hw/libs/i2coled/epd.py") + i2caddr = self._config['i2c_addr'] if 'i2c_addr' in self._config else 0x3C + width = self._config['width'] if 'width' in self._config else 128 + height = self._config['height'] if 'height' in self._config else 64 + + logging.info("initializing %dx%d I2C Oled Display on address 0x%X" % (width, height, i2caddr)) + from pwnagotchi.ui.hw.libs.i2coled.epd import EPD - self._display = EPD() + self._display = EPD(address=i2caddr, width=width, height=height) self._display.Init() self._display.Clear() diff --git a/pwnagotchi/ui/hw/libs/i2coled/SSD1306.py b/pwnagotchi/ui/hw/libs/i2coled/SSD1306.py index e5c02ecd..815f53cf 100644 --- a/pwnagotchi/ui/hw/libs/i2coled/SSD1306.py +++ b/pwnagotchi/ui/hw/libs/i2coled/SSD1306.py @@ -100,13 +100,19 @@ class SSD1306Base(object): """Send command byte to display.""" # I2C write. assert(len(cmd) <= 31) - self.bus.write_i2c_block_data(self.addr, self.cmd_mode, list(cmd)) + try: + self.bus.write_i2c_block_data(self.addr, self.cmd_mode, list(cmd)) + except Exception as e: + logging.exception(e) def data(self, data): """Send byte of data to display.""" # I2C write. - for i in range(0, len(data), 31): - self.bus.write_i2c_block_data(self.addr, self.data_mode, list(data[i:i+31])) + try: + for i in range(0, len(data), 31): + self.bus.write_i2c_block_data(self.addr, self.data_mode, list(data[i:i+31])) + except Exception as e: + logging.exception(e) def begin(self, vccstate=SSD1306_SWITCHCAPVCC): """Initialize display.""" @@ -116,7 +122,7 @@ class SSD1306Base(object): self._initialize() # Turn on the display. self.command(SSD1306_DISPLAYON) - + def ShowImage(self): """ The image on the "canvas" is flushed through to the hardware display. @@ -128,10 +134,12 @@ class SSD1306Base(object): self.command(SSD1306_PAGEADDR) self.command(0) # Page start address. (0 = reset) self.command(self._pages-1) # Page end address. - - for i in range(0, len(self._buffer), 16): - self.bus.write_i2c_block_data(self.addr, self.data_mode, self._buffer[i:i+16]) - + try: + for i in range(0, len(self._buffer), 16): + self.bus.write_i2c_block_data(self.addr, self.data_mode, self._buffer[i:i+16]) + except Exception as e: + logging.exception(e) + def getbuffer(self, image): """Set buffer to value of Python Imaging Library image. The image should be in 1 bit mode and a size equal to the display size. @@ -229,7 +237,7 @@ class SSD1306_128_64(SSD1306Base): class SSD1306_128_32(SSD1306Base): def __init__(self, width, height, address=None, bus=None): # Call base class constructor. - super(SSD1306_128_64, self).__init__(128, 64, address, bus) + super(SSD1306_128_32, self).__init__(128, 32, address, bus) def _initialize(self): # 128x32 pixel specific initialization. diff --git a/pwnagotchi/ui/hw/libs/i2coled/epd.py b/pwnagotchi/ui/hw/libs/i2coled/epd.py index 898aee36..c35ee8c9 100644 --- a/pwnagotchi/ui/hw/libs/i2coled/epd.py +++ b/pwnagotchi/ui/hw/libs/i2coled/epd.py @@ -8,20 +8,27 @@ EPD_HEIGHT = 64 # disp = SSD1306.SSD1306_128_32(128, 32, address=0x3C) # disp = SSD1306.SSD1306_96_16(96, 16, address=0x3C) # If you change for different resolution, you have to modify the layout in pwnagotchi/ui/hw/i2coled.py -disp = SSD1306.SSD1306_128_64(128, 64, address=0x3C) class EPD(object): - def __init__(self): - self.width = EPD_WIDTH - self.height = EPD_HEIGHT + def __init__(self, address=0x3D, width=EPD_WIDTH, height=EPD_HEIGHT): + self.width = width + self.height = height + + # choose subclass based on dimensions + if height == 32: + self.disp = SSD1306.SSD1306_128_32(width, height, address) + elif height == 16: + self.disp = SSD1306.SSD1306_96_16(width, height, address) + else: + self.disp = SSD1306.SSD1306_128_64(width, height, address) def Init(self): - disp.begin() + self.disp.begin() def Clear(self): - disp.clear() + self.disp.clear() def display(self, image): - disp.getbuffer(image) - disp.ShowImage() \ No newline at end of file + self.disp.getbuffer(image) + self.disp.ShowImage() \ No newline at end of file diff --git a/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/SSD1306.py b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/SSD1306.py index 4ca97dad..c529c48f 100644 --- a/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/SSD1306.py +++ b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/SSD1306.py @@ -106,7 +106,7 @@ class SSD1306Base(object): self._initialize() # Turn on the display. self.command(SSD1306_DISPLAYON) - + def ShowImage(self): """ The image on the "canvas" is flushed through to the hardware display. @@ -118,10 +118,10 @@ class SSD1306Base(object): self.command(SSD1306_PAGEADDR) self.command(0) # Page start address. (0 = reset) self.command(self._pages-1) # Page end address. - + for i in range(0, len(self._buffer), 16): self.bus.write_i2c_block_data(self.addr, self.data_mode, self._buffer[i:i+16]) - + def getbuffer(self, image): """Set buffer to value of Python Imaging Library image. The image should be in 1 bit mode and a size equal to the display size. diff --git a/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/ST7789vert.py b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/ST7789vert.py index f8894b98..b0f0763d 100644 --- a/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/ST7789vert.py +++ b/pwnagotchi/ui/hw/libs/waveshare/oled/oledlcd/ST7789vert.py @@ -95,11 +95,8 @@ class ST7789(object): offset_left=0, offset_top=0): """Create an instance of the display using SPI communication. - Must provide the GPIO pin number for the D/C pin and the SPI driver. - Can optionally provide the GPIO pin number for the reset pin as the rst parameter. - :param port: SPI port number :param cs: SPI chip-select number (0 or 1 for BCM :param backlight: Pin for controlling backlight @@ -109,7 +106,6 @@ class ST7789(object): :param rotation: Rotation of display connected to ST7789 :param invert: Invert display :param spi_speed_hz: SPI speed (in Hz) - """ if rotation not in [0, 90, 180, 270]: raise ValueError("Invalid rotation {}".format(rotation)) @@ -287,9 +283,7 @@ class ST7789(object): def begin(self): """Set up the display - Deprecated. Included in __init__. - """ pass @@ -326,9 +320,7 @@ class ST7789(object): def display(self, image): """Write the provided image to the hardware. - :param image: Should be RGB format and the same dimensions as the display hardware. - """ # Set address bounds to entire display. self.set_window()