Files
SmartHome/Pioneer600/Oled/oled.py
2023-07-02 13:11:51 +08:00

69 lines
1.5 KiB
Python

#!/usr/bin/python
# -*- coding:utf-8 -*-
import spidev as SPI
from PIL import Image,ImageDraw,ImageFont
from . import SSD1306
# Raspberry Pi pin configuration:
OLED_GPIO_RST = 19
OLED_GPIO_DC = 16
OLED_SPI_BUS = 0
OLED_SPI_CS = 0
OLED_WIDTH = 128
OLED_HEIGHT = 64
class OLED(object):
"""class for OLED."""
def __init__(self, gpio_rst=OLED_GPIO_RST, gpio_dc=OLED_GPIO_DC, spi_bus=OLED_SPI_BUS, spi_cs=OLED_SPI_CS):
self.rst = gpio_rst
self.dc = gpio_dc
self.spi = SPI.SpiDev(spi_bus, spi_cs)
self.disp = SSD1306.SSD1306(gpio_rst, gpio_dc, self.spi)
self.disp.width = OLED_WIDTH
self.disp.height = OLED_HEIGHT
disp = self.disp
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
self.image = Image.new('1', (disp.width, disp.height))
# Get drawing object to draw on image.
self.draw = ImageDraw.Draw(self.image)
def draw_rectangle(self, x, y, width, height):
disp = self.disp
draw = self.draw
image = self.image
# Draw a black filled box to clear the image.
draw.rectangle((x, y, width, height), outline=0, fill=0)
# Display image.
disp.image(image)
disp.display()
def draw_text(self, x, y, size, text):
disp = self.disp
draw = self.draw
image = self.image
# Load default font.
font = ImageFont.load_default()
# Write two lines of text.
draw.text((x, y), text, font=font, fill=255)
# Display image.
disp.image(image)
disp.display()