{"id":8528,"date":"2015-11-10T23:41:49","date_gmt":"2015-11-10T14:41:49","guid":{"rendered":"http:\/\/www.maocat.net\/?p=8528"},"modified":"2017-09-25T21:26:22","modified_gmt":"2017-09-25T12:26:22","slug":"pi%e3%81%a7java%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.maocat.net\/?p=8528","title":{"rendered":"Pi\u3067Java\u3059\u308b"},"content":{"rendered":"<p>\u3053\u3053\u304b\u3089\u7d30\u304b\u3044\u5236\u5fa1\u304c\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u308b\u3002\u30de\u30aa\u3067\u3059\u3002<\/p>\n<p>\u4eca\u307e\u3067\u306f\u30b3\u30de\u30f3\u30c9\u3067\u500b\u5225\u306b\u90e8\u54c1\u3092\u5236\u5fa1\u3057\u3066\u305f\u3051\u3069\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u5236\u5fa1\u3059\u308b\u4e8b\u306b\u3088\u308a\u8907\u96d1\u306a\u4e8b\u304c\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u308b\u3002<br \/>\n\u4f7f\u3048\u308b\u8a00\u8a9e\u306f\u69d8\u3005\u3067\u63a8\u5968\u306fPython\u3089\u3057\u3044\u3002<br \/>\n\u3053\u3053\u3067\u306f\u65e2\u306b\u958b\u767a\u74b0\u5883\u304c\u6574\u3063\u3066\u3044\u308bJava\u3092\u4f7f\u7528\u3057\u3066\u3044\u304f\u3002<\/p>\n<p>GPIO\u3092\u4f7f\u3046\u306b\u306f\u3001<a href=\"http:\/\/pi4j.com\/\" target=\"_blank\" rel=\"noopener\">Pi4J<\/a>\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u5fc5\u8981\u306b\u306a\u308b\u3002<br \/>\n\u3082\u3057\u3001RaspberryPi\u304c\u76f4\u63a5\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306b\u7e4b\u304c\u3063\u3066\u3044\u308b\u306a\u3089\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306f\u7c21\u5358\u306b\u7d42\u308f\u308b\u3002<\/p>\n<pre>curl -s get.pi4j.com | sudo bash<\/pre>\n<p>\u3053\u308c\u3092sudo\u304c\u51fa\u6765\u308b\u30e6\u30fc\u30b6\u3067\u5b9f\u884c\u3059\u308b\u3060\u3051\u3060\u3002<br \/>\nJDK\u306a\u3093\u304b\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u5165\u3063\u3066\u308b\u3088\u3046\u306a\u306e\u3067\u3001\u554f\u984c\u7121\u3044\u3063\u307d\u3044\u3002<br \/>\n\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u300c\/opt\/pi4j\/lib\u300d\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u308b\u3002<\/p>\n<p>\u4eca\u56de\u306e\u56de\u8def\u306f<a href=\"https:\/\/www.maocat.net\/?p=8502\">\u524d\u56de\u306e\u30bf\u30af\u30bf\u30a4\u30eb\u30b9\u30a4\u30c3\u30c1<\/a>\u3068<a href=\"https:\/\/www.maocat.net\/?p=8469\">\u524d\u3005\u56de\u306e\u30d5\u30eb\u30ab\u30e9\u30fcLED<\/a>\u3092\u4f7f\u3063\u3066\u3001\u30b9\u30a4\u30c3\u30c1\u3092\u62bc\u3059\u3054\u3068\u306b\u8272\u304c\u5909\u308f\u3063\u3066\u3044\u304f\u7269\u3092\u4f5c\u308b\u3002<br \/>\n\u6ce8\u610f\u70b9\u306f\u30b3\u30de\u30f3\u30c9\u306e\u6642\u3068GPIO\u306e\u756a\u53f7\u304c\u9055\u3046\u6240\u3060\u3002<br \/>\n\u30b3\u30de\u30f3\u30c9\u3067\u306fGPIO16\u3060\u3063\u305f\u6240\u304c\u3001Pi4J\u3067\u306fGPIO27\u3068\u306a\u308b\u3002<br \/>\n\u8a73\u7d30\u306f\u516c\u5f0f\u306ePin Numbering(Raspberry Pi 2 Model B\u306f<a href=\"http:\/\/pi4j.com\/pins\/model-2b-rev1.html\" target=\"_blank\" rel=\"noopener\">\u3053\u3053<\/a>)\u306b\u3042\u308b\u306e\u3067\u3001\u78ba\u8a8d\u3057\u3088\u3046\u3002<\/p>\n<pre>import java.io.BufferedReader;\r\nimport java.io.IOException;\r\nimport java.io.InputStreamReader;\r\n\r\nimport com.pi4j.io.gpio.GpioController;\r\nimport com.pi4j.io.gpio.GpioFactory;\r\nimport com.pi4j.io.gpio.GpioPinDigitalInput;\r\nimport com.pi4j.io.gpio.GpioPinDigitalOutput;\r\nimport com.pi4j.io.gpio.PinPullResistance;\r\nimport com.pi4j.io.gpio.PinState;\r\nimport com.pi4j.io.gpio.RaspiPin;\r\nimport com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;\r\nimport com.pi4j.io.gpio.event.GpioPinListenerDigital;\r\n\r\npublic class ButtonDeLedTikatika {\r\n\r\n    public static void main(String[] args) {\r\n\r\n        \/\/ #1\r\n        final GpioController gpio = GpioFactory.getInstance();\r\n\r\n        \/\/ #2\r\n        final GpioPinDigitalOutput led_red = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_29, PinState.HIGH);\r\n        led_red.setShutdownOptions(true);\r\n\r\n        final GpioPinDigitalOutput led_green = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_25, PinState.HIGH);\r\n        led_green.setShutdownOptions(true);\r\n\r\n        final GpioPinDigitalOutput led_blue = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_28, PinState.HIGH);\r\n        led_blue.setShutdownOptions(true);\r\n\r\n        \/\/ #3\r\n        final GpioPinDigitalInput button = gpio.provisionDigitalInputPin(RaspiPin.GPIO_27, PinPullResistance.PULL_UP);\r\n        button.setShutdownOptions(true);\r\n\r\n        \/\/ #4\r\n        button.addListener(new GpioPinListenerDigital() {\r\n\r\n            private byte flag;\r\n\r\n            @Override\r\n            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent pin_event) {\r\n\r\n                if(pin_event.getState().isLow()) {\r\n                    flag += flag &lt; 7 ? 1 : -flag;\r\n\r\n                    led_red.setState((flag &amp; 1) != 0 ? PinState.LOW : PinState.HIGH);\r\n                    led_green.setState((flag &amp; 2) != 0 ? PinState.LOW : PinState.HIGH);\r\n                    led_blue.setState((flag &amp; 4) != 0 ? PinState.LOW : PinState.HIGH);\r\n                }\r\n\r\n            }\r\n        });\r\n\r\n        \/\/ #5\r\n        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));\r\n        try {\r\n            while(!in.ready()) {\r\n                Thread.sleep(1000);\r\n            }\r\n\r\n            in.readLine();\r\n        } catch (IOException | InterruptedException e) {\r\n            e.printStackTrace();\r\n        }\r\n\r\n         \/\/ #6\r\n         gpio.shutdown();\r\n\r\n    }\r\n\r\n}<\/pre>\n<p>\u4ee5\u4e0a\u304c\u5236\u5fa1\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3002<br \/>\n#1\u3067GPIO\u3092\u306a\u3093\u3084\u304b\u3093\u3084\u3059\u308b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u8cb0\u3046\u3002<br \/>\n\u306a\u3093\u3084\u304b\u3093\u3084\u3059\u308b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f7f\u3063\u3066\u3001#2\u3067LED\u90e8\u5206\u306e\u8a2d\u5b9a\u3092\u3057\u3066\u3044\u308b\u3002<br \/>\n\u8d64\u3092GPIO29\u3001\u7dd1\u3092GPIO25\u3001\u9752\u3092GPIO28\u306b\u7e4b\u3044\u3067\u3042\u308b\u3002<br \/>\n\u4e8c\u3064\u76ee\u306e\u5f15\u6570\u306b\u3042\u308bPinState.HIGH\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u51fa\u529b\u72b6\u614b\u3092\u6307\u5b9a\u3057\u3066\u3044\u308b\u3002<br \/>\n\u3053\u306eLED\u306f\u96fb\u5727\u304c\u304b\u304b\u3063\u3066\u3044\u308b\u3068\u6d88\u3048\u308b\u306e\u3067\u3001\u5b9f\u884c\u3055\u308c\u308b\u3068\u6d88\u3048\u305f\u72b6\u614b\u306b\u306a\u308b\u3002<br \/>\nsetShutdownOptions\u306f\u7d42\u4e86\u6642\u306b\u95a2\u308f\u308b\u8a2d\u5b9a\u306a\u306e\u3067\u3001\u305d\u3063\u3061\u3067\u8aac\u660e\u3059\u308b\u306e\u3067\u98db\u3070\u3059\u3002<br \/>\n#3\u306f\u30bf\u30af\u30bf\u30a4\u30eb\u30b9\u30a4\u30c3\u30c1\u90e8\u5206\u306e\u8a2d\u5b9a\u3060\u3002<br \/>\nGPIO27\u306b\u63a5\u7d9a\u3057\u3066\u3042\u308a\u3001\u4e8c\u3064\u76ee\u306e\u5f15\u6570PinPullResistance.PULL_UP\u306f\u30d7\u30eb\u30a2\u30c3\u30d7\u62b5\u6297\u306e\u8a2d\u5b9a\u3092\u3057\u3066\u3044\u308b\u3002<br \/>\n#4\u306f\u30bf\u30af\u30bf\u30a4\u30eb\u30b9\u30a4\u30c3\u30c1\u306e\u72b6\u614b\u304c\u5909\u5316\u3055\u308c\u305f\u6642\u306e\u51e6\u7406\u3092\u767b\u9332\u3057\u3066\u3044\u308b\u3002<br \/>\n\u5404\u8272\u306e\u72b6\u614b\u306f3\u30d3\u30c3\u30c8(0\uff5e7)\u3067\u8a18\u9332\u3057\u3066\u3001\u30d3\u30c3\u30c8\u304c\u7acb\u3063\u3066\u3044\u308b\u90e8\u5206\u304c\u5149\u308b\u3002<br \/>\n\u30b9\u30a4\u30c3\u30c1\u304c\u62bc\u3055\u308c\u308b\u3068\u51e6\u7406\u304c\u3055\u308c\u3001\u96fb\u5727\u304cLow\u306b\u306a\u308b\u306e\u3067if\u5206\u306e\u4e2d\u304c\u5b9f\u884c\u3055\u308c\u308b\u3002<br \/>\n\u30d5\u30e9\u30b0\u7ba1\u7406\u3092\u3057\u3066\u3044\u308bflag\u306b1\u3092\u52a0\u7b97\u3059\u308b\u304b\u30017\u304b\u3089\u6ea2\u308c\u308b\u5834\u5408\u306f0\u306b\u623b\u3057\u3066\u3044\u308b\u3002<br \/>\nsetState\u3067PinState.LOW\u304bPinState.HIGH\u3092\u6307\u5b9a\u3059\u308b\u4e8b\u3067\u3001\u96fb\u5727\u3092\u5909\u66f4\u3059\u308b\u4e8b\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u3053\u308c\u306b\u3088\u308a\u30b9\u30a4\u30c3\u30c1\u3092\u62bc\u3059\u305f\u3073\u306b\u3001\u6d88\u3048\u305f\u72b6\u614b\u304b\u3089\u30ec\u30c3\u30c9\u2192\u30b0\u30ea\u30fc\u30f3\u2192\u30a4\u30a8\u30ed\u30fc\u2192\u30d6\u30eb\u30fc\u2192\u30de\u30bc\u30f3\u30bf\u2192\u30b7\u30a2\u30f3\u2192\u30db\u30ef\u30a4\u30c8\u3068\u306a\u308a\u3001\u6b21\u3067\u307e\u305f\u6d88\u3048\u308b\u3002<br \/>\n#5\u306f\u30a8\u30f3\u30bf\u30fc\u30ad\u30fc\u3092\u62bc\u3059\u307e\u3067\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u629c\u3051\u306a\u3044\u3088\u3046\u306b\u30eb\u30fc\u30d7\u3055\u305b\u3066\u3044\u308b\u3002<br \/>\nGPIO\u306e\u5236\u5fa1\u90e8\u5206\u306f\u5225\u30b9\u30ec\u30c3\u30c9\u3067\u52d5\u304f\u70ba\u306b\u3001\u3053\u306e\u90e8\u5206\u304c\u7121\u3044\u3068\u51e6\u7406\u304c\u7d42\u4e86\u3057\u3066\u3057\u307e\u3046\u3002<br \/>\n#6\u306e\u90e8\u5206\u3067GPIO\u3092\u958b\u653e(\u30b3\u30de\u30f3\u30c9\u306b\u304a\u3051\u308bunexport)\u3057\u305f\u308a\u3059\u308b\u3002<br \/>\n\u3057\u304b\u3057\u3001#2\u3084#3\u3067\u3084\u3063\u3066\u3044\u308bsetShutdownOptions(true)\u3092\u6307\u5b9a\u3057\u3066\u3044\u306a\u3044\u3068\u958b\u653e\u3057\u3066\u304f\u308c\u306a\u3044\u3002<br \/>\nsetShutdownOptions\u3067\u306f\u8272\u3005\u3068\u958b\u653e\u5f8c\u306e\u30d4\u30f3\u306e\u72b6\u614b\u3082\u6307\u5b9a\u51fa\u6765\u305f\u308a\u3059\u308b\u3002<\/p>\n<p>\u5b9f\u884c\u306f\u4e00\u822c\u30e6\u30fc\u30b6\u304b\u3089\u30af\u30e9\u30b9\u30d5\u30a1\u30a4\u30eb\u304c\u7f6e\u3044\u3066\u3042\u308b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3067\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u51fa\u6765\u308b\u3002<\/p>\n<pre>sudo java -cp .:\"\/opt\/pi4j\/lib\/*\" ButtonDeLedTikatika<\/pre>\n<p>\u610f\u5916\u3068\u30b5\u30af\u30b5\u30af\u3068\u3053\u3053\u307e\u3067\u51fa\u6765\u3066\u3057\u307e\u3063\u305f\u3002<br \/>\n\u57fa\u790e\u306f\u7d42\u308f\u3063\u305f\u306e\u3067\u3001\u3053\u3053\u304b\u3089\u96e3\u6613\u5ea6\u304c\u30c9\u30f3\u30c9\u30f3\u4e0a\u304c\u3063\u3066\u5927\u5909\u306a\u4e8b\u306b\u306a\u308a\u305d\u3046\u3060\u30fb\u30fb\u30fb(\u9060\u3044\u76ee)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3053\u304b\u3089\u7d30\u304b\u3044\u5236\u5fa1\u304c\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u308b\u3002\u30de\u30aa\u3067\u3059\u3002 \u4eca\u307e\u3067\u306f\u30b3\u30de\u30f3\u30c9\u3067\u500b\u5225\u306b\u90e8\u54c1\u3092\u5236\u5fa1\u3057\u3066\u305f\u3051\u3069\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u5236\u5fa1\u3059\u308b\u4e8b\u306b\u3088\u308a\u8907\u96d1\u306a\u4e8b\u304c\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u308b\u3002 \u4f7f\u3048\u308b\u8a00\u8a9e\u306f\u69d8\u3005\u3067\u63a8\u5968\u306fPython\u3089\u3057\u3044\u3002 \u3053\u3053\u3067\u306f\u65e2\u306b\u958b\u767a\u74b0\u5883 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","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_memberships_contains_paid_content":false,"footnotes":""},"categories":[127],"tags":[122,214],"class_list":["post-8528","post","type-post","status-publish","format-standard","hentry","category-127","tag-java","tag-raspberry_pi"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1vAGx-2dy","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.maocat.net\/index.php?rest_route=\/wp\/v2\/posts\/8528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.maocat.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.maocat.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.maocat.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.maocat.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8528"}],"version-history":[{"count":4,"href":"https:\/\/www.maocat.net\/index.php?rest_route=\/wp\/v2\/posts\/8528\/revisions"}],"predecessor-version":[{"id":10068,"href":"https:\/\/www.maocat.net\/index.php?rest_route=\/wp\/v2\/posts\/8528\/revisions\/10068"}],"wp:attachment":[{"href":"https:\/\/www.maocat.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.maocat.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.maocat.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}