changeset 4:2674b567ac3c

Add library for 7seg and 14seg displays
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Wed, 11 May 2016 00:11:15 +0200
parents a3591e80b2c9
children 3a29c19f9fd2
files ht16k33_matrix.py ht16k33_seg.py
diffstat 2 files changed, 202 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/ht16k33_matrix.py	Thu May 05 22:56:04 2016 +0200
+++ b/ht16k33_matrix.py	Wed May 11 00:11:15 2016 +0200
@@ -4,34 +4,35 @@
 HT16K33_OSCILATOR_ON = const(0x21)
 
 
-class Matrix:
+class HT16K33:
     def __init__(self, i2c, address=0x70):
         self.i2c = i2c
         self.address = address
-        self.temp = bytearray(1)
+        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):
-        self.temp[0] = byte
-        self.i2c.writeto(self.address, self.temp)
+    def _write_cmd(self, byte):
+        self._temp[0] = byte
+        self.i2c.writeto(self.address, self._temp)
 
     def blink_rate(self, rate=None):
         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):
         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):
         self.i2c.writeto_mem(self.address, 0x00, self.buffer)
@@ -41,7 +42,7 @@
         for i in range(16):
             self.buffer[i] = fill
 
-    def pixel(self, x, y, color=None):
+    def _pixel(self, x, y, color=None):
         mask = 1 << x
         if color is None:
             return bool((self.buffer[y] | self.buffer[y + 1] << 8) & mask)
@@ -53,20 +54,20 @@
             self.buffer[y * 2 + 1] &= ~(mask >> 8)
 
 
-class Matrix8x16(Matrix):
+class Matrix8x16(HT16K33):
     def pixel(self, x, y, color=None):
         if not 0 <= x <= 15:
             return
         if not 0 <= y <= 8:
             return
-        return super().pixel(x, y, color)
+        return super()._pixel(x, y, color)
 
 
-class Matrix8x8(Matrix):
+class Matrix8x8(HT16K33):
     def pixel(self, x, y, color=None):
         if not 0 <= x <= 8:
             return
         if not 0 <= y <= 8:
             return
         x = (x - 1) % 8
-        return super().pixel(x, y, color)
+        return super()._pixel(x, y, color)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ht16k33_seg.py	Wed May 11 00:11:15 2016 +0200
@@ -0,0 +1,188 @@
+from ht16k33_matrix import HT16K33
+
+
+CHARS = [
+    0b00000000, 0b00000000, #
+    0b00000000, 0b00000110, # !
+    0b00000010, 0b00100000, # "
+    0b00010010, 0b11001110, # #
+    0b00010010, 0b11101101, # $
+    0b00001100, 0b00100100, # %
+    0b00100011, 0b01011101, # &
+    0b00000100, 0b00000000, # '
+    0b00100100, 0b00000000, # (
+    0b00001001, 0b00000000, # )
+    0b00111111, 0b11000000, # *
+    0b00010010, 0b11000000, # +
+    0b00001000, 0b00000000, # ,
+    0b00000000, 0b11000000, # -
+    0b00000000, 0b00000000, # .
+    0b00001100, 0b00000000, # /
+    0b00001100, 0b00111111, # 0
+    0b00000000, 0b00000110, # 1
+    0b00000000, 0b11011011, # 2
+    0b00000000, 0b10001111, # 3
+    0b00000000, 0b11100110, # 4
+    0b00100000, 0b01101001, # 5
+    0b00000000, 0b11111101, # 6
+    0b00000000, 0b00000111, # 7
+    0b00000000, 0b11111111, # 8
+    0b00000000, 0b11101111, # 9
+    0b00010010, 0b00000000, # :
+    0b00001010, 0b00000000, # ;
+    0b00100100, 0b00000000, # <
+    0b00000000, 0b11001000, # =
+    0b00001001, 0b00000000, # >
+    0b00010000, 0b10000011, # ?
+    0b00000010, 0b10111011, # @
+    0b00000000, 0b11110111, # A
+    0b00010010, 0b10001111, # B
+    0b00000000, 0b00111001, # C
+    0b00010010, 0b00001111, # D
+    0b00000000, 0b11111001, # E
+    0b00000000, 0b01110001, # F
+    0b00000000, 0b10111101, # G
+    0b00000000, 0b11110110, # H
+    0b00010010, 0b00000000, # I
+    0b00000000, 0b00011110, # J
+    0b00100100, 0b01110000, # K
+    0b00000000, 0b00111000, # L
+    0b00000101, 0b00110110, # M
+    0b00100001, 0b00110110, # N
+    0b00000000, 0b00111111, # O
+    0b00000000, 0b11110011, # P
+    0b00100000, 0b00111111, # Q
+    0b00100000, 0b11110011, # R
+    0b00000000, 0b11101101, # S
+    0b00010010, 0b00000001, # T
+    0b00000000, 0b00111110, # U
+    0b00001100, 0b00110000, # V
+    0b00101000, 0b00110110, # W
+    0b00101101, 0b00000000, # X
+    0b00010101, 0b00000000, # Y
+    0b00001100, 0b00001001, # Z
+    0b00000000, 0b00111001, # [
+    0b00100001, 0b00000000, #
+    0b00000000, 0b00001111, # ]
+    0b00001100, 0b00000011, # ^
+    0b00000000, 0b00001000, # _
+    0b00000001, 0b00000000, # `
+    0b00010000, 0b01011000, # a
+    0b00100000, 0b01111000, # b
+    0b00000000, 0b11011000, # c
+    0b00001000, 0b10001110, # d
+    0b00001000, 0b01011000, # e
+    0b00000000, 0b01110001, # f
+    0b00000100, 0b10001110, # g
+    0b00010000, 0b01110000, # h
+    0b00010000, 0b00000000, # i
+    0b00000000, 0b00001110, # j
+    0b00110110, 0b00000000, # k
+    0b00000000, 0b00110000, # l
+    0b00010000, 0b11010100, # m
+    0b00010000, 0b01010000, # n
+    0b00000000, 0b11011100, # o
+    0b00000001, 0b01110000, # p
+    0b00000100, 0b10000110, # q
+    0b00000000, 0b01010000, # r
+    0b00100000, 0b10001000, # s
+    0b00000000, 0b01111000, # t
+    0b00000000, 0b00011100, # u
+    0b00100000, 0b00000100, # v
+    0b00101000, 0b00010100, # w
+    0b00101000, 0b11000000, # x
+    0b00100000, 0b00001100, # y
+    0b00001000, 0b01001000, # z
+    0b00001001, 0b01001001, # {
+    0b00010010, 0b00000000, # |
+    0b00100100, 0b10001001, # }
+    0b00000101, 0b00100000, # ~
+    0b00111111, 0b11111111,
+]
+NUMBERS = [
+    0x3F, # 0
+    0x06, # 1
+    0x5B, # 2
+    0x4F, # 3
+    0x66, # 4
+    0x6D, # 5
+    0x7D, # 6
+    0x07, # 7
+    0x7F, # 8
+    0x6F, # 9
+    0x77, # a
+    0x7C, # b
+    0x39, # C
+    0x5E, # d
+    0x79, # E
+    0x71, # F
+    0x40, # -
+]
+
+
+class Seg14x4(HT16K33):
+    def scroll(self, count=1):
+        if count >= 0:
+            offset = 0
+        else:
+            offset = 2
+        for i in range(6):
+            self.buffer[i + offset] = self.buffer[i + 2 * count]
+
+    def put(self, char, index=0):
+        if not 0 <= index <= 3:
+            return
+        if not 32 <= ord(char) <= 127:
+            return
+        if char == '.':
+            self.buffer[index * 2 + 1] |= 0b01000000
+            return
+        c = ord(char) * 2 + 32
+        self.buffer[index * 2] = CHARS[1 + c]
+        self.buffer[index * 2 + 1] = CHARS[c]
+
+    def push(self, char):
+        if char != '.':
+            self.scroll()
+        self.put(char, 3)
+
+    def text(self, text):
+        for c in text:
+            self.push(c)
+
+    def number(self, number):
+        s = "{:f}".format(number)
+        self.fill(False)
+        self.text(s)
+
+    def hex(self, number):
+        s = "{:x}".format(number)
+        self.fill(False)
+        self.text(s)
+
+
+class Seg7x4(Seg14x4):
+    def scroll(self, count=1):
+        if count >= 0:
+            offset = 0
+        else:
+            offset = 1
+        for i in range(3):
+            self.buffer[i + offset] = self.buffer[i + count]
+
+    def put(self, char, index=0):
+        if not 0 <= index <= 3:
+            return
+        char = char.lower()
+        if char == '.':
+            self.buffer[index * 2 + 1] |= 0b01000000
+            return
+        elif char in 'abcdef':
+            c = ord(char) - 97 + 10
+        elif char == '-':
+            c = 16
+        elif char in '0123456789':
+            c = ord(char) - 48
+        else:
+            return
+        self.buffer[index] = NUMBERS[c]