1

私はこのようなことをしようとしています。問題は、それを行うループを構築できないことです。 回路:

これが私のコードです:

import parallel 
import time
p=parallel.Parallel() #object to use the parallel port
print ("Enter a string of numbers: ")
numStr = raw_input() #read line
numList=list(numSTr) #converts string to list
numlen=len(numList) #the length of the list
numBin=[['1','0001'], ['2','0010'],
 ['4','0100'], ['5','0101'],
 ['6','0110'], ['7','0111'],
 ['8','1000'], ['9','1001'],
 ['3','0011'], ['0','0000']] #Less significant bits of the numbers from 0 to 9 in a bidimesional array
p.setData(0) #clear the displays
pos=['0001','0010','0100','1000'] #Unique possible positions for the number from 0 to 9. 
c=(str(pos[])+str((numBin[][1]))) #here if the number in the list numList exist and also is in numBin. It joins the position and the number in binary, creating an number that will be send in decimal to the parallel port.
p.setData(int(c,2)) #send the binary number in decimal

誰かが私を助けることができれば、それは満足のいくものになるでしょう

numBinにある最上位ビットは、オンにするディスプレイを定義します。そして、それほど重要ではないものが数を定義します。例えば:

文字列は{'7'、 '1'、 '5'、 '4'、'8'}です。したがって、最後の表示に表示される最初の数字は「7」です。したがって、「0111」であるバイナリ7を取得し、そのバイナリ文字列を「0001」である最初の表示位置に結合します。したがって、2進数「00010111」を作成します。その数値を10進数に変換し、パラレルポートに送信します。パラレルポートはlasディスプレイをオンにし、番号7を表示します。2回目は、2番目と1番目の位置に「7」と「1」を表示する必要があります。

X X X X
X X X 7
X X 7 1
X 7 1 5
7 1 5 4
1 5 4 8
5 4 8 X
4 8 X X
8 X X X
X X X X

「X」はディスプレイがオフであることを表し、数字は回路でわかるようにディスプレイ位置にあることを表します。

4

2 に答える 2

1
import parallel 
import time
p=parallel.Parallel()                        # object to use the parallel port
print ("Enter a string of numbers: ")
numStr = bytearray(raw_input())
p.setData(0)                                 # clear the displays
while True:                                  # refresh as fast as you need to
    for i,n in enumerate(numStr,4):
        p.setData(1<<i | n&0xf)

forループでiは、値4、5、6、71<<iを取得するため、次のようになります。

4 => 0b00010000
5 => 0b00100000
6 => 0b01000000
7 => 0b10000000

これはビット単位であるか、または番号のASCIIコードの最後の4ビットで、パラレルポートに書き込む必要のある値を提供します。

于 2012-03-29T06:11:47.753 に答える
1

回路を見ると、実際には異なる数値を同時に表示することはできません。デモFPGAボードにこのような回路があり、目で検出できる速度よりも速い速度でディスプレイ上の数字を正しい位置で点滅させるソフトウェアドライバーを作成する必要がありました。

以下は、Mockオブジェクトを使用して、テスト用のパラレルポートと表示をシミュレートする大まかなアルゴリズムです。改行なしのキャリッジリターンをサポートする端末で実行する必要があります。

代わりに並列ライブラリにドロップできるはずですが、ハードウェアに一致するように制御ビットを調整する必要がある場合があります。

import sys

class ParallelMock(object):

    def __init__(self):
        '''Init and blank the "display".'''
        self.display = [' '] * 4
        self._update()

    def setData(self,data):
        '''Bits 0-3 are the "value".
           Bits 4-7 are positions 0-3 (first-to-last).
        '''
        self.display = [' '] * 4
        value = data & 0xF
        if data & 0x10:
            self.display[0] = str(value)
        if data & 0x20:
            self.display[1] = str(value)
        if data & 0x40:
            self.display[2] = str(value)
        if data & 0x80:
            self.display[3] = str(value)
        self._update()

    def _update(self):
        '''Write over the same four terminal positions each time.'''
        sys.stdout.write(''.join(self.display) + '\r')

if __name__ == '__main__':
    p = ParallelMock()

    nums = raw_input("Enter a string of numbers: ")

    # Shift over the steam four-at-a-time.
    stream = 'XXXX' + nums + 'XXXX'
    data = [0] * 4
    for i in range(len(stream)-3):
        # Precompute data
        for pos in range(4):
            value = stream[i+pos]
            data[pos] = 0 if value == 'X' else (1<<(pos+4)) + int(value)
        # "Flicker" the display...
        for delay in xrange(1000):
            # Display each position briefly.
            for d in data:
                p.setData(d)
        # Clear the display when done
        p.setData(0)
于 2012-03-29T07:33:38.717 に答える