{"id":7336,"date":"2025-01-25T16:49:58","date_gmt":"2025-01-25T07:49:58","guid":{"rendered":"https:\/\/robot.e-nat.org\/?p=7336"},"modified":"2025-01-25T16:50:58","modified_gmt":"2025-01-25T07:50:58","slug":"runloop%e3%81%a7%e3%83%9e%e3%83%ab%e3%83%81%e3%82%bf%e3%82%b9%e3%82%af%e5%87%a6%e7%90%86","status":"publish","type":"post","link":"https:\/\/robot.e-nat.org\/?p=7336","title":{"rendered":"runloop\u3067\u30de\u30eb\u30c1\u30bf\u30b9\u30af\u51e6\u7406"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"1024\" src=\"https:\/\/robot.e-nat.org\/wp-content\/uploads\/2025\/01\/robotWithForceAndColor-768x1024.jpg\" alt=\"\" class=\"wp-image-7337\" style=\"width:351px;height:auto\" srcset=\"https:\/\/robot.e-nat.org\/wp-content\/uploads\/2025\/01\/robotWithForceAndColor-768x1024.jpg 768w, https:\/\/robot.e-nat.org\/wp-content\/uploads\/2025\/01\/robotWithForceAndColor-225x300.jpg 225w, https:\/\/robot.e-nat.org\/wp-content\/uploads\/2025\/01\/robotWithForceAndColor-1152x1536.jpg 1152w, https:\/\/robot.e-nat.org\/wp-content\/uploads\/2025\/01\/robotWithForceAndColor-1536x2048.jpg 1536w, https:\/\/robot.e-nat.org\/wp-content\/uploads\/2025\/01\/robotWithForceAndColor-45x60.jpg 45w, https:\/\/robot.e-nat.org\/wp-content\/uploads\/2025\/01\/robotWithForceAndColor-scaled.jpg 1920w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n\n\n\n<p>\u91d1\u5b8f\u548c\u5be6\u3067\u3059\u3002<br>\u30de\u30eb\u30c1\u30bf\u30b9\u30af\u306e\u8aac\u660e\u3059\u308b\u305f\u3081\u306b\u3001\u30dd\u30fc\u30c8B\u306b\u30ab\u30e9\u30fc\u30bb\u30f3\u30b5\u30fc\u3001\u30dd\u30fc\u30c8A\u306b\u30d5\u30a9\u30fc\u30b9\u30bb\u30f3\u30b5\u30fc\uff08\u30bf\u30c3\u30c1\u30bb\u30f3\u30b5\u30fc\uff09\u3092\u63a5\u7d9a\u3057\u305f\u3002\u8d85\u97f3\u6ce2\u30bb\u30f3\u30b5\u30fc\u3082F\u30dd\u30fc\u30c8\u306b\u63a5\u7d9a\u3057\u3066\u3044\u308b\u304c\u4eca\u56de\u306f\u5229\u7528\u3057\u3066\u3044\u306a\u3044\u3002<br>\u524d\u56de\u3001Spike\u30a2\u30d7\u30ea\u3067\u306frunloop\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u3063\u3066\u30de\u30eb\u30c1\u30bf\u30b9\u30af\u304c\u5b9f\u73fe\u3067\u304d\u308b\u3068\u3044\u3046\u8a71\u3092\u66f8\u3044\u305f\u304c\u3001runloop\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u3048\u3070\u30de\u30eb\u30c1\u30bf\u30b9\u30af\u306b\u306a\u308b\u3068\u3044\u3046\u308f\u3051\u3067\u306f\u306a\u3044\u3002\u307e\u305a\u3001\u6b21\u306e\u30de\u30eb\u30c1\u30bf\u30b9\u30af\u3067\u306a\u3044\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u898b\u3066\u3082\u3089\u3044\u305f\u3044\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>from hub import sound,port\nimport force_sensor\nimport runloop\nimport color\nimport color_sensor\n\ndef red_detected():\n    return color_sensor.color(port.B) == color.RED\n\ndef force_pressed():\n    return force_sensor.pressed(port.A)\n\nasync def main():\n    while True:\n        if red_detected():\n            sound.beep(440, 1000000, 100)\n            while red_detected():\n                await runloop.sleep_ms(1)\n        elif force_pressed():\n            sound.beep(880, 200, 100)\n            while force_pressed():\n                await runloop.sleep_ms(1)\n        else:\n            sound.stop()\n\nrunloop.run(main())\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8a18\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u306fasync\u3067main\u95a2\u6570\u3092\u5b9a\u7fa9\u3057\u3066\u3001runloop.run(main())\u3067main\u95a2\u6570\u3092\u5b9f\u884c\u3057\u3066\u3044\u308b\u3002await\u3067\u5f85\u3064\u51e6\u7406\u3082\u5165\u308c\u3066\u3044\u308b\u306e\u3067\u30de\u30eb\u30c1\u30bf\u30b9\u30af\u51e6\u7406\u3092\u4f5c\u3063\u3066\u3044\u308b\u3088\u3046\u306b\u898b\u3048\u308b\u3002\u3057\u304b\u3057\u3001main\u95a2\u6570\u306ewhile\u30eb\u30fc\u30d7\u306e\u4e2d\u306e\u30b3\u30fc\u30c9\u306b\u6ce8\u76ee\u3057\u3066\u307b\u3057\u3044\u3002\u30dd\u30fc\u30c8B\u306e\u30ab\u30e9\u30fc\u30bb\u30f3\u30b5\u30fc\u304cif red_detected():\u3067True\uff08\u771f\uff09\u3092\u8fd4\u3059\u9593\u306f\u3001\u30dd\u30fc\u30c8A\u306e\u30d5\u30a9\u30fc\u30b9\u30bb\u30f3\u30b5\u30fc\u304c\u62bc\u3055\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u691c\u77e5\u3059\u308b\u3053\u3068\u306f\u306a\u3044\u3002\u3064\u307e\u308a\u3001\u8d64\u4fe1\u53f7\u3067\u6b62\u307e\u308a\u3001\u4f55\u304b\u306b\u3076\u3064\u304b\u3063\u305f\u3089\u30d0\u30c3\u30af\u3057\u306a\u3055\u3044\u3068\u30d7\u30ed\u30b0\u30e9\u30e0\u3057\u3066\u3082\u8d64\u4fe1\u53f7\u3067\u6b62\u307e\u3063\u3066\u3044\u308b\u9593\u306b\u30d5\u30a9\u30fc\u30b9\u30bb\u30f3\u30b5\u30fc\u304c\u62bc\u3055\u308c\u3066\u3082\u30ed\u30dc\u30c3\u30c8\u306f\u30d0\u30c3\u30af\u3057\u306a\u3044\u3002\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u306b\u306f\u3001\u8d64\u4fe1\u53f7\u3067\u6b62\u307e\u308a\u306a\u3055\u3044\u3068\u3044\u3046\u30bf\u30b9\u30af\u3068\u4f55\u304b\u306b\u3076\u3064\u304b\u3063\u305f\u3089\u30d0\u30c3\u30af\u3057\u306a\u3055\u3044\u3068\u3044\u3046\u30bf\u30b9\u30af\u3092\u540c\u6642\u306b\uff08\u53b3\u5bc6\u306b\u8a00\u3046\u3068\u540c\u6642\u3067\u306f\u306a\u3044\u304c\u91ce\u7403\u3067\u8a00\u3046\u540c\u6642\u30a2\u30a6\u30c8\u306e\u3088\u3046\u306a\u540c\u6642\uff09\u52d5\u304b\u3055\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3002\u305d\u308c\u304c\u30de\u30eb\u30c1\u30bf\u30b9\u30af\u3060\u3002<\/p>\n\n\n\n<p>runloop\u3067\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u8907\u6570run\u3059\u308b\u3068\u30de\u30eb\u30c1\u30bf\u30b9\u30af\u304c\u5b9f\u73fe\u3067\u304d\u308b\u3002\u30b3\u30eb\u30fc\u30c1\u30f3\uff08co-routine\uff09\u3068\u306f\u51e6\u7406\u3092\u4e2d\u65ad\u3057\u3066\u3001\u4efb\u610f\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u518d\u958b\u3067\u304d\u308b\u30eb\u30fc\u30c1\u30f3\u306e\u3053\u3068\u3067\u3042\u308b\u304c\u3001\u3053\u306e\u8aac\u660e\u3060\u3068\u3061\u3087\u3063\u3068\u308f\u304b\u308a\u306b\u304f\u3044\u3060\u308d\u3046\u3002<br>\u5177\u4f53\u7684\u306b\u8a00\u3046\u3068\u3001\u30b3\u30eb\u30fc\u30c1\u30f3\u306b\u306fawait\u30b3\u30de\u30f3\u30c9\u304c\u4e00\u3064\u5fc5\u8981\u3067\u3042\u308b\u3002\u5b9f\u884c\u6642\u9593\u304c\u77ed\u3044while \u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u30eb\u30fc\u30d7\u5185\u306b await runloop.sleep_ms() \u3092\u5165\u308c\u3066\u3001\u4ed6\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u304c\u958b\u59cb\u3067\u304d\u308b\u3088\u3046\u3059\u308b\u3002\u3064\u307e\u308a\u3001\u4ed6\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u306b\u5b9f\u884c\u3067\u304d\u308b\u9699\u3092\u4f5c\u308b\u308f\u3051\u3060\u3002\u305d\u3057\u3066\u3001\u8907\u6570\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u3092runloop.run()\u3067\u5b9f\u884c\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>from hub import port, sound\nimport color\nimport color_sensor\nimport force_sensor\nimport runloop\n\ndef red_detected():\n    return color_sensor.color(port.B) == color.RED\n\ndef force_pressed():\n    return force_sensor.pressed(port.A)\n\nasync def check_color():\n    while True:\n        while not red_detected():\n            await runloop.sleep_ms(1)\n        sound.beep(440, 1000000, 100)\n        while red_detected():\n            await runloop.sleep_ms(1)\n        sound.stop()\n\nasync def check_force_pressed():\n    while True:        \n        while not force_pressed():\n            await runloop.sleep_ms(1)\n        sound.beep(880, 200, 100)\n        # Wait until the force sensor is released.\n        while force_pressed():\n            await runloop.sleep_ms(1)\n\nrunloop.run(check_color(), check_force_pressed())\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8a18\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u306fcheck_color(), check_force_pressed()\u306e\u4e8c\u3064\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u5b9a\u7fa9\u3057\u3001runloop.run()\u3067\u5b9f\u884c\u3057\u3066\u3044\u308b\u3002\u30ab\u30e9\u30fc\u30bb\u30f3\u30b5\u30fc\u304c\u8d64\u8272\u3092\u898b\u3064\u3051\u305f\u5834\u5408\u3082\u30d5\u30a9\u30fc\u30b9\u30bb\u30f3\u30b5\u30fc\u304c\u62bc\u3055\u308c\u305f\u5834\u5408\u3082sound.beep()\u3067\u30d3\u30fc\u30d7\u97f3\u3092\u9cf4\u3089\u3059\u3060\u3051\u3060\u304c440\u3001880\u3068\u5468\u6ce2\u6570\u3092\u5909\u3048\u3066\u533a\u5225\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3057\u3001\u30ab\u30e9\u30fc\u30bb\u30f3\u30b5\u30fc\u304c\u8d64\u8272\u3092\u898b\u3064\u3051\u3066\u3044\u308b\u9593\u306b\u30d5\u30a9\u30fc\u30b9\u30bb\u30f3\u30b5\u30fc\u3092\u62bc\u3059\u3068\u9055\u3046\u97f3\u304c\u9cf4\u308b\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u91d1\u5b8f\u548c\u5be6\u3067\u3059\u3002\u30de\u30eb\u30c1\u30bf\u30b9\u30af\u306e\u8aac\u660e\u3059\u308b\u305f\u3081\u306b\u3001\u30dd\u30fc\u30c8B\u306b\u30ab\u30e9\u30fc\u30bb\u30f3\u30b5\u30fc\u3001\u30dd\u30fc\u30c8A\u306b\u30d5\u30a9\u30fc\u30b9\u30bb\u30f3\u30b5\u30fc\uff08\u30bf\u30c3\u30c1\u30bb\u30f3\u30b5 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[41],"tags":[42,38,15,14],"class_list":["post-7336","post","type-post","status-publish","format-standard","hentry","category-python","tag-python","tag-spikeprime","tag-15","tag-14","latest_post"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/robot.e-nat.org\/index.php?rest_route=\/wp\/v2\/posts\/7336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/robot.e-nat.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robot.e-nat.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robot.e-nat.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robot.e-nat.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7336"}],"version-history":[{"count":12,"href":"https:\/\/robot.e-nat.org\/index.php?rest_route=\/wp\/v2\/posts\/7336\/revisions"}],"predecessor-version":[{"id":7349,"href":"https:\/\/robot.e-nat.org\/index.php?rest_route=\/wp\/v2\/posts\/7336\/revisions\/7349"}],"wp:attachment":[{"href":"https:\/\/robot.e-nat.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robot.e-nat.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robot.e-nat.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}