-
Notifications
You must be signed in to change notification settings - Fork 0
/
chaconEmitter.js
154 lines (140 loc) · 3.67 KB
/
chaconEmitter.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var wpi = require('wiringpi-node')
var pin = 0;
var dimLevels = 15;
// Init chacon emitter
function init(GPIOPin) {
if (GPIOPin) {
pin = GPIOPin;
}
wpi.setup('wpi');
wpi.pinMode(pin, wpi.OUTPUT);
console.log('wpi setup on PIN :' + pin);
}
function setWPI(awpi) {
this.wpi = awpi;
}
function getWPI() {
return wpi;
}
// Send pulse
function sendBit(bool) {
if (bool) {
wpi.digitalWrite(pin, wpi.HIGH);
wpi.delayMicroseconds(310);
wpi.digitalWrite(pin, wpi.LOW);
wpi.delayMicroseconds(1340);
}
else {
wpi.digitalWrite(pin, wpi.HIGH);
wpi.delayMicroseconds(310);
wpi.digitalWrite(pin, wpi.LOW);
wpi.delayMicroseconds(310);
}
}
// Sends the DIMMER special bit
function sendDimmerBit() {
wpi.digitalWrite(pin, wpi.HIGH);
wpi.delayMicroseconds(310);
wpi.digitalWrite(pin, wpi.LOW);
wpi.delayMicroseconds(310);
wpi.digitalWrite(pin, wpi.HIGH);
wpi.delayMicroseconds(310);
wpi.digitalWrite(pin, wpi.LOW);
wpi.delayMicroseconds(310);
}
// Send pair of bits to simulate real bit send
// 0 = 01 , 1 = 10
function sendPair(bool) {
if (bool) {
sendBit(true);
sendBit(false);
}
else {
sendBit(false);
sendBit(true);
}
}
// build a power on/off command
function buildOrder(emitterId, deviceId, powerOn) {
// bit[0-25] convert the emitterId
var order = intToBytes(emitterId, 26);
// 26th bit group command
order += '0';
// 27th bit : power on/off bit
order += powerOn?'1':'0';
// bit[28->32] deviceId
order += intToBytes(deviceId, 4);
// bit[32->36] dim level
return order;
}
// build a Dim command, the dimLevel is an integer between 0-100
function buildDimOrder(emitterId, deviceId, dimLevel) {
var dim = Math.floor((dimLevel * dimLevels) / 100);
var order = buildOrder(emitterId, deviceId, true);
order += intToBytes(dim, 4);
return order;
}
function toBool(str) {
return str == '1'?true:false;
}
// convert the emitterId;
function intToBytes(emitterId, limit) {
var emitterIdByte = emitterId.toString(2);
if (emitterIdByte.length > limit) {
// truncate
emitterIdByte = emitterIdByte.substring(0, limit);
}
else {
emitterIdByte = pad(emitterIdByte, limit - emitterIdByte.length);
}
return emitterIdByte;
}
function pad(str, size) {
var pad = "000000000000000000000000000000000000";
return pad.substring(0, size) + str;
}
// Transmit over the air the data
function transmit(order, dimmer) {
// console.log('order : ' + order);
for (var i = 0; i < 5; i++) {
doTransmit(order, dimmer);
wpi.delayMicroseconds(10);
}
}
function doTransmit(order, dimmer) {
// lock sequence to begin signal emission
wpi.digitalWrite(pin, wpi.HIGH);
wpi.delayMicroseconds(275);
wpi.digitalWrite(pin, wpi.LOW);
wpi.delayMicroseconds(9900);
wpi.digitalWrite(pin, wpi.HIGH);
wpi.delayMicroseconds(275);
wpi.digitalWrite(pin, wpi.LOW);
wpi.delayMicroseconds(2675);
wpi.digitalWrite(pin, wpi.HIGH);
// send order data
for (var i = 0; i < order.length; i++) {
if (dimmer && i == 27) { // send special bit
sendDimmerBit();
continue;
}
// console.log(i);
sendPair(toBool(order.charAt(i)));
}
wpi.digitalWrite(pin, wpi.HIGH);
wpi.delayMicroseconds(275);
wpi.digitalWrite(pin, wpi.LOW);
// console.log('transmit finished');
}
// exports
module.exports.setWPI = setWPI;
module.exports.sendBit = sendBit;
module.exports.sendPair = sendPair;
module.exports.getWPI = getWPI;
module.exports.transmit = transmit;
module.exports.pad = pad;
module.exports.intToBytes = intToBytes;
module.exports.buildOrder = buildOrder;
module.exports.init = init;
module.exports.doTransmit = doTransmit;
module.exports.buildDimOrder = buildDimOrder;