Mose Code的项目终于结束了

搞了一个多月的Morse Code的项目终于可以解码了,同时也了解了Python的工作原理。
抄袭别人的程序真是学习程序的好办法,特别是手工录入的代码中有错误的时候。:)

morse-code.py

#!/usr/bin/python3.4
import pygame
import time
from RPi import GPIO
from array import array
from pygame.locals import *
from morse_lookup import *
import _thread

pygame.mixer.pre_init(44100, -16, 1, 1024)
pygame.init()

class ToneSound(pygame.mixer.Sound):
    def __init__(self, frequency, volume):
        self.frequency = frequency
        pygame.mixer.Sound.__init__(self, self.build_samples())
        self.set_volume(volume)

    def build_samples(self):
        period = int(round(pygame.mixer.get_init()[0] / self.frequency))
        samples = array("h", [0] * period)
        amplitude = 2 ** (abs(pygame.mixer.get_init()[1]) - 1) - 1
        for time in range(period):
            if time < period / 2: samples[time] = amplitude else: samples[time] = -amplitude return samples def wait_for_keydown(pin): while GPIO.input(pin): time.sleep(0.01) def wait_for_keyup(pin): while not GPIO.input(pin): time.sleep(0.01) def decoder_thread(): global key_up_time global buffer new_word = False while True: time.sleep(.01) key_up_length = time.time() - key_up_time if len(buffer) > 0 and key_up_length >= .5:
 #           print(key_down_length)
            new_word = True
            bit_string = "".join(buffer)
            try_decode(bit_string)
            del buffer[:]
        elif new_word and key_up_length >= 1.5:
            new_word = False
            sys.stdout.write(" ")
            sys.stdout.flush()

tone_obj = ToneSound(frequency = 800, volume = .5)

print("program start")

pin = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)

DOT = "."
DASH = "-"

key_down_time = 0
key_down_length = 0
key_up_time = 0
buffer = []

_thread.start_new_thread(decoder_thread,())

print("Ready")

while True:
#Wait for key down, if key down, speak beep and led blink
    wait_for_keydown(pin)
    key_down_time = time.time()
    tone_obj.play(-1) 
    GPIO.output(22,GPIO.HIGH)
    GPIO.output(13,GPIO.HIGH)
#if the key up, stop the speak beep and shutdown the blink
    wait_for_keyup(pin)
    key_up_time = time.time()
    key_down_length = time.time() - key_down_time
    tone_obj.stop()
    GPIO.output(22,GPIO.LOW)
    GPIO.output(13,GPIO.LOW)
    buffer.append(DASH if key_down_length > 0.15 else DOT)

morse_lookup.py

#!/usr/bin/python3.4
import sys 

morse_code_lookup = {
    ".-": "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",
    ".----": "1",
    "..---": "2",
    "...--": "3",
    "....-": "4",
    ".....": "5",
    "-....": "6",
    "--...": "7",
    "---..": "8",
    "----.": "9",
    "-----": "0"
}

def try_decode(bit_string):
    if bit_string in morse_code_lookup.keys():
        sys.stdout.write(morse_code_lookup[bit_string])
        sys.stdout.flush()

已发布

分类

, ,

来自

标签: