注釈
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
参加する理由は?
エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。
独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。
特別割引:最新製品の独占割引をお楽しみください。
祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[ここ]をクリックして今すぐ参加しましょう!
4.1.11 モールス信号ジェネレータ¶
はじめに¶
このプロジェクトでは、Raspberry Piに英字のシリーズを入力し、それをモールス信号として表示するモールス信号ジェネレータを作成します。
必要な部品¶
このプロジェクトには、次のコンポーネントが必要です。
一式を購入するのが便利です、こちらがリンクです:
名前 |
このキットのアイテム |
リンク |
---|---|---|
Raphael Kit |
337 |
以下のリンクから別々に購入することもできます。
コンポーネントの紹介 |
購入リンク |
---|---|
- |
|
回路図¶
T-Board Name |
physical |
wiringPi |
BCM |
GPIO17 |
Pin 11 |
0 |
17 |
GPIO22 |
Pin 15 |
3 |
22 |
実験手順¶
ステップ1: 回路を組み立てます。(ブザーの極性に注意してください:+ラベルのある方が正極で、もう一方が負極です。)
ステップ2: コードファイルを開きます。
cd ~/raphael-kit/python-pi5
ステップ3: 実行します。
sudo python3 3.1.11_MorseCodeGenerator_zero.py
プログラムが実行されたら、一連の文字列を入力し、ブザーとLEDが対応するモールス信号を送信します。
コード
#!/usr/bin/env python3
from gpiozero import Buzzer, LED
import time
# Initialize Buzzer and LED to GPIO pins
BeepPin = Buzzer(22)
ALedPin = LED(17)
# Morse code representation for characters
MORSECODE = {
'A': '01', 'B': '1000', 'C': '1010', 'D': '100', 'E': '0', 'F': '0010', 'G': '110',
'H': '0000', 'I': '00', 'J': '0111', 'K': '101', 'L': '0100', 'M': '11', 'N': '10',
'O': '111', 'P': '0110', 'Q': '1101', 'R': '010', 'S': '000', 'T': '1',
'U': '001', 'V': '0001', 'W': '011', 'X': '1001', 'Y': '1011', 'Z': '1100',
'1': '01111', '2': '00111', '3': '00011', '4': '00001', '5': '00000',
'6': '10000', '7': '11000', '8': '11100', '9': '11110', '0': '11111',
'?': '001100', '/': '10010', ',': '110011', '.': '010101', ';': '101010',
'!': '101011', '@': '011010', ':': '111000',
}
def on():
""" Turn on the buzzer and LED. """
BeepPin.on()
ALedPin.on()
def off():
""" Turn off the buzzer and LED. """
BeepPin.off()
ALedPin.off()
def beep(dt): # dt for delay time.
"""
Produce a beep sound and LED flash for the specified duration.
:param dt: Duration for the beep and flash.
"""
on()
time.sleep(dt)
off()
time.sleep(dt)
def morsecode(code):
"""
Convert the input code into Morse code and signal it using the buzzer and LED.
:param code: The text to be converted to Morse code.
"""
pause = 0.25
for letter in code:
for tap in MORSECODE[letter]:
if tap == '0':
beep(pause / 2) # Short beep for dot
if tap == '1':
beep(pause) # Long beep for dash
time.sleep(pause) # Pause between letters
def destroy():
""" Clean up resources on script termination. """
print("")
BeepPin.off()
ALedPin.off()
try:
while True:
code = input("Please input the messenger:")
code = code.upper() # Convert to uppercase for Morse code lookup
print(code)
morsecode(code)
except KeyboardInterrupt:
destroy()
コードの説明
このコードは、gpiozeroライブラリからBuzzerおよびLEDクラスをインポートしています。これらのクラスは、Raspberry Pi上の対応するGPIOデバイスを制御するために必要です。
#!/usr/bin/env python3 from gpiozero import Buzzer, LED import time
GPIOピン22のブザーとGPIOピン17のLEDを初期化して、これらのコンポーネントを制御するのに役立てます。
# Initialize Buzzer and LED to GPIO pins BeepPin = Buzzer(22) ALedPin = LED(17)
MORSECODE
構造を定義します。これは、AからZまでの文字、0から9までの数字、および「?」、「/」、「:」、「,」、「.」、「;」、「!」、「@」などの記号のモールス信号表現を含む辞書です。ここで、0
はドットを示し、1
はダッシュを示します。# Morse code representation for characters MORSECODE = { 'A': '01', 'B': '1000', 'C': '1010', 'D': '100', 'E': '0', 'F': '0010', 'G': '110', 'H': '0000', 'I': '00', 'J': '0111', 'K': '101', 'L': '0100', 'M': '11', 'N': '10', 'O': '111', 'P': '0110', 'Q': '1101', 'R': '010', 'S': '000', 'T': '1', 'U': '001', 'V': '0001', 'W': '011', 'X': '1001', 'Y': '1011', 'Z': '1100', '1': '01111', '2': '00111', '3': '00011', '4': '00001', '5': '00000', '6': '10000', '7': '11000', '8': '11100', '9': '11110', '0': '11111', '?': '001100', '/': '10010', ',': '110011', '.': '010101', ';': '101010', '!': '101011', '@': '011010', ':': '111000', }
on()
関数はブザーとLEDをオンにします。off()
関数はブザーとLEDをオフにします。def on(): """ Turn on the buzzer and LED. """ BeepPin.on() ALedPin.on() def off(): """ Turn off the buzzer and LED. """ BeepPin.off() ALedPin.off()
beep()
関数は、ブザーとLEDを指定された時間間隔で音を鳴らし、点滅させるために使用されます。def beep(dt): # dt for delay time. """ Produce a beep sound and LED flash for the specified duration. :param dt: Duration for the beep and flash. """ on() time.sleep(dt) off() time.sleep(dt)
morsecode()
関数は、入力された文字のモールス信号を処理し、ブザーとLEDを使用してそれを示します。def morsecode(code): """ Convert the input code into Morse code and signal it using the buzzer and LED. :param code: The text to be converted to Morse code. """ pause = 0.25 for letter in code: for tap in MORSECODE[letter]: if tap == '0': beep(pause / 2) # Short beep for dot if tap == '1': beep(pause) # Long beep for dash time.sleep(pause) # Pause between letters
destroy()
関数は、スクリプトが終了したときにブザーとLEDの両方をオフにするようにします。これにより、GPIOピンがアクティブな状態にならないようになります。def destroy(): """ Clean up resources on script termination. """ print("") BeepPin.off() ALedPin.off()
キーボードで関連する文字を入力すると、
upper()
が入力文字を大文字に変換します。printf()
はコンピュータ画面にクリアテキストを表示し、morsecode()
関数はブザーとLEDにモールス信号を出力させます。try: while True: code = input("Please input the messenger:") code = code.upper() # Convert to uppercase for Morse code lookup print(code) morsecode(code) except KeyboardInterrupt: destroy()