Migrated OLED_SH1106_SSD1306.py to python3

This commit is contained in:
Benjamin Burkhardt 2021-12-04 16:21:05 +00:00
parent eba3bef12a
commit 707f6015ae

View File

@ -50,7 +50,7 @@
# As before, as soon as the with block completes, the canvas buffer is flushed # As before, as soon as the with block completes, the canvas buffer is flushed
# to the device # to the device
import smbus2 import smbus2 as smbus
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
@ -79,7 +79,7 @@ class device(object):
device - maximum allowed in one transaction is 32 bytes, so if device - maximum allowed in one transaction is 32 bytes, so if
data is larger than this it is sent in chunks. data is larger than this it is sent in chunks.
""" """
for i in xrange(0, len(data), 32): for i in range(0, len(data), 32):
self.bus.write_i2c_block_data(self.addr, self.bus.write_i2c_block_data(self.addr,
self.data_mode, self.data_mode,
list(data[i:i+32])) list(data[i:i+32]))
@ -129,16 +129,16 @@ class sh1106(device):
page = 0xB0 page = 0xB0
pix = list(image.getdata()) pix = list(image.getdata())
step = self.width * 8 step = self.width * 8
for y in xrange(0, self.pages * step, step): for y in range(0, self.pages * step, step):
# move to given page, then reset the column address # move to given page, then reset the column address
self.command(page, 0x02, 0x10) self.command(page, 0x02, 0x10)
page += 1 page += 1
buf = [] buf = []
for x in xrange(self.width): for x in range(self.width):
byte = 0 byte = 0
for n in xrange(0, step, self.width): for n in range(0, step, self.width):
byte |= (pix[x + y + n] & 0x01) << 8 byte |= (pix[x + y + n] & 0x01) << 8
byte >>= 1 byte >>= 1
@ -194,11 +194,11 @@ class ssd1306(device):
pix = list(image.getdata()) pix = list(image.getdata())
step = self.width * 8 step = self.width * 8
buf = [] buf = []
for y in xrange(0, self.pages * step, step): for y in range(0, self.pages * step, step):
i = y + self.width-1 i = y + self.width-1
while i >= y: while i >= y:
byte = 0 byte = 0
for n in xrange(0, step, self.width): for n in range(0, step, self.width):
byte |= (pix[i + n] & 0x01) << 8 byte |= (pix[i + n] & 0x01) << 8
byte >>= 1 byte >>= 1