{"id":32,"date":"2016-06-12T20:48:00","date_gmt":"2016-06-12T12:48:00","guid":{"rendered":"https:\/\/bh4bqi.cn\/?p=32"},"modified":"2022-09-26T20:56:09","modified_gmt":"2022-09-26T12:56:09","slug":"mose-code%e7%9a%84%e9%a1%b9%e7%9b%ae%e7%bb%88%e4%ba%8e%e7%bb%93%e6%9d%9f%e4%ba%86","status":"publish","type":"post","link":"https:\/\/bh4bqi.cn\/?p=32","title":{"rendered":"Mose Code\u7684\u9879\u76ee\u7ec8\u4e8e\u7ed3\u675f\u4e86"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u641e\u4e86\u4e00\u4e2a\u591a\u6708\u7684Morse Code\u7684\u9879\u76ee\u7ec8\u4e8e\u53ef\u4ee5\u89e3\u7801\u4e86\uff0c\u540c\u65f6\u4e5f\u4e86\u89e3\u4e86Python\u7684\u5de5\u4f5c\u539f\u7406\u3002\u2029\u6284\u88ad\u522b\u4eba\u7684\u7a0b\u5e8f\u771f\u662f\u5b66\u4e60\u7a0b\u5e8f\u7684\u597d\u529e\u6cd5\uff0c\u7279\u522b\u662f\u624b\u5de5\u5f55\u5165\u7684\u4ee3\u7801\u4e2d\u6709\u9519\u8bef\u7684\u65f6\u5019\u3002:)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">morse-code.py<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python3.4\nimport pygame\nimport time\nfrom RPi import GPIO\nfrom array import array\nfrom pygame.locals import *\nfrom morse_lookup import *\nimport _thread\n\npygame.mixer.pre_init(44100, -16, 1, 1024)\npygame.init()\n\nclass ToneSound(pygame.mixer.Sound):\n    def __init__(self, frequency, volume):\n        self.frequency = frequency\n        pygame.mixer.Sound.__init__(self, self.build_samples())\n        self.set_volume(volume)\n\n    def build_samples(self):\n        period = int(round(pygame.mixer.get_init()&#91;0] \/ self.frequency))\n        samples = array(\"h\", &#91;0] * period)\n        amplitude = 2 ** (abs(pygame.mixer.get_init()&#91;1]) - 1) - 1\n        for time in range(period):\n            if time &lt; period \/ 2: samples&#91;time] = amplitude else: samples&#91;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:\n #           print(key_down_length)\n            new_word = True\n            bit_string = \"\".join(buffer)\n            try_decode(bit_string)\n            del buffer&#91;:]\n        elif new_word and key_up_length >= 1.5:\n            new_word = False\n            sys.stdout.write(\" \")\n            sys.stdout.flush()\n\ntone_obj = ToneSound(frequency = 800, volume = .5)\n\nprint(\"program start\")\n\npin = 7\nGPIO.setmode(GPIO.BOARD)\nGPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)\nGPIO.setup(22, GPIO.OUT)\nGPIO.setup(13, GPIO.OUT)\n\nDOT = \".\"\nDASH = \"-\"\n\nkey_down_time = 0\nkey_down_length = 0\nkey_up_time = 0\nbuffer = &#91;]\n\n_thread.start_new_thread(decoder_thread,())\n\nprint(\"Ready\")\n\nwhile True:\n#Wait for key down, if key down, speak beep and led blink\n    wait_for_keydown(pin)\n    key_down_time = time.time()\n    tone_obj.play(-1) \n    GPIO.output(22,GPIO.HIGH)\n    GPIO.output(13,GPIO.HIGH)\n#if the key up, stop the speak beep and shutdown the blink\n    wait_for_keyup(pin)\n    key_up_time = time.time()\n    key_down_length = time.time() - key_down_time\n    tone_obj.stop()\n    GPIO.output(22,GPIO.LOW)\n    GPIO.output(13,GPIO.LOW)\n    buffer.append(DASH if key_down_length > 0.15 else DOT)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">morse_lookup.py<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python3.4\nimport sys \n\nmorse_code_lookup = {\n    \".-\": \"A\",\n    \"-...\": \"B\",\n    \"-.-.\": \"C\",\n    \"-..\": \"D\",\n    \".\": \"E\",\n    \"..-.\": \"F\",\n    \"--.\": \"G\",\n    \"....\": \"H\",\n    \"..\": \"I\",\n    \".---\": \"J\",\n    \"-.-\": \"K\",\n    \".-..\": \"L\",\n    \"--\": \"M\",\n    \"-.\": \"N\",\n    \"---\": \"O\",\n    \".--.\": \"P\",\n    \"--.-\": \"Q\",\n    \".-.\": \"R\",\n    \"...\": \"S\",\n    \"-\": \"T\",\n    \"..-\": \"U\",\n    \"...-\": \"V\",\n    \".--\": \"W\",\n    \"-..-\": \"X\",\n    \"-.--\": \"Y\",\n    \"--..\": \"Z\",\n    \".----\": \"1\",\n    \"..---\": \"2\",\n    \"...--\": \"3\",\n    \"....-\": \"4\",\n    \".....\": \"5\",\n    \"-....\": \"6\",\n    \"--...\": \"7\",\n    \"---..\": \"8\",\n    \"----.\": \"9\",\n    \"-----\": \"0\"\n}\n\ndef try_decode(bit_string):\n    if bit_string in morse_code_lookup.keys():\n        sys.stdout.write(morse_code_lookup&#91;bit_string])\n        sys.stdout.flush()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u641e\u4e86\u4e00\u4e2a\u591a\u6708\u7684Morse Code\u7684\u9879\u76ee\u7ec8\u4e8e\u53ef\u4ee5\u89e3\u7801\u4e86\uff0c\u540c\u65f6\u4e5f\u4e86\u89e3\u4e86Python\u7684\u5de5\u4f5c\u539f\u7406\u3002\u2029\u6284\u88ad\u522b\u4eba\u7684\u7a0b\u5e8f\u771f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[4,5,6],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-ham","category-linux","category-raspberry-pi"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/bh4bqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bh4bqi.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bh4bqi.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bh4bqi.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bh4bqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=32"}],"version-history":[{"count":1,"href":"https:\/\/bh4bqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":33,"href":"https:\/\/bh4bqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/32\/revisions\/33"}],"wp:attachment":[{"href":"https:\/\/bh4bqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bh4bqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bh4bqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}