{"id":146,"date":"2018-10-02T20:20:42","date_gmt":"2018-10-02T12:20:42","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=146"},"modified":"2020-06-20T23:11:55","modified_gmt":"2020-06-20T15:11:55","slug":"%e5%89%8d%e7%ab%af%e6%9c%ba%e5%99%a8%e4%ba%ba%e5%bc%80%e5%8f%91%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2018\/10\/02\/%e5%89%8d%e7%ab%af%e6%9c%ba%e5%99%a8%e4%ba%ba%e5%bc%80%e5%8f%91%e5%9f%ba%e7%a1%80\/","title":{"rendered":"\u524d\u7aef\u673a\u5668\u4eba\u5f00\u53d1\u57fa\u7840"},"content":{"rendered":"<hr \/>\n<p><img src=\"http:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/web_embedded.jpg\" alt=\"web embedded\" \/><\/p>\n<p>\u968f\u7740\u6280\u672f\u7684\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4f01\u4e1a\u5f00\u59cb\u62db\u8058\u524d\u7aef\u786c\u4ef6\uff0c\u5176\u5f00\u53d1\u6548\u7387\u548c\u6027\u80fd\u6ca1\u6709\u4efb\u4f55\u4e00\u79cd\u6280\u672f\u53ef\u4ee5\u5ab2\u7f8e\uff0c\u524d\u666f\u8d8a\u6765\u8d8a\u597d\u3002\u8fd9\u91cc\u7ed9\u5f00\u53d1\u4eba\u5458\u4e00\u4e2a\u673a\u5668\u4eba\u5f00\u53d1\u793a\u4f8b\uff0c\u7531\u4e8e\u673a\u5668\u4eba\u5f00\u53d1\u540e\u671f\u9700\u8981\u5927\u91cf\u7b97\u6cd5\u652f\u6301\uff0c\u8fd9\u91cc\u53ea\u505a\u786c\u4ef6\u5c42\u7684\u8bb2\u89e3\u3002<\/p>\n<p>\u786c\u4ef6\u8981\u6c42\u548c\u57fa\u7840\u914d\u7f6e\u8be6\u8baf\u76f8\u5173\u4e13\u4e1a\u4eba\u58eb\uff0c\u8fd9\u91cc\u5c06\u6240\u6709\u9700\u8981\u6587\u4ef6\u4e0b\u8f7d\u5230\u672c\u5730\uff0c\u5e76\u4f7f\u7528<code>npm<\/code>\u4f5c\u4e3a\u5f00\u53d1\u914d\u7f6e\u5de5\u5177\u3002<\/p>\n<h3>\u521b\u5efa\u9879\u76ee\u53ca\u76f8\u5173\u6587\u4ef6<\/h3>\n<p>\u9996\u5148\u4f7f\u7528\u521b\u5efa\u9879\u76ee\u5e76\u7528npm\u521d\u59cb\u5316\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">mkdir &amp;&amp; cd robot-demo\nnpm init -y\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u5728\u521d\u59cb\u5316\u65f6\u4f7f\u7528\u4e86npm\u9ed8\u8ba4\u7684\u914d\u7f6e\uff0c\u6839\u636e\u9700\u6c42\u4fee\u6539\uff0c\u4e5f\u53ef\u4ee5\u521b\u5efa\u5b8c\u6210\u540e\u4fee\u6539\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">app name: robot-demo\nversion: 0.1.0\ndescription: Hello, Robot!\nauthor: indeex\n<\/code><\/pre>\n<p>\u7531\u4e8e\u662f\u524d\u7aef\u7684\u4e00\u4e9b\u5e38\u7528\u6587\u4ef6\uff0c\u521b\u5efa\u5176\u4ed6\u6587\u4ef6\u7565\u3002<\/p>\n<h3>\u6dfb\u52a0\u5f00\u5173\u5916\u8bbe<\/h3>\n<p>\u6dfb\u52a0\u5f00\u5173\u548c<code>led<\/code>\u5916\u8bbe\uff0c\u8fd9\u91cc\u5c06\u5f00\u5173\u578b\u53f7\u70e7\u6210<code>Btn001<\/code>\uff0c\u5f00\u5173\u540d\u70e7\u6210<code>button<\/code>:<\/p>\n<p>\u6dfb\u52a0\u5f00\u5173\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">npm add --dev device button\n<\/code><\/pre>\n<p>\u6dfb\u52a0led\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">npm add --dev device led\n<\/code><\/pre>\n<p>\u586b\u5165\u578b\u53f7\u5e76\u786e\u8ba4\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">model: Btn001\nselect a driver for device \"button\"(Btn001): (Use arrow keys)\n<\/code><\/pre>\n<h3>\u63a5\u7ebf<\/h3>\n<p>\u83b7\u53d6\u63a5\u7ebf\u5e03\u5c40\uff08\u56fe\uff09\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">npm get device layout\n<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u53ef\u89c6\u5316\u63a5\u7ebf\u56fe\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">npm get device layout --visual\n<\/code><\/pre>\n<p>\u6839\u636e\u63a5\u7ebf\u56fe\u5c06\u76f8\u5e94\u7684\u7ebf\u63a5\u4e0a\u5c31\u53ef\u4ee5\u5f00\u53d1\u4e86\u3002<\/p>\n<h3>\u9a71\u52a8<\/h3>\n<p>\u5f00\u53d1\u9a71\u52a8\u7684\u65f6\u5019\u6ce8\u610f\u5bf9\u5185\u5b58\u7684\u63a7\u5236\uff0c\u9a71\u52a8\u5f00\u53d1\u7565\u3002<\/p>\n<h3>\u5f00\u5173\u63a7\u5236<\/h3>\n<p>\u8fd9\u91cc\u9700\u8981\u5bf9\u5f00\u5173\u7684\u6309\u4e0b\u548c\u91ca\u653e\u6dfb\u52a0\u4e8b\u4ef6\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">class Button{\n    public button!: any;\n    public led!: any;\n    constructor(){\n        super();\n        this.ready();\n        this.end();\n    }\n\n    private ready(error: Error, device: any): void{\n        if (error) {\n            console.log(error);\n            throw error;\n        }\n\n        this.button = device.getByName('button');\n        this.led = device.getByName('led-red');\n\n        this.button.on('push', () =&gt; {\n            console.log('btn down');\n            this.led.turnOn();\n        });\n\n        this.button.on('release', () =&gt; {\n            console.log('btn up');\n            this.led.turnOff();\n        });\n    }\n\n    public end(): void{\n        this.led.turnOff();\n    }\n}\n<\/code><\/pre>\n<h3>\u6dfb\u52a0\u8ddd\u79bb\u4f20\u611f\u5668<\/h3>\n<pre><code class=\"language-javascript line-numbers\">npm add --dev device distance\n<\/code><\/pre>\n<h3>\u83b7\u53d6\u8ddd\u79bb\u4f20\u611f\u5668\u6570\u636e<\/h3>\n<pre><code class=\"language-javascript line-numbers\">this.distance = device.getByName('distance');\n\n\/\/...\n\nthis.distance.getDistance( (error: Error, distance: any) =&gt; {\n  console.log('distance: ' + distance);\n});\n\n\/\/...\n\n\nthis.distance.getTemperature( (error: Error, temperature: any) =&gt; {\n  console.log('temperature: ' + temperature);\n});\n\n\n<\/code><\/pre>\n<p>\u7531\u4e8e\u5728\u786c\u4ef6\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u6e29\u5ea6\u90fd\u4f1a\u4e0a\u5347\uff0c\u8fd9\u91cc\u5728\u70e7\u5236\u65f6\u5bf9\u8ddd\u79bb\u4f20\u611f\u5668\u6dfb\u52a0\u4e86\u6e29\u5ea6\u4f20\u611f\uff0c\u53ef\u4ee5\u6d4b\u91cf\u4f20\u611f\u5668\u81ea\u8eab\u6e29\u5ea6\u3002<\/p>\n<h3>\u7f51\u7edc<\/h3>\n<p>\u5c06\u6240\u6709\u65e5\u5fd7\u8fdb\u884c\u7f51\u7edc\u4f20\u8f93\uff0c\u5c31\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u67e5\u770b\u5404\u786c\u4ef6\u7684\u8fd0\u884c\u60c5\u51b5\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">public http!: Http;\n\n\n\/\/...\n\nconst options = {\n    host: 'http:\/\/indeex.cc',\n    path: '\/post',\n    method: 'POST',\n    headers: {\n    }\n};\n\n\/\/...\n\n\npublic postState(state: any) :void{\n    options.headers['Content-Length'] = state.length;\n    let req = http.request(options, (res) =&gt; {\n        res.on('data', (chunk) =&gt; {\n            console.log('chunk: ' + chunk);\n        });\n    });\n\n    req.write(state);\n    req.end();\n}\n\n\n\/\/...\n\nthis.button.on('push', () =&gt; {\n    console.log('btn down');\n    this.led.turnOn(() =&gt; {\n        this.postState('turn on');\n    });\n});\n\nthis.button.on('release', () =&gt; {\n    console.log('btn up');\n    this.led.turnOff(() =&gt; {\n        this.postState('turn off');\n    });\n});\n<\/code><\/pre>\n<h3>\u6d4b\u8bd5<\/h3>\n<p>\u91cd\u65b0\u90e8\u7f72\u540e\u5f00\u59cb\u6d4b\u8bd5\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">npm get device layout -S\nnpm test\n<\/code><\/pre>\n<p>\u7136\u540e\u6309\u4e0b\u5f00\u5173\u6d4b\u8bd5\u76f8\u5e94\u64cd\u4f5c\u7684\u54cd\u5e94\u3001\u5ef6\u8fdf\u7b49\u6570\u636e\uff0c\u53ef\u4ee5\u542f\u7528log\u3002<\/p>\n<h3>log<\/h3>\n<p>\u4e3a\u4e86\u51cf\u5c11\u67e5\u627ebug\u7684\u65f6\u95f4\uff0c\u9700\u8981\u5f97\u5230\u8fd0\u884c\u65e5\u5fd7\uff0c\u6240\u4ee5\u8fd9\u91cc\u90e8\u7f72log\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">npm get log --all\n<\/code><\/pre>\n<p>\u91cd\u65b0\u90e8\u7f72\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002<\/p>\n<p>code enjoy! &#x1f92a;&#x1f92a;&#x1f92a;<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/indeex.cc\/\">http:\/\/indeex.cc<\/a><\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u6280\u672f\u7684\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4f01\u4e1a\u5f00\u59cb\u62db\u8058\u524d\u7aef\u786c\u4ef6\uff0c\u5176\u5f00\u53d1\u6548\u7387\u548c\u6027\u80fd\u6ca1\u6709\u4efb\u4f55\u4e00\u79cd\u6280\u672f\u53ef\u4ee5\u5ab2\u7f8e\uff0c\u524d\u666f\u8d8a\u6765\u8d8a<a href=\"https:\/\/blog.indeex.club\/index.php\/2018\/10\/02\/%e5%89%8d%e7%ab%af%e6%9c%ba%e5%99%a8%e4%ba%ba%e5%bc%80%e5%8f%91%e5%9f%ba%e7%a1%80\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/146"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":149,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/146\/revisions\/149"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}