Skip to content

Commit

Permalink
A faster display method for sh1106 (#110)
Browse files Browse the repository at this point in the history
Framerates for a SPI SH1106 display running perfloop.py on a r-pi 3B based on arch linux:

Python 2.7.13, increased from 43 to 93 fps.
Python 3.6.0, increased from 29 to 66 fps.
  • Loading branch information
rm-hull committed Jan 9, 2017
2 parents 9435e36 + 8e96686 commit 31f1faf
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions oled/device.py
Expand Up @@ -149,26 +149,28 @@ def display(self, image):

image = self.preprocess(image)

page = 0xB0
pix = list(image.getdata())
step = self._w * 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._w):
byte = 0
for n in range(0, step, self._w):
bit = 1 if pix[x + y + n] > 0 else 0
byte |= bit << 8
byte >>= 1

buf.append(byte)

self.data(buf)
set_page_address = 0xB0
image_data = image.getdata()
pixels_per_page = self.width * 8
buf = bytearray(self.width)

for y in range(0, int(self._pages * pixels_per_page), pixels_per_page):
self.command(set_page_address, 0x02, 0x10)
set_page_address += 1
offsets = [y + self.width * i for i in range(8)]

for x in range(self.width):
buf[x] = \
(image_data[x + offsets[0]] and 0x01) | \
(image_data[x + offsets[1]] and 0x02) | \
(image_data[x + offsets[2]] and 0x04) | \
(image_data[x + offsets[3]] and 0x08) | \
(image_data[x + offsets[4]] and 0x10) | \
(image_data[x + offsets[5]] and 0x20) | \
(image_data[x + offsets[6]] and 0x40) | \
(image_data[x + offsets[7]] and 0x80)

self.data(list(buf))


class ssd1306(device):
Expand Down

0 comments on commit 31f1faf

Please sign in to comment.