Skip to content

Instantly share code, notes, and snippets.

@nenjiru
Last active April 21, 2018 13:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nenjiru/2087cc1bc25cb8341a9d to your computer and use it in GitHub Desktop.
Save nenjiru/2087cc1bc25cb8341a9d to your computer and use it in GitHub Desktop.
Artnet for NodeJS
/*
--------------------------------------------------------------------------------
Config
--------------------------------------------------------------------------------
*/
var dgram = require('dgram')
, Buffer = require('buffer').Buffer;
var HEADER = [65, 114, 116, 45, 78, 101, 116, 0, 0, 80, 0, 14, 0, 0, 0, 0];
var HOST = '192.168.0.255';
var PORT = 6454;
/*
--------------------------------------------------------------------------------
UDP Client
--------------------------------------------------------------------------------
*/
var udp = dgram.createSocket('udp4');
/**
* Send UDP
*/
function _send(data, host, port)
{
var upper = Math.floor(data.length / 256)
, lower = data.length % 256
, data = HEADER.concat([upper, lower]).concat(data)
, buffer = Buffer(data);
udp.send(buffer, 0, buffer.length, port, host, function(){});
}
/*
--------------------------------------------------------------------------------
Send Artnet
--------------------------------------------------------------------------------
*/
var CHANNEL = 150
, LED = []
;
for (var i = 0; i < CHANNEL; i++)
{
LED[i] = 255;
}
_send(LED, HOST, PORT);
@xseignard
Copy link

xseignard commented Apr 21, 2018

Hello, thanks for the code!

I'd like to send other OpCodes, but I don't get how you convert them. In the example above, you have an OpDMX code, but what's the conversion involved from 0x5000 to [0, 80] in JavaScript ?

I know I can do something like this in c++

#include <iostream>
using namespace std;

int main()
{
    uint16_t OpCode = 0x2400; // OpCommand
    uint8_t lo;
    uint8_t hi;
    lo = (uint8_t)OpCode;
    hi = (uint8_t)(OpCode >> 8);
    cout << unsigned(lo) << endl;
    cout << unsigned(hi) << endl;
    return 0;
}

but I'd like to see how to do it in JavaScript?

Regards,

Xavier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment