【问题标题】:Function Bypass - Raspberry Pi - Morse Project in Python 2函数绕过 - Raspberry Pi - Python 2 中的 Morse 项目
【发布时间】:2019-02-24 03:38:46
【问题描述】:

大家!目前,我正在我的 Raspberry Pi 3 上进行 Morse 项目,该项目将用户输入的文本解释为面包板上的 LED 信号。在开始之前,我用另一段代码检查了电路,它表明它工作正常。但是,当我尝试在我的项目上运行代码时,它基本上忽略了一个显示信号的函数。有一个代码:

#!/usr/bin/env python
from __future__ import print_function
import RPi.GPIO as GPIO
import time
import os



# Source of Morse is https://morsecode.scphillips.com/morse2.html
# 'step'    = 1                     /signal on/off
# 'dit'     = step                  /dot
# 'dash'    = 3 * step              /dash
# 'pause'   = 3 * step              /pause after letters
# 'space'   = 7 * step              /pause between words
#
#
# Average number of letters for English word is 4.79
# However, the most common range is [5,6]

LED   = 11
none  = None

morse = [
    [' '],              # ' ' 32
    ['-.-.--'],         # !
    ['.-..-.'],         # "
    [none],             # #
    [none],             # $
    [none],             # %
    ['.-...'],          # &
    ['.----.'],         # '
    ['-.--.'],          # (
    ['-.--.-'],         # )
    [none],             # *
    ['.-.-.'],          # +
    ['--..--'],         # ,
    ['-....-'],         # -
    ['.-.-.-'],         # .
    ['-..-.'],          # /
    ['----'],           # 0
    ['.---'],           # 1
    ['..---'],          # 2
    ['...--'],          # 3
    ['....-'],          # 4
    ['.....'],          # 5
    ['-....'],          # 6
    ['--...'],          # 7
    ['---..'],          # 8
    ['----.'],          # 9
    ['---...'],         # :
    [none],             # ;
    [none],             # <
    [none],             # >
    ['..--..'],         # ?
    ['.--.-.'],         # @
    ['.-'],             # A 65
    ['-...'],           # 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
]





def setup():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(LED, GPIO.OUT)
        GPIO.output(LED, GPIO.HIGH)

def destroy():
        GPIO.output(LED, GPIO.HIGH)
        GPIO.cleanup()


def outPut(letter):
    row = ord(letter) - 32
    if (row >= 0) and (row <= 58):
            for symb in range(len(morse[row])):
            print (letter)
                if (morse[row][symb] == '.'):
                        GPIO.output(LED, GPIO.LOW)
                        time.sleep(dot)
                        GPIO.output(LED, GPIO.HIGH)
                        time.sleep(dot)

                elif (morse[row][symb] == '-'):
                        GPIO.output(LED, GPIO.LOW)
                        time.sleep(3 * dot)
                        GPIO.output(LED, GPIO.HIGH)
                        time.sleep(dot)

                elif (morse[row][symb] == ' '):
                        GPIO.output(LED, GPIO.HIGH)
                        time.sleep(7 * dot)

                else:
                        break


def exe():
    while True:
        os.system('clear')
        freq = 0.5
        print ('Make a choise:\n')
        print (' [1] - Translate text to Morse Code\n [2] - Change tempo\n [Ctrl + C] - Quit\n')
        print ('\n(Default tempo is 120 BPM ~ 2 steps per second)\n')
        choice = raw_input()

        if (choice == '1'):
            os.system('clear')
                    print ('Enter the string: ')
                    inp = raw_input()
                    inp = inp.upper()

                    for i in range(len(inp)):
                            outPut(inp[i])                    #Bypassing there

            elif (choice == '2'):
                os.system('clear')
                print ('Choose tempo.\n')
                print (' [1] - Words per Minute\n [2] - Beats per minute\n [3] - Characters per minute\n [B] - Back\n')
                print ('Your choice: ')

            pref = True
                userValue = raw_input()

            while pref:
                if (userValue == '1'):
                    print ('\nHow many words per minute you want?\n')
                    move = float(raw_input())
                    freq = 60 / (move * 20)
                    pref = False
                elif (userValue == '2'):
                    print ('\nHow many beats per minute do you want?\n')
                    move = float(raw_input())
                    freq = ((move / 60) ** (-1))
                    pref = False
                elif (userValue == '3'):
                    print ('\nHow many characters per minute do you want?\n')
                    move = float(raw_input())
                    freq = 60 / move
                    pref = False
                elif (userValue == 'b') or (userValue == 'B'):
                    pref = False
                else:
                    print('INCORRECT CHOISE! TRY AGAIN!')
                    time.sleep(3)

        else:
            print ('INCORRECT CHOISE! TRY AGAIN!')
            time.sleep(3)


if __name__ == '__main__':
    setup()
        try:
            exe()
        except KeyboardInterrupt:
            destroy()

它绕过了函数outPut()。谁能告诉我怎么了?

【问题讨论】:

    标签: python-2.7 raspberry-pi3


    【解决方案1】:

    所以,当我后来处理数组时,我注意到二维数组的元素用逗号分隔。这让我想到了 Python 将 Morse 数组解释为 3D 数组的理论,因为它们中的每一个都只有字符串。 例如,此代码将仅在括号中打印整行数组:

    print(morse[6])
    

    输出:

    ['.-...']
    

    如果我会这样说:

    print(morse[6][0])
    

    输出将是:

    .-...
    

    当我最终进入时:

    print(morse[6][0][2])
    

    它会给出元素:

    .
    

    因此,数组中有一个字符串是 3D 数组。当我发现这个问题时,我用逗号分隔了所有元素。不过以后可以做3D了,不知道耗时多少。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      相关资源
      最近更新 更多