changeset 13:96520f002017

Docstrings and small optimizations
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Tue, 21 Jun 2016 18:01:17 +0200
parents cfdc7ce0d25b
children 8ec76a6b0661
files ht16k33_matrix.py ht16k33_seg.py
diffstat 2 files changed, 40 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/ht16k33_matrix.py	Sat Jun 11 14:27:28 2016 +0200
+++ b/ht16k33_matrix.py	Tue Jun 21 18:01:17 2016 +0200
@@ -1,48 +1,56 @@
-HT16K33_BLINK_CMD = const(0x80)
-HT16K33_BLINK_DISPLAYON = const(0x01)
-HT16K33_CMD_BRIGHTNESS = const(0xE0)
-HT16K33_OSCILATOR_ON = const(0x21)
+_HT16K33_BLINK_CMD = const(0x80)
+_HT16K33_BLINK_DISPLAYON = const(0x01)
+_HT16K33_CMD_BRIGHTNESS = const(0xE0)
+_HT16K33_OSCILATOR_ON = const(0x21)
 
 
 class HT16K33:
+    """The base class for all HT16K33-based backpacks and wings."""
+
     def __init__(self, i2c, address=0x70):
         self.i2c = i2c
         self.address = address
         self._temp = bytearray(1)
         self.buffer = bytearray(16)
         self.fill(0)
-        self._write_cmd(HT16K33_OSCILATOR_ON)
+        self._write_cmd(_HT16K33_OSCILATOR_ON)
         self.blink_rate(0)
         self.brightness(15)
 
     def _write_cmd(self, byte):
+        """Send a command."""
         self._temp[0] = byte
         self.i2c.writeto(self.address, self._temp)
 
     def blink_rate(self, rate=None):
+        """Get or set the blink rate."""
         if rate is None:
             return self._blink_rate
         rate = rate & 0x02
         self._blink_rate = rate
-        self._write_cmd(HT16K33_BLINK_CMD |
-                        HT16K33_BLINK_DISPLAYON | rate << 1)
+        self._write_cmd(_HT16K33_BLINK_CMD |
+                        _HT16K33_BLINK_DISPLAYON | rate << 1)
 
     def brightness(self, brightness):
+        """Get or set the brightness."""
         if brightness is None:
             return self._brightness
         brightness = brightness & 0x0F
         self._brightness = brightness
-        self._write_cmd(HT16K33_CMD_BRIGHTNESS | brightness)
+        self._write_cmd(_HT16K33_CMD_BRIGHTNESS | brightness)
 
     def show(self):
+        """Actually send all the changes to the device."""
         self.i2c.writeto_mem(self.address, 0x00, self.buffer)
 
     def fill(self, color):
+        """Fill the display with given color."""
         fill = 0xff if color else 0x00
         for i in range(16):
             self.buffer[i] = fill
 
     def _pixel(self, x, y, color=None):
+        """Set a single pixel in the frame buffer to specified color."""
         mask = 1 << x
         if color is None:
             return bool((self.buffer[y] | self.buffer[y + 1] << 8) & mask)
@@ -55,7 +63,10 @@
 
 
 class Matrix8x16(HT16K33):
+    """The double matrix."""
+
     def pixel(self, x, y, color=None):
+        """Set a single pixel in the frame buffer to specified color."""
         if not 0 <= x <= 15:
             return
         if not 0 <= y <= 8:
@@ -64,7 +75,10 @@
 
 
 class Matrix8x8(HT16K33):
+    """The single matrix."""
+
     def pixel(self, x, y, color=None):
+        """Set a single pixel in the frame buffer to specified color."""
         if not 0 <= x <= 8:
             return
         if not 0 <= y <= 8:
--- a/ht16k33_seg.py	Sat Jun 11 14:27:28 2016 +0200
+++ b/ht16k33_seg.py	Tue Jun 21 18:01:17 2016 +0200
@@ -1,7 +1,7 @@
 from ht16k33_matrix import HT16K33
 
 
-CHARS = [
+CHARS = (
     0b00000000, 0b00000000, #
     0b01000000, 0b00000110, # !
     0b00000010, 0b00100000, # "
@@ -98,8 +98,8 @@
     0b00100100, 0b10001001, # }
     0b00000101, 0b00100000, # ~
     0b00111111, 0b11111111,
-]
-NUMBERS = [
+)
+NUMBERS = (
     0x3F, # 0
     0x06, # 1
     0x5B, # 2
@@ -117,11 +117,14 @@
     0x79, # E
     0x71, # F
     0x40, # -
-]
+)
 
 
 class Seg14x4(HT16K33):
+    """The alpha-numeric 14-segment display."""
+
     def scroll(self, count=1):
+        """Scroll the display by specified number of places."""
         if count >= 0:
             offset = 0
         else:
@@ -130,6 +133,7 @@
             self.buffer[i + offset] = self.buffer[i + 2 * count]
 
     def put(self, char, index=0):
+        """Put a character at the specified place."""
         if not 0 <= index <= 3:
             return
         if not 32 <= ord(char) <= 127:
@@ -142,16 +146,19 @@
         self.buffer[index * 2 + 1] = CHARS[c]
 
     def push(self, char):
+        """Scroll the display and add a character at the end."""
         if char != '.' or self.buffer[7] & 0b01000000:
             self.scroll()
             self.put(' ', 3)
         self.put(char, 3)
 
     def text(self, text):
+        """Display the specified text."""
         for c in text:
             self.push(c)
 
     def number(self, number):
+        """Display the specified decimal number."""
         s = "{:f}".format(number)
         if len(s) > 4:
             if s.find('.') > 4:
@@ -163,6 +170,7 @@
         self.text(s[:places])
 
     def hex(self, number):
+        """Display the specified hexadecimal number."""
         s = "{:x}".format(number)
         if len(s) > 4:
             raise ValueError("Overflow")
@@ -171,9 +179,12 @@
 
 
 class Seg7x4(Seg14x4):
-    P = [0, 2, 6, 8]
+    """The numeric 7-segment display."""
+
+    P = [0, 2, 6, 8] #  The positions of characters.
 
     def scroll(self, count=1):
+        """Scroll the display by specified number of places."""
         if count >= 0:
             offset = 0
         else:
@@ -182,12 +193,14 @@
             self.buffer[self.P[i + offset]] = self.buffer[self.P[i + count]]
 
     def push(self, char):
+        """Scroll the display and add a character at the end."""
         if char in ':;':
             self.put(char)
         else:
             super().push(char)
 
     def put(self, char, index=0):
+        """Put a character at the specified place."""
         if not 0 <= index <= 3:
             return
         char = char.lower()