Skip to content
This repository was archived by the owner on Mar 17, 2020. It is now read-only.

Commit 5cb8360

Browse files
committed
Make reading the color use uint8 array instead of string
1 parent e184d6b commit 5cb8360

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

server/main.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var board = new five.Board({
88
var beacon = require("eddystone-beacon");
99
var util = require('util');
1010
var bleno = require('eddystone-beacon/node_modules/bleno');
11-
var parse = require('parse-color');
1211

1312
DEVICE_NAME = 'Edison';
1413

@@ -82,29 +81,28 @@ var ColorCharacteristic = function() {
8281
properties: ['read', 'write'],
8382
value: null
8483
});
85-
this._value = 'ffffff';
84+
this._value = new Buffer([255, 255, 255]);
8685
this._led = null;
8786
};
8887

8988
util.inherits(ColorCharacteristic, bleno.Characteristic);
9089

9190
ColorCharacteristic.prototype.onReadRequest = function(offset, callback) {
92-
var data = new Buffer(this._value);
93-
callback(this.RESULT_SUCCESS, data);
91+
callback(this.RESULT_SUCCESS, this._value);
9492
};
9593

9694
ColorCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
97-
var value = data.hexSlice();
95+
var value = data;
9896
if (!value) {
9997
callback(this.RESULT_SUCCESS);
10098
return;
10199
}
102100

103101
this._value = value;
104-
console.log(value);
102+
console.log(value.hexSlice());
105103

106104
if (this._led) {
107-
this._led.color(this._value);
105+
this._led.color(this._value.hexSlice());
108106
}
109107
callback(this.RESULT_SUCCESS);
110108
};
@@ -173,6 +171,6 @@ board.on("ready", function() {
173171
});
174172

175173
colorCharacteristic._led = led;
176-
led.color(colorCharacteristic._value);
174+
led.color(colorCharacteristic._value.hexSlice());
177175
led.intensity(30);
178176
});

static/index.html

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,25 @@
189189
return new Uint8Array(colors);
190190
}
191191

192+
String.prototype.padStart = function (maxLength, fillString=' ') {
193+
let str = String(this);
194+
if (str.length >= maxLength) {
195+
return str;
196+
}
197+
198+
fillString = String(fillString);
199+
if (fillString.length === 0) {
200+
fillString = ' ';
201+
}
202+
203+
let fillLen = maxLength - str.length;
204+
let timesToRepeat = Math.ceil(fillLen / fillString.length);
205+
let truncatedStringFiller = fillString
206+
.repeat(timesToRepeat)
207+
.slice(0, fillLen);
208+
return truncatedStringFiller + str;
209+
};
210+
192211
window.onload = () => {
193212
var app = document.querySelector('#app');
194213

@@ -243,9 +262,10 @@
243262
return this.colorLedCharacteristic.readValue()
244263
.then(function(data) {
245264
data = data.buffer ? data : new DataView(data);
246-
let decoder = new TextDecoder("utf-8");
247-
let decodedString = decoder.decode(data);
248-
document.querySelector('#color').value = decodedString;
265+
let r = data.getUint8(0).toString(16).padStart(2, '0');
266+
let g = data.getUint8(1).toString(16).padStart(2, '0');
267+
let b = data.getUint8(2).toString(16).padStart(2, '0');
268+
document.querySelector('#color').value = "#" + r + g + b;
249269
});
250270
},
251271
writeLedColor: function() {

0 commit comments

Comments
 (0)