|
5 | 5 | <script src="bower_components/webcomponentsjs/webcomponents-lite.js"></script> |
6 | 6 | <link rel="import" href="bower_components/polymer/polymer.html"> |
7 | 7 | <link rel="import" href="bower_components/paper-button/paper-button.html"> |
8 | | - <link rel="import" href="bower_components/paper-input/paper-input.html"> |
| 8 | + <link rel="import" href="bower_components/paper-color-picker/paper-color-input.html"> |
9 | 9 | <link rel="import" href="bower_components/paper-spinner/paper-spinner.html"> |
10 | 10 | <link rel="import" href="bower_components/paper-toast/paper-toast.html"> |
11 | 11 | <link rel="import" href="bower_components/iron-list/iron-list.html"> |
|
57 | 57 | flex: auto; |
58 | 58 | font-family: monospace; |
59 | 59 | font-size: 8pt; |
| 60 | + @apply(--layout-horizontal); |
60 | 61 | } |
61 | 62 | .consoleLine:first-of-type { |
62 | 63 | border-top: none !important; |
63 | 64 | } |
| 65 | + .consoleLine .consoleColor { |
| 66 | + width: 20px; |
| 67 | + height: 20px; |
| 68 | + } |
64 | 69 |
|
65 | 70 | .error:last-child { |
66 | 71 | border-bottom: 1px solid #ffd7d7; |
|
144 | 149 | </div> |
145 | 150 | <div id="update">(not updated yet)</div> |
146 | 151 | <br></br> |
147 | | - <paper-input label="LED color" placeholder="Type color name here" id="color" class="flex"></paper-input> |
148 | | - <div class="horizontal layout end-justified"> |
149 | | - <paper-button id="submit" on-click="writeLedColor">Submit</paper-button> |
150 | | - </div> |
| 152 | + <paper-color-input shape="circle" type="hsv" label="LED light color" |
| 153 | + id="color" class="flex"></paper-color-input> |
151 | 154 | </div> |
152 | 155 | <br></br> |
153 | 156 | <div id="console" class="flex"> |
154 | 157 | <iron-list class="messages" items="[[consoleLines]]" as="item"> |
155 | 158 | <template> |
156 | 159 | <div class$="consoleLine [[item.type]]"> |
157 | | - [[item.value]] |
| 160 | + <div class="flex">[[item.value]]</div> |
| 161 | + <template is="dom-if" if="[[item.color]]"> |
| 162 | + <div class="consoleColor" style="[[item.color]]"></div> |
| 163 | + </template> |
158 | 164 | </div> |
159 | 165 | </template> |
160 | 166 | </iron-list> |
|
187 | 193 |
|
188 | 194 | let style = window.getComputedStyle(document.body.appendChild(a)); |
189 | 195 | let colors = style.color.match(/\d+/g).map(a => parseInt(a, 10)); |
| 196 | + console.log("Translated to hex color " + JSON.stringify(colors)); |
190 | 197 |
|
191 | 198 | document.body.removeChild(a); |
192 | 199 |
|
|
253 | 260 | computeButtonTitle: function(server) { |
254 | 261 | return server ? "Disconnect" : "Connect"; |
255 | 262 | }, |
256 | | - log: function(text) { |
257 | | - this.push('consoleLines', { value: text, type: "log" }); |
| 263 | + log: function(text, hexColor) { |
| 264 | + if (hexColor) |
| 265 | + hexColor = "background: " + hexColor + ";"; |
| 266 | + this.push('consoleLines', { value: text, type: "log", color: hexColor }); |
258 | 267 | }, |
259 | 268 | error: function(text) { |
260 | 269 | this.push('consoleLines', { value: text, type: "error" }); |
|
263 | 272 | this.consoleLines = []; |
264 | 273 | }, |
265 | 274 | readLedColor: function() { |
266 | | - return this.colorLedCharacteristic.readValue() |
267 | | - .then(function(data) { |
| 275 | + if (!this.colorLedCharacteristic) |
| 276 | + return Promise.resolve(); |
| 277 | + |
| 278 | + return this.colorLedCharacteristic.readValue().then(data => { |
268 | 279 | data = data.buffer ? data : new DataView(data); |
269 | 280 | let r = data.getUint8(0).toString(16).padStart(2, '0'); |
270 | 281 | let g = data.getUint8(1).toString(16).padStart(2, '0'); |
271 | 282 | let b = data.getUint8(2).toString(16).padStart(2, '0'); |
272 | | - document.querySelector('#color').value = "#" + r + g + b; |
| 283 | + |
| 284 | + // Ignore events from changing the color. |
| 285 | + this.reading = true; |
| 286 | + let value = "#" + r + g + b; |
| 287 | + this.log("Received LED color: " + value, value); |
| 288 | + document.querySelector('#color').valueAsHex = value; |
| 289 | + this.reading = false; |
273 | 290 | }); |
274 | 291 | }, |
275 | | - writeLedColor: function() { |
276 | | - this.$.submit.style.backgroundColor = "#646FB7"; |
| 292 | + writeLedColor: function(hexValue) { |
| 293 | + if (this.writing || this.reading) |
| 294 | + return; |
| 295 | + |
| 296 | + this.writing = true; |
277 | 297 |
|
278 | 298 | let done = () => { |
279 | | - this.$.submit.style.backgroundColor = "#303F9F"; |
| 299 | + this.writing = false; |
280 | 300 | this.readLedColor(); |
281 | 301 | } |
282 | 302 |
|
283 | 303 | if (!this.colorLedCharacteristic) { |
284 | | - this.error("Error: No Color Characteristic found.") |
| 304 | + this.error("Error: No Color Characteristic found."); |
285 | 305 | return done(); |
286 | 306 | } |
287 | 307 |
|
288 | | - let value = document.querySelector('#color').value; |
| 308 | + let value = toHexColor(hexValue); |
| 309 | + this.log("Sending LED color: " + hexValue, hexValue); |
289 | 310 |
|
290 | | - return this.colorLedCharacteristic.writeValue(toHexColor(value)) |
| 311 | + return this.colorLedCharacteristic.writeValue(value) |
291 | 312 | .catch(err => { this.error(err); }) |
292 | 313 | .then(done); |
293 | 314 | }, |
|
388 | 409 | }); |
389 | 410 | }, |
390 | 411 | ready: function() { |
391 | | - document.querySelector('#color').addEventListener('keydown', ev => { |
392 | | - if (ev.keyCode === 13) |
393 | | - this.writeLedColor(); |
| 412 | + let picker = document.querySelector('#color'); |
| 413 | + picker.addEventListener('value-changed', ev => { |
| 414 | + let hexValue = picker.valueAsHex; |
| 415 | + if (hexValue) |
| 416 | + this.writeLedColor(hexValue); |
394 | 417 | }); |
395 | 418 | } |
396 | 419 | }); |
|
0 commit comments