-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
91 lines (74 loc) · 2.32 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// https://github.com/sparkfun/Serial7SegmentDisplay/wiki/Special-Commands#i2cAddress
var util = require('util');
var EventEmitter = require('events').EventEmitter;
function SegmentLED (address, hardware) {
if (arguments.length < 2) {
hardware = address;
address = SegmentLED.DEFAULT_ADDRESS;
}
this.i2c = hardware.I2C(address);
this.clear(function () {
this.emit('ready');
}.bind(this));
}
util.inherits(SegmentLED, EventEmitter);
SegmentLED.DEFAULT_ADDRESS = 0x71;
var digits = [
1 + 2 + 4+ 8 + 16 + 32,
2 + 4,
1 + 2 + 64 + 16 + 8,
1 + 2 + 4 + 8 + 64,
2 + 4 + 32 + 64,
1 + 4 + 8 + 32 + 64,
1 + 4 + 8 + 16 + 32 + 64,
1 + 2 + 4,
1 + 2 + 4 + 8 + 16 + 32 + 64,
1 + 2 + 4 + 8 + 32 + 64,
];
SegmentLED.prototype.reset = function (next) {
// Factory reset
this.i2c.send(new Buffer([0x81]), next)
}
SegmentLED.prototype.clear = function (next) {
// Clear display
this.i2c.send(new Buffer([0x76]), next)
}
SegmentLED.prototype.segment = function (index, mask, next) {
// number
index = Number(index);
if (!(index >= 0) || !(index < 4)) {
throw new Error('Please specify a decimal index 0-3.');
}
this.i2c.send(new Buffer([0x7B + index, mask]), next);
}
SegmentLED.prototype.digit = function (index, value, next) {
this.segment(index, value != null ? digits[value] : 0, next);
}
SegmentLED.prototype.brightness = function (level, next) {
this.i2c.send(new Buffer([0x7A, Math.floor(level * 255)]), next);
};
SegmentLED.prototype.decimal = function (value, next) {
this.i2c.send(new Buffer([0x77, value]), next);
}
SegmentLED.prototype.display = function (number, opts, next) {
if (typeof opts == 'function' ) {
next = opts;
opts = {};
}
opts = opts || {};
if (typeof opts.places == 'number') {
this.decimal(1 << (3 - opts.places));
number *= Math.pow(10, opts.places);
}
number = Math.floor(number);
this.digit(3, number % 10);
number = Math.floor(number / 10);
if (opts.leadingZero != false || number > 0) this.digit(2, number % 10); else this.digit(2, null);
number = Math.floor(number / 10);
if (opts.leadingZero != false || number > 0) this.digit(1, number % 10); else this.digit(1, null);
number = Math.floor(number / 10);
if (opts.leadingZero != false || number > 0) this.digit(0, number % 10, next); else this.digit(0, null, next);
}
exports.use = function (hardware) {
return new SegmentLED(hardware);
}