changeset 5:5bc720d5b3df draft

Simplify the pcd8544 driver
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Thu, 22 Feb 2018 10:54:31 +0100
parents 1871e8d08220
children a1bf7bed1be2
files pcd8544_spi.py
diffstat 1 files changed, 17 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/pcd8544_spi.py	Thu Feb 22 01:14:15 2018 +0100
+++ b/pcd8544_spi.py	Thu Feb 22 10:54:31 2018 +0100
@@ -5,56 +5,52 @@
 class Display:
     width = 84
     height = 48
-    _command = bytearray((0x40, 0x80))
 
     def __init__(self, spi, dc, rst=None, cs=None):
         self._spi = spi
         self._cs = cs or (lambda x: x)
         self._rst = rst or (lambda x: x)
         self._dc = dc
-        self._fn = bytearray([0x20])
+        self._fn = 0x20
         buffer = bytearray(504)
-        self.fb = framebuf.FrameBuffer(buffer, 84, 48, framebuf.MVLSB)
+        self.fb = framebuf.FrameBuffer(buffer, 84, 48, framebuf.MONO_VLSB)
         self._buffer = memoryview(buffer)
 
         self.reset()
-        self.contrast()
 
-    def _write(self, data, command=True):
-        self._dc(not command)
+    def _command(self, command):
+        self._dc(0)
         self._cs(0)
-        self._spi.write(data)
+        self._spi.write(command)
         self._cs(1)
 
     def reset(self):
         self._rst(0)
         time.sleep_us(100)
         self._rst(1)
+        time.sleep_us(100)
+        self.contrast()
+        self.inverse(0)
 
     def active(self, val):
         if val:
-            self._fn[0] &= ~0x04
+            self._fn &= ~0x04
         else:
-            self._fn[0] |= 0x04
-        self._write(self._fn)
+            self._fn |= 0x04
+        self._command(bytes((self._fn,)))
 
     def inverse(self, val):
-        self._write(b'\x0d' if val else b'\x0c')
+        self._command(b'\x0d' if val else b'\x0c')
 
     def contrast(self, val=63, bias=20, temp=6):
         if not (0 <= val <= 127 and 16 <= bias <= 23 and 4 <= temp <= 7):
             raise ValueError()
-        for c in (self._fn[0] | 0x01, temp, bias, 0x80 | val, self._fn[0]):
-            self._write(bytes((c,)))
+        self._command(bytes((self._fn | 1, temp, bias, 0x80 | val, self._fn)))
 
     def update(self):
+        self._dc(0)
         self._cs(0)
-        index = 0
-        for page in range(6):
-            self._command[0] = 0x40 | page
-            self._dc(0)
-            self._spi.write(self._command)
-            self._dc(1)
-            self._spi.write(self._buffer[index:index + 84])
-            index += self.width
+        self._spi.write(b'\x40\x80')
+        self._dc(1)
+        self._spi.write(self._buffer)
         self._cs(1)