changeset 0:a28b87ff3a97 draft

Initial commit
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Tue, 17 Oct 2017 01:24:38 +0200
parents
children 3d7d948fc7e9
files README.md sh1106_i2c.py sh1106_spi.py ssd1306_i2c.py ssd1306_spi.py
diffstat 5 files changed, 277 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Tue Oct 17 01:24:38 2017 +0200
@@ -0,0 +1,32 @@
+Examples
+********
+
+SH1106 I2C Example
+==================
+
+```python
+import sh1106_i2c
+from machine import I2C, Pin
+
+i2c = I2C(-1, sda=Pin(4), scl=Pin(5), freq=400000)
+d = sh1106_i2c.Display(i2c)
+d.fb.fill(1)
+d.fb.pixel(64, 32, 0)
+d.update()
+```
+
+SH1106 SPI Example
+==================
+
+```python
+import sh1106_spi
+from machine import SPI, Pin
+
+spi = SPI(1, baudrate=20000000)
+dc = Pin(12, Pin.OUT)
+cs = Pin(15, Pin.OUT)
+d = sh1106_spi.Display(spi, dc, cs)
+d.fb.fill(1)
+d.fb.pixel(64, 32, 0)
+d.update()
+```
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sh1106_i2c.py	Tue Oct 17 01:24:38 2017 +0200
@@ -0,0 +1,53 @@
+import framebuf
+
+
+class Display:
+    _byte = bytearray(1)
+    _word = bytearray(2)
+
+    def __init__(self, i2c, address=0x3c, width=128, height=64):
+        self._i2c = i2c
+        self._address = address
+        self.width = width
+        self.height = height
+
+        self._command = bytearray(b'\xb0\x02\x10')
+        if width > 128:
+            self._command[1] = 0x00
+        self._i2c.writeto_mem(self._address, 0x00, b'\xae\xd5\x80\xa8\x3f\xd3'
+                              b'\x00\x40\x80\x14\x20\x00\xc0\xa0\xda\x12'
+                              b'\x81\xcf\xd9\xf1\xdb\x40\xa4\xa6\xaf')
+
+        buffer = bytearray(1024)
+        self.fb = framebuf.FrameBuffer(buffer, width, height, framebuf.MVLSB)
+        self._buffer = memoryview(buffer)
+
+    def active(self, val):
+        self._i2c.writeto_mem(self._address, 0x00, b'\xaf' if val else b'\xae')
+
+    def inverse(self, val):
+        self._i2c.writeto_mem(self._address, 0x00, b'\xa7' if val else b'\xa6')
+
+    def vscroll(self, dy):
+        self._byte[0] = 0x40 | dy & 0x3f
+        self._i2c.writeto_mem(self._address, 0x00, self._byte)
+
+    def flip(self, val):
+        self._i2c.writeto_mem(self._address, 0x00, b'\xc0' if val else b'\xc8')
+
+    def mirror(self, val):
+        self._i2c.writeto_mem(self._address, 0x00, b'\xa0' if val else b'\xa1')
+
+    def contrast(self, val):
+        self._word[0] = 0x81
+        self._word[1] = val & 0xff
+        self._i2c.writeto_mem(self._address, 0x00, self._word)
+
+    def update(self):
+        index = 0
+        for page in range(self.height // 8):
+            self._command[0] = 0xb0 | page
+            self._i2c.writeto_mem(self._address, 0x00, self._command)
+            self._i2c.writeto_mem(self._address, 0x40,
+                                  self._buffer[index:index + self.width])
+            index += self.width
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sh1106_spi.py	Tue Oct 17 01:24:38 2017 +0200
@@ -0,0 +1,63 @@
+import framebuf
+
+
+class Display:
+    _byte = bytearray(1)
+    _word = bytearray(2)
+
+    def __init__(self, spi, dc, cs=None, width=128, height=64):
+        self._spi = spi
+        self._cs = cs or (lambda x: x)
+        self._dc = dc
+        self.width = width
+        self.height = height
+
+        self._command = bytearray(b'\xb0\x02\x10')
+        if width > 128:
+            self._command[1] = 0x00
+        self._write(b'\xae\xd5\x80\xa8\x3f\xd3'
+                    b'\x00\x40\x80\x14\x20\x00\xc0\xa0\xda\x12'
+                    b'\x81\xcf\xd9\xf1\xdb\x40\xa4\xa6\xaf')
+
+        buffer = bytearray(1024)
+        self.fb = framebuf.FrameBuffer(buffer, width, height, framebuf.MVLSB)
+        self._buffer = memoryview(buffer)
+
+    def _write(self, data, command=True):
+        self._dc(not command)
+        self._cs(0)
+        self._spi.write(data)
+        self._cs(1)
+
+    def active(self, val):
+        self._write(b'\xaf' if val else b'\xae')
+
+    def inverse(self, val):
+        self._write(b'\xa7' if val else b'\xa6')
+
+    def vscroll(self, dy):
+        self._byte[0] = 0x40 | dy & 0x3f
+        self._write(self._byte)
+
+    def flip(self, val):
+        self._write(b'\xc0' if val else b'\xc8')
+
+    def mirror(self, val):
+        self._write(b'\xa0' if val else b'\xa1')
+
+    def contrast(self, val):
+        self._word[0] = 0x81
+        self._word[1] = val & 0xff
+        self._write(self._word)
+
+    def update(self):
+        self._cs(0)
+        index = 0
+        for page in range(self.height // 8):
+            self._command[0] = 0xb0 | page
+            self._dc(0)
+            self._spi.write(self._command)
+            self._dc(1)
+            self._spi.write(self._buffer[index:index + self.width])
+            index += self.width
+        self._cs(1)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ssd1306_i2c.py	Tue Oct 17 01:24:38 2017 +0200
@@ -0,0 +1,59 @@
+import framebuf
+
+
+class Display:
+    _byte = bytearray(1)
+    _word = bytearray(2)
+
+    def __init__(self, i2c, address=0x3c, width=128, height=64):
+        self._i2c = i2c
+        self._address = address
+        self.width = width
+        self.height = height
+        pages = height // 8
+
+        self._command = bytearray(b'\x21\x00\x7f\x22\x00\x0f')
+        self._command[2] = width - 1
+        if width == 64:
+            self._command[1] += 32
+            self._command[2] += 32
+        self._command[5] = pages - 1
+        self._i2c.writeto_mem(self._address, 0x00, b'\xae\x20\x00\x40\x00\xa1'
+                              b'\xc8\xd3\x00\xd5\x80\xd9\xf1\xdb\x30\x8d\x14'
+                              b'\x81\xff\xa4\xa6')
+        self._word[0] = 0xa8
+        self._word[1] = height - 1
+        self._i2c.writeto_mem(self._address, 0x00, self._word)
+        self._word[0] = 0xda
+        self._word[1] = 0x02 if height == 32 else 0x12
+        self._i2c.writeto_mem(self._address, 0x00, self._word)
+        self.active(True)
+
+        buffer = bytearray(width * pages)
+        self.fb = framebuf.FrameBuffer(buffer, width, height, framebuf.MVLSB)
+        self._buffer = buffer
+
+    def active(self, val):
+        self._i2c.writeto_mem(self._address, 0x00, b'\xaf' if val else b'\xae')
+
+    def inverse(self, val):
+        self._i2c.writeto_mem(self._address, 0x00, b'\xa7' if val else b'\xa6')
+
+    def vscroll(self, dy):
+        self._byte[0] = 0x40 | dy & 0x3f
+        self._i2c.writeto_mem(self._address, 0x00, self._byte)
+
+    def flip(self, val):
+        self._i2c.writeto_mem(self._address, 0x00, b'\xc0' if val else b'\xc8')
+
+    def mirror(self, val):
+        self._i2c.writeto_mem(self._address, 0x00, b'\xa0' if val else b'\xa1')
+
+    def contrast(self, val):
+        self._word[0] = 0x81
+        self._word[1] = val & 0xff
+        self._i2c.writeto_mem(self._address, 0x00, self._word)
+
+    def update(self):
+        self._i2c.writeto_mem(self._address, 0x00, self._command)
+        self._i2c.writeto_mem(self._address, 0x40, self._buffer)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ssd1306_spi.py	Tue Oct 17 01:24:38 2017 +0200
@@ -0,0 +1,70 @@
+import framebuf
+
+
+class Display:
+    _byte = bytearray(1)
+    _word = bytearray(2)
+
+    def __init__(self, spi, dc, cs=None, width=128, height=64):
+        self._spi = spi
+        self._cs = cs or (lambda x: x)
+        self._dc = dc
+        self.width = width
+        self.height = height
+        pages = height // 8
+
+        self._command = bytearray(b'\x21\x00\x7f\x22\x00\x0f')
+        self._command[2] = width - 1
+        if width == 64:
+            self._command[1] += 32
+            self._command[2] += 32
+        self._command[5] = pages - 1
+        self._write(b'\xae\x20\x00\x40\x00\xa1'
+                    b'\xc8\xd3\x00\xd5\x80\xd9\xf1\xdb\x30\x8d\x14'
+                    b'\x81\xff\xa4\xa6')
+        self._word[0] = 0xa8
+        self._word[1] = height - 1
+        self._write(self._word)
+        self._word[0] = 0xda
+        self._word[1] = 0x02 if height == 32 else 0x12
+        self._write(self._word)
+        self.active(True)
+
+        buffer = bytearray(width * pages)
+        self.fb = framebuf.FrameBuffer(buffer, width, height, framebuf.MVLSB)
+        self._buffer = buffer
+
+    def _write(self, data, command=True):
+        self._dc(not command)
+        self._cs(0)
+        self._spi.write(data)
+        self._cs(1)
+
+    def active(self, val):
+        self._write(b'\xaf' if val else b'\xae')
+
+    def inverse(self, val):
+        self._write(b'\xa7' if val else b'\xa6')
+
+    def vscroll(self, dy):
+        self._byte[0] = 0x40 | dy & 0x3f
+        self._write(self._byte)
+
+    def flip(self, val):
+        self._write(b'\xc0' if val else b'\xc8')
+
+    def mirror(self, val):
+        self._write(b'\xa0' if val else b'\xa1')
+
+    def contrast(self, val):
+        self._word[0] = 0x81
+        self._word[1] = val & 0xff
+        self._write(self._word)
+
+    def update(self):
+        self._cs(0)
+        self._dc(0)
+        self._spi.write(self._command)
+        self._dc(1)
+        self._spi.write(self._buffer)
+        self._cs(1)