|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + |
| 4 | +<head> |
| 5 | + <meta charset="UTF-8"> |
| 6 | + <title>Jigglypuff</title> |
| 7 | + |
| 8 | + <style> |
| 9 | + img { |
| 10 | + display: none; |
| 11 | + width: 100%; |
| 12 | + max-width: 300px; |
| 13 | + } |
| 14 | + #jigglypuff.ready img.stand { |
| 15 | + display: block; |
| 16 | + } |
| 17 | + #jigglypuff.action img.sing { |
| 18 | + display: block; |
| 19 | + } |
| 20 | + #control { |
| 21 | + width: 100%; |
| 22 | + max-width: 300px; |
| 23 | + margin: 10px 0 10px 0; |
| 24 | + } |
| 25 | + #control button { |
| 26 | + display: inline-block; |
| 27 | + width: 40%; |
| 28 | + height: 50px; |
| 29 | + } |
| 30 | + #control.start .msg::after { |
| 31 | + content: "超音波啟動中..."; |
| 32 | + } |
| 33 | + #control.starting .msg::after { |
| 34 | + content: "超音波已啟動"; |
| 35 | + } |
| 36 | + form { |
| 37 | + width: 100%; |
| 38 | + max-width: 300px; |
| 39 | + } |
| 40 | + form>* { |
| 41 | + margin-top: 2px; |
| 42 | + } |
| 43 | + form>input { |
| 44 | + width: 40%; |
| 45 | + height: 20px; |
| 46 | + } |
| 47 | + form>button { |
| 48 | + width: 50%; |
| 49 | + height: 50px; |
| 50 | + } |
| 51 | + button.setting::after { |
| 52 | + content: "設定"; |
| 53 | + } |
| 54 | + button.finish::after { |
| 55 | + content: "設定完成"; |
| 56 | + } |
| 57 | + </style> |
| 58 | + |
| 59 | + <script src="../../../webcomponentsjs/webcomponents.js"></script> |
| 60 | + <link rel='import' href='../../web-arduino.html'></link> |
| 61 | + <link rel='import' href='../../wa-ultrasonic.html'></link> |
| 62 | + <link rel='import' href='../../wa-led.html'></link> |
| 63 | +</head> |
| 64 | + |
| 65 | +<body> |
| 66 | + <web-arduino id='board'> |
| 67 | + <wa-ultrasonic id='ultrasonic'></wa-ultrasonic> |
| 68 | + <wa-led id='led'></wa-led> |
| 69 | + </web-arduino> |
| 70 | + |
| 71 | + <audio id='player'> |
| 72 | + <source src="jigglypuff.mp3" type="audio/mpeg"> |
| 73 | + </audio> |
| 74 | + |
| 75 | + <form> |
| 76 | + <label>arduino: device</label> |
| 77 | + <br> |
| 78 | + <input type="text" id="device" value="rm63"> |
| 79 | + <br> |
| 80 | + |
| 81 | + <label>超音波: trig / echo</label> |
| 82 | + <br> |
| 83 | + <input type="text" id="trig" value="11">/ |
| 84 | + <input type="text" id="echo" value="10"> |
| 85 | + <br> |
| 86 | + |
| 87 | + <label>LED: pin / gnd</label> |
| 88 | + <br> |
| 89 | + <input type="text" id="pin" value="6">/ |
| 90 | + <input type="text" id="gnd" value="8"> |
| 91 | + <br> |
| 92 | + <button class="setting" id="set"></button> |
| 93 | + </form> |
| 94 | + |
| 95 | + <div id="control"> |
| 96 | + <button id="start">啟動超音波</button> |
| 97 | + <button id="stop">關閉</button> |
| 98 | + <div class="msg"></div> |
| 99 | + </div> |
| 100 | + |
| 101 | + <div id="jigglypuff" class='ready'> |
| 102 | + <img class="stand" src="stand.jpg"> |
| 103 | + <img class="sing" src="sing.gif"> |
| 104 | + </div> |
| 105 | + |
| 106 | + <script type="text/javascript"> |
| 107 | + window.addEventListener('WebComponentsReady', function() { |
| 108 | + var board = document.getElementById('board'), |
| 109 | + ultrasonic = document.getElementById('ultrasonic'), |
| 110 | + led = document.getElementById('led'), |
| 111 | + device = document.getElementById('device'), |
| 112 | + trig = document.getElementById('trig'), |
| 113 | + echo = document.getElementById('echo'), |
| 114 | + pin = document.getElementById('pin'), |
| 115 | + gnd = document.getElementById('gnd'), |
| 116 | + setBtn = document.getElementById('set'); |
| 117 | + |
| 118 | + setBtn.addEventListener('click', function(e) { |
| 119 | + board.device = device.value; |
| 120 | + ultrasonic.trig = trig.value; |
| 121 | + ultrasonic.echo = echo.value; |
| 122 | + led.pin = pin.value; |
| 123 | + led.gnd = gnd.value; |
| 124 | + board.on('ready', ready); |
| 125 | + board.init(); |
| 126 | + e.preventDefault(); |
| 127 | + e.stopPropagation(); |
| 128 | + return false; |
| 129 | + }, false); |
| 130 | + |
| 131 | + function ready() { |
| 132 | + var state, |
| 133 | + control = document.getElementById('control'), |
| 134 | + start = document.getElementById('start'), |
| 135 | + stop = document.getElementById('stop'), |
| 136 | + ultrasonic = document.getElementById('ultrasonic'), |
| 137 | + jigglypuff = document.getElementById('jigglypuff'), |
| 138 | + led = document.getElementById('led'), |
| 139 | + player = document.getElementById('player'); |
| 140 | + |
| 141 | + // 示意 board ready |
| 142 | + led.on(); |
| 143 | + setTimeout(function() { |
| 144 | + led.off(); |
| 145 | + }, 1000); |
| 146 | + |
| 147 | + stop.addEventListener('click', function() { |
| 148 | + var ultrasonic = document.getElementById('ultrasonic'); |
| 149 | + control.className = ""; |
| 150 | + jigglypuff.className = "ready"; |
| 151 | + led.off(); |
| 152 | + ultrasonic.stopPing(); |
| 153 | + player.load(); |
| 154 | + state = ""; |
| 155 | + console.log('stop ping'); |
| 156 | + }); |
| 157 | + |
| 158 | + start.addEventListener('click', function() { |
| 159 | + var ratio; |
| 160 | + |
| 161 | + control.className = "start"; |
| 162 | + state = "start"; |
| 163 | + |
| 164 | + ultrasonic.ping(function(cm) { |
| 165 | + // console.log(cm); |
| 166 | + // 一公尺內唱歌、亮燈 |
| 167 | + if (state !== "start") { |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + if (control.className !== "starting") { |
| 172 | + control.className = "starting"; |
| 173 | + } |
| 174 | + |
| 175 | + console.log('ping...', cm); |
| 176 | + if (cm < 100) { |
| 177 | + led.on(); |
| 178 | + player.play(); |
| 179 | + jigglypuff.className = "action"; |
| 180 | + |
| 181 | + // 調整音量、亮度 |
| 182 | + ratio = getRatio(cm / 100, 0.25); |
| 183 | + player.volume = ratio; |
| 184 | + led.intensity = ratio; |
| 185 | + } else { |
| 186 | + led.off(); |
| 187 | + player.load(); |
| 188 | + jigglypuff.className = "ready"; |
| 189 | + } |
| 190 | + }, 500); |
| 191 | + |
| 192 | + }, false); |
| 193 | + } |
| 194 | + |
| 195 | + function getRatio(val, step) { |
| 196 | + var ro; |
| 197 | + |
| 198 | + val = val < 0.05 ? 1 : (1 - val); |
| 199 | + ro = Math.floor(val / step); |
| 200 | + ro = ro * step; |
| 201 | + return ro > 1 ? 1 : ro; |
| 202 | + } |
| 203 | + |
| 204 | + }, false); |
| 205 | + </script> |
| 206 | +</body> |
| 207 | + |
| 208 | +</html> |
0 commit comments