Create a gist now

Instantly share code, notes, and snippets.

Shows live Wikipedia Changes on a Matelight. And is a small API
var WebSocket = require('ws');
/*[{"starts": 0, "ends": 20}], //Row 1
[{"starts": 64, "ends": 84}] // Row 2
];
*/
//kiste 1 = 0-20
//kiste 2 = 64
var device = {};
var getDeviceList = {"type": "list_connected_devices"};
var sendCanvasString = {"type": "device_pixels", "pixels": [], "device": {}};
var Cases = [[{"starts": 0, "ends": 20}], [{"starts": 64, "ends": 84}]]
var ColumnsPerCase = 5;
var RowsPerCase = 4;
var connected = false;
var Canvas = function () {
var canvas = [];
this.emptyCanvas = function () {
for (var x = 0; x < this.getWidth(); x++) {
for (var y = 0; y < this.getHeigth(); y++) {
canvas = canvas.concat([[0, 0, 0]]);
}
}
}
this.getSingleLayerCanvas = function () {
var newCanvas = [];
for (var pixel = 0; pixel < canvas.length; pixel++) {
if (pixel == 20) {
for (var i = 0; i < 44; i++) {
newCanvas.push(0, 0, 0)
}
}
if (canvas[pixel] != undefined) {
for (var color = 0; color < canvas[pixel].length; color++) {
newCanvas.push(canvas[pixel][color]);
}
}
}
return newCanvas;
}
this.setRandomPixel = function (grb) {
var y = Math.floor(Math.random() * this.getHeigth());
var x = Math.floor(Math.random() * this.getWidth());
this.setPixel(x, y, grb);
}
this.getCaseHeigth = function () {
return Cases.length;
}
this.getCaseWidth = function (row = 0) {
return Cases[row].length;
}
this.getHeigth = function () {
return heigth;
}
this.getWidth = function () {
return width;
}
this.getCanvas = function () {
return canvas;
}
this.setCanvas = function (canvas) {
this.canvas = canvas;
}
this.setPixel = function (x, y, grb) {
var pixel = y * ColumnsPerCase + x
canvas[pixel] = grb;
}
var heigth = this.getCaseHeigth() * RowsPerCase;
var width = this.getCaseWidth() * ColumnsPerCase;
return this;
}
var MateLightConnection = function () {
var ws = new WebSocket('ws://matelight.local:7890');
this.sendCanvas = function (canvas) {
if (connected) {
var data = sendCanvasString;
data.pixels = canvas;
data.device = device;
ws.send(JSON.stringify(data));
}
}
ws.on('open', function open() {
connected = true;
console.log("Connected!");
ws.send(JSON.stringify(getDeviceList));
});
ws.on('message', function message(data, flags) {
reply = JSON.parse(data);
if (reply.type == "list_connected_devices") {
device = reply.devices[0];
}
});
}
var WikimediaConnection = function (canvas) {
var ws = new WebSocket('ws://wikimon.hatnote.com:9000');
ws.on('open', function open() {
console.log("Connected!");
});
ws.on('message', function message(data, flags) {
reply = JSON.parse(data);
var color = []
// GREEN RED BLUE
var green = [255, 0, 0];
var red = [0, 255, 0];
var blue = [0, 0, 255];
var violett = [89, 155, 182];
var white = [255, 255, 255];
if (reply.page_title == 'Special:Log/newusers' &&
data.url != 'byemail') {
color = blue; //Blue
} else {
if (reply.is_anon) {
color = green //Green
} else if (reply.is_bot) {
color = violett //Violett
} else {
color = white //White
}
}
canvas.setRandomPixel(color);
});
}
function fillCanvasWithRandomNumbers(canvas) {
for (var x = 0; x < canvas.getWidth(); x++) {
for (var y = 0; y < canvas.getHeigth(); y++) {
canvas.setPixel(x, y, [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)]);
}
}
return canvas.getSingleLayerCanvas();
}
function fadeAllOut(cavas, fadeOutSpeed = 4) {
var oldCanvas = canvas.getCanvas();
var newCanvas = [];
for (var pixel = 0; pixel < oldCanvas.length; pixel++){
var currentPixel = oldCanvas[pixel];
for (color in currentPixel) {
if (currentPixel[color] > 0 && currentPixel[color] > fadeOutSpeed) {
currentPixel[color] = currentPixel[color] - fadeOutSpeed;
} else if (currentPixel[color] > 0 && currentPixel[color] < fadeOutSpeed) {
currentPixel[color] = 0;
}
}
newCanvas.push(currentPixel);
}
canvas.setCanvas(newCanvas);
}
var matelightConnection = new MateLightConnection();
var canvas = new Canvas();
var wikimediaConnection = new WikimediaConnection(canvas);
canvas.emptyCanvas();
setInterval(function() {
fadeAllOut(canvas, 8); //Fade the the Complete Canvas
matelightConnection.sendCanvas(canvas.getSingleLayerCanvas()); //Send the Canvas
//matelightConnection.sendCanvas(fillCanvasWithRandomNumbers(canvas)); //Random Colors
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment