{"id":116,"date":"2021-07-10T21:54:00","date_gmt":"2021-07-10T13:54:00","guid":{"rendered":"http:\/\/www.espruino.cn\/?p=116"},"modified":"2021-07-10T22:01:11","modified_gmt":"2021-07-10T14:01:11","slug":"%e8%b6%a3%e5%91%b3%e7%94%b5%e5%ad%90%e7%bb%95%e7%8e%af%e6%b8%b8%e6%88%8fwire-loop-game","status":"publish","type":"post","link":"http:\/\/www.espruino.cn\/?p=116","title":{"rendered":"\u8da3\u5473\u7535\u5b50\u7ed5\u73af\u6e38\u620f(Wire Loop Game)"},"content":{"rendered":"\n<p>\u5b98\u65b9\u6559\u7a0b\u94fe\u63a5\uff1a<a rel=\"noreferrer noopener\" href=\"http:\/\/www.espruino.com\/Pico+Wire+Loop+Game\" target=\"_blank\">http:\/\/www.espruino.com\/Pico+Wire+Loop+Game<\/a><\/p>\n\n\n\n<p>\u6240\u9700\u6750\u6599<br>1. Espruino\u5f00\u53d1\u677f\u4e00\u4e2a<br>2. Nokia 5110 LCD\u4e00\u4e2a<br>3. \u8702\u9e23\u5668\u4e00\u4e2a<br>4. \u9762\u5305\u677f\u4e00\u4e2a<br>5. \u6309\u94ae\u5f00\u5173\u4e00\u4e2a<br>6. \u76f4\u5f840.6mm\u7684\u5b9e\u5fc3\u5bfc\u7ebf1\u7c73\u5de6\u53f3<\/p>\n\n\n\n<p>LCD\u63a5\u7ebf\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>LCD pin<\/td><td>Pin type<\/td><td>Espruino Board<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><td>GND<\/td><\/tr><tr><td>LIGHT<\/td><td>Any<\/td><td>A0<\/td><\/tr><tr><td>VCC<\/td><td>3.3v<\/td><td>A1<\/td><\/tr><tr><td>CLK<\/td><td>SPI SCK<\/td><td>B3<\/td><\/tr><tr><td>DIN<\/td><td>SPI MOSI<\/td><td>B5<\/td><\/tr><tr><td>DC<\/td><td>Any<\/td><td>B6<\/td><\/tr><tr><td>CE<\/td><td>Any<\/td><td>B7<\/td><\/tr><tr><td>RST<\/td><td>Any<\/td><td>B8<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5176\u4ed6\u90e8\u5206\u63a5\u7ebf\u65b9\u6cd5\u8bf7\u53c2\u8003\u4ee3\u7801\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"931\" height=\"698\" src=\"http:\/\/www.espruino.cn\/wp-content\/uploads\/2021\/07\/201417sny2ninhdyf22d52.jpg\" alt=\"\" class=\"wp-image-117\" srcset=\"http:\/\/www.espruino.cn\/wp-content\/uploads\/2021\/07\/201417sny2ninhdyf22d52.jpg 931w, http:\/\/www.espruino.cn\/wp-content\/uploads\/2021\/07\/201417sny2ninhdyf22d52-300x225.jpg 300w, http:\/\/www.espruino.cn\/wp-content\/uploads\/2021\/07\/201417sny2ninhdyf22d52-768x576.jpg 768w\" sizes=\"(max-width: 931px) 100vw, 931px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"994\" height=\"747\" src=\"http:\/\/www.espruino.cn\/wp-content\/uploads\/2021\/07\/201421xyuyove8smxsz80j.jpg\" alt=\"\" class=\"wp-image-118\" srcset=\"http:\/\/www.espruino.cn\/wp-content\/uploads\/2021\/07\/201421xyuyove8smxsz80j.jpg 994w, http:\/\/www.espruino.cn\/wp-content\/uploads\/2021\/07\/201421xyuyove8smxsz80j-300x225.jpg 300w, http:\/\/www.espruino.cn\/wp-content\/uploads\/2021\/07\/201421xyuyove8smxsz80j-768x577.jpg 768w\" sizes=\"(max-width: 994px) 100vw, 994px\" \/><\/figure>\n\n\n\n<p>\u56e0\u4e3a\u5b98\u65b9\u7528\u7684\u662fEspruino Pico\uff0c\u6211\u7528\u7684\u662fEspruino Board\uff0c\u6240\u4ee5\u63a5\u7ebf\u7565\u6709\u4e0d\u540c\uff0c\u53e6\u5916\u6211\u7684\u8fd9\u4e2aLCD\u6ca1\u6cd5\u76f4\u63a5\u4f7f\u7528\u5b98\u65b9\u7684\u5e93\uff0c\u6211\u5c31\u628aLCD\u7684\u521d\u59cb\u5316\u4ee3\u7801\u653e\u5728onInit\u51fd\u6570\u91cc\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>A0.write(1); \/\/ LCD Light\nA1.write(1); \/\/ LCD VCC\nvar LOOP = C0; \/\/ the pin we've got our loop attached to\nvar SPEAKER = B9; \/\/ where the speaker is attached\nvar BTN_RST = C1; \/\/ where the Reset button is attached\n\nvar g;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;\/\/ the LCD's graphics\nvar score = 0; \/\/ current score\n\nfunction drawScore() {\n&nbsp;&nbsp;g.clear();\n&nbsp;&nbsp;\/\/ draw the score\n&nbsp;&nbsp;g.setFontVector(40);\n&nbsp;&nbsp;g.drawString(score,(g.getWidth()-g.stringWidth(score))\/2,0);\n&nbsp;&nbsp;\/\/ send the graphics to the display\n&nbsp;&nbsp;g.flip();\n}\n\n\n\/\/ flash and make a sound, and call the callback when done\nfunction hasHit(callback) {\n&nbsp;&nbsp;\/\/ increment the score...\n&nbsp;&nbsp;score++;\n\n&nbsp;&nbsp;var i = 1;\n\n&nbsp;&nbsp;\/\/ This will make the beeping and flashing\n&nbsp;&nbsp;function siren() {\n&nbsp; &nbsp; if (i&gt;7) {\n&nbsp; &nbsp;&nbsp; &nbsp;clearInterval(interval);\n&nbsp; &nbsp;&nbsp; &nbsp;digitalWrite(LED1, 0); \/\/ turn the LED off\n&nbsp; &nbsp;&nbsp; &nbsp;\/\/ set normal colours on LCD\n&nbsp; &nbsp;&nbsp; &nbsp;g.setColor(1);\n&nbsp; &nbsp;&nbsp; &nbsp;g.setBgColor(0);\n&nbsp; &nbsp;&nbsp; &nbsp;\/\/ turn the sound off\n&nbsp; &nbsp;&nbsp; &nbsp;digitalRead(SPEAKER);\n&nbsp; &nbsp;&nbsp; &nbsp;callback();\n&nbsp; &nbsp;&nbsp; &nbsp;return;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; \/\/ every other time around the loop:\n&nbsp; &nbsp; if (i&amp;1) {\n&nbsp; &nbsp;&nbsp; &nbsp;digitalWrite(LED1, 1); \/\/ turn red LED on\n&nbsp; &nbsp;&nbsp; &nbsp;\/\/ set inverted colours on LCD\n&nbsp; &nbsp;&nbsp; &nbsp;g.setColor(0);\n&nbsp; &nbsp;&nbsp; &nbsp;g.setBgColor(1);\n&nbsp; &nbsp;&nbsp; &nbsp;\/\/ beep high frequency\n&nbsp; &nbsp;&nbsp; &nbsp;analogWrite(SPEAKER, 0.5, {freq:1000});\n&nbsp; &nbsp; } else {\n&nbsp; &nbsp;&nbsp; &nbsp;digitalWrite(LED1, 0); \/\/ turn the LED off\n&nbsp; &nbsp;&nbsp; &nbsp;\/\/ set normal colours on LCD\n&nbsp; &nbsp;&nbsp; &nbsp;g.setColor(1);\n&nbsp; &nbsp;&nbsp; &nbsp;g.setBgColor(0);\n&nbsp; &nbsp;&nbsp; &nbsp;\/\/ beep low frequency\n&nbsp; &nbsp;&nbsp; &nbsp;analogWrite(SPEAKER, 0.5, {freq:700});\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; \/\/ draw the text 'hit' on the LCD\n&nbsp; &nbsp; g.clear();\n&nbsp; &nbsp; g.setFontVector(40);\n&nbsp; &nbsp; g.drawString(\"Hit!\",(g.getWidth()-g.stringWidth(\"Hit!\"))\/2,0);\n&nbsp; &nbsp; g.flip();\n\n&nbsp; &nbsp; i++;\n&nbsp;&nbsp;}\n&nbsp;&nbsp;\/\/ call the siren function every so often (and call it once immediately)\n&nbsp;&nbsp;var interval = setInterval(siren, 300);\n&nbsp;&nbsp;siren();\n}\n\n\nfunction onInit() {\n&nbsp;&nbsp;\/\/ Setup SPI for LCD\n&nbsp;&nbsp;SPI1.setup({ baud: 1000000, sck:B3, mosi:B5 });\n&nbsp;&nbsp;g = Graphics.createArrayBuffer(84,48,1,{vertical_byte:true});\n&nbsp;&nbsp;var spi = SPI1;\n&nbsp;&nbsp;var dc = B6;\n&nbsp;&nbsp;var ce = B7;\n&nbsp;&nbsp;var rst = B8;\n&nbsp;&nbsp;digitalPulse(rst, 0, 10); \/\/ pulse reset low\n&nbsp;&nbsp;setTimeout(function() {\n&nbsp; &nbsp; digitalWrite(dc,0); \/\/ cmd\n&nbsp; &nbsp; spi.send(\n&nbsp; &nbsp;&nbsp; &nbsp;[0x21, \/\/ fnset extended\n&nbsp; &nbsp;&nbsp; &nbsp;0x80 | 0x25, \/\/ setvop (experiment with 2nd val to get the right contrast)\n&nbsp; &nbsp;&nbsp; &nbsp;0x14, \/\/ setbias 4\n&nbsp; &nbsp;&nbsp; &nbsp;0x04 | 0x02, \/\/ temp control\n&nbsp; &nbsp;&nbsp; &nbsp;0x20, \/\/ fnset normal\n&nbsp; &nbsp;&nbsp; &nbsp;0x08 | 0x04], ce); \/\/ dispctl normal\n&nbsp;&nbsp;}, 100);\n&nbsp;&nbsp;g.flip = function () {\n&nbsp; &nbsp; for (var i=0;i&lt;6;i++) {\n&nbsp; &nbsp;&nbsp; &nbsp;digitalWrite(dc,0); \/\/ cmd\n&nbsp; &nbsp;&nbsp; &nbsp;spi.send(0x40|i, ce); \/\/ Y addr\n&nbsp; &nbsp;&nbsp; &nbsp;spi.send(0x80, ce); \/\/ X addr\n&nbsp; &nbsp;&nbsp; &nbsp;digitalWrite(dc,1); \/\/ data\n&nbsp; &nbsp;&nbsp; &nbsp;spi.send(new Uint8Array(g.buffer,i*84,84), ce);\n&nbsp; &nbsp; }\n&nbsp;&nbsp;};\n&nbsp;&nbsp;drawScore();\n&nbsp;&nbsp;startWatchingLoop();\n}\n\n\/\/ Add the internal pull-up resistor to the loop, so when it touches the wire (which is GND) it'll get pulled down\npinMode(LOOP, \"input_pullup\");\npinMode(BTN_RST, \"input_pulldown\");\n\/\/ This code watches the 'loop' to see if it has touched the wire\nvar loopWatch;\nfunction startWatchingLoop() {\n&nbsp;&nbsp;\/\/ first, ensure we don't start watching the wire loop twice\n&nbsp;&nbsp;if (loopWatch) clearWatch(loopWatch);\n&nbsp;&nbsp;loopWatch = setWatch(function() {\n&nbsp; &nbsp; loopWatch = undefined;\n&nbsp; &nbsp; \/\/ now if we've touched, flash everything and make noises\n&nbsp; &nbsp; hasHit(function() {\n&nbsp; &nbsp;&nbsp; &nbsp;\/\/ When that's done, draw the score and start watching the loop again\n&nbsp; &nbsp;&nbsp; &nbsp;drawScore();\n&nbsp; &nbsp;&nbsp; &nbsp;startWatchingLoop();\n&nbsp; &nbsp; });\n&nbsp;&nbsp;}, LOOP, { repeat: false, edge: \"falling\" });\n}\n\n\/\/ When button is pressed, reset the game:\nsetWatch(function() {\n&nbsp;&nbsp;clearInterval();\n&nbsp;&nbsp;digitalRead(SPEAKER); \/\/ make sure speaker is off\n&nbsp;&nbsp;score = 0;\n&nbsp;&nbsp;drawScore();\n&nbsp;&nbsp;startWatchingLoop();\n}, BTN_RST, { repeat: true, edge: \"rising\", debounce: 50 });\n\n\n\/\/ Finally, start everything going\nonInit();<\/code><\/pre>\n\n\n\n<p>\u5728Web IDE\u4e2d\u5c06\u8fd9\u4e9b\u4ee3\u7801\u4e0a\u4f20\u81f3Espruino\u4e4b\u540e\uff0c\u7136\u540e\u5728Web IDE\u5de6\u8fb9\u7684console\u4e2d\u6267\u884c\u4e00\u4e0bsave()\u51fd\u6570\u5c31\u53ef\u4ee5\u628a\u8fd9\u4e9b\u4ee3\u7801\u70e7\u5f55\u5230espruino\u4e86~<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"http:\/\/www.espruino.cn\/wp-content\/uploads\/2021\/07\/5-MINUTE-WIRE-LOOP-GAME.js.txt\">5 MINUTE WIRE LOOP GAME.js<\/a><a href=\"http:\/\/www.espruino.cn\/wp-content\/uploads\/2021\/07\/5-MINUTE-WIRE-LOOP-GAME.js.txt\" class=\"wp-block-file__button\" download>\u4e0b\u8f7d<\/a><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b98\u65b9\u6559\u7a0b\u94fe\u63a5\uff1ahttp:\/\/www.espruino.com\/Pico+Wire+Loop+Game \u6240\u9700\u6750\u6599 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.espruino.cn\/index.php?rest_route=\/wp\/v2\/posts\/116"}],"collection":[{"href":"http:\/\/www.espruino.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.espruino.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.espruino.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.espruino.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=116"}],"version-history":[{"count":3,"href":"http:\/\/www.espruino.cn\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":122,"href":"http:\/\/www.espruino.cn\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions\/122"}],"wp:attachment":[{"href":"http:\/\/www.espruino.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.espruino.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.espruino.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}