{"id":164,"date":"2019-08-23T21:35:53","date_gmt":"2019-08-23T13:35:53","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=164"},"modified":"2020-06-20T23:20:04","modified_gmt":"2020-06-20T15:20:04","slug":"led%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2019\/08\/23\/led%e6%8e%a7%e5%88%b6\/","title":{"rendered":"LED\u63a7\u5236"},"content":{"rendered":"<hr \/>\n<p>\u7eda\u4e3d\u591a\u5f69\u7684\u706f\u5149\u5f88\u5438\u5f15\u4eba\uff0c\u90a3\u4e48\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528LED\u3002<\/p>\n<p>\u5143\u5668\u4ef6\uff1a<\/p>\n<blockquote><p>\n  \u6240\u6709\u7528\u5230\u7684\u5143\u5668\u4ef6\uff0c\u8fd9\u91cc\u4e0d\u5728\u5217\u51fa\uff0c\u4e3b\u8981LED\u3001\u9634\u6781\u7ba1\u3001\u963b\u5c3c\u5668\u3001\u4e3b\u677f\u3001\u6309\u94ae\u3001\u7535\u7ebf\n<\/p><\/blockquote>\n<p>\u8fd9\u91cc\u4f7f\u7528\u7684\u901a\u7528\u578bLED\uff0c\u5206\u522b\u662fR\u3001G\u3001B\u3001Y\u3001O LED\uff0cGPIO\u63a5\u53e3\uff1a<\/p>\n<p><img src=\"https:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/led1.jpg\" alt=\"LED\u63a7\u5236\" \/><\/p>\n<p>\u8f93\u5165\u662f\uff1a<\/p>\n<p><img src=\"https:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/led2.jpg\" alt=\"LED\u63a7\u5236\" \/><\/p>\n<p>\u7531\u4e8eLED\u957f\u65f6\u95f4\u6ca1\u6709\u7528\uff0c\u9996\u5148\u6d4b\u8bd5\u4e00\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">const Io: unknow = require('yourself deivce');\nconst board\uff1a Io.Board = new Io.Board();\n\nboard.on('ready', (): void =&gt; {\n  const led1: unknow = new Io.Led(2);\n  const led2: unknow = new Io.Led(3);\n\n  board.repl.inject({\n    led1: led1,\n    led2: led2\n  });\n});\n<\/code><\/pre>\n<p>\u7136\u540e\u5f00\u542fLED\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">&gt;&gt; led1.on()\n&gt;&gt; led2.brightness(128)\n<\/code><\/pre>\n<p>\u5982\u679c\u62a5\u9519\uff0c\u8bf7\u5c1d\u8bd5REPL\u3002<\/p>\n<h3>\u5b9a\u4e49\u95ea\u70c1\u89c4\u5219<\/h3>\n<p>\u5f53LED\u53ef\u4ee5\u95ea\u70c1\u540e\uff0c\u6539\u53d8\u4e00\u4e0b\u95ea\u70c1\u89c4\u5219\uff0cAnimateLed\u7c7b\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">const options: Object = {\n  easing    : 'inOutSine',\n  metronomic: true,\n  loop      : true,\n  keyFrames : [0, 255],\n  duration  : 1000\n};\nconst pulsingLED: any = new Io.Led(3);\nconst options: any    = { \/* \u95ea\u70c1\u4e86\u3002\u3002 *\/ }; \nconst animation: any  = new Io.Animation(pulsingLED);\/\/\u4e0d\u540c\u4e3b\u677f\u95ea\u70c1\u89c4\u5219\u8bf7\u53c2\u8003\u76f8\u5173\u6587\u6863\nanimation.enqueue(options);\n\n\/\/pulsingLED.pulse(1000); \u89c4\u5f8b\u95ea\u70c1\n<\/code><\/pre>\n<p>\u6216\u8005\u8fd9\u6837\u8bbe\u7f6e\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">board.on('ready', (): void =&gt; {\n  const pulsingLED: any = new Io.Led(3);\n  const timerLength: number = 10000;\n  let t: number = setTimeout((): void =&gt; {\n    pulsingLED.pulse();\n  }, timerLength);\n});\n<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4e0e\u5149\u654f\u7535\u963b\u7ed3\u5408\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">const Io: unknow = require('yourself deivce');\nconst board\uff1a Io.Board = new Io.Board();\n\nboard.on('ready', (): void =&gt; {\n  const nightlight: any = new Io.Led(3);\n  const ldr: any = new Io.Light({ pin : 'A0', freq: 500 });\n  let dimmest: number = 1024;\n  let brightest: number = 0;\n  ldr.on('change', (): void =&gt; {\n    if (ldr.value &lt; dimmest)   { dimmest   = ldr.value; }\n    if (ldr.value &gt; brightest) { brightest = ldr.value; }\n\n    const relativeValue: any = Io.Fn.scale(ldr.value, dimmest, brightest, 0, 511);\n    if (relativeValue &lt;= 255) {\n      nightlight.brightness((relativeValue &gt;&gt; 1) ^ 255);\n    } else {\n      nightlight.off();\n    }\n  });\n});\n<\/code><\/pre>\n<h3>\u5f69\u8272LED<\/h3>\n<p>\u8fd9\u91cc\u63a5\u5165\u5929\u6c14API\u6765\u6f14\u793a\uff08\u6709\u5f88\u591a\u53ef\u4ee5\u514d\u8d39\u4f7f\u7528\u6b21\u6570\u7684\u5929\u6c14API\uff0c\u53ef\u4ee5\u53bb\u767e\u5ea6\u7684API\u5e02\u573a\u3002\u6216\u8005\u81ea\u5df1\u6293\u63a5\u53e3\uff0c\u4f46\u4e0d\u5efa\u8bae\uff09\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">const API_KEY: string = '\u8fd9\u91cc\u662f\u4f60API\u7684KEY\uff0c\u4e00\u822c\u662f\u8981KEY\u7684';\nconst LAT: string = '43.3463760';\nconst LONG: string = '-72.6395340';\nconst API_URL: string  = 'https:\/\/api.xxx.xx\/xxx';\/\/\u7528\u81ea\u5df1\u627e\u7684\uff0c\u6709\u5f88\u591a\n\nconst Io: unknow = require('yourself deivce');\nconst board\uff1a Io.Board = new Io.Board();\n\nconst DEFAULT_TIMER: number = 60000;\nconst UPPER_LIMIT: number = 99 * 60000;\nconst LOWER_LIMIT: number = 1000;  \n\nboard.on('ready', (): void =&gt; {\n  console.log('wahahahaha');\n  const rgb: any = new Io.Led.RGB({ pins: [3, 5, 6] });\n  const requestURL: string = `${API_URL}\/${API_KEY}\/${LAT},${LONG}`;\n\n  request(requestURL, function (error: any, response: any, body: any) {\n    if (error) {\n      console.error(error);\n    } else if (response.statusCode === 200) {\n        const forecast: Object= JSON.parse(body);\n        const daily: any= forecast.daily.data;\n        const willBeDamp: any = daily[1].precipProbability &gt; 0.2;\n        const tempDelta: any  = daily[1].temperatureMax - daily[0].temperatureMax;\n        console.log(forecast);\n\n        if (tempDelta &gt; 4) {\n          rgb.color('#ff0000');\n        } else if (tempDelta &lt; -4) {\n          rgb.color('#ffffff');\n        } else {\n          rgb.color('#00ff00');\n        }\n        if (willBeDamp) { rgb.strobe(1000); } \n    }\n  });\n});\n<\/code><\/pre>\n<h3>LED\u663e\u793a\u5c4f<\/h3>\n<p>LED\u53ef\u4ee5\u663e\u793a\u57fa\u672c\u7684\u5b57\u7b26\u548c\u5f62\u72b6\uff0c\u5f53\u7136\uff0c\u975e\u5b57\u7b26\u7684\u5f62\u72b6\u4e5f\u53ef\u4ee5\u663e\u793a\uff0c\u80fd\u663e\u793a\u591a\u5c11\u53d6\u51b3\u4e0e\u70b9\u9635\u6570\uff0c\u4e00\u822c\u6f14\u793a16<em>2\u6216\u80055<\/em>7\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u7f16\u5199Advanced\u7c7b\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">const Io: unknow = require('yourself deivce');\nconst board\uff1a Io.Board = new Io.Board();\n\nboard.on('ready', (): void =&gt; {\n  const downButton: any = new Io.Button(2);\n  const upButton: any = new Io.Button(3);\n  const goButton: any = new Io.Button(4);\n  const lcd: any = new Io.LCD([7, 8, 9, 10, 11, 12]);\n  \/\/lcd.cursor(0, 0).print('hello, world');\n  \/\/lcd.cursor(1, 0).print('hello, indeex');\n\n  const alertLED: any = new Io.Led(6);\n  let remaining: any, timer: any, timeString: string, lastTimeString: string, timestamp: nunber, lastTimestamp: number;\n\n  downButton.on('press', () =&gt; {\n    console.log('down');\n  });\n  upButton.on('press', () =&gt; {\n    console.log('up');\n  });\n  goButton.on('press', () =&gt; {\n    console.log('go');\n  });\n});\n<\/code><\/pre>\n<p>\u7136\u540e\u662f\u65f6\u95f4\u7684\u8ba1\u7b97\u548c\u6309\u94ae\u5904\u7406\uff0c\u5355\u72ec\u7f16\u5199\u4e00\u4e2a\u7c7b\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">private remaining: any = DEFAULT_TIMER;\nprivate lastTimeString: string = '00:00';\nprivate timeString: string= '';\n\nprivate init (): void {\n    showRemaining(remaining, lastTimeString, timeString);\n}\n\nprivate showRemaining (remaining: any, lastTimeString: any, timeString: any): void {\n    let minutes: any, seconds: any, minPad: any, secPad: any;\n    minutes = Math.floor(remaining \/ 60000);\n    seconds = Math.floor((remaining % 60000) \/ 1000);\n    minPad = (minutes &lt; 10) ? '0' : '';\n    secPad = (seconds &lt; 10) ? '0' : '';\n    timeString = `${minPad}${minutes}:${secPad}${seconds}`;\n    if (timeString !== lastTimeString) {\n      lcd.cursor(0, 0).print(timeString);\n    }\n}\n\nprivate adjustTime (delta: any): void {\n    remaining += delta;\n    if (remaining &lt; LOWER_LIMIT) {\n      remaining = LOWER_LIMIT;\n    } else if (remaining &gt; UPPER_LIMIT) {\n      remaining = UPPER_LIMIT;\n    }\n    showRemaining(remaining, lastTimeString, timeString);\n}\n\nprivate start (): void {\n    lcd.clear();\n    timestamp = Date.now();\n    timer = setInterval(tick, 250);\n    tick();\n  }\n\n  private pause (): void {\n    timer = clearInterval(timer);\n    lcd.cursor(0, 9).print('PAUSED');\n  }\n\n  private tick (): void {\n    lastTimestamp = timestamp;\n    timestamp = Date.now();\n    remaining -= (timestamp - lastTimestamp);\n    if (remaining &lt;= 0) {\n      timer = clearInterval(timer);\n      \/\/chime();\n      init();\n    }\n    showRemaining(remaining, lastTimeString, timeString);\n  }\n\ndownButton.on('press', (): void =&gt; {\n    adjustTime(-1000);\n});\nupButton.on('press', (): void =&gt; {\n    adjustTime(1000);\n});\n\ngoButton.on('press', (): void =&gt; {\n    if (!timer) {\n      start();\n    } else {\n      pause();\n    }\n})\n\ninit();\n<\/code><\/pre>\n<h3>\u6dfb\u52a0\u58f0\u97f3<\/h3>\n<p>\u63a5\u7740\u5728<em>tick<\/em>\u51fd\u6570\u91cc\u6dfb\u52a0\u58f0\u97f3\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">private chime () {\n    alertChime.play({\n    tempo: 120,\n    song: [\n      ['e5', 1],\n      ['g#5', 1],\n      ['f#5', 1],\n      ['b4', 2],\n      ['e5', 1],\n      ['f#5', 1],\n      ['g#5', 1],\n      ['e5', 2],\n      ['g#5', 1],\n      ['e5', 1],\n      ['f#5', 1],\n      ['b4', 2],\n      ['b4', 1],\n      ['f#5', 1],\n      ['g#5', 1],\n      ['e5', 2]\n    ]\n  });\n  lcd.cursor(0, 9).print('DUANG!');\n}\n<\/code><\/pre>\n<p>\u5f53\u7136\u4e0d\u4f1a\u53d1\u51fa<em>DUANG<\/em>\u7684\u58f0\u97f3\uff0c\u53ea\u4f1a\u53d1\u51fa\u6709\u9891\u7387\u7684\u7535\u538b\u58f0\u3002<\/p>\n\n<p>code enjoy!<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/indeex.cc\/\">https:\/\/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>\u7eda\u4e3d\u591a\u5f69\u7684\u706f\u5149\u5f88\u5438\u5f15\u4eba\uff0c\u90a3\u4e48\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528LED\u3002 \u5143\u5668\u4ef6\uff1a \u6240\u6709\u7528\u5230\u7684\u5143\u5668\u4ef6\uff0c\u8fd9\u91cc\u4e0d\u5728\u5217\u51fa\uff0c\u4e3b\u8981LE<a href=\"https:\/\/blog.indeex.club\/index.php\/2019\/08\/23\/led%e6%8e%a7%e5%88%b6\/\" 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\/164"}],"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=164"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/164\/revisions\/171"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}