""" 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 SH1106(Device): """ A device encapsulates the I2C connection (address/port) to the SH1106 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(SH1106, self).__init__(port, address) self.width = 128 self.height = 64 self.pages = round(self.height / 8) self.command( const.DISPLAYOFF, const.MEMORYMODE, const.SETHIGHCOLUMN, 0xB0, 0xC8, const.SETLOWCOLUMN, 0x10, 0x40, const.SETCONTRAST, 0x7F, const.SETSEGMENTREMAP, const.NORMALDISPLAY, const.SETMULTIPLEX, 0x3F, const.DISPLAYALLON_RESUME, const.SETDISPLAYOFFSET, 0x00, const.SETDISPLAYCLOCKDIV, 0xF0, const.SETPRECHARGE, 0x22, const.SETCOMPINS, 0x12, const.SETVCOMDETECT, 0x20, const.CHARGEPUMP, 0x14, const.DISPLAYON) def display(self, image): """ Takes a 1-bit image and dumps it to the SH1106 OLED display. """ assert(image.mode == '1') assert(image.size[0] == self.width) assert(image.size[1] == self.height) page = 0xB0 pix = list(image.getdata()) step = self.width * 8 for y in range(0, self.pages * step, step): # move to given page, then reset the column address self.command(page, 0x02, 0x10) page += 1 buf = [] for x in range(self.width): byte = 0 for n in range(0, step, self.width): byte |= (pix[x + y + n] & 0x01) << 8 byte >>= 1 buf.append(byte) 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