{"id":4143,"date":"2015-10-04T00:17:31","date_gmt":"2015-10-03T15:17:31","guid":{"rendered":"http:\/\/mirror.boy.jp\/?p=2309"},"modified":"2015-10-04T00:17:31","modified_gmt":"2015-10-03T15:17:31","slug":"raspberry-pi-2-openframeworks-i2cinvensense-itg-3200","status":"publish","type":"post","link":"https:\/\/inage.ksd6700.net\/?p=4143","title":{"rendered":"Raspberry Pi 2 &amp; openFrameworks &#12391;I2C&#25509;&#32154;&#12398;&#12472;&#12515;&#12452;&#12525;&#12475;&#12531;&#12469;&#12540;(InvenSense ITG-3200)&#12434;&#20351;&#12358;"},"content":{"rendered":"<h3>Raspberry Pi2 &amp; openFrameworks\u3067I2C\u63a5\u7d9a\u306e\u30b8\u30e3\u30a4\u30ed\u30bb\u30f3\u30b5\u30fc\u3092\u4f7f\u3063\u3066\u307f\u305f<\/h3>\n<p><iframe loading=\"lazy\" src=\"https:\/\/vine.co\/v\/e2ztYvETj1r\/embed\/simple?audio=1\" width=\"600\" height=\"600\" frameborder=\"0\"><\/iframe><script src=\"https:\/\/platform.vine.co\/static\/scripts\/embed.js\"><\/script><br \/>\nInvenSense ITG-3200\u306e\u30d6\u30ec\u30a4\u30af\u30a2\u30a6\u30c8\u30dc\u30fc\u30c9\u3092SwitchScience\u3067\u8cfc\u5165\u3057\u3001\u30bb\u30c3\u30c6\u30a3\u30f3\u30b0\u3084\u30b3\u30fc\u30c9\u306f\u3001<a href=\"https:\/\/learn.sparkfun.com\/tutorials\/itg-3200-hookup-guide\">\u3053\u3061\u3089\u306e\u30da\u30fc\u30b8<\/a>\u3092\u53c2\u8003\u306b\u3057\u307e\u3057\u305f\u3002Raspberry Pi\u306b\u304a\u3051\u308bwiringPi\u3068I2C\u81ea\u4f53\u306e\u30bb\u30c3\u30c6\u30a3\u30f3\u30b0\u306f\u3001\u30b0\u30b0\u308b\u3068\u8272\u3005\u3042\u308b\u306e\u3067openFrameworks\u306e\u90e8\u5206\u306e\u307f\u3002wiringPiI2CSetup\u3092\u4f7f\u3063\u305f\u3089\u3068\u306b\u304b\u304f\u697d\u3067\u3057\u305f\u3002<\/p>\n<h3>openFrameworks\u306e\u30b3\u30fc\u30c9<\/h3>\n<\/p>\n<pre class=\"crayon-plain-tag\">#include &quot;ofApp.h&quot;\r\n#include &quot;wiringPiI2C.h&quot;\r\n\r\nusing namespace std;\r\n\r\nconst auto itgAddress = 0x69;\r\n\r\nconst char WHO_AM_I = 0x00;\r\nconst char SMPLRT_DIV= 0x15;\r\nconst char DLPF_FS = 0x16;\r\nconst char GYRO_XOUT_H = 0x1D;\r\nconst char GYRO_XOUT_L = 0x1E;\r\nconst char GYRO_YOUT_H = 0x1F;\r\nconst char GYRO_YOUT_L = 0x20;\r\nconst char GYRO_ZOUT_H = 0x21;\r\nconst char GYRO_ZOUT_L = 0x22;\r\n\r\nconst char DLPF_CFG_0 = 1&lt;&lt;0;\r\nconst char DLPF_CFG_1 = 1&lt;&lt;1;\r\nconst char DLPF_CFG_2 = 1&lt;&lt;2;\r\nconst char DLPF_FS_SEL_0 = 1&lt;&lt;3;\r\nconst char DLPF_FS_SEL_1 = 1&lt;&lt;4;\r\n\r\nauto fd = -1;\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::setup(){\r\n\r\n        \/\/I2C Setting\r\n        fd = wiringPiI2CSetup(itgAddress);\r\n        cout &lt;&lt; &quot;fd:&quot; &lt;&lt; fd  &lt;&lt; &quot;\\n&quot;;\r\n        cout &lt;&lt; &quot;WHO_AM_I:&quot; &lt;&lt; wiringPiI2CReadReg16(fd,WHO_AM_I) &lt;&lt; &quot;\\n&quot;;       \r\n        cout &lt;&lt; &quot;Write DLPF_FS:&quot; &lt;&lt; wiringPiI2CWriteReg8(fd,DLPF_FS,(DLPF_FS_SEL_0|DLPF_FS_SEL_1|DLPF_CFG_0)) &lt;&lt; &quot;\\n&quot;;\r\n        cout &lt;&lt; &quot;Write SMPLRT_DIV:&quot; &lt;&lt; wiringPiI2CWriteReg8(fd,SMPLRT_DIV,9) &lt;&lt; &quot;\\n&quot;;\r\n\r\n        ofSetFullscreen(true);  \r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::update(){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::draw(){\r\n\r\n        ofPushMatrix();\r\n\r\n        ofTranslate(0,ofGetHeight()\/2);\r\n\r\n        ofBackground(0);\r\n\r\n        function&lt;int16_t(char H,char L)&gt; getValue = [](char H,char L){\r\n\r\n                int data = 0;\r\n                data = static_cast&lt;int8_t&gt;(wiringPiI2CReadReg8(fd,H)) &lt;&lt; 8;\r\n                data += static_cast&lt;int8_t&gt;(wiringPiI2CReadReg8(fd,L));\r\n\r\n                return data;\r\n        };\r\n\r\n        ofSetColor(255);\r\n\r\n        const auto MAX_DATA = 10000;\r\n\r\n        auto x = getValue(GYRO_XOUT_H,GYRO_XOUT_L);\r\n        ofRect(0,0,ofGetWidth()\/3.,ofMap(x,-MAX_DATA,MAX_DATA,ofGetHeight()\/2.,-ofGetHeight()\/2.));\r\n\r\n        ofTranslate(ofGetWidth()\/3,0);\r\n        auto y = getValue(GYRO_YOUT_H,GYRO_YOUT_L);\r\n        ofRect(0,0,ofGetWidth()\/3,ofMap(y,-MAX_DATA,MAX_DATA,ofGetHeight()\/2,-ofGetHeight()\/2));\r\n\r\n        ofTranslate(ofGetWidth()\/3,0);\r\n        auto z = getValue(GYRO_ZOUT_H,GYRO_ZOUT_L);\r\n        ofRect(0,0,ofGetWidth()\/3,ofMap(z,-MAX_DATA,MAX_DATA,ofGetHeight()\/2,-ofGetHeight()\/2));\r\n\r\n        ofPopMatrix();\r\n}<\/pre>\n<p>Raspberry Pi\u7528\u306eopenFrameworks\u304cNightly Build\u3067\u3057\u304b\u52d5\u304b\u305b\u306a\u304b\u3063\u305f\u3002\u3051\u308c\u3069\u3001openFrameworks0.9.0\u304b\u3089C++11\u306b\u306a\u3063\u3066\u305f\u306e\u3067function\u3084\u30e9\u30e0\u30c0\u5f0f\u3092\u4f7f\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Raspberry Pi2 &amp; openFrameworks&#12391; &#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/inage.ksd6700.net\/index.php?rest_route=\/wp\/v2\/posts\/4143"}],"collection":[{"href":"https:\/\/inage.ksd6700.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/inage.ksd6700.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/inage.ksd6700.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/inage.ksd6700.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4143"}],"version-history":[{"count":2,"href":"https:\/\/inage.ksd6700.net\/index.php?rest_route=\/wp\/v2\/posts\/4143\/revisions"}],"predecessor-version":[{"id":4152,"href":"https:\/\/inage.ksd6700.net\/index.php?rest_route=\/wp\/v2\/posts\/4143\/revisions\/4152"}],"wp:attachment":[{"href":"https:\/\/inage.ksd6700.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/inage.ksd6700.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/inage.ksd6700.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}