用pyfirmata控制16x16 led点矩阵

时间:2018-11-09 23:26:30

标签: python python-3.x matrix arduino led

好吧,我想做的是,能够通过pyfirmata用python动态地重新绘制16x16 arduino兼容的led点矩阵。我似乎找不到解决方法... 我想知道怎么做,就是使用python将16x16矩阵转换为led矩阵显示。 -这是我使用pyfirmata在python中制作的滚动时钟的示例。 (此示例适用于8x8 led点矩阵,而不是16x16,这正是我想要的...

"""led_matrix.py"""

from time import sleep

from pyfirmata import Arduino

HIGH = 1
LOW = 0

dataIn = 2
load = 3
clock = 4
maxInUse = 1
Intensity = 3

max7219_reg_noop = 0x00
max7219_reg_digit0 = 0x01
max7219_reg_digit1 = 0x02
max7219_reg_digit2 = 0x03
max7219_reg_digit3 = 0x04
max7219_reg_digit4 = 0x05
max7219_reg_digit5 = 0x06
max7219_reg_digit6 = 0x07
max7219_reg_digit7 = 0x08
max7219_reg_decodeMode = 0x09
max7219_reg_intensity = 0x0a
max7219_reg_scanLimit = 0x0b
max7219_reg_shutdown = 0x0c
max7219_reg_displayTest = 0x0f

intensity = 0x01

class LedMatrix:
    def __init__(self, board, dataIn, load, clock, maxInUse=1):
        self._board = board

        self.pins = dict()
        self.pins['dataIn'] = dataIn
        self.pins['load'] = load
        self.pins['clock'] = clock
        self.maxInUse = maxInUse

    def _digitalWrite(self, pin, val):
        self._board.digital[pin].write(val)

    def _putByte(self, data):
        for i in range(8, 0, -1):
            mask = 0x01 << (i - 1)
            self._digitalWrite(self.pins["clock"], LOW)
            if data & mask:
                self._digitalWrite(self.pins["dataIn"], HIGH)
            else:
                self._digitalWrite(self.pins["dataIn"], LOW)
            self._digitalWrite(self.pins["clock"], HIGH)

    def maxSingle(self, reg, col):
        """ Change the row of lights at reg to read as binary of col. """
        self._digitalWrite(self.pins["load"], LOW)
        self._putByte(reg)
        self._putByte(col)
        self._digitalWrite(self.pins["load"], LOW)
        self._digitalWrite(self.pins["load"], HIGH)

    def maxAll(self, reg, col):
        """ Like calling maxSingle on every chained matrix """
        self._digitalWrite(self.pins["load"], LOW)
        for _ in range(0, self.maxInUse):
            self._putByte(reg)
            self._putByte(col)
        self._digitalWrite(self.pins["load"], LOW)
        self._digitalWrite(self.pins["load"], HIGH)

    def maxOne(self, maxNr, reg, col):
        """ Specify the matrix to be written to with maxNr. Then acts as maxSingle on that specific matrix. """
        self._digitalWrite(self.pins["load"], LOW)

        for _ in range(self.maxInUse, maxNr, -1):
            self._putByte(0)
            self._putByte(0)

        self._putByte(reg)
        self._putByte(col)

        for _ in range(maxNr - 1, 0, -1):
            self._putByte(0)
            self._putByte(0)

        self._digitalWrite(self.pins["load"], LOW)
        self._digitalWrite(self.pins["load"], HIGH)

    def clear(self):
        for e in range(1, 9):
            self.maxAll(e, 0)

    def draw_matrix(self, point_matrix):
        for col, pointlist in enumerate(point_matrix):
            self.maxSingle(col + 1, int(''.join(str(v) for v in pointlist), 2))

    def setup(self):
        print('Initializing _matrix...')
        self._digitalWrite(13, HIGH)
        self.maxAll(max7219_reg_scanLimit, 0x07)
        self.maxAll(max7219_reg_decodeMode, 0x00)
        self.maxAll(max7219_reg_shutdown, 0x01)
        self.maxAll(max7219_reg_displayTest, 0x00)
        self.clear()
        self.maxAll(max7219_reg_intensity, intensity & intensity)
        print('Done')

rotated = True

import numpy as np

def convertarray(array):

    m = np.array(array)
    m = np.rot90(m, 3)

    return m.tolist()

import datetime as date



import json

data = json.loads(open('jsonNums.json', 'r').read())

def getTime():
    n = date.datetime.now()
    hour = n.hour
    minute = n.minute
    second = n.second

    PM = False
    if hour > 12: hour -= 12; PM = True

    if len(str(hour)) < 2: hour = str( f"0{hour}" )
    hour=str(hour)

    if len(str(minute)) < 2: minute = str( f"0{minute}" )
    minute = str(minute)

    if len(str(second)) < 2: second = str( f"0{second}" )
    second = str(second)

    pm = "AM"
    if PM: pm = "PM"

    fullTime = f"{hour}:{minute}.{pm}"

    display = []
    for i in range(8):
        row = [0,0,0,0,0,0,0,0]
        for char in fullTime:
            if char == "0":
                row.extend(data["0"][i])
            elif char == "1":
                row.extend(data["1"][i])
            elif char == "2":
                row.extend(data["2"][i])
            elif char == "3":
                row.extend(data["3"][i])
            elif char == "4":
                row.extend(data["4"][i])
            elif char == "5":
                row.extend(data["5"][i])
            elif char == "6":
                row.extend(data["6"][i])
            elif char == "7":
                row.extend(data["7"][i])
            elif char == "8":
                row.extend(data["8"][i])
            elif char == "9":
                row.extend(data["9"][i])

            elif char == ":":
                row.extend(data["COLON"][i])
            elif char == ".":
                row.extend(data["POINT"][i])

            elif char == "A":
                row.extend(data["AM"][i])
                break

            elif char == "P":
                row.extend(data["PM"][i])
                break

        row.extend([0,0,0,0,0,0,0,0])

        display.append(row)

    return display




for row in (getTime()): print(row)

scrollMultiplier = 1

if __name__ == "__main__":
    board = Arduino('COM1')
    matrix = LedMatrix(board, dataIn, load, clock)
    matrix.setup()
    print("Loop running")



    while True:

        # print("New iteration")
        displayx = getTime()
        # print("Time collected")

        r = int(len(displayx[0]) - 8)
        # print(f"Repeat = {r}")

        for i in range(0, r, scrollMultiplier): # Scrolling effect

            x=[]

            for XX in displayx:

                # print(f"XX = {XX}")

                r=[]
                nr = XX[i:i+8]

                # print(f"nr = {nr}")

                x.append(nr)

                # print("X.append(nr)")

            x = convertarray(x)

            matrix.draw_matrix(x)

我似乎无法在线找到任何帮助,所以这就是为什么我在这里问... 任何帮助表示赞赏! :)

1 个答案:

答案 0 :(得分:0)

欢迎来到stackoverflow。

从您的代码中可以看到,有一些变量控制点矩阵显示的单元格数量。

max7219_reg_digit{ 0-1 }

row = [0,0,0,0,0,0,0,0]

r = int(len(displayx[0]) - 8)

def _putByte(self, data):
    for i in range(8, 0, -1): 
        nr = XX[i:i+8]`

如果我猜对了,您的库将完成将小数转换为二进制值数组的工作。如果是这样,您的工作将是将与8个单​​元格的限制相关的所有变量更改为16个单元格。

希望这对您有所帮助!