# The MIT License (MIT)
#
# Copyright (c) 2016 <NAME> & <NAME> for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
Matrix Displays
================

"""

from ht16k33.ht16k33 import HT16K33

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"

 # Font table 0x20 ~ 0x7f
FontTable = [
    [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], #  
    [0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x00], # !
    [0x00, 0x00, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00], # "
    [0x00, 0x0a, 0x1f, 0x0a, 0x1f, 0x0a, 0x00, 0x00], # #
    [0x00, 0x0e, 0x0a, 0x1f, 0x0a, 0x0b, 0x00, 0x00], # $
    [0x00, 0x11, 0x02, 0x04, 0x08, 0x11, 0x00, 0x00], # %
    [0x00, 0x0a, 0x15, 0x15, 0x02, 0x05, 0x00, 0x00], # &
    [0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], # '
    [0x00, 0x00, 0x0e, 0x11, 0x00, 0x00, 0x00, 0x00], # (
    [0x00, 0x00, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00], # )
    [0x00, 0x00, 0x04, 0x0e, 0x04, 0x00, 0x00, 0x00], # *
    [0x00, 0x04, 0x04, 0x1f, 0x04, 0x04, 0x00, 0x00], # +
    [0x00, 0x00, 0x01, 0x06, 0x00, 0x00, 0x00, 0x00], # ,
    [0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00], # -
    [0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00], # .
    [0x00, 0x00, 0x03, 0x04, 0x18, 0x00, 0x00, 0x00], # /
    [0x00, 0x1f, 0x13, 0x15, 0x19, 0x1f, 0x00, 0x00], # 0
    [0x00, 0x09, 0x1f, 0x01, 0x00, 0x00, 0x00, 0x00], # 1
    [0x00, 0x13, 0x15, 0x15, 0x15, 0x09, 0x00, 0x00], # 2
    [0x00, 0x11, 0x15, 0x15, 0x15, 0x1b, 0x00, 0x00], # 3
    [0x00, 0x1e, 0x02, 0x07, 0x02, 0x02, 0x00, 0x00], # 4
    [0x00, 0x1d, 0x15, 0x15, 0x15, 0x12, 0x00, 0x00], # 5
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x17, 0x00, 0x00], # 6
    [0x00, 0x10, 0x10, 0x13, 0x14, 0x18, 0x00, 0x00], # 7
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x1f, 0x00, 0x00], # 8
    [0x00, 0x1d, 0x15, 0x15, 0x15, 0x1f, 0x00, 0x00], # 9
    [0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00], # :
    [0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00], # ;
    [0x00, 0x00, 0x04, 0x0a, 0x11, 0x00, 0x00, 0x00], # <
    [0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x00, 0x00], # =
    [0x00, 0x00, 0x11, 0x0a, 0x04, 0x00, 0x00, 0x00], # >
    [0x00, 0x08, 0x10, 0x15, 0x08, 0x00, 0x00, 0x00], # ?
    [0x00, 0x1f, 0x11, 0x15, 0x15, 0x1d, 0x00, 0x00], # @
    [0x00, 0x1f, 0x12, 0x12, 0x12, 0x1f, 0x00, 0x00], # A
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x1b, 0x00, 0x00], # B
    [0x00, 0x1f, 0x11, 0x11, 0x11, 0x11, 0x00, 0x00], # C
    [0x00, 0x1f, 0x11, 0x11, 0x11, 0x0e, 0x00, 0x00], # D
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x11, 0x00, 0x00], # E
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x10, 0x00, 0x00], # F
    [0x00, 0x1f, 0x11, 0x11, 0x15, 0x17, 0x00, 0x00], # G
    [0x00, 0x1f, 0x04, 0x04, 0x04, 0x1f, 0x00, 0x00], # H
    [0x00, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x00, 0x00], # I
    [0x00, 0x03, 0x01, 0x01, 0x11, 0x1f, 0x00, 0x00], # J
    [0x00, 0x1f, 0x04, 0x04, 0x0a, 0x11, 0x00, 0x00], # K
    [0x00, 0x1f, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00], # L
    [0x00, 0x1f, 0x08, 0x04, 0x08, 0x1f, 0x00, 0x00], # M
    [0x00, 0x1f, 0x08, 0x04, 0x02, 0x1f, 0x00, 0x00], # N
    [0x00, 0x0e, 0x11, 0x11, 0x11, 0x0e, 0x00, 0x00], # O
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x08, 0x00, 0x00], # P
    [0x00, 0x1e, 0x12, 0x13, 0x12, 0x1e, 0x00, 0x00], # Q
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x0b, 0x00, 0x00], # R
    [0x00, 0x1d, 0x15, 0x15, 0x15, 0x17, 0x00, 0x00], # S
    [0x00, 0x10, 0x10, 0x1f, 0x10, 0x10, 0x00, 0x00], # T
    [0x00, 0x1f, 0x01, 0x01, 0x01, 0x1f, 0x00, 0x00], # U
    [0x00, 0x18, 0x06, 0x01, 0x06, 0x18, 0x00, 0x00], # V
    [0x00, 0x1e, 0x01, 0x06, 0x01, 0x1e, 0x00, 0x00], # W
    [0x00, 0x11, 0x0a, 0x04, 0x0a, 0x11, 0x00, 0x00], # X
    [0x00, 0x18, 0x04, 0x03, 0x04, 0x18, 0x00, 0x00], # Y
    [0x00, 0x11, 0x13, 0x15, 0x19, 0x11, 0x00, 0x00], # Z
    [0x00, 0x00, 0x1f, 0x11, 0x11, 0x00, 0x00, 0x00], # [
    [0x00, 0x00, 0x18, 0x04, 0x03, 0x00, 0x00, 0x00], # \
    [0x00, 0x11, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00], # ]
    [0x00, 0x00, 0x08, 0x10, 0x08, 0x00, 0x00, 0x00], # ^
    [0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00], # _
    [0x00, 0x10, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00], # `
    [0x00, 0x1f, 0x12, 0x12, 0x12, 0x1f, 0x00, 0x00], # a
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x1b, 0x00, 0x00], # b
    [0x00, 0x1f, 0x11, 0x11, 0x11, 0x11, 0x00, 0x00], # c
    [0x00, 0x1f, 0x11, 0x11, 0x11, 0x0e, 0x00, 0x00], # d
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x11, 0x00, 0x00], # e
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x10, 0x00, 0x00], # f
    [0x00, 0x1f, 0x11, 0x11, 0x15, 0x17, 0x00, 0x00], # g
    [0x00, 0x1f, 0x04, 0x04, 0x04, 0x1f, 0x00, 0x00], # h
    [0x00, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x00, 0x00], # i
    [0x00, 0x03, 0x01, 0x01, 0x11, 0x1f, 0x00, 0x00], # j
    [0x00, 0x1f, 0x04, 0x04, 0x0a, 0x11, 0x00, 0x00], # k
    [0x00, 0x1f, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00], # l
    [0x00, 0x1f, 0x08, 0x04, 0x08, 0x1f, 0x00, 0x00], # m
    [0x00, 0x1f, 0x08, 0x04, 0x02, 0x1f, 0x00, 0x00], # n
    [0x00, 0x0e, 0x11, 0x11, 0x11, 0x0e, 0x00, 0x00], # o
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x08, 0x00, 0x00], # p
    [0x00, 0x1e, 0x12, 0x13, 0x12, 0x1e, 0x00, 0x00], # q
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x0b, 0x00, 0x00], # r
    [0x00, 0x1d, 0x15, 0x15, 0x15, 0x17, 0x00, 0x00], # s
    [0x00, 0x10, 0x10, 0x1f, 0x10, 0x10, 0x00, 0x00], # t
    [0x00, 0x1f, 0x01, 0x01, 0x01, 0x1f, 0x00, 0x00], # u
    [0x00, 0x18, 0x06, 0x01, 0x06, 0x18, 0x00, 0x00], # v
    [0x00, 0x1e, 0x01, 0x06, 0x01, 0x1e, 0x00, 0x00], # w
    [0x00, 0x11, 0x0a, 0x04, 0x0a, 0x11, 0x00, 0x00], # x
    [0x00, 0x18, 0x04, 0x03, 0x04, 0x18, 0x00, 0x00], # y
    [0x00, 0x11, 0x13, 0x15, 0x19, 0x11, 0x00, 0x00], # z
    [0x00, 0x00, 0x04, 0x1f, 0x11, 0x00, 0x00, 0x00], # {
    [0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00], # |
    [0x00, 0x00, 0x11, 0x1f, 0x04, 0x00, 0x00, 0x00], # }
    [0x00, 0x04, 0x08, 0x04, 0x08, 0x00, 0x00, 0x00], # ~
    [0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00], #
]


def getFontData(char):
    value = ord(char)
    if not 0x20 <= value <= 0x7f:
        return None
    return FontTable[value-0x20]


class Matrix16x8(HT16K33):
    """A double matrix or the matrix wing."""
    def pixel(self, x, y, color=None):
        """Get or set the color of a given pixel."""
        if not 0 <= x <= 15:
            return None
        if not 0 <= y <= 7:
            return None
        if x >= 8:
            x -= 8
            y += 8
        return super()._pixel(y, x, color)

    def __getitem__(self, key):
        x, y = key
        return self.pixel(x, y)

    def __setitem__(self, key, value):
        x, y = key
        self.pixel(x, y, value)

    def putChar(self, left, right):
        total_buff = bytearray(16)
        left_buff  = getFontData(left)
        right_buff = getFontData(right)
        for i in range(0, 8):
            total_buff[i*2]   = right_buff[i]
            total_buff[i*2+1] = left_buff[i]
        self.set_buffer(total_buff)


class Matrix8x8(HT16K33):
    """A single matrix."""
    def pixel(self, x, y, color=None):
        """Get or set the color of a given pixel."""
        if not 0 <= x <= 7:
            return None
        if not 0 <= y <= 7:
            return None
        x = (x - 1) % 8
        return super()._pixel(x, y, color)

    def __getitem__(self, key):
        x, y = key
        return self.pixel(x, y)

    def __setitem__(self, key, value):
        x, y = key
        self.pixel(x, y, value)

class Matrix8x8x2(HT16K33):
    """A bi-color matrix."""
    def pixel(self, x, y, color=None):
        """Get or set the color of a given pixel."""
        if not 0 <= x <= 7:
            return None
        if not 0 <= y <= 7:
            return None
        if color is not None:
            super()._pixel(y, x, (color & 0x01))
            super()._pixel(y + 8, x, (color >> 1) & 0x01)
        else:
            return super()._pixel(y, x) | super()._pixel(y + 8, x) << 1
        return None

    def __getitem__(self, key):
        x, y = key
        return self.pixel(x, y)

    def __setitem__(self, key, value):
        x, y = key
        self.pixel(x, y, value)

    def fill(self, color):
        """Fill the whole display with the given color."""
        fill1 = 0xff if color & 0x01 else 0x00
        fill2 = 0xff if color & 0x02 else 0x00
        for i in range(8):
            self._set_buffer(i * 2, fill1)
            self._set_buffer(i * 2 + 1, fill2)
        if self._auto_write:
            self.show()

# The MIT License (MIT)
#
# Copyright (c) 2016 <NAME> & <NAME> for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
Matrix Displays
================

"""

from ht16k33.ht16k33 import HT16K33

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"

 # Font table 0x20 ~ 0x7f
FontTable = [
    [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], #  
    [0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x00], # !
    [0x00, 0x00, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00], # "
    [0x00, 0x0a, 0x1f, 0x0a, 0x1f, 0x0a, 0x00, 0x00], # #
    [0x00, 0x0e, 0x0a, 0x1f, 0x0a, 0x0b, 0x00, 0x00], # $
    [0x00, 0x11, 0x02, 0x04, 0x08, 0x11, 0x00, 0x00], # %
    [0x00, 0x0a, 0x15, 0x15, 0x02, 0x05, 0x00, 0x00], # &
    [0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], # '
    [0x00, 0x00, 0x0e, 0x11, 0x00, 0x00, 0x00, 0x00], # (
    [0x00, 0x00, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00], # )
    [0x00, 0x00, 0x04, 0x0e, 0x04, 0x00, 0x00, 0x00], # *
    [0x00, 0x04, 0x04, 0x1f, 0x04, 0x04, 0x00, 0x00], # +
    [0x00, 0x00, 0x01, 0x06, 0x00, 0x00, 0x00, 0x00], # ,
    [0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00], # -
    [0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00], # .
    [0x00, 0x00, 0x03, 0x04, 0x18, 0x00, 0x00, 0x00], # /
    [0x00, 0x1f, 0x13, 0x15, 0x19, 0x1f, 0x00, 0x00], # 0
    [0x00, 0x09, 0x1f, 0x01, 0x00, 0x00, 0x00, 0x00], # 1
    [0x00, 0x13, 0x15, 0x15, 0x15, 0x09, 0x00, 0x00], # 2
    [0x00, 0x11, 0x15, 0x15, 0x15, 0x1b, 0x00, 0x00], # 3
    [0x00, 0x1e, 0x02, 0x07, 0x02, 0x02, 0x00, 0x00], # 4
    [0x00, 0x1d, 0x15, 0x15, 0x15, 0x12, 0x00, 0x00], # 5
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x17, 0x00, 0x00], # 6
    [0x00, 0x10, 0x10, 0x13, 0x14, 0x18, 0x00, 0x00], # 7
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x1f, 0x00, 0x00], # 8
    [0x00, 0x1d, 0x15, 0x15, 0x15, 0x1f, 0x00, 0x00], # 9
    [0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00], # :
    [0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00], # ;
    [0x00, 0x00, 0x04, 0x0a, 0x11, 0x00, 0x00, 0x00], # <
    [0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x00, 0x00], # =
    [0x00, 0x00, 0x11, 0x0a, 0x04, 0x00, 0x00, 0x00], # >
    [0x00, 0x08, 0x10, 0x15, 0x08, 0x00, 0x00, 0x00], # ?
    [0x00, 0x1f, 0x11, 0x15, 0x15, 0x1d, 0x00, 0x00], # @
    [0x00, 0x1f, 0x12, 0x12, 0x12, 0x1f, 0x00, 0x00], # A
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x1b, 0x00, 0x00], # B
    [0x00, 0x1f, 0x11, 0x11, 0x11, 0x11, 0x00, 0x00], # C
    [0x00, 0x1f, 0x11, 0x11, 0x11, 0x0e, 0x00, 0x00], # D
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x11, 0x00, 0x00], # E
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x10, 0x00, 0x00], # F
    [0x00, 0x1f, 0x11, 0x11, 0x15, 0x17, 0x00, 0x00], # G
    [0x00, 0x1f, 0x04, 0x04, 0x04, 0x1f, 0x00, 0x00], # H
    [0x00, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x00, 0x00], # I
    [0x00, 0x03, 0x01, 0x01, 0x11, 0x1f, 0x00, 0x00], # J
    [0x00, 0x1f, 0x04, 0x04, 0x0a, 0x11, 0x00, 0x00], # K
    [0x00, 0x1f, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00], # L
    [0x00, 0x1f, 0x08, 0x04, 0x08, 0x1f, 0x00, 0x00], # M
    [0x00, 0x1f, 0x08, 0x04, 0x02, 0x1f, 0x00, 0x00], # N
    [0x00, 0x0e, 0x11, 0x11, 0x11, 0x0e, 0x00, 0x00], # O
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x08, 0x00, 0x00], # P
    [0x00, 0x1e, 0x12, 0x13, 0x12, 0x1e, 0x00, 0x00], # Q
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x0b, 0x00, 0x00], # R
    [0x00, 0x1d, 0x15, 0x15, 0x15, 0x17, 0x00, 0x00], # S
    [0x00, 0x10, 0x10, 0x1f, 0x10, 0x10, 0x00, 0x00], # T
    [0x00, 0x1f, 0x01, 0x01, 0x01, 0x1f, 0x00, 0x00], # U
    [0x00, 0x18, 0x06, 0x01, 0x06, 0x18, 0x00, 0x00], # V
    [0x00, 0x1e, 0x01, 0x06, 0x01, 0x1e, 0x00, 0x00], # W
    [0x00, 0x11, 0x0a, 0x04, 0x0a, 0x11, 0x00, 0x00], # X
    [0x00, 0x18, 0x04, 0x03, 0x04, 0x18, 0x00, 0x00], # Y
    [0x00, 0x11, 0x13, 0x15, 0x19, 0x11, 0x00, 0x00], # Z
    [0x00, 0x00, 0x1f, 0x11, 0x11, 0x00, 0x00, 0x00], # [
    [0x00, 0x00, 0x18, 0x04, 0x03, 0x00, 0x00, 0x00], # \
    [0x00, 0x11, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00], # ]
    [0x00, 0x00, 0x08, 0x10, 0x08, 0x00, 0x00, 0x00], # ^
    [0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00], # _
    [0x00, 0x10, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00], # `
    [0x00, 0x1f, 0x12, 0x12, 0x12, 0x1f, 0x00, 0x00], # a
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x1b, 0x00, 0x00], # b
    [0x00, 0x1f, 0x11, 0x11, 0x11, 0x11, 0x00, 0x00], # c
    [0x00, 0x1f, 0x11, 0x11, 0x11, 0x0e, 0x00, 0x00], # d
    [0x00, 0x1f, 0x15, 0x15, 0x15, 0x11, 0x00, 0x00], # e
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x10, 0x00, 0x00], # f
    [0x00, 0x1f, 0x11, 0x11, 0x15, 0x17, 0x00, 0x00], # g
    [0x00, 0x1f, 0x04, 0x04, 0x04, 0x1f, 0x00, 0x00], # h
    [0x00, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x00, 0x00], # i
    [0x00, 0x03, 0x01, 0x01, 0x11, 0x1f, 0x00, 0x00], # j
    [0x00, 0x1f, 0x04, 0x04, 0x0a, 0x11, 0x00, 0x00], # k
    [0x00, 0x1f, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00], # l
    [0x00, 0x1f, 0x08, 0x04, 0x08, 0x1f, 0x00, 0x00], # m
    [0x00, 0x1f, 0x08, 0x04, 0x02, 0x1f, 0x00, 0x00], # n
    [0x00, 0x0e, 0x11, 0x11, 0x11, 0x0e, 0x00, 0x00], # o
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x08, 0x00, 0x00], # p
    [0x00, 0x1e, 0x12, 0x13, 0x12, 0x1e, 0x00, 0x00], # q
    [0x00, 0x1f, 0x14, 0x14, 0x14, 0x0b, 0x00, 0x00], # r
    [0x00, 0x1d, 0x15, 0x15, 0x15, 0x17, 0x00, 0x00], # s
    [0x00, 0x10, 0x10, 0x1f, 0x10, 0x10, 0x00, 0x00], # t
    [0x00, 0x1f, 0x01, 0x01, 0x01, 0x1f, 0x00, 0x00], # u
    [0x00, 0x18, 0x06, 0x01, 0x06, 0x18, 0x00, 0x00], # v
    [0x00, 0x1e, 0x01, 0x06, 0x01, 0x1e, 0x00, 0x00], # w
    [0x00, 0x11, 0x0a, 0x04, 0x0a, 0x11, 0x00, 0x00], # x
    [0x00, 0x18, 0x04, 0x03, 0x04, 0x18, 0x00, 0x00], # y
    [0x00, 0x11, 0x13, 0x15, 0x19, 0x11, 0x00, 0x00], # z
    [0x00, 0x00, 0x04, 0x1f, 0x11, 0x00, 0x00, 0x00], # {
    [0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00], # |
    [0x00, 0x00, 0x11, 0x1f, 0x04, 0x00, 0x00, 0x00], # }
    [0x00, 0x04, 0x08, 0x04, 0x08, 0x00, 0x00, 0x00], # ~
    [0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00], #
]


def getFontData(char):
    value = ord(char)
    if not 0x20 <= value <= 0x7f:
        return None
    return FontTable[value-0x20]


class Matrix16x8(HT16K33):
    """A double matrix or the matrix wing."""
    def pixel(self, x, y, color=None):
        """Get or set the color of a given pixel."""
        if not 0 <= x <= 15:
            return None
        if not 0 <= y <= 7:
            return None
        if x >= 8:
            x -= 8
            y += 8
        return super()._pixel(y, x, color)

    def __getitem__(self, key):
        x, y = key
        return self.pixel(x, y)

    def __setitem__(self, key, value):
        x, y = key
        self.pixel(x, y, value)

    def putChar(self, left, right):
        total_buff = bytearray(16)
        left_buff  = getFontData(left)
        right_buff = getFontData(right)
        for i in range(0, 8):
            total_buff[i*2]   = right_buff[i]
            total_buff[i*2+1] = left_buff[i]
        self.set_buffer(total_buff)


class Matrix8x8(HT16K33):
    """A single matrix."""
    def pixel(self, x, y, color=None):
        """Get or set the color of a given pixel."""
        if not 0 <= x <= 7:
            return None
        if not 0 <= y <= 7:
            return None
        x = (x - 1) % 8
        return super()._pixel(x, y, color)

    def __getitem__(self, key):
        x, y = key
        return self.pixel(x, y)

    def __setitem__(self, key, value):
        x, y = key
        self.pixel(x, y, value)

class Matrix8x8x2(HT16K33):
    """A bi-color matrix."""
    def pixel(self, x, y, color=None):
        """Get or set the color of a given pixel."""
        if not 0 <= x <= 7:
            return None
        if not 0 <= y <= 7:
            return None
        if color is not None:
            super()._pixel(y, x, (color & 0x01))
            super()._pixel(y + 8, x, (color >> 1) & 0x01)
        else:
            return super()._pixel(y, x) | super()._pixel(y + 8, x) << 1
        return None

    def __getitem__(self, key):
        x, y = key
        return self.pixel(x, y)

    def __setitem__(self, key, value):
        x, y = key
        self.pixel(x, y, value)

    def fill(self, color):
        """Fill the whole display with the given color."""
        fill1 = 0xff if color & 0x01 else 0x00
        fill2 = 0xff if color & 0x02 else 0x00
        for i in range(8):
            self._set_buffer(i * 2, fill1)
            self._set_buffer(i * 2 + 1, fill2)
        if self._auto_write:
            self.show()

# The MIT License (MIT) # # Copyright (c) 2016 <NAME> & <NAME> for Adafruit Industries # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. Matrix Displays ================ # Font table 0x20 ~ 0x7f # # ! # " # # # $ # % # & # ' # ( # ) # * # + # , # - # . # / # 0 # 1 # 2 # 3 # 4 # 5 # 6 # 7 # 8 # 9 # : # ; # < # = # > # ? # @ # A # B # C # D # E # F # G # H # I # J # K # L # M # N # O # P # Q # R # S # T # U # V # W # X # Y # Z # [ # \ # ] # ^ # _ # ` # a # b # c # d # e # f # g # h # i # j # k # l # m # n # o # p # q # r # s # t # u # v # w # x # y # z # { # | # } # ~ # A double matrix or the matrix wing. Get or set the color of a given pixel. A single matrix. Get or set the color of a given pixel. A bi-color matrix. Get or set the color of a given pixel. Fill the whole display with the given color.