RPiGPIO.makertools/RPiGPIOmakertools/drivers/OLED_SSD1306.py
2021-12-04 17:10:09 +00:00

124 lines
3.9 KiB
Python
Executable File

"""
The MIT License (MIT)
Copyright (c) 2015 Richard Hull
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 smbus2 as smbus
from PIL import Image, ImageDraw
from .I2C_DEVICE import Device
class SSD1306(Device):
"""
A device encapsulates the I2C connection (address/port) to the SSD1306
OLED display hardware. The init method pumps commands to the display
to properly initialize it. Further control commands can then be
called to affect the brightness. Direct use of the command() and
data() methods are discouraged.
"""
def __init__(self, port=1, address=0x3C):
super(SSD1306, self).__init__(port, address)
self.width = 128
self.height = 64
self.pages = round(self.height / 8)
self.command(
const.DISPLAYOFF,
const.SETDISPLAYCLOCKDIV, 0x80,
const.SETMULTIPLEX, 0x3F,
const.SETDISPLAYOFFSET, 0x00,
const.SETSTARTLINE,
const.CHARGEPUMP, 0x14,
const.MEMORYMODE, 0x00,
const.SEGREMAP,
const.COMSCANDEC,
const.SETCOMPINS, 0x12,
const.SETCONTRAST, 0xCF,
const.SETPRECHARGE, 0xF1,
const.SETVCOMDETECT, 0x40,
const.DISPLAYALLON_RESUME,
const.NORMALDISPLAY,
const.DISPLAYON)
def display(self, image):
"""
Takes a 1-bit image and dumps it to the SSD1306 OLED display.
"""
assert(image.mode == '1')
assert(image.size[0] == self.width)
assert(image.size[1] == self.height)
self.command(
const.COLUMNADDR, 0x00, self.width-1, # Column start/end address
const.PAGEADDR, 0x00, self.pages-1) # Page start/end address
pix = list(image.getdata())
step = self.width * 8
buf = []
for y in range(0, self.pages * step, step):
i = y + self.width-1
while i >= y:
byte = 0
for n in range(0, step, self.width):
byte |= (pix[i + n] & 0x01) << 8
byte >>= 1
buf.append(byte)
i -= 1
self.data(buf)
def draw(self):
self.image = Image.new('1', (self.width, self.height))
return ImageDraw.Draw(self.image)
class const:
CHARGEPUMP = 0x8D
COLUMNADDR = 0x21
COMSCANDEC = 0xC8
COMSCANINC = 0xC0
DISPLAYALLON = 0xA5
DISPLAYALLON_RESUME = 0xA4
DISPLAYOFF = 0xAE
DISPLAYON = 0xAF
EXTERNALVCC = 0x1
INVERTDISPLAY = 0xA7
MEMORYMODE = 0x20
NORMALDISPLAY = 0xA6
PAGEADDR = 0x22
SEGREMAP = 0xA0
SETCOMPINS = 0xDA
SETCONTRAST = 0x81
SETDISPLAYCLOCKDIV = 0xD5
SETDISPLAYOFFSET = 0xD3
SETHIGHCOLUMN = 0x10
SETLOWCOLUMN = 0x00
SETMULTIPLEX = 0xA8
SETPRECHARGE = 0xD9
SETSEGMENTREMAP = 0xA1
SETSTARTLINE = 0x40
SETVCOMDETECT = 0xDB
SWITCHCAPVCC = 0x2