This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
193 lines (170 loc) · 5.24 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict";
let Nuimo = require("nuimojs"),
mqtt = require("mqtt"),
fs = require("fs"),
_ = require("lodash"),
matrix = require("./matrix.js");
let state = {
broker: { connected: false },
nuimo : { connected: false }
}
let config = JSON.parse(fs.readFileSync('/data/options.json', 'utf8'));
const client = mqtt.connect('mqtt://' + config.mqtt.host, {
username: config.mqtt.username,
password: config.mqtt.password
});
client.on("connect", () => {
console.log("MQTT connected")
state.broker.connected = true;
client.subscribe('home/nuimo/set');
client.publish('home/nuimo', 'Connected to Nuimo controller');
});
client.on("message", (topic, message) => {
console.log("Received message", message.toString())
if (state.nuimo.connected) {
var data = JSON.parse(message);
switch (data.action) {
case "render":
actions.writeToMatrix(data.text);
break;
}
}
});
const nuimo = new Nuimo();
nuimo.on("discover", (device) => {
console.log(`Discovered Nuimo (${device.uuid})`);
device.on("connect", () => {
console.log("Nuimo connected");
state.nuimo.connected = true;
});
device.on("disconnect", () => {
console.log("Nuimo disconnected");
state.nuimo.connected = false;
});
// @TODO: Send battery state via MQTT
device.on("press", () => {
device.setLEDMatrix(matrix.icons.bulb, 255, 2000);
client.publish("home/nuimo/press");
});
device.on("swipe", (direction) => {
let value = "";
switch (direction) {
case Nuimo.Swipe.LEFT:
value = 'LEFT';
break;
case Nuimo.Swipe.RIGHT:
value = 'RIGHT';
break;
case Nuimo.Swipe.UP:
value = 'UP';
break;
case Nuimo.Swipe.DOWN:
value = 'DOWN';
actions.ticker('ABC');
break;
}
client.publish("home/nuimo/swipe", JSON.stringify({ value: value, raw_value: direction }));
});
device.on("touch", (area) => {
let value = "";
switch (area) {
case Nuimo.Area.LEFT:
value = "LEFT";
break;
case Nuimo.Area.RIGHT:
value = "RIGHT";
break;
case Nuimo.Area.TOP:
value = "TOP";
break;
case Nuimo.Area.BOTTOM:
value = "BOTTOM";
break;
case Nuimo.Area.LONGLEFT:
value = "LONGLEFT";
break;
case Nuimo.Area.LONGBOTTOM:
value = "LONGBOTTOM";
break;
case Nuimo.Area.LONGRIGHT:
value = "LONGRIGHT";
break;
}
client.publish("home/nuimo/touch", JSON.stringify({ value: value, raw_value: area }));
});
device.on("rotate", (amount) => {
console.log('amount', amount)
rotation += amount;
rotate(client, rotation)
});
device.connect();
});
// Debounce ("throttle") the calls to rotate event. This event can be triggered multiple times during a physical rotation
let rotation = 0;
const rotate = _.debounce((client, amount) => {
let direction = amount > 0 ? "RIGHT" : "LEFT";
if (state.broker.connected) {
client.publish("home/nuimo/rotate", JSON.stringify({ value: direction, raw_value: amount }));
}
// Reset rotation value
rotation = 0;
}, 200);
const actions = {
writeToMatrix (message) {
if (state.nuimo.connected) {
var devices = nuimo.getConnectedDevices();
let iv;
devices.forEach((d) => {
let matrizes = [];
message.split("").forEach((c) => {
matrizes.push(helpers.convertCharToMatrix(c));
});
iv = setInterval(() => {
d.setLEDMatrix(matrizes.shift(), 255, 1000, 16);
if (matrizes.length < 1) {
clearInterval(iv);
}
}, 1000);
});
}
},
ticker (text) {
// @TODO: Implement this
let matrizes = [];
text.split("").forEach((c) => {
matrizes.push(helpers.convertCharToMatrix(c));
});
let dim = Math.sqrt(matrizes[0].length);
let lng = [];
let disp = [];
matrizes.forEach((m, j) => {
m.forEach((c) => {
lng.push(c);
});
});
lng.foreach
// var devices = nuimo.getConnectedDevices();
// devices.forEach((device) => {
// });
}
}
const helpers = {
convertCharToMatrix(str) {
return matrix.chars[str.toUpperCase()];
},
buildMatrix (text) {
const size = 9;
let result = [];
let chars = text.split("");
let matrizes = [];
chars.forEach((chr) => {
let currentMatrix = helpers.convertCharToMatrix(chr);
matrizes.push(currentMatrix);
});
},
getMatrixRow (matrix, row, dim) {
return matrix.filter((v,k) => k % dim === row);
}
}
nuimo.scan();
actions.ticker('AB');