{"id":2481,"date":"2023-08-03T21:08:09","date_gmt":"2023-08-03T13:08:09","guid":{"rendered":"https:\/\/104.es\/?p=2481"},"modified":"2023-08-03T21:08:09","modified_gmt":"2023-08-03T13:08:09","slug":"microbitmicropython-button-control","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2023\/08\/03\/microbitmicropython-button-control\/","title":{"rendered":"\u3010micro:bit\u3011MicroPython\u6309\u9215\u63a7\u5236"},"content":{"rendered":"\n<p>micro:bit\u6709\u5169\u500b\u53ef\u7a0b\u5f0f\u5316\u6309\u9215\uff0c\u8b93\u6211\u5011\u53ef\u4ee5\u548cmicro:bit\u9032\u884c\u4e92\u52d5\uff0c\u9019\u5169\u500b\u6309\u9215\u5728\u8a2d\u8a08\u5c0f\u904a\u6232\u7684\u6642\u5019\u975e\u5e38\u597d\u7528\u3002\u90a3\u9ebc\uff0c\u5982\u679c\u4f7f\u7528MicroPython\uff0c\u8981\u5982\u4f55\u53bb\u5075\u6e2c\u9019\u500b\u6309\u9215\u5462\uff1f\u7b54\u6848\u662fis_pressed()\u9019\u500b\u51fd\u6578\uff0c\u800c\u6309\u9215A\u662fbutton_a\uff0c\u6309\u9215B\u5247\u662fbutton_b\uff0c\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u904b\u7528\u6309\u9215\u6539\u8b8a\u5716\u6848\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\n\r\ndisplay.show(Image.HEART)\r\nwhile True:\r\n    if button_a.is_pressed():\r\n        display.show(Image.HAPPY)\r\n    elif button_b.is_pressed():\r\n        display.show(Image.SAD)\r\n<\/code><\/pre>\n\n\n\n<p>\u9762\u9019\u500b\u7a0b\u5f0f\u5728\u4e00\u958b\u59cb\u57f7\u884c\u7684\u6642\u5019\u6703\u986f\u793a\u611b\u5fc3\u5716\u6848\uff0c\u5982\u679c\u6309\u4e0b\u53bbA\u6309\u9215\u5c31\u6703\u8b8a\u6210\u7b11\u81c9\uff0c\u6309\u4e0b\u53bbB\u6309\u9215\u5247\u8b8a\u6210\u54ed\u81c9\u3002\u4e4b\u5f8c\u5c31\u53ea\u80fd\u5728\u7b11\u81c9\u548c\u54ed\u81c9\u4e4b\u9593\u505a\u8b8a\u5316\uff0c\u56e0\u70ba\u5728\u7121\u7aae\u8ff4\u5708\u4e2d\u53ea\u6709\u9019\u5169\u7a2e\u9078\u64c7\u3002<\/p>\n\n\n\n<p>\u5047\u8a2d\u6211\u5011\u60f3\u8981\u8b93\u4f7f\u7528\u8005\u5728\u540c\u6642\u6309\u4e0bA\u548cB\u6309\u9215\uff08\u4e5f\u5c31\u662f\u7a4d\u6728\u4e2d\u7684A+B\u6309\u4e0b\u4e8b\u4ef6\uff09\u5c31\u6062\u5fa9\u6210\u611b\u5fc3\u5716\u6848\u7684\u8a71\uff0c\u4e5f\u8a31\u4f60\u6703\u60f3\u5230\u4ee5\u4e0b\u6b64\u7a2e\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<p>\u8acb\u6ce8\u610f\uff0c\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u662f\u932f\u8aa4\u7684\uff01<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\n\r\ndisplay.show(Image.HEART)\r\nwhile True:\r\n    if button_a.is_pressed():\r\n        display.show(Image.HAPPY)\r\n    elif button_b.is_pressed():\r\n        display.show(Image.SAD)\r\n    elif button_a.is_pressed() and button_b.is_pressed():\r\n        display.show(Image.HEART)<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u9019\u500b\u7a0b\u5f0f\u7684\u908f\u8f2f\u662f\uff0c\u5982\u679cA\u6309\u9215\u88ab\u6309\u4e0b\u53bb\u5c31\u986f\u793a\u7b11\u81c9\uff0c\u5426\u5247\u5982\u679cB\u6309\u9215\u88ab\u6309\u4e0b\u53bb\u7684\u8a71\u5c31\u986f\u793a\u54ed\u81c9\uff0c\u5426\u5247\u5982\u679cA\u6309\u9215\u88ab\u6309\u4e0b\u53bb\u800c\u4e14B\u6309\u9215\u4e5f\u88ab\u6309\u4e0b\u53bb\u7684\u8a71\uff0c\u5c31\u6703\u986f\u793a\u611b\u5fc3\u5716\u6848\u3002<\/p>\n\n\n\n<p>\u908f\u8f2f\u4e0a\u770b\u8d77\u4f86\u6c92\u554f\u984c\uff0c\u4f46\u5176\u5be6\u5b58\u5728\u4e00\u500b\u56b4\u91cd\u7684\u7455\u75b5\uff0c\u90a3\u5c31\u662f\u7b2c9\u884c\u7684\u689d\u4ef6\u5176\u5be6\u540c\u6642\u5305\u542b\u4e86\u7b2c5\u884c\u548c\u7b2c7\u884c\u7684\u689d\u4ef6\uff0c\u610f\u601d\u662f\u8aaa\uff0c\u4efb\u4e00\u500b\u6309\u9215\u88ab\u6309\u4e0b\u53bb\u6642\uff0c\u4e00\u5b9a\u6703\u5148\u6eff\u8db3\u7b2c5\u884c\u6216\u662f\u7b2c7\u884c\u7684\u689d\u4ef6\uff0c\u5982\u6b64\u7b2c9\u884c\u7684\u90a3\u500b\u689d\u4ef6\u6839\u672c\u5c31\u6c92\u6709\u6a5f\u6703\u88ab\u57f7\u884c\u5230\u3002<\/p>\n\n\n\n<p>\u6539\u6b63\u7684\u65b9\u6cd5\u662f\uff0c\u628a\u7b2c9\u884c\u7684\u689d\u4ef6\u79fb\u5230\u6700\u524d\u9762\u53bb\uff0c\u5148\u6aa2\u67e5\u662f\u5426\u5169\u500b\u6309\u9215\u540c\u6642\u88ab\u6309\u4e0b\uff0c\u5982\u679c\u6709\u5c31\u63db\u6210\u611b\u5fc3\u5716\u6848\uff0c\u5982\u679c\u6c92\u6709\u7684\u8a71\uff0c\u518d\u5206\u5225\u6aa2\u67e5A\u548cB\u6309\u9215\u6709\u6c92\u6709\u5176\u4e2d\u4e00\u500b\u88ab\u6309\u4e0b\u53bb\u3002\u4fee\u6b63\u5f8c\u7684\u7a0b\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u8acb\u6ce8\u610f\uff0c\u5e95\u4e0b\u7684\u7a0b\u5f0f\u4ecd\u7136\u6709\u4e00\u4e9b\u554f\u984c\u5594<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\n\r\ndisplay.show(Image.HEART)\r\nwhile True:\r\n    if button_a.is_pressed() and button_b.is_pressed():\r\n        display.show(Image.HEART)\r\n    elif button_a.is_pressed():\r\n        display.show(Image.HAPPY)\r\n    elif button_b.is_pressed():\r\n        display.show(Image.SAD)<\/code><\/pre>\n\n\n\n<p>\u540c\u5b78\u5011\u53ef\u4ee5\u57f7\u884c\u4e0a\u8ff0\u7684\u7a0b\u5f0f\u770b\u770b\u554f\u984c\u5728\u54ea\u88e1\uff01<\/p>\n\n\n\n<p>\u767c\u73fe\u4e86\u55ce\uff1fA\u6309\u9215\u548cB\u6309\u9215\u90fd\u6c92\u554f\u984c\uff0c\u4f46\u662f\u7576A\u548cB\u540c\u6642\u6309\u4e0b\u6642\u7684\u90a3\u4e00\u77ac\u9593\u662f\u53ef\u4ee5\u5207\u63db\u6210\u611b\u5fc3\u7684\uff0c\u53ef\u662f\u7576\u624b\u4e00\u9b06\u958b\u6309\u9215\u7684\u6642\u5019\u5c31\u6703\u767c\u73fe\uff0c\u5716\u6848\u99ac\u4e0a\u53c8\u6062\u5fa9\u6210\u7b11\u81c9\u6216\u54ed\u81c9\u4e86\uff0c\u7aef\u770b\u4f60\u7684\u90a3\u4e00\u96bb\u624b\u6307\u653e\u5f97\u6bd4\u8f03\u6162\u4e00\u4e9b\u3002<\/p>\n\n\n\n<p>\u9020\u6210\u9019\u500b\u554f\u984c\u7684\u539f\u56e0\u5728\u65bcA\u548cB\u6309\u9215\u540c\u6642\u653e\u958b\u7684\u6642\u5019\uff0c\u5176\u4e2d\u4e00\u500b\u6309\u9215\u6703\u653e\u5f97\u6162\u4e00\u4e9b\uff0c\u800c\u7531\u65bc\u7a0b\u5f0f\u904b\u884c\u7684\u901f\u5ea6\u5f88\u5feb\uff0c\u5f88\u5feb\u5730\u53c8\u56de\u5230\u6309\u9215\u7684\u5224\u65b7\uff0c\u6b64\u6642\u5f88\u5feb\u5730\u53c8\u5224\u65b7\u5230\u5176\u4e2d\u4e00\u500b\u6309\u9215\u88ab\u6309\u4e0b\u53bb\uff0c\u56e0\u6b64\u7acb\u523b\u53c8\u88ab\u5207\u63db\u6210\u8a72\u6309\u9215\u7684\u7b26\u865f\u4e86\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u5728\u63db\u6210\u611b\u5fc3\u5716\u6848\u4e4b\u5f8c\u8b93\u7a0b\u5f0f\u66ab\u505c\u500b\u4e00\u79d2\u9418\uff0c\u4f7f\u5f97\u5011\u7684\u624b\u6307\u4f86\u5f97\u53ca\u540c\u6642\u9b06\u958b\u6309\u9215\uff0c\u5c31\u4e0d\u6703\u6709\u554f\u984c\u4e86\u3002\u5b8c\u6210\u5f8c\u7684\u7a0b\u5f0f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\n\r\ndisplay.show(Image.HEART)\r\nwhile True:\r\n    if button_a.is_pressed() and button_b.is_pressed():\r\n        display.show(Image.HEART)\r\n        sleep(1000)\r\n    elif button_a.is_pressed():\r\n        display.show(Image.HAPPY)\r\n    elif button_b.is_pressed():\r\n        display.show(Image.SAD)<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u9019\u500b\u7a0b\u5f0f\u57f7\u884c\u4e4b\u5f8c\uff0c\u5e73\u6642\u5b83\u6703\u4ee5\u986f\u793a\u611b\u5fc3\u7684\u7b26\u865f\uff0c\u7576\u6309\u4e0bA\u6309\u9215\u6642\uff0c\u5247\u6703\u986f\u793a\u7b11\u81c9\uff0c\u5728\u6309\u4e0bB\u6309\u9215\u5f8c\uff0c\u5247\u6703\u986f\u793a\u54ed\u81c9\u3002\u7136\u800c\u6709\u4e00\u500b\u8981\u8b93\u540c\u5b78\u5011\u6bd4\u8f03\u7684\u5730\u65b9\u662f\uff0c\u5728\u7a0b\u5f0f\u4e2d\uff0c\u6bcf\u986f\u793a\u4e00\u500b\u5716\u6848\u90fd\u6703\u66ab\u505c5\u79d2\u9418\uff0c\u5728\u9019\u6bb5\u671f\u9593\u5167\uff0c\u7a0b\u5f0f\u662f\u6c92\u6709\u5728\u904b\u4f5c\u7684\uff0c\u5982\u679c\u4f60\u5728\u9019\u6bb5\u66ab\u505c\u6642\u9593\u5167\u6309\u4e0b\u6309\u9215\u7684\u8a71\uff0c\u9019\u500b\u6309\u4e0b\u53bb\u7684\u52d5\u4f5c\u6703\u88ab\u5ffd\u7565\u6389\uff0c\u9019\u4e5f\u662f\u70ba\u4ec0\u9ebc\u4e0b\u9762\u9019\u500b\u7a0b\u5f0f\u5728\u57f7\u884c\u7684\u6642\u5019\uff0c\u4f60\u6703\u89ba\u5f97\u6309\u9215\u597d\u50cf\u4e0d\u592a\u9748\u654f\u7684\u6a23\u5b50\uff0c\u6709\u6642\u5019\u53ef\u4ee5\u63db\u5716\u6848\uff0c\u6709\u6642\u5019\u53c8\u4e0d\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\n\r\nwhile True:\r\n    if button_a.is_pressed():\r\n        display.show(Image.HAPPY)\r\n        sleep(5000)\r\n    elif button_b.is_pressed():\r\n        display.show(Image.SAD)\r\n        sleep(5000)\r\n    else:\r\n        display.show(Image.HEART)\r\n        sleep(5000)<\/code><\/pre>\n\n\n\n<p>\u70ba\u4e86\u907f\u514d\u4e0a\u8ff0\u7684\u554f\u984c\u767c\u751f\uff0c\u7576\u7136\u628asleep\u7684\u6642\u9593\u6e1b\u5c11\u662f\u4e00\u500b\u65b9\u6cd5\uff0c\u4f46\u662f\uff0c\u6700\u597d\u7684\u65b9\u6cd5\u662f\u628ais_pressed()\u6539\u6210was_pressed()\uff0c\u9019\u500b\u51fd\u6578\u4e0d\u662f\u56de\u50b3\u5075\u6e2c\u7684\u7576\u6642\u6309\u9215\u88ab\u6309\u4e0b\u53bb\u7684\u60c5\u5f62\uff0c\u800c\u662f\u6aa2\u67e5\u5728\u524d\u4e00\u6b21\u5075\u6e2c\u5230\u9019\u4e00\u6b21\u7684\u5075\u6e2c\u671f\u9593\u6309\u9215\u662f\u5426\u66fe\u7d93\u88ab\u6309\u4e0b\u904e\uff0c\u53ea\u8981\u6309\u904e\u5c31\u6703\u56de\u50b3True\uff0c\u5982\u6b64\u5c31\u53ef\u4ee5\u6539\u5584\u524d\u4e00\u500b\u7a0b\u5f0f\u6240\u9047\u5230\u7684\u554f\u984c\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\n\r\nwhile True:\r\n    if button_a.was_pressed():\r\n        display.show(Image.HAPPY)\r\n        sleep(5000)\r\n    elif button_b.was_pressed():\r\n        display.show(Image.SAD)\r\n        sleep(5000)\r\n    else:\r\n        display.show(Image.HEART)\r\n        sleep(5000)<\/code><\/pre>\n\n\n\n<p>\u9664\u4e86is_pressed()\u4ee5\u53cawas_pressed()\u9019\u5169\u500b\u51fd\u6578\u4e4b\u5916\uff0c\u9084\u6709\u4e00\u500b\u51fd\u6578get_presses()\u662f\u7528\u4f86\u7d00\u9304\u5f9e\u4e0a\u4e00\u6b21\u547c\u53eb\u6aa2\u67e5\u6309\u9215\u7684\u51fd\u6578\u5230\u73fe\u5728\uff0c\u67d0\u4e00\u500b\u6309\u9215\u7e3d\u5171\u88ab\u6309\u4e86\u5e7e\u6b21\u3002\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u4f8b\u57f7\u884c\u7684\u6642\u5019\u6703\u958b\u59cb\u5012\u657810\u79d2\uff0c\u6642\u9593\u5230\u4e4b\u5f8c\u6703\u986f\u793a\u5728\u9019\u671f\u9593\uff0c\u6309\u9215A\u7e3d\u5171\u88ab\u6309\u4e86\u5e7e\u6b21\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\n\r\nfor i in range(9, -1, -1):\r\n    display.show(i)\r\n    sleep(1000)\r\ndisplay.show(Image.HEART)\r\nsleep(1000)\r\nnumber = button_a.get_presses()\r\ndisplay.show(number)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>micro:bit\u6709\u5169\u500b\u53ef\u7a0b\u5f0f\u5316\u6309\u9215\uff0c\u8b93\u6211\u5011\u53ef\u4ee5\u548cmicro:bit\u9032\u884c\u4e92\u52d5\uff0c\u9019\u5169\u500b\u6309\u9215\u5728\u8a2d\u8a08\u5c0f\u904a\u6232\u7684\u6642\u5019\u975e\u5e38\u597d\u7528\u3002\u90a3\u9ebc\uff0c\u5982\u679c\u4f7f\u7528MicroPython\uff0c\u8981\u5982\u4f55\u53bb\u5075\u6e2c\u9019\u500b\u6309\u9215\u5462\uff1f\u7b54\u6848\u662fis_pressed()\u9019\u500b\u51fd\u6578\uff0c\u800c\u6309\u9215A\u662fbutton_a\uff0c\u6309\u9215B\u5247\u662fbutton_b\uff0c<\/p>\n","protected":false},"author":1,"featured_media":2480,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11,15],"tags":[],"class_list":["post-2481","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microbit","category-python"],"jetpack_featured_media_url":"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/oqtafyt5ktw.jpg","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=2481"}],"version-history":[{"count":1,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2481\/revisions"}],"predecessor-version":[{"id":2482,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2481\/revisions\/2482"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media\/2480"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}