-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmansaa.js
76 lines (58 loc) · 2.33 KB
/
mansaa.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var util = require('util');
var NobleDevice = require('noble-device');
// MansaaLed.prototype.setGradualMode = function(on, callback) {
// this.writeServiceStringCharacteristic(EFFECT_UUID, on ? 'TS' : 'TE', callback);
// };
module.exports = function(customMixin){
var SERVICE_UUID = 'ffb0';
var CONTROL_UUID = 'ffb2';
var EFFECT_UUID = 'ffb2';
var MansaaLed = function(peripheral) {
NobleDevice.call(this, peripheral);
};
MansaaLed.SCAN_UUIDS = [SERVICE_UUID];
MansaaLed.prototype.RGBtoBuffer = function (rgbString, brightness) {
rgbString = rgbString || "000000";
brightness = brightness || "d1";
var fromString = brightness + rgbString
return Buffer.from(fromString, "hex")
};
MansaaLed.is = function(peripheral) {
/* This is a big nono , return correct info */
var localName = peripheral.advertisement.localName;
console.log(localName);
return true;
// return ((localName === undefined) || (localName === 'Cnlight') );
};
NobleDevice.Util.inherits(MansaaLed, NobleDevice);
if(customMixin){
NobleDevice.Util.mixin(MansaaLed,customMixin);
}
MansaaLed.prototype.writeServiceStringCharacteristic = function(uuid, string, callback) {
this.writeStringCharacteristic(SERVICE_UUID, uuid, string, callback);
};
MansaaLed.prototype.writeControlCharateristic = function(red, green, blue, brightness, callback) {
var rgbString = red + green + blue;
var brightNess = brightness;
var command = this.RGBtoBuffer(rgbString,brightNess);
this.writeServiceStringCharacteristic(CONTROL_UUID, command, callback);
};
MansaaLed.prototype.turnOn = function(callback) {
this.writeControlCharateristic( "FF", "FF", "FF", "FF", callback);
};
MansaaLed.prototype.turnOff = function(callback) {
this.writeControlCharateristic("00", "00", "00", "00",callback);
};
MansaaLed.prototype.setColorAndBrightness = function(red, green, blue, brightness, callback) {
function convert(integer) {
var str = Number(integer).toString(16);
return str.length == 1 ? "0" + str : str;
};
red = convert(red);
blue = convert(blue);
green = convert(green);
brightness = brightness?convert(brightness):"00";
this.writeControlCharateristic(brightness, red, green, blue, callback);
};
return MansaaLed;
};